Add sfinv.set_page, plus other helper functions

This commit is contained in:
rubenwardy 2017-02-06 18:26:33 +00:00
parent 86849d9eec
commit e3dd3d19cd
2 changed files with 56 additions and 35 deletions

@ -412,18 +412,28 @@ Sfinv API
### sfinv Methods
* sfinv.set_player_inventory_formspec(player, context) - builds page formspec
and calls set_inventory_formspec().
If context is nil, it is either found or created.
* sfinv.get_formspec(player, context) - builds current page's formspec
* sfinv.get_nav_fs(player, context, nav, current_idx) - see above
**Pages**
* sfinv.set_page(player, pagename) - changes the page
* sfinv.get_homepage_name(player) - get the page name of the first page to show to a player
* sfinv.make_formspec(player, context, content, show_inv, size) - adds a theme to a formspec
* show_inv, defaults to false. Whether to show the player's main inventory
* size, defaults to `size[8,8.6]` if not specified
* sfinv.register_page(name, def) - register a page, see section below
* sfinv.override_page(name, def) - overrides fields of an page registered with register_page.
* Note: Page must already be defined, (opt)depend on the mod defining it.
* sfinv.set_player_inventory_formspec(player) - (re)builds page formspec
and calls set_inventory_formspec().
* sfinv.get_formspec(player, context) - builds current page's formspec
**Contexts**
* sfinv.get_or_create_context(player) - gets the player's context
* sfinv.set_context(player, context)
**Theming**
* sfinv.make_formspec(player, context, content, show_inv, size) - adds a theme to a formspec
* show_inv, defaults to false. Whether to show the player's main inventory
* size, defaults to `size[8,8.6]` if not specified
* sfinv.get_nav_fs(player, context, nav, current_idx) - creates tabheader or ""
### sfinv Members

@ -91,22 +91,42 @@ function sfinv.get_formspec(player, context)
end
end
function sfinv.set_player_inventory_formspec(player, context)
function sfinv.get_or_create_context(player)
local name = player:get_player_name()
local context = sfinv.contexts[name]
if not context then
local name = player:get_player_name()
context = sfinv.contexts[name]
if not context then
context = {
page = sfinv.get_homepage_name(player)
}
sfinv.contexts[name] = context
end
context = {
page = sfinv.get_homepage_name(player)
}
sfinv.contexts[name] = context
end
return context
end
local fs = sfinv.get_formspec(player, context)
function sfinv.set_context(player, context)
sfinv.contexts[player:get_player_name()] = context
end
function sfinv.set_player_inventory_formspec(player, context)
local fs = sfinv.get_formspec(player,
context or sfinv.get_or_create_context(player))
player:set_inventory_formspec(fs)
end
function sfinv.set_page(player, pagename)
local context = sfinv.get_or_create_context(player)
local oldpage = sfinv.pages[context.page]
if oldpage and oldpage.on_leave then
oldpage:on_leave(player, context)
end
context.page = pagename
local page = sfinv.pages[pagename]
if page.on_enter then
page:on_enter(player, context)
end
sfinv.set_player_inventory_formspec(player, context)
end
minetest.register_on_joinplayer(function(player)
if sfinv.enabled then
minetest.after(0.5, function()
@ -132,30 +152,21 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
return false
end
-- Handle Events
-- Was a tab selected?
if fields.tabs and context.nav then
local tid = tonumber(fields.tabs)
if tid and tid > 0 then
local id = context.nav[tid]
local page = sfinv.pages[id]
if id and page then
local oldpage = sfinv.pages[context.page]
if oldpage and oldpage.on_leave then
oldpage:on_leave(player, context)
end
context.page = id
if page.on_enter then
page:on_enter(player, context)
end
sfinv.set_player_inventory_formspec(player, context)
sfinv.set_page(player, id)
end
end
return
end
-- Pass to page
local page = sfinv.pages[context.page]
if page and page.on_player_receive_fields then
return page:on_player_receive_fields(player, context, fields)
else
-- Pass event to page
local page = sfinv.pages[context.page]
if page and page.on_player_receive_fields then
return page:on_player_receive_fields(player, context, fields)
end
end
end)