1
0
mirror of https://github.com/Uberi/Minetest-WorldEdit.git synced 2025-06-30 15:10:45 +02: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

@ -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.
]]
local pages = {} --mapping of identifiers to options
worldedit.pages = {} --mapping of identifiers to options
local identifiers = {} --ordered list of identifiers
worldedit.register_gui_function = function(identifier, options)
pages[identifier] = options
worldedit.pages[identifier] = options
table.insert(identifiers, identifier)
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)
if pages[identifier] then
return pages[identifier].get_formspec(name)
if worldedit.pages[identifier] then
return worldedit.pages[identifier].get_formspec(name)
end
return pages["worldedit_gui"].get_formspec(name)
return worldedit.pages["worldedit_gui"].get_formspec(name)
end
worldedit.show_page = function(name, page)
@ -73,42 +99,16 @@ if inventory_plus then
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", {
name = "WorldEdit GUI",
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 width, height = 3, 0.8
local columns = 5
for i, identifier in pairs(identifiers) do
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") ..
"[%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,
})
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")