Add sfinv.set_page, plus other helper functions
This commit is contained in:
parent
86849d9eec
commit
e3dd3d19cd
26
game_api.txt
26
game_api.txt
@ -412,18 +412,28 @@ Sfinv API
|
|||||||
|
|
||||||
### sfinv Methods
|
### sfinv Methods
|
||||||
|
|
||||||
* sfinv.set_player_inventory_formspec(player, context) - builds page formspec
|
**Pages**
|
||||||
and calls set_inventory_formspec().
|
|
||||||
If context is nil, it is either found or created.
|
* sfinv.set_page(player, pagename) - changes the page
|
||||||
* sfinv.get_formspec(player, context) - builds current page's formspec
|
|
||||||
* sfinv.get_nav_fs(player, context, nav, current_idx) - see above
|
|
||||||
* sfinv.get_homepage_name(player) - get the page name of the first page to show to a player
|
* 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.register_page(name, def) - register a page, see section below
|
||||||
* sfinv.override_page(name, def) - overrides fields of an page registered with register_page.
|
* 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.
|
* 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
|
### sfinv Members
|
||||||
|
|
||||||
|
@ -91,22 +91,42 @@ function sfinv.get_formspec(player, context)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function sfinv.set_player_inventory_formspec(player, context)
|
function sfinv.get_or_create_context(player)
|
||||||
if not context then
|
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
context = sfinv.contexts[name]
|
local context = sfinv.contexts[name]
|
||||||
if not context then
|
if not context then
|
||||||
context = {
|
context = {
|
||||||
page = sfinv.get_homepage_name(player)
|
page = sfinv.get_homepage_name(player)
|
||||||
}
|
}
|
||||||
sfinv.contexts[name] = context
|
sfinv.contexts[name] = context
|
||||||
end
|
end
|
||||||
|
return context
|
||||||
end
|
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)
|
player:set_inventory_formspec(fs)
|
||||||
end
|
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)
|
minetest.register_on_joinplayer(function(player)
|
||||||
if sfinv.enabled then
|
if sfinv.enabled then
|
||||||
minetest.after(0.5, function()
|
minetest.after(0.5, function()
|
||||||
@ -132,30 +152,21 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Handle Events
|
-- Was a tab selected?
|
||||||
if fields.tabs and context.nav then
|
if fields.tabs and context.nav then
|
||||||
local tid = tonumber(fields.tabs)
|
local tid = tonumber(fields.tabs)
|
||||||
if tid and tid > 0 then
|
if tid and tid > 0 then
|
||||||
local id = context.nav[tid]
|
local id = context.nav[tid]
|
||||||
local page = sfinv.pages[id]
|
local page = sfinv.pages[id]
|
||||||
if id and page then
|
if id and page then
|
||||||
local oldpage = sfinv.pages[context.page]
|
sfinv.set_page(player, id)
|
||||||
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)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return
|
else
|
||||||
end
|
-- Pass event to page
|
||||||
|
|
||||||
-- Pass to page
|
|
||||||
local page = sfinv.pages[context.page]
|
local page = sfinv.pages[context.page]
|
||||||
if page and page.on_player_receive_fields then
|
if page and page.on_player_receive_fields then
|
||||||
return page:on_player_receive_fields(player, context, fields)
|
return page:on_player_receive_fields(player, context, fields)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user