]> git.lizzy.rs Git - worldedit.git/blob - worldedit_gui/init.lua
Update worldedit_gui to work with latest unified_inventory (thanks LazyJ!).
[worldedit.git] / worldedit_gui / init.lua
1 worldedit = worldedit or {}
2
3 --[[
4 Example:
5
6     worldedit.register_gui_function("worldedit_gui_hollow_cylinder", {
7         name = "Make Hollow Cylinder",
8         privs = {worldedit=true},
9         get_formspec = function(name) return "some formspec here" end,
10         on_select = function(name) print(name .. " clicked the button!") end,
11     })
12
13 Use `nil` for the `options` parameter to unregister the function associated with the given identifier.
14
15 Use `nil` for the `get_formspec` field to denote that the function does not have its own screen.
16
17 Use `nil` for the `privs` field to denote that no special privileges are required to use the function.
18
19 If the identifier is already registered to another function, it will be replaced by the new one.
20
21 The `on_select` function must not call `worldedit.show_page`
22 ]]
23
24 worldedit.pages = {} --mapping of identifiers to options
25 local identifiers = {} --ordered list of identifiers
26 worldedit.register_gui_function = function(identifier, options)
27         worldedit.pages[identifier] = options
28         table.insert(identifiers, identifier)
29 end
30
31 --[[
32 Example:
33
34     worldedit.register_gui_handler("worldedit_gui_hollow_cylinder", function(name, fields)
35         print(minetest.serialize(fields))
36     end)
37 ]]
38
39 worldedit.register_gui_handler = function(identifier, handler)
40         minetest.register_on_player_receive_fields(function(player, formname, fields)
41                 --ensure the form is not being exited since this is a duplicate message
42                 if fields.quit then
43                         return false
44                 end
45                 
46                 local name = player:get_player_name()
47                 
48                 --ensure the player has permission to perform the action
49                 local entry = worldedit.pages[identifier]
50                 if entry and minetest.check_player_privs(name, entry.privs or {}) then
51                         return handler(name, fields)
52                 end
53                 return false
54         end)
55 end
56
57 worldedit.get_formspec_header = function(identifier)
58         local entry = worldedit.pages[identifier] or {}
59         return "button[0,0;2,0.5;worldedit_gui;Back]" ..
60                 string.format("label[2,0;WorldEdit GUI > %s]", entry.name or "")
61 end
62
63 local get_formspec = function(name, identifier)
64         if worldedit.pages[identifier] then
65                 return worldedit.pages[identifier].get_formspec(name)
66         end
67         return worldedit.pages["worldedit_gui"].get_formspec(name) --default to showing main page if an unknown page is given
68 end
69
70 if unified_inventory then
71         local old_func = worldedit.register_gui_function
72         worldedit.register_gui_function = function(identifier, options)
73                 old_func(identifier, options)
74                 unified_inventory.register_page(identifier, {get_formspec=function(player) return {formspec=options.get_formspec(player:get_player_name())} end})
75         end
76
77         unified_inventory.register_button("worldedit_gui", {
78                 type = "image",
79                 image = "inventory_plus_worldedit_gui.png",
80         })
81
82         minetest.register_on_player_receive_fields(function(player, formname, fields)
83                 local name = player:get_player_name()
84                 if fields.worldedit_gui then --main page
85                         worldedit.show_page(name, "worldedit_gui")
86                         return true
87                 elseif fields.worldedit_gui_exit then --return to original page
88                         unified_inventory.set_inventory_formspec(minetest.get_player_by_name(name), "craft")
89                         return true
90                 end
91                 return false
92         end)
93
94         worldedit.show_page = function(name, page)
95                 minetest.get_player_by_name(name):set_inventory_formspec(get_formspec(name, page))
96         end
97 elseif inventory_plus then
98         minetest.register_on_joinplayer(function(player)
99                 inventory_plus.register_button(player, "worldedit_gui", "WorldEdit")
100         end)
101
102         --show the form when the button is pressed and hide it when done
103         local gui_player_formspecs = {}
104         minetest.register_on_player_receive_fields(function(player, formname, fields)
105                 local name = player:get_player_name()
106                 if fields.worldedit_gui then --main page
107                         gui_player_formspecs[name] = player:get_inventory_formspec()
108                         worldedit.show_page(name, "worldedit_gui")
109                         return true
110                 elseif fields.worldedit_gui_exit then --return to original page
111                         if gui_player_formspecs[name] then
112                                 inventory_plus.set_inventory_formspec(player, gui_player_formspecs[name])
113                         end
114                         return true
115                 end
116                 return false
117         end)
118
119         worldedit.show_page = function(name, page)
120                 inventory_plus.set_inventory_formspec(minetest.get_player_by_name(name), get_formspec(name, page))
121         end
122 else
123         worldedit.show_page = function(name, page)
124                 minetest.log("error", "WorldEdit GUI cannot be shown, requires unified_inventory or inventory_plus")
125         end
126         minetest.log("error", "WorldEdit GUI is unavailable, requires unified_inventory or inventory_plus")
127 end
128
129 worldedit.register_gui_function("worldedit_gui", {
130         name = "WorldEdit GUI",
131         get_formspec = function(name)
132                 --create a form with all the buttons arranged in a grid --wip: show only buttons that the player has privs for
133                 local buttons, x, y, index = {}, 0, 1, 0
134                 local width, height = 3, 0.8
135                 local columns = 5
136                 for i, identifier in pairs(identifiers) do
137                         if identifier ~= "worldedit_gui" then
138                                 local entry = worldedit.pages[identifier]
139                                 table.insert(buttons, string.format((entry.get_formspec and "button" or "button_exit") ..
140                                         "[%g,%g;%g,%g;%s;%s]", x, y, width, height, identifier, minetest.formspec_escape(entry.name)))
141
142                                 index, x = index + 1, x + width
143                                 if index == columns then --row is full
144                                         x, y = 0, y + height
145                                         index = 0
146                                 end
147                         end
148                 end
149                 if index == 0 then --empty row
150                         y = y - height
151                 end
152                 return string.format("size[%g,%g]", math.max(columns * width, 5), math.max(y + 0.5, 3)) ..
153                         "button[0,0;2,0.5;worldedit_gui_exit;Back]" ..
154                         "label[2,0;WorldEdit GUI]" ..
155                         table.concat(buttons)
156         end,
157 })
158
159 worldedit.register_gui_handler("worldedit_gui", function(name, fields)
160         for identifier, entry in pairs(worldedit.pages) do --check for WorldEdit GUI main formspec button selection
161                 if fields[identifier] and identifier ~= "worldedit_gui" then
162                         --ensure player has permission to perform action
163                         local has_privs, missing_privs = minetest.check_player_privs(name, entry.privs or {})
164                         if not has_privs then
165                                 worldedit.player_notify(name, "you are not allowed to use this function (missing privileges: " .. table.concat(missing_privs, ", ") .. ")")
166                                 return false
167                         end
168                         if entry.on_select then
169                                 entry.on_select(name)
170                         end
171                         if entry.get_formspec then
172                                 worldedit.show_page(name, identifier)
173                         end
174                         return true
175                 end
176         end
177         return false
178 end)
179
180 dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/functionality.lua")