]> git.lizzy.rs Git - worldedit.git/blob - worldedit_gui/init.lua
424d61f064f887d3955cf7c5d66743b2de52e5b7
[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 The `privs` field may not be `nil`.
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         if options.privs == nil or next(options.privs) == nil then
28                 error("privs unset")
29         end
30         worldedit.pages[identifier] = options
31         table.insert(identifiers, identifier)
32 end
33
34 --[[
35 Example:
36
37     worldedit.register_gui_handler("worldedit_gui_hollow_cylinder", function(name, fields)
38         print(minetest.serialize(fields))
39     end)
40 ]]
41
42 worldedit.register_gui_handler = function(identifier, handler)
43         local enabled = true
44         minetest.register_on_player_receive_fields(function(player, formname, fields)
45                 if not enabled then return false end
46                 enabled = false
47                 minetest.after(0.2, function() enabled = true end)
48                 local name = player:get_player_name()
49
50                 --ensure the player has permission to perform the action
51                 local entry = worldedit.pages[identifier]
52                 if entry and minetest.check_player_privs(name, entry.privs) then
53                         return handler(name, fields)
54                 end
55                 return false
56         end)
57 end
58
59 worldedit.get_formspec_header = function(identifier)
60         local entry = worldedit.pages[identifier] or {}
61         return "button[0,0;2,0.5;worldedit_gui;Back]" ..
62                 string.format("label[2,0;WorldEdit GUI > %s]", entry.name or "")
63 end
64
65 local get_formspec = function(name, identifier)
66         if worldedit.pages[identifier] then
67                 return worldedit.pages[identifier].get_formspec(name)
68         end
69         return worldedit.pages["worldedit_gui"].get_formspec(name) --default to showing main page if an unknown page is given
70 end
71
72 --implement worldedit.show_page(name, page) in different ways depending on the available APIs
73 if minetest.global_exists("unified_inventory") then -- unified inventory installed
74         local old_func = worldedit.register_gui_function
75         worldedit.register_gui_function = function(identifier, options)
76                 old_func(identifier, options)
77                 unified_inventory.register_page(identifier, {get_formspec=function(player) return {formspec=options.get_formspec(player:get_player_name())} end})
78         end
79
80         unified_inventory.register_button("worldedit_gui", {
81                 type = "image",
82                 image = "inventory_plus_worldedit_gui.png",
83         })
84
85         minetest.register_on_player_receive_fields(function(player, formname, fields)
86                 local name = player:get_player_name()
87                 if fields.worldedit_gui then --main page
88                         worldedit.show_page(name, "worldedit_gui")
89                         return true
90                 elseif fields.worldedit_gui_exit then --return to original page
91                         local player = minetest.get_player_by_name(name)
92                         if player then
93                                 unified_inventory.set_inventory_formspec(player, "craft")
94                         end
95                         return true
96                 end
97                 return false
98         end)
99
100         worldedit.show_page = function(name, page)
101                 local player = minetest.get_player_by_name(name)
102                 if player then
103                         player:set_inventory_formspec(get_formspec(name, page))
104                 end
105         end
106 elseif minetest.global_exists("inventory_plus") then -- inventory++ installed
107         minetest.register_on_joinplayer(function(player)
108                 local can_worldedit = minetest.check_player_privs(player:get_player_name(), {worldedit=true})
109                 if can_worldedit then
110                         inventory_plus.register_button(player, "worldedit_gui", "WorldEdit")
111                 end
112         end)
113
114         --show the form when the button is pressed and hide it when done
115         local gui_player_formspecs = {}
116         minetest.register_on_player_receive_fields(function(player, formname, fields)
117                 local name = player:get_player_name()
118                 if fields.worldedit_gui then --main page
119                         gui_player_formspecs[name] = player:get_inventory_formspec()
120                         worldedit.show_page(name, "worldedit_gui")
121                         return true
122                 elseif fields.worldedit_gui_exit then --return to original page
123                         if gui_player_formspecs[name] then
124                                 inventory_plus.set_inventory_formspec(player, inventory_plus.get_formspec(player, "main"))
125                         end
126                         return true
127                 end
128                 return false
129         end)
130
131         worldedit.show_page = function(name, page)
132                 local player = minetest.get_player_by_name(name)
133                 if player then
134                         inventory_plus.set_inventory_formspec(player, get_formspec(name, page))
135                 end
136         end
137 elseif minetest.global_exists("smart_inventory") then -- smart_inventory installed
138         -- redefinition: Update the code element on inventory page to show the we-page
139         function worldedit.show_page(name, page)
140                 local state = smart_inventory.get_page_state("worldedit_gui", name)
141                 if state then
142                         state:get("code"):set_we_formspec(page)
143                         state.location.rootState:show() -- update inventory page
144                 end
145         end
146
147         -- smart_inventory page callback. Contains just a "custom code" element
148         local function smart_worldedit_gui_callback(state)
149                 local codebox = state:element("code", { name = "code", code = "" })
150                 function codebox:set_we_formspec(we_page)
151                         local new_formspec = get_formspec(state.location.rootState.location.player, we_page)
152                         new_formspec = new_formspec:gsub('button_exit','button') --no inventory closing
153                         self.data.code = "container[1,1]".. new_formspec .. "container_end[]"
154                 end
155                 codebox:set_we_formspec("worldedit_gui")
156
157                 -- process input (the back button)
158                 state:onInput(function(state, fields, player)
159                         if fields.worldedit_gui then --main page
160                                 state:get("code"):set_we_formspec("worldedit_gui")
161                         elseif fields.worldedit_gui_exit then --return to original page
162                                 state:get("code"):set_we_formspec("worldedit_gui")
163                                 state.location.parentState:get("crafting_button"):submit() -- switch to the crafting tab
164                         end
165                 end)
166         end
167
168         -- all handler should return false to force inventory UI update
169         local orig_register_gui_handler = worldedit.register_gui_handler
170         worldedit.register_gui_handler = function(identifier, handler)
171                 local wrapper = function(...)
172                         handler(...)
173                         return false
174                 end
175                 orig_register_gui_handler(identifier, wrapper)
176         end
177
178         -- register the inventory button
179         smart_inventory.register_page({
180                 name = "worldedit_gui",
181                 tooltip = "Edit your World!",
182                 icon = "inventory_plus_worldedit_gui.png",
183                 smartfs_callback = smart_worldedit_gui_callback,
184                 sequence = 99
185         })
186 elseif minetest.global_exists("sfinv") then -- sfinv installed
187         assert(sfinv.enabled)
188         local orig_get = sfinv.pages["sfinv:crafting"].get
189         sfinv.override_page("sfinv:crafting", {
190                 get = function(self, player, context)
191                         local can_worldedit = minetest.check_player_privs(player, {worldedit=true})
192                         local fs = orig_get(self, player, context)
193                         return fs .. (can_worldedit and "image_button[0,0;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;]" or "")
194                 end
195         })
196
197         --show the form when the button is pressed and hide it when done
198         minetest.register_on_player_receive_fields(function(player, formname, fields)
199                 if fields.worldedit_gui then --main page
200                         worldedit.show_page(player:get_player_name(), "worldedit_gui")
201                         return true
202                 elseif fields.worldedit_gui_exit then --return to original page
203                         sfinv.set_page(player, "sfinv:crafting")
204                         return true
205                 end
206                 return false
207         end)
208
209         worldedit.show_page = function(name, page)
210                 local player = minetest.get_player_by_name(name)
211                 if player then
212                         player:set_inventory_formspec(get_formspec(name, page))
213                 end
214         end
215 else
216         error(
217                 "worldedit_gui requires a supported gui management mod to be installed.\n"..
218                 "To use the it you need to either:\n"..
219                 "* use minetest_game or another sfinv-compatible subgame\n"..
220                 "* install Unified Inventory, Inventory++ or Smart Inventory\n"..
221                 "If you don't want to use worldedit_gui, disable it by editing world.mt or from the main menu."
222         )
223 end
224
225 worldedit.register_gui_function("worldedit_gui", {
226         name = "WorldEdit GUI",
227         privs = {interact=true},
228         get_formspec = function(name)
229                 --create a form with all the buttons arranged in a grid
230                 local buttons, x, y, index = {}, 0, 1, 0
231                 local width, height = 3, 0.8
232                 local columns = 5
233                 for i, identifier in pairs(identifiers) do
234                         if identifier ~= "worldedit_gui" then
235                                 local entry = worldedit.pages[identifier]
236                                 table.insert(buttons, string.format((entry.get_formspec and "button" or "button_exit") ..
237                                         "[%g,%g;%g,%g;%s;%s]", x, y, width, height, identifier, minetest.formspec_escape(entry.name)))
238
239                                 index, x = index + 1, x + width
240                                 if index == columns then --row is full
241                                         x, y = 0, y + height
242                                         index = 0
243                                 end
244                         end
245                 end
246                 if index == 0 then --empty row
247                         y = y - height
248                 end
249                 return string.format("size[%g,%g]", math.max(columns * width, 5), math.max(y + 0.5, 3)) ..
250                         "button[0,0;2,0.5;worldedit_gui_exit;Back]" ..
251                         "label[2,0;WorldEdit GUI]" ..
252                         table.concat(buttons)
253         end,
254 })
255
256 worldedit.register_gui_handler("worldedit_gui", function(name, fields)
257         for identifier, entry in pairs(worldedit.pages) do --check for WorldEdit GUI main formspec button selection
258                 if fields[identifier] and identifier ~= "worldedit_gui" then
259                         --ensure player has permission to perform action
260                         local has_privs, missing_privs = minetest.check_player_privs(name, entry.privs)
261                         if not has_privs then
262                                 worldedit.player_notify(name, "you are not allowed to use this function (missing privileges: " .. table.concat(missing_privs, ", ") .. ")")
263                                 return false
264                         end
265                         if entry.on_select then
266                                 entry.on_select(name)
267                         end
268                         if entry.get_formspec then
269                                 worldedit.show_page(name, identifier)
270                         end
271                         return true
272                 end
273         end
274         return false
275 end)
276
277 dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/functionality.lua")