diff options
author | Uberi <azhang9@gmail.com> | 2013-12-10 17:07:10 -0500 |
---|---|---|
committer | Uberi <azhang9@gmail.com> | 2013-12-10 17:07:10 -0500 |
commit | 3fa9dfd19cf9ba466abcb49f3d0bc77f1b1aba7c (patch) | |
tree | 6373cfcaf808f1b276f25dc1483fe4c930eb64fc /worldedit_gui/init.lua | |
parent | d4b39cba9bd1be36b8daf4f6b50b9e3eaf2efb80 (diff) |
Improve worldedit_gui.
Diffstat (limited to 'worldedit_gui/init.lua')
-rw-r--r-- | worldedit_gui/init.lua | 86 |
1 files changed, 53 insertions, 33 deletions
diff --git a/worldedit_gui/init.lua b/worldedit_gui/init.lua index 82936d8..3aca3a9 100644 --- a/worldedit_gui/init.lua +++ b/worldedit_gui/init.lua @@ -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")
\ No newline at end of file |