From a66b9a7299e9e18c4748d0aa06132123827edf53 Mon Sep 17 00:00:00 2001 From: Alexander Weber Date: Sat, 15 Aug 2020 18:31:25 +0200 Subject: [PATCH] add is_visible_func to page definition to allow differen per-player buttons --- API.md | 2 ++ inventory_framework.lua | 46 +++++++++++++++++++++-------------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/API.md b/API.md index 1850e2d..dfeb075 100644 --- a/API.md +++ b/API.md @@ -13,6 +13,7 @@ smart_inventory.register_page({ smartfs_callback = function, sequence = number, on_button_click = function, + is_visible_func = function, }) ``` - name - unique short name, used for identification @@ -22,6 +23,7 @@ smart_inventory.register_page({ - smartfs_callback(state) - smartfs callback function See [smartfs documentation](https://github.com/minetest-mods/smartfs/blob/master/docs) and existing pages implementations for reference. - sequence - The buttons are sorted by this number (crafting=10, creative=15, player=20) - on_button_click(state) - function called each page button click +- is_visible_func(state) - function for dynamic page enabelling. Should return bool value. ### Get the definition for registered smart_inventory page ```smart_inventory.get_registered_page(pagename)``` diff --git a/inventory_framework.lua b/inventory_framework.lua index 2619017..b4cb694 100644 --- a/inventory_framework.lua +++ b/inventory_framework.lua @@ -52,29 +52,31 @@ local inventory_form = smartfs.create("smart_inventory:main", function(state) for _, def in ipairs(smart_inventory.registered_pages) do assert(def.smartfs_callback, "Callback function needed") assert(def.name, "Name is needed") - local tabdef = {} - local label - if not def.label then - label = "" - else - label = def.label - end - tabdef.button = state:button(button_x,11.2,1,1,def.name.."_button",label) - if def.icon then - tabdef.button:setImage(def.icon) - end - tabdef.button:setTooltip(def.tooltip) - tabdef.button:onClick(function(self) - tab_controller:set_active(def.name) - if def.on_button_click then - def.on_button_click(tabdef.viewstate) + if not def.is_visible_func or def.is_visible_func(state) then + local tabdef = {} + local label + if not def.label then + label = "" + else + label = def.label end - end) - tabdef.view = state:container(0,1,def.name.."_container") - tabdef.viewstate = tabdef.view:getContainerState() - def.smartfs_callback(tabdef.viewstate) - tab_controller:tab_add(def.name, tabdef) - button_x = button_x + 1 + tabdef.button = state:button(button_x,11.2,1,1,def.name.."_button",label) + if def.icon then + tabdef.button:setImage(def.icon) + end + tabdef.button:setTooltip(def.tooltip) + tabdef.button:onClick(function(self) + tab_controller:set_active(def.name) + if def.on_button_click then + def.on_button_click(tabdef.viewstate) + end + end) + tabdef.view = state:container(0,1,def.name.."_container") + tabdef.viewstate = tabdef.view:getContainerState() + def.smartfs_callback(tabdef.viewstate) + tab_controller:tab_add(def.name, tabdef) + button_x = button_x + 1 + end end tab_controller:set_active(smart_inventory.registered_pages[1].name) end)