Add tooltip support
[minetest_sfinv_buttons.git] / init.lua
blob9f6cef976a147765f104a96f1892968ad0c10459
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 return buttons_num > 0
28 end,
29 get = function(self, player, context)
30 local f = ""
31 local y = 0
32 local x = 0
33 local w
34 if buttons_num > MAX_ROWS then
35 w = 3
36 else
37 w = 7
38 end
39 for name, def in pairs(buttons) do
40 if def.image ~= nil then
41 f = f .. "image["..(x+0.1)..","..(y+0.1)..";0.8,0.8;"..def.image.."]"
42 end
43 local button_id = minetest.formspec_escape(button_prefix .. name)
44 f = f .. "button["..
45 (x+1)..","..y..";"..w..",1;"..
46 button_id..";"..
47 minetest.formspec_escape(def.title)..
48 "]"
49 if def.tooltip ~= nil then
50 f = f .. "tooltip["..button_id..";"..
51 minetest.formspec_escape(def.tooltip).."]"
52 end
53 y = y + 1
54 if y >= MAX_ROWS then
55 y = 0
56 x = x + 4
57 end
58 end
59 return sfinv.make_formspec(player, context, f)
60 end,
61 on_player_receive_fields = function(self, player, context, fields)
62 for widget_name, _ in pairs(fields) do
63 local id = string.sub(widget_name, string.len(button_prefix) + 1, -1)
64 if buttons[id] ~= nil and buttons[id].action ~= nil then
65 buttons[id].action(player)
66 end
67 end
68 end,