Add description
[minetest_sfinv_buttons.git] / init.lua
blobc3b3fc97fd5a3c184d4d86ca306d2814b3cb24ec
1 -- Boilerplate to support localized strings if intllib mod is installed.
2 local S
3 if minetest.get_modpath("intllib") then
4 S = intllib.Getter()
5 else
6 S = function(s) return s end
7 end
9 local buttons = {}
10 local buttons_num = 0
12 local button_prefix = "sfinv_button_"
14 sfinv_buttons = {}
17 sfinv_buttons.register_button = function(name, def)
18 buttons[name] = def
19 buttons_num = buttons_num + 1
20 end
22 local MAX_ROWS = 9
24 sfinv.register_page("sfinv_buttons:buttons", {
25 title = S("More"),
26 is_in_nav = function(self, player, context)
27 -- Tab is shown only if at least 1 button is visible to player
28 for _, def in pairs(buttons) do
29 if def.show == nil or def.show(player) == true then
30 return true
31 end
32 end
33 return false
34 end,
35 get = function(self, player, context)
36 local f = ""
37 local y = 0
38 local x = 0
39 local w
40 if buttons_num > MAX_ROWS then
41 w = 3
42 else
43 w = 7
44 end
45 for name, def in pairs(buttons) do
46 if def.show == nil or def.show(player) == true then
47 if def.image ~= nil then
48 f = f .. "image["..(x+0.1)..","..(y+0.1)..";0.8,0.8;"..def.image.."]"
49 end
50 local button_id = minetest.formspec_escape(button_prefix .. name)
51 f = f .. "button["..
52 (x+1)..","..y..";"..w..",1;"..
53 button_id..";"..
54 minetest.formspec_escape(def.title)..
55 "]"
56 if def.tooltip ~= nil then
57 f = f .. "tooltip["..button_id..";"..
58 minetest.formspec_escape(def.tooltip).."]"
59 end
60 y = y + 1
61 if y >= MAX_ROWS then
62 y = 0
63 x = x + 4
64 end
65 end
66 end
67 return sfinv.make_formspec(player, context, f)
68 end,
69 on_player_receive_fields = function(self, player, context, fields)
70 for widget_name, _ in pairs(fields) do
71 local id = string.sub(widget_name, string.len(button_prefix) + 1, -1)
72 if buttons[id] ~= nil and buttons[id].action ~= nil then
73 buttons[id].action(player)
74 end
75 end
76 end,