sfinv_buttons: enhance, cleanup and tidy code.

- Allow parameter for button position
- allow multiple colums for button position > 9
This commit is contained in:
Alexander Weber 2020-08-27 10:31:20 +02:00
parent e73565b923
commit 25594009b0

View File

@ -1,62 +1,59 @@
-- Based on "https://gist.githubusercontent.com/rubenwardy/10074b66bd6e409c72c3ad0f79f5771a/raw/47389bbf0dc6badd79362361d0592053add7ceab/init.lua -- Based on "https://gist.githubusercontent.com/rubenwardy/10074b66bd6e409c72c3ad0f79f5771a/raw/47389bbf0dc6badd79362361d0592053add7ceab/init.lua
-- Boilerplate to support localized strings if intllib mod is installed.
local S
if minetest.get_modpath("intllib") then
S = intllib.Getter()
else
S = function(s) return s end
end
local buttons = {}
local button_names_sorted = {}
local buttons_num = 0
local button_prefix = "sfinv_button_" local button_prefix = "sfinv_button_"
local last_button = 0
-- Stores selected index in textlist sfinv_buttons = { registered_buttons = {}, MAX_ROWS = 9 }
local player_indexes = {}
local player_selections = {}
sfinv_buttons = {}
sfinv_buttons.register_button = function(name, def) sfinv_buttons.register_button = function(name, def)
buttons[name] = def
table.insert(button_names_sorted, name)
buttons_num = buttons_num + 1
end
-- Turns a textlist index to a button name def.button_name = button_prefix .. minetest.get_current_modname() .. '_' .. name
local index_to_button_name = function(index, player)
local internal_index = 1 local idx_start = def.position or 1
for i=1, #button_names_sorted do local idx_end = last_button + 1
local name = button_names_sorted[i] if idx_start > idx_end then
local def = buttons[name] sfinv_buttons.registered_buttons[idx_start] = def
if internal_index == index then if idx_start > last_button then
if def.show == nil or def.show(player) == true then last_button = idx_start
return name end
else
local bubble = def
for idx = idx_start, idx_end do
local current = sfinv_buttons.registered_buttons[idx]
if not current then
-- free slot found
sfinv_buttons.registered_buttons[idx] = bubble
if idx > last_button then
last_button = idx
end
break
elseif bubble.position and (not current.position or current.position < bubble.position ) then
-- swap bubble
sfinv_buttons.registered_buttons[idx] = bubble
bubble = current
end end
end end
internal_index = internal_index + 1
end end
return
end end
smart_sfinv_api.register_enhancement({ smart_sfinv_api.register_enhancement({
make_formspec = function(handler, player, context, content, show_inv) make_formspec = function(handler, player, context, content, show_inv)
handler.formspec_size_add_w = handler.formspec_size_add_w + 1 if last_button == 0 then -- no buttons
return
end
local x = 8.1 handler.formspec_size_add_w = handler.formspec_size_add_w + math.floor(last_button / sfinv_buttons.MAX_ROWS) + 1
local y = 0
local retval = "" local retval = ""
local buttons_added = 0 for idx = 1, last_button do
for i=1, #button_names_sorted do def = sfinv_buttons.registered_buttons[idx]
local name = button_names_sorted[i]
local def = buttons[name] local x = 8.1 + math.floor(idx / sfinv_buttons.MAX_ROWS)
if def.show == nil or def.show(player) == true then local y = (idx - 1) % sfinv_buttons.MAX_ROWS
local button_id = minetest.formspec_escape(button_prefix .. name)
if def and (def.show == nil or def.show == true or def.show(player, context, content, show_inv) == true) then
local button_id = minetest.formspec_escape(def.button_name)
retval = retval .. table.concat({ retval = retval .. table.concat({
"image_button[", "image_button[",
x, ",", y, ";", x, ",", y, ";",
@ -64,38 +61,34 @@ smart_sfinv_api.register_enhancement({
def.image, ";", def.image, ";",
button_id, ";]"}, "") button_id, ";]"}, "")
local tooltip = def.title local tooltip
if def.tooltip and def.tooltip ~= def.title then if def.title then
tooltip = tooltip or "" if def.tooltip and def.tooltip ~= def.title then
tooltip = tooltip .. " " .. def.tooltip tooltip = def.tooltip .. " " .. def.tooltip
else
tooltip = def.title
end
else
tooltip = def.tooltip
end end
if tooltip ~= nil then if tooltip then
retval = retval .. "tooltip["..button_id..";".. retval = retval .. "tooltip["..button_id..";"..
minetest.formspec_escape(tooltip).."]" minetest.formspec_escape(tooltip).."]"
end end
y = y + 1
end end
end end
handler.formspec_after_navfs = handler.formspec_after_navfs .. retval handler.formspec_after_navfs = handler.formspec_after_navfs .. retval
end, end,
receive_fields = function(handler, player, context, fields) receive_fields = function(handler, player, context, fields)
local player_name = player:get_player_name() local player_name = player:get_player_name()
-- TODO: Test support case when some buttons are hidden for player local button_prefix_len = button_prefix:len()
if fields.sfinv_buttons_action then for idx = 1, last_button do
local button = buttons[player_selections[player_name]] def = sfinv_buttons.registered_buttons[idx]
if button ~= nil and button.action ~= nil then if def and fields[def.button_name] and def.action then
button.action(player) def.action(player)
end break
else
for widget_name, _ in pairs(fields) do
local id = string.sub(widget_name, string.len(button_prefix) + 1, -1)
if buttons[id] ~= nil and buttons[id].action ~= nil then
buttons[id].action(player)
end
end end
end end
end end