1
0
mirror of https://github.com/Uberi/Minetest-WorldEdit.git synced 2024-12-26 02:30:40 +01:00

Improve worldedit_gui.

This commit is contained in:
Uberi 2013-12-10 17:07:10 -05:00
parent d4b39cba9b
commit 3fa9dfd19c
2 changed files with 168 additions and 79 deletions

View File

@ -1,6 +1,6 @@
worldedit.register_gui_function("worldedit_gui_about", { worldedit.register_gui_function("worldedit_gui_about", {
name = "About", name = "About",
privs = {worldedit=1}, privs = minetest.chatcommands["/about"].privs,
on_select = function(name) on_select = function(name)
minetest.chatcommands["/about"].func(name, "") minetest.chatcommands["/about"].func(name, "")
end, end,
@ -8,7 +8,7 @@ worldedit.register_gui_function("worldedit_gui_about", {
worldedit.register_gui_function("worldedit_gui_inspect", { worldedit.register_gui_function("worldedit_gui_inspect", {
name = "Toggle Inspection", name = "Toggle Inspection",
privs = {worldedit=1}, privs = minetest.chatcommands["/inspect"].privs,
on_select = function(name) on_select = function(name)
minetest.chatcommands["/inspect"].func(name, worldedit.inspect[name] and "disable" or "enable") minetest.chatcommands["/inspect"].func(name, worldedit.inspect[name] and "disable" or "enable")
end, end,
@ -16,7 +16,7 @@ worldedit.register_gui_function("worldedit_gui_inspect", {
worldedit.register_gui_function("worldedit_gui_reset", { worldedit.register_gui_function("worldedit_gui_reset", {
name = "Reset Region", name = "Reset Region",
privs = {worldedit=1}, privs = minetest.chatcommands["/reset"].privs,
on_select = function(name) on_select = function(name)
minetest.chatcommands["/reset"].func(name, "") minetest.chatcommands["/reset"].func(name, "")
end, end,
@ -24,7 +24,7 @@ worldedit.register_gui_function("worldedit_gui_reset", {
worldedit.register_gui_function("worldedit_gui_mark", { worldedit.register_gui_function("worldedit_gui_mark", {
name = "Mark Region", name = "Mark Region",
privs = {worldedit=1}, privs = minetest.chatcommands["/mark"].privs,
on_select = function(name) on_select = function(name)
minetest.chatcommands["/mark"].func(name, "") minetest.chatcommands["/mark"].func(name, "")
end, end,
@ -32,7 +32,7 @@ worldedit.register_gui_function("worldedit_gui_mark", {
worldedit.register_gui_function("worldedit_gui_unmark", { worldedit.register_gui_function("worldedit_gui_unmark", {
name = "Unmark Region", name = "Unmark Region",
privs = {worldedit=1}, privs = minetest.chatcommands["/unmark"].privs,
on_select = function(name) on_select = function(name)
minetest.chatcommands["/unmark"].func(name, "") minetest.chatcommands["/unmark"].func(name, "")
end, end,
@ -40,7 +40,7 @@ worldedit.register_gui_function("worldedit_gui_unmark", {
worldedit.register_gui_function("worldedit_gui_pos1", { worldedit.register_gui_function("worldedit_gui_pos1", {
name = "Position 1 Here", name = "Position 1 Here",
privs = {worldedit=1}, privs = minetest.chatcommands["/pos1"].privs,
on_select = function(name) on_select = function(name)
minetest.chatcommands["/pos1"].func(name, "") minetest.chatcommands["/pos1"].func(name, "")
end, end,
@ -48,7 +48,7 @@ worldedit.register_gui_function("worldedit_gui_pos1", {
worldedit.register_gui_function("worldedit_gui_pos2", { worldedit.register_gui_function("worldedit_gui_pos2", {
name = "Position 2 Here", name = "Position 2 Here",
privs = {worldedit=1}, privs = minetest.chatcommands["/pos2"].privs,
on_select = function(name) on_select = function(name)
minetest.chatcommands["/pos2"].func(name, "") minetest.chatcommands["/pos2"].func(name, "")
end, end,
@ -56,7 +56,7 @@ worldedit.register_gui_function("worldedit_gui_pos2", {
worldedit.register_gui_function("worldedit_gui_p", { worldedit.register_gui_function("worldedit_gui_p", {
name = "Get/Set Positions", name = "Get/Set Positions",
privs = {worldedit=1}, privs = minetest.chatcommands["/p"].privs,
get_formspec = function(name) get_formspec = function(name)
return "size[12,2]" .. return "size[12,2]" ..
"button[0,0;2,0.5;worldedit_gui;Back]" .. "button[0,0;2,0.5;worldedit_gui;Back]" ..
@ -68,12 +68,7 @@ worldedit.register_gui_function("worldedit_gui_p", {
end, end,
}) })
minetest.register_on_player_receive_fields(function(player, formname, fields) worldedit.register_gui_handler("worldedit_gui_p", function(name, fields)
if fields.quit then
return false
end
local name = player:get_player_name()
if fields.worldedit_gui_p_get then if fields.worldedit_gui_p_get then
minetest.chatcommands["/p"].func(name, "get") minetest.chatcommands["/p"].func(name, "get")
return true return true
@ -92,7 +87,7 @@ end)
worldedit.register_gui_function("worldedit_gui_fixedpos", { worldedit.register_gui_function("worldedit_gui_fixedpos", {
name = "Fixed Positions", name = "Fixed Positions",
privs = {worldedit=1}, privs = minetest.chatcommands["/fixedpos"].privs,
get_formspec = function(name) get_formspec = function(name)
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
return "size[6.5,4]" .. return "size[6.5,4]" ..
@ -110,18 +105,14 @@ worldedit.register_gui_function("worldedit_gui_fixedpos", {
end end
}) })
minetest.register_on_player_receive_fields(function(player, formname, fields) worldedit.register_gui_handler("worldedit_gui_fixedpos", function(name, fields)
if fields.quit then
return false
end
if fields.worldedit_gui_fixedpos_submit then if fields.worldedit_gui_fixedpos_submit then
if tonumber(fields.worldedit_gui_fixedpos_pos1x) and tonumber(fields.worldedit_gui_fixedpos_pos1y) and tonumber(fields.worldedit_gui_fixedpos_pos1z) then if tonumber(fields.worldedit_gui_fixedpos_pos1x) and tonumber(fields.worldedit_gui_fixedpos_pos1y) and tonumber(fields.worldedit_gui_fixedpos_pos1z) then
minetest.chatcommands["/fixedpos"].func(player:get_player_name(), string.format("set1 %d %d %d", minetest.chatcommands["/fixedpos"].func(name, string.format("set1 %d %d %d",
tonumber(fields.worldedit_gui_fixedpos_pos1x), tonumber(fields.worldedit_gui_fixedpos_pos1y), tonumber(fields.worldedit_gui_fixedpos_pos1z))) tonumber(fields.worldedit_gui_fixedpos_pos1x), tonumber(fields.worldedit_gui_fixedpos_pos1y), tonumber(fields.worldedit_gui_fixedpos_pos1z)))
end end
if tonumber(fields.worldedit_gui_fixedpos_pos2x) and tonumber(fields.worldedit_gui_fixedpos_pos2y) and tonumber(fields.worldedit_gui_fixedpos_pos2z) then if tonumber(fields.worldedit_gui_fixedpos_pos2x) and tonumber(fields.worldedit_gui_fixedpos_pos2y) and tonumber(fields.worldedit_gui_fixedpos_pos2z) then
minetest.chatcommands["/fixedpos"].func(player:get_player_name(), string.format("set2 %d %d %d", minetest.chatcommands["/fixedpos"].func(name, string.format("set2 %d %d %d",
tonumber(fields.worldedit_gui_fixedpos_pos2x), tonumber(fields.worldedit_gui_fixedpos_pos2y), tonumber(fields.worldedit_gui_fixedpos_pos2z))) tonumber(fields.worldedit_gui_fixedpos_pos2x), tonumber(fields.worldedit_gui_fixedpos_pos2y), tonumber(fields.worldedit_gui_fixedpos_pos2z)))
end end
return true return true
@ -131,48 +122,126 @@ end)
worldedit.register_gui_function("worldedit_gui_volume", { worldedit.register_gui_function("worldedit_gui_volume", {
name = "Region Volume", name = "Region Volume",
privs = {worldedit=1}, privs = minetest.chatcommands["/volume"].privs,
on_select = function(name) on_select = function(name)
minetest.chatcommands["/volume"].func(name, "") minetest.chatcommands["/volume"].func(name, "")
end, end,
}) })
local search_nodes = {} local gui_nodename_set = {}
worldedit.register_gui_function("worldedit_gui_set", { worldedit.register_gui_function("worldedit_gui_set", {
name = "Set Nodes", name = "Set Nodes",
privs = {worldedit=1}, privs = minetest.chatcommands["/set"].privs,
get_formspec = function(name) get_formspec = function(name)
local value = search_nodes[name] local value = gui_nodename_set[name] or "Cobblestone"
local nodename local nodename = worldedit.normalize_nodename(value)
if value then value = nodename or value
nodename = worldedit.normalize_nodename(value) return "size[6.5,3]" ..
if nodename then
value = nodename
end
end
return "size[6,3]" ..
"button[0,0;2,0.5;worldedit_gui;Back]" .. "button[0,0;2,0.5;worldedit_gui;Back]" ..
"label[2,0;WorldEdit GUI > Set Nodes]" .. "label[2,0;WorldEdit GUI > Set Nodes]" ..
string.format("field[0.5,1.5;4,0.8;worldedit_gui_set_node;Name;%s]", value and minetest.formspec_escape(value) or "") .. string.format("field[0.5,1.5;4,0.8;worldedit_gui_set_node;Name;%s]", minetest.formspec_escape(value)) ..
"button[4,1.17;2,0.8;worldedit_gui_set_search;Search]" .. "button[4,1.18;1.5,0.8;worldedit_gui_set_search;Search]" ..
(nodename and string.format("item_image[4.5,2;1,1;%s]", nodename) or "image[4.5,2;1,1;unknown_node.png]") .. (nodename and string.format("item_image[5.5,1.18;1,1;%s]", nodename)
or "image[5.5,1.18;1,1;unknown_node.png]") ..
"button_exit[0,2.5;4,0.8;worldedit_gui_set_submit;Set Nodes]" "button_exit[0,2.5;4,0.8;worldedit_gui_set_submit;Set Nodes]"
end, end,
}) })
minetest.register_on_player_receive_fields(function(player, formname, fields) worldedit.register_gui_handler("worldedit_gui_set", function(name, fields)
if fields.quit then
return false
end
local name = player:get_player_name()
if fields.worldedit_gui_set_search then if fields.worldedit_gui_set_search then
search_nodes[name] = fields.worldedit_gui_set_node gui_nodename_set[name] = fields.worldedit_gui_set_node
worldedit.show_page(name, "worldedit_gui_set") worldedit.show_page(name, "worldedit_gui_set")
return true return true
elseif fields.worldedit_gui_set_submit then elseif fields.worldedit_gui_set_submit then
search_nodes[name] = fields.worldedit_gui_set_node gui_nodename_set[name] = fields.worldedit_gui_set_node
minetest.chatcommands["/set"].func(name, fields.worldedit_gui_set_node) minetest.chatcommands["/set"].func(name, gui_nodename_set[name])
return true
end
return false
end)
local gui_nodename_replace = {}
worldedit.register_gui_function("worldedit_gui_replace", {
name = "Replace Nodes",
privs = minetest.chatcommands["/replace"].privs,
get_formspec = function(name)
local search_value = gui_nodename_set[name] or "Cobblestone"
local search_nodename = worldedit.normalize_nodename(search_value)
search_value = search_nodename or search_value
local replace_value, replace_nodename = gui_nodename_replace[name] or "Stone"
local replace_nodename = worldedit.normalize_nodename(replace_value)
replace_value = replace_nodename or replace_value
return "size[6,4]" ..
"button[0,0;2,0.5;worldedit_gui;Back]" ..
"label[2,0;WorldEdit GUI > Replace Nodes]" ..
string.format("field[0.5,1.5;4,0.8;worldedit_gui_replace_search;Name;%s]", minetest.formspec_escape(search_value)) ..
"button[4,1.18;1.5,0.8;worldedit_gui_replace_search_search;Search]" ..
(search_nodename and string.format("item_image[5.5,1.18;1,1;%s]", search_nodename)
or "image[5.5,1.18;1,1;unknown_node.png]") ..
string.format("field[0.5,2.5;4,0.8;worldedit_gui_replace_replace;Name;%s]", minetest.formspec_escape(replace_value)) ..
"button[4,2.18;1.5,0.8;worldedit_gui_replace_replace_search;Search]" ..
(replace_nodename and string.format("item_image[5.5,2.18;1,1;%s]", replace_nodename)
or "image[5.5,2.18;1,1;unknown_node.png]") ..
"button_exit[0,3.5;4,0.8;worldedit_gui_replace_submit;Replace Nodes]"
end,
})
worldedit.register_gui_handler("worldedit_gui_replace", function(name, fields)
if fields.worldedit_gui_replace_search_search then
gui_nodename_set[name] = fields.worldedit_gui_replace_search
worldedit.show_page(name, "worldedit_gui_replace")
return true
elseif fields.worldedit_gui_replace_replace_search then
gui_nodename_replace[name] = fields.worldedit_gui_replace_replace
worldedit.show_page(name, "worldedit_gui_replace")
return true
elseif fields.worldedit_gui_replace_submit then
gui_nodename_set[name] = fields.worldedit_gui_replace_search
gui_nodename_replace[name] = fields.worldedit_gui_replace_replace
minetest.chatcommands["/replace"].func(name, string.format("%s %s", gui_nodename_set[name], gui_nodename_replace[name]))
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_replaceinverse", {
name = "Replace Inverse",
privs = minetest.chatcommands["/replaceinverse"].privs,
get_formspec = function(name)
local search_value = gui_nodename_set[name] or "Cobblestone"
local search_nodename = worldedit.normalize_nodename(search_value)
search_value = search_nodename or search_value
local replace_value, replace_nodename = gui_nodename_replace[name] or "Stone"
local replace_nodename = worldedit.normalize_nodename(replace_value)
replace_value = replace_nodename or replace_value
return "size[6,4]" ..
"button[0,0;2,0.5;worldedit_gui;Back]" ..
"label[2,0;WorldEdit GUI > Replace Inverse]" ..
string.format("field[0.5,1.5;4,0.8;worldedit_gui_replaceinverse_search;Name;%s]", minetest.formspec_escape(search_value)) ..
"button[4,1.18;1.5,0.8;worldedit_gui_replaceinverse_search_search;Search]" ..
(search_nodename and string.format("item_image[5.5,1.18;1,1;%s]", search_nodename)
or "image[5.5,1.18;1,1;unknown_node.png]") ..
string.format("field[0.5,2.5;4,0.8;worldedit_gui_replaceinverse_replace;Name;%s]", minetest.formspec_escape(replace_value)) ..
"button[4,2.18;1.5,0.8;worldedit_gui_replaceinverse_replace_search;Search]" ..
(replace_nodename and string.format("item_image[5.5,2.18;1,1;%s]", replace_nodename)
or "image[5.5,2.18;1,1;unknown_node.png]") ..
"button_exit[0,3.5;4,0.8;worldedit_gui_replaceinverse_submit;Replace Inverse]"
end,
})
worldedit.register_gui_handler("worldedit_gui_replaceinverse", function(name, fields)
if fields.worldedit_gui_replaceinverse_search_search then
gui_nodename_set[name] = fields.worldedit_gui_replaceinverse_search
worldedit.show_page(name, "worldedit_gui_replaceinverse")
return true
elseif fields.worldedit_gui_replaceinverse_replace_search then
gui_nodename_replace[name] = fields.worldedit_gui_replaceinverse_replace
worldedit.show_page(name, "worldedit_gui_replaceinverse")
return true
elseif fields.worldedit_gui_replaceinverse_submit then
gui_nodename_set[name] = fields.worldedit_gui_replaceinverse_search
gui_nodename_replace[name] = fields.worldedit_gui_replaceinverse_replace
minetest.chatcommands["/replaceinverse"].func(name, string.format("%s %s", gui_nodename_set[name], gui_nodename_replace[name]))
return true return true
end end
return false return false

View File

@ -22,18 +22,44 @@ Use `nil` for the `privs` field to denote that no special privileges are require
If the identifier is already registered to another function, it will be replaced by the new one. If the identifier is already registered to another function, it will be replaced by the new one.
]] ]]
local pages = {} --mapping of identifiers to options worldedit.pages = {} --mapping of identifiers to options
local identifiers = {} --ordered list of identifiers local identifiers = {} --ordered list of identifiers
worldedit.register_gui_function = function(identifier, options) worldedit.register_gui_function = function(identifier, options)
pages[identifier] = options worldedit.pages[identifier] = options
table.insert(identifiers, identifier) table.insert(identifiers, identifier)
end end
--[[
Example:
worldedit.register_gui_handler("worldedit_gui_hollow_cylinder", function(name, fields)
print(minetest.serialize(fields))
end)
]]
worldedit.register_gui_handler = function(identifier, handler)
minetest.register_on_player_receive_fields(function(player, formname, fields)
--ensure the form is not being exited since this is a duplicate message
if fields.quit then
return false
end
local name = player:get_player_name()
--ensure the player has permission to perform the action
local entry = worldedit.pages[identifier]
if entry and minetest.check_player_privs(name, entry.privs or {}) then
return handler(name, fields)
end
return false
end)
end
local get_formspec = function(name, identifier) local get_formspec = function(name, identifier)
if pages[identifier] then if worldedit.pages[identifier] then
return pages[identifier].get_formspec(name) return worldedit.pages[identifier].get_formspec(name)
end end
return pages["worldedit_gui"].get_formspec(name) return worldedit.pages["worldedit_gui"].get_formspec(name)
end end
worldedit.show_page = function(name, page) worldedit.show_page = function(name, page)
@ -73,42 +99,16 @@ if inventory_plus then
end end
end end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if fields.quit then
return false
end
--check for WorldEdit GUI main formspec button selection
for identifier, entry in pairs(pages) do
if fields[identifier] then
local name = player:get_player_name()
--ensure player has permission to perform action
if entry.privs and not minetest.check_player_privs(name, entry.privs) then
return false
end
if entry.on_select then
entry.on_select(name)
end
if entry.get_formspec then
worldedit.show_page(name, identifier)
end
return true
end
end
return false
end)
worldedit.register_gui_function("worldedit_gui", { worldedit.register_gui_function("worldedit_gui", {
name = "WorldEdit GUI", name = "WorldEdit GUI",
get_formspec = function(name) get_formspec = function(name)
--create a form with all the buttons arranged in a grid --create a form with all the buttons arranged in a grid --wip: show only buttons that the player has privs for
local buttons, x, y, index = {}, 0, 1, 0 local buttons, x, y, index = {}, 0, 1, 0
local width, height = 3, 0.8 local width, height = 3, 0.8
local columns = 5 local columns = 5
for i, identifier in pairs(identifiers) do for i, identifier in pairs(identifiers) do
if identifier ~= "worldedit_gui" then if identifier ~= "worldedit_gui" then
local entry = pages[identifier] local entry = worldedit.pages[identifier]
table.insert(buttons, string.format((entry.get_formspec and "button" or "button_exit") .. table.insert(buttons, string.format((entry.get_formspec and "button" or "button_exit") ..
"[%g,%g;%g,%g;%s;%s]", x, y, width, height, identifier, minetest.formspec_escape(entry.name))) "[%g,%g;%g,%g;%s;%s]", x, y, width, height, identifier, minetest.formspec_escape(entry.name)))
@ -126,4 +126,24 @@ worldedit.register_gui_function("worldedit_gui", {
end, end,
}) })
worldedit.register_gui_handler("worldedit_gui", function(name, fields)
--check for WorldEdit GUI main formspec button selection
for identifier, entry in pairs(worldedit.pages) do
if fields[identifier] then
--ensure player has permission to perform action
if not minetest.check_player_privs(name, entry.privs or {}) then
return false
end
if entry.on_select then
entry.on_select(name)
end
if entry.get_formspec then
worldedit.show_page(name, identifier)
end
return true
end
end
return false
end)
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/functionality.lua") dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/functionality.lua")