Add sfinv.set_page, plus other helper functions 1549/head
authorrubenwardy <rubenwardy@gmail.com>
Mon, 6 Feb 2017 18:26:33 +0000 (18:26 +0000)
committerrubenwardy <rubenwardy@gmail.com>
Fri, 10 Feb 2017 23:54:57 +0000 (23:54 +0000)
game_api.txt
mods/sfinv/api.lua

index cef46f66b2a1ffd4257d45dec8558433ae4b3195..0d518f1c884a70607a8f587bec78eff80aebfe12 100644 (file)
@@ -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
 
index 2fef3c8439e7da05607ebae860e2c902940a9bc9..b6e976677cb3eca5e4c5514694d60d0aee63bc4a 100644 (file)
@@ -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
+
+function sfinv.set_context(player, context)
+       sfinv.contexts[player:get_player_name()] = context
+end
 
-       local fs = sfinv.get_formspec(player, context)
+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)