]> git.lizzy.rs Git - worldedit.git/blob - worldedit_gui/functionality.lua
29405eac2cd92e676cb1c7c74c3538addde67d54
[worldedit.git] / worldedit_gui / functionality.lua
1 --saved state for each player\r
2 local gui_nodename1 = {} --mapping of player names to node names (arbitrary strings may also appear as values)\r
3 local gui_nodename2 = {} --mapping of player names to node names (arbitrary strings may also appear as values)\r
4 local gui_radius = {} --mapping of player names to radii (arbitrary strings may also appear as values)\r
5 local gui_axis = {} --mapping of player names to axes (one of 1, 2, 3, or 4, representing the axes in the `axis_indices` table below)\r
6 local gui_length = {} --mapping of player names to lengths (arbitrary strings may also appear as values)\r
7 local gui_formspec = {} --mapping of player names to formspecs (arbitrary strings may also appear as values)\r
8 \r
9 local axis_indices = {["X axis"]=1, ["Y axis"]=2, ["Z axis"]=3, ["Look direction"]=4}\r
10 local axis_values = {"x", "y", "z", "?"}\r
11 \r
12 local register_gui_chatcommand = function(identifier, name, command, callback)\r
13         callback = callback or function(name, command) command(name, "") end\r
14         worldedit.register_gui_function(identifier, {\r
15                 name = name,\r
16                 privs = minetest.chatcommands[command].privs,\r
17                 on_select = function(name)\r
18                         return callback(name, minetest.chatcommands[command].func)\r
19                 end,\r
20         })\r
21 end\r
22 \r
23 register_gui_chatcommand("worldedit_gui_about", "About", "/about")\r
24 register_gui_chatcommand("worldedit_gui_inspect", "Toggle Inspection", "/inspect", function(name, command)\r
25         command(name, worldedit.inspect[name] and "disable" or "enable")\r
26 end)\r
27 register_gui_chatcommand("worldedit_gui_reset", "Reset Region", "/reset")\r
28 register_gui_chatcommand("worldedit_gui_mark", "Mark Region", "/mark")\r
29 register_gui_chatcommand("worldedit_gui_unmark", "Unmark Region", "/unmark")\r
30 \r
31 worldedit.register_gui_function("worldedit_gui_p", {\r
32         name = "Get/Set Positions", privs = minetest.chatcommands["/p"].privs,\r
33         get_formspec = function(name)\r
34                 local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]\r
35                 return "size[9,6]" .. worldedit.get_formspec_header("worldedit_gui_p") ..\r
36                         "button_exit[0,1;3,0.8;worldedit_gui_p_get;Get Positions]" ..\r
37                         "button_exit[3,1;3,0.8;worldedit_gui_p_set1;Choose Position 1]" ..\r
38                         "button_exit[6,1;3,0.8;worldedit_gui_p_set2;Choose Position 2]" ..\r
39                         "button_exit[0,2;3,0.8;worldedit_gui_pos1;Position 1 Here]" ..\r
40                         "button_exit[3,2;3,0.8;worldedit_gui_pos2;Position 2 Here]" ..\r
41                         "label[0,3.7;Position 1]" ..\r
42                         string.format("field[2,4;1.5,0.8;worldedit_gui_fixedpos_pos1x;X ;%s]", pos1 and pos1.x or "") ..\r
43                         string.format("field[3.5,4;1.5,0.8;worldedit_gui_fixedpos_pos1y;Y ;%s]", pos1 and pos1.y or "") ..\r
44                         string.format("field[5,4;1.5,0.8;worldedit_gui_fixedpos_pos1z;Z ;%s]", pos1 and pos1.z or "") ..\r
45                         "button_exit[6.5,3.68;2.5,0.8;worldedit_gui_fixed_pos1_submit;Set Position 1]" ..\r
46                         "label[0,5.2;Position 2]" ..\r
47                         string.format("field[2,5.5;1.5,0.8;worldedit_gui_fixedpos_pos2x;X ;%s]", pos2 and pos2.x or "") ..\r
48                         string.format("field[3.5,5.5;1.5,0.8;worldedit_gui_fixedpos_pos2y;Y ;%s]", pos2 and pos2.y or "") ..\r
49                         string.format("field[5,5.5;1.5,0.8;worldedit_gui_fixedpos_pos2z;Z ;%s]", pos2 and pos2.z or "") ..\r
50                         "button_exit[6.5,5.18;2.5,0.8;worldedit_gui_fixed_pos2_submit;Set Position 2]"\r
51         end,\r
52 })\r
53 \r
54 worldedit.register_gui_handler("worldedit_gui_p", function(name, fields)\r
55         if fields.worldedit_gui_p_get then\r
56                 minetest.chatcommands["/p"].func(name, "get")\r
57                 return true\r
58         elseif fields.worldedit_gui_p_set1 then\r
59                 minetest.chatcommands["/p"].func(name, "set1")\r
60                 return true\r
61         elseif fields.worldedit_gui_p_set2 then\r
62                 minetest.chatcommands["/p"].func(name, "set2")\r
63                 return true\r
64         elseif fields.worldedit_gui_pos1 then\r
65                 minetest.chatcommands["/pos1"].func(name, "")\r
66                 worldedit.show_page(name, "worldedit_gui_p")\r
67                 return true\r
68         elseif fields.worldedit_gui_pos2 then\r
69                 minetest.chatcommands["/pos2"].func(name, "")\r
70                 worldedit.show_page(name, "worldedit_gui_p")\r
71                 return true\r
72         elseif fields.worldedit_gui_fixedpos_pos1_submit then\r
73                 minetest.chatcommands["/fixedpos"].func(name, string.format("set1 %s %s %s",\r
74                         tostring(fields.worldedit_gui_fixedpos_pos1x),\r
75                         tostring(fields.worldedit_gui_fixedpos_pos1y),\r
76                         tostring(fields.worldedit_gui_fixedpos_pos1z)))\r
77                 worldedit.show_page(name, "worldedit_gui_p")\r
78                 return true\r
79         elseif fields.worldedit_gui_fixedpos_pos2_submit then\r
80                 minetest.chatcommands["/fixedpos"].func(name, string.format("set2 %s %s %s",\r
81                         tostring(fields.worldedit_gui_fixedpos_pos2x),\r
82                         tostring(fields.worldedit_gui_fixedpos_pos2y),\r
83                         tostring(fields.worldedit_gui_fixedpos_pos2z)))\r
84                 worldedit.show_page(name, "worldedit_gui_p")\r
85                 return true\r
86         end\r
87         return false\r
88 end)\r
89 \r
90 register_gui_chatcommand("worldedit_gui_volume", "Region Volume", "/volume")\r
91 \r
92 worldedit.register_gui_function("worldedit_gui_set", {\r
93         name = "Set Nodes", privs = minetest.chatcommands["/set"].privs,\r
94         get_formspec = function(name)\r
95                 local node = gui_nodename1[name] or "Cobblestone"\r
96                 local nodename = worldedit.normalize_nodename(node)\r
97                 return "size[6.5,3]" .. worldedit.get_formspec_header("worldedit_gui_set") ..\r
98                         string.format("field[0.5,1.5;4,0.8;worldedit_gui_set_node;Name;%s]", minetest.formspec_escape(node)) ..\r
99                         "button[4,1.18;1.5,0.8;worldedit_gui_set_search;Search]" ..\r
100                         (nodename and string.format("item_image[5.5,1.1;1,1;%s]", nodename)\r
101                                 or "image[5.5,1.1;1,1;unknown_node.png]") ..\r
102                         "button_exit[0,2.5;3,0.8;worldedit_gui_set_submit;Set Nodes]"\r
103         end,\r
104 })\r
105 \r
106 worldedit.register_gui_handler("worldedit_gui_set", function(name, fields)\r
107         if fields.worldedit_gui_set_search then\r
108                 gui_nodename1[name] = tostring(fields.worldedit_gui_set_node)\r
109                 worldedit.show_page(name, "worldedit_gui_set")\r
110                 return true\r
111         elseif fields.worldedit_gui_set_submit then\r
112                 gui_nodename1[name] = tostring(fields.worldedit_gui_set_node)\r
113                 worldedit.show_page(name, "worldedit_gui_set")\r
114                 minetest.chatcommands["/set"].func(name, gui_nodename1[name])\r
115                 return true\r
116         end\r
117         return false\r
118 end)\r
119 \r
120 worldedit.register_gui_function("worldedit_gui_replace", {\r
121         name = "Replace Nodes", privs = minetest.chatcommands["/replace"].privs,\r
122         get_formspec = function(name)\r
123                 local search = gui_nodename1[name] or "Cobblestone"\r
124                 local search_nodename = worldedit.normalize_nodename(search)\r
125                 local replace = gui_nodename2[name] or "Stone"\r
126                 local replace_nodename = worldedit.normalize_nodename(replace)\r
127                 return "size[6.5,4]" .. worldedit.get_formspec_header("worldedit_gui_replace") ..\r
128                         string.format("field[0.5,1.5;4,0.8;worldedit_gui_replace_search;Name;%s]", minetest.formspec_escape(search)) ..\r
129                         "button[4,1.18;1.5,0.8;worldedit_gui_replace_search_search;Search]" ..\r
130                         (search_nodename and string.format("item_image[5.5,1.1;1,1;%s]", search_nodename)\r
131                                 or "image[5.5,1.1;1,1;unknown_node.png]") ..\r
132                         string.format("field[0.5,2.5;4,0.8;worldedit_gui_replace_replace;Name;%s]", minetest.formspec_escape(replace)) ..\r
133                         "button[4,2.18;1.5,0.8;worldedit_gui_replace_replace_search;Search]" ..\r
134                         (replace_nodename and string.format("item_image[5.5,2.1;1,1;%s]", replace_nodename)\r
135                                 or "image[5.5,2.1;1,1;unknown_node.png]") ..\r
136                         "button_exit[0,3.5;3,0.8;worldedit_gui_replace_submit;Replace Nodes]" ..\r
137                         "button_exit[3.5,3.5;3,0.8;worldedit_gui_replace_submit_inverse;Replace Inverse]"\r
138         end,\r
139 })\r
140 \r
141 worldedit.register_gui_handler("worldedit_gui_replace", function(name, fields)\r
142         if fields.worldedit_gui_replace_search_search then\r
143                 gui_nodename1[name] = tostring(fields.worldedit_gui_replace_search)\r
144                 worldedit.show_page(name, "worldedit_gui_replace")\r
145                 return true\r
146         elseif fields.worldedit_gui_replace_replace_search then\r
147                 gui_nodename2[name] = tostring(fields.worldedit_gui_replace_replace)\r
148                 worldedit.show_page(name, "worldedit_gui_replace")\r
149                 return true\r
150         elseif fields.worldedit_gui_replace_submit or fields.worldedit_gui_replace_submit_inverse then\r
151                 gui_nodename1[name] = tostring(fields.worldedit_gui_replace_search)\r
152                 gui_nodename2[name] = tostring(fields.worldedit_gui_replace_replace)\r
153                 worldedit.show_page(name, "worldedit_gui_replace")\r
154                 if fields.worldedit_gui_replace_submit then\r
155                         minetest.chatcommands["/replace"].func(name, string.format("%s %s", gui_nodename1[name], gui_nodename2[name]))\r
156                 else\r
157                         minetest.chatcommands["/replaceinverse"].func(name, string.format("%s %s", gui_nodename1[name], gui_nodename2[name]))\r
158                 end\r
159                 return true\r
160         end\r
161         return false\r
162 end)\r
163 \r
164 worldedit.register_gui_function("worldedit_gui_sphere_dome", {\r
165         name = "Sphere/Dome", privs = minetest.chatcommands["/sphere"].privs,\r
166         get_formspec = function(name)\r
167                 local node = gui_nodename1[name] or "Cobblestone"\r
168                 local radius = gui_radius[name] or "5"\r
169                 local nodename = worldedit.normalize_nodename(node)\r
170                 return "size[6.5,5]" .. worldedit.get_formspec_header("worldedit_gui_sphere_dome") ..\r
171                         string.format("field[0.5,1.5;4,0.8;worldedit_gui_sphere_dome_node;Name;%s]", minetest.formspec_escape(node)) ..\r
172                         "button[4,1.18;1.5,0.8;worldedit_gui_sphere_dome_search;Search]" ..\r
173                         (nodename and string.format("item_image[5.5,1.1;1,1;%s]", nodename)\r
174                                 or "image[5.5,1.1;1,1;unknown_node.png]") ..\r
175                         string.format("field[0.5,2.5;4,0.8;worldedit_gui_sphere_dome_radius;Radius;%s]", minetest.formspec_escape(radius)) ..\r
176                         "button_exit[0,3.5;3,0.8;worldedit_gui_sphere_dome_submit_hollow;Hollow Sphere]" ..\r
177                         "button_exit[3.5,3.5;3,0.8;worldedit_gui_sphere_dome_submit_solid;Solid Sphere]" ..\r
178                         "button_exit[0,4.5;3,0.8;worldedit_gui_sphere_dome_submit_hollow_dome;Hollow Dome]" ..\r
179                         "button_exit[3.5,4.5;3,0.8;worldedit_gui_sphere_dome_submit_solid_dome;Solid Dome]"\r
180         end,\r
181 })\r
182 \r
183 worldedit.register_gui_handler("worldedit_gui_sphere_dome", function(name, fields)\r
184         if fields.worldedit_gui_sphere_dome_search then\r
185                 gui_nodename1[name] = tostring(fields.worldedit_gui_sphere_dome_node)\r
186                 worldedit.show_page(name, "worldedit_gui_sphere_dome")\r
187                 return true\r
188         elseif fields.worldedit_gui_sphere_dome_submit_hollow or fields.worldedit_gui_sphere_dome_submit_solid\r
189         or fields.worldedit_gui_sphere_dome_submit_hollow_dome or fields.worldedit_gui_sphere_dome_submit_solid_dome then\r
190                 gui_nodename1[name] = tostring(fields.worldedit_gui_sphere_dome_node)\r
191                 gui_radius[name] = tostring(fields.worldedit_gui_sphere_dome_radius)\r
192                 worldedit.show_page(name, "worldedit_gui_sphere_dome")\r
193                 if fields.worldedit_gui_sphere_dome_submit_hollow then\r
194                         minetest.chatcommands["/hollowsphere"].func(name, string.format("%s %s", gui_radius[name], gui_nodename1[name]))\r
195                 elseif fields.worldedit_gui_sphere_dome_submit_solid then\r
196                         minetest.chatcommands["/sphere"].func(name, string.format("%s %s", gui_radius[name], gui_nodename1[name]))\r
197                 elseif fields.worldedit_gui_sphere_dome_submit_hollow_dome then\r
198                         minetest.chatcommands["/hollowdome"].func(name, string.format("%s %s", gui_radius[name], gui_nodename1[name]))\r
199                 else --fields.worldedit_gui_sphere_dome_submit_solid_dome\r
200                         minetest.chatcommands["/dome"].func(name, string.format("%s %s", gui_radius[name], gui_nodename1[name]))\r
201                 end\r
202                 return true\r
203         end\r
204         return false\r
205 end)\r
206 \r
207 worldedit.register_gui_function("worldedit_gui_cylinder", {\r
208         name = "Cylinder", privs = minetest.chatcommands["/cylinder"].privs,\r
209         get_formspec = function(name)\r
210                 local node = gui_nodename1[name] or "Cobblestone"\r
211                 local axis = gui_axis[name] or 4\r
212                 local length = gui_length[name] or "10"\r
213                 local radius = gui_radius[name] or "5"\r
214                 local nodename = worldedit.normalize_nodename(node)\r
215                 return "size[6.5,5]" .. worldedit.get_formspec_header("worldedit_gui_cylinder") ..\r
216                         string.format("field[0.5,1.5;4,0.8;worldedit_gui_cylinder_node;Name;%s]", minetest.formspec_escape(node)) ..\r
217                         "button[4,1.18;1.5,0.8;worldedit_gui_cylinder_search;Search]" ..\r
218                         (nodename and string.format("item_image[5.5,1.1;1,1;%s]", nodename)\r
219                                 or "image[5.5,1.1;1,1;unknown_node.png]") ..\r
220                         string.format("field[0.5,2.5;4,0.8;worldedit_gui_cylinder_length;Length;%s]", minetest.formspec_escape(length)) ..\r
221                         string.format("dropdown[4,2.18;2.5;worldedit_gui_cylinder_axis;X axis,Y axis,Z axis,Look direction;%d]", axis) ..\r
222                         string.format("field[0.5,3.5;4,0.8;worldedit_gui_cylinder_radius;Radius;%s]", minetest.formspec_escape(radius)) ..\r
223                         "button_exit[0,4.5;3,0.8;worldedit_gui_cylinder_submit_hollow;Hollow Cylinder]" ..\r
224                         "button_exit[3.5,4.5;3,0.8;worldedit_gui_cylinder_submit_solid;Solid Cylinder]"\r
225         end,\r
226 })\r
227 \r
228 worldedit.register_gui_handler("worldedit_gui_cylinder", function(name, fields)\r
229         if fields.worldedit_gui_cylinder_search then\r
230                 gui_nodename1[name] = fields.worldedit_gui_cylinder_node\r
231                 worldedit.show_page(name, "worldedit_gui_cylinder")\r
232                 return true\r
233         elseif fields.worldedit_gui_cylinder_submit_hollow or fields.worldedit_gui_cylinder_submit_solid then\r
234                 gui_nodename1[name] = tostring(fields.worldedit_gui_cylinder_node)\r
235                 gui_axis[name] = axis_indices[fields.worldedit_gui_cylinder_axis] or 4\r
236                 gui_length[name] = tostring(fields.worldedit_gui_cylinder_length)\r
237                 gui_radius[name] = tostring(fields.worldedit_gui_cylinder_radius)\r
238                 worldedit.show_page(name, "worldedit_gui_cylinder")\r
239                 if fields.worldedit_gui_cylinder_submit_hollow then\r
240                         minetest.chatcommands["/hollowcylinder"].func(name, string.format("%s %s %s %s", axis_values[gui_axis[name]], gui_length[name], gui_radius[name], gui_nodename1[name]))\r
241                 else --fields.worldedit_gui_cylinder_submit_solid\r
242                         minetest.chatcommands["/cylinder"].func(name, string.format("%s %s %s %s", axis_values[gui_axis[name]], gui_length[name], gui_radius[name], gui_nodename1[name]))\r
243                 end\r
244                 return true\r
245         end\r
246         return false\r
247 end)\r
248 \r
249 worldedit.register_gui_function("worldedit_gui_copy_move", {\r
250         name = "Copy/Move", privs = minetest.chatcommands["/move"].privs,\r
251         get_formspec = function(name)\r
252                 local axis = gui_axis[name] or 4\r
253                 local amount = gui_length[name] or "10"\r
254                 return "size[6.5,3]" .. worldedit.get_formspec_header("worldedit_gui_copy_move") ..\r
255                         string.format("field[0.5,1.5;4,0.8;worldedit_gui_copy_move_amount;Amount;%s]", minetest.formspec_escape(amount)) ..\r
256                         string.format("dropdown[4,1.18;2.5;worldedit_gui_copy_move_axis;X axis,Y axis,Z axis,Look direction;%d]", axis) ..\r
257                         "button_exit[0,2.5;3,0.8;worldedit_gui_copy_move_copy;Copy Region]" ..\r
258                         "button_exit[3.5,2.5;3,0.8;worldedit_gui_copy_move_move;Move Region]"\r
259         end,\r
260 })\r
261 \r
262 worldedit.register_gui_handler("worldedit_gui_copy_move", function(name, fields)\r
263         if fields.worldedit_gui_copy_move_copy or fields.worldedit_gui_copy_move_move then\r
264                 gui_axis[name] = axis_indices[fields.worldedit_gui_cylinder_axis] or 4\r
265                 gui_length[name] = tostring(fields.worldedit_gui_copy_move_amount)\r
266                 worldedit.show_page(name, "worldedit_gui_copy_move")\r
267                 if fields.worldedit_gui_copy_move_copy then\r
268                         minetest.chatcommands["/copy"].func(name, string.format("%s %s", axis_values[gui_axis[name]], gui_length[name]))\r
269                 else --fields.worldedit_gui_copy_move_move\r
270                         minetest.chatcommands["/move"].func(name, string.format("%s %s", axis_values[gui_axis[name]], gui_length[name]))\r
271                 end\r
272                 return true\r
273         end\r
274         return false\r
275 end)\r
276 \r
277 worldedit.register_gui_function("worldedit_gui_formspec_tester", {\r
278         name = "Formspec Tester",\r
279         get_formspec = function(name)\r
280                 local value = gui_formspec[name] or ""\r
281                 return "size[8,6.5]" .. worldedit.get_formspec_header("worldedit_gui_formspec_tester") ..\r
282                         string.format("textarea[0.5,1;7.5,5.5;worldedit_gui_formspec_tester_value;Formspec Code;%s]", minetest.formspec_escape(value)) ..\r
283                         "button_exit[0,6;3,0.8;worldedit_gui_formspec_tester_show;Show Formspec]"\r
284         end,\r
285 })\r
286 \r
287 worldedit.register_gui_handler("worldedit_gui_formspec_tester", function(name, fields)\r
288         if fields.worldedit_gui_formspec_tester_show then\r
289                 gui_formspec[name] = fields.worldedit_gui_formspec_tester_value or ""\r
290                 worldedit.show_page(name, "worldedit_gui_formspec_tester")\r
291                 minetest.show_formspec(name, "worldedit:formspec_tester", gui_formspec[name])\r
292                 return true\r
293         end\r
294         return false\r
295 end)