add is_visible_func to page definition to allow differen per-player buttons

master
Alexander Weber 2020-08-15 18:31:25 +02:00
parent cbd843311a
commit a66b9a7299
2 changed files with 26 additions and 22 deletions

2
API.md
View File

@ -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)```

View File

@ -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)