123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- worldedit = worldedit or {}
- --[[
- Example:
- worldedit.register_gui_function("worldedit_gui_hollow_cylinder", {
- name = "Make Hollow Cylinder",
- privs = {worldedit=true},
- get_formspec = function(name) return "some formspec here" end,
- on_select = function(name) print(name .. " clicked the button!") end,
- })
- Use `nil` for the `options` parameter to unregister the function associated with the given identifier.
- Use `nil` for the `get_formspec` field to denote that the function does not have its own screen.
- The `privs` field may not be `nil`.
- If the identifier is already registered to another function, it will be replaced by the new one.
- The `on_select` function must not call `worldedit.show_page`
- ]]
- worldedit.pages = {} --mapping of identifiers to options
- local identifiers = {} --ordered list of identifiers
- worldedit.register_gui_function = function(identifier, options)
- if options.privs == nil or next(options.privs) == nil then
- error("privs unset")
- end
- 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)
- local enabled = true
- minetest.register_on_player_receive_fields(function(player, formname, fields)
- if not enabled then return false end
- enabled = false
- minetest.after(0.2, function() enabled = true 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) then
- return handler(name, fields)
- end
- return false
- end)
- end
- worldedit.get_formspec_header = function(identifier)
- local entry = worldedit.pages[identifier] or {}
- return "button[0,0;2,0.5;worldedit_gui;Back]" ..
- string.format("label[2,0;WorldEdit GUI > %s]", entry.name or "")
- end
- local get_formspec = function(name, identifier)
- if worldedit.pages[identifier] then
- return worldedit.pages[identifier].get_formspec(name)
- end
- return worldedit.pages["worldedit_gui"].get_formspec(name) --default to showing main page if an unknown page is given
- end
- if minetest.global_exists("sfinv") then -- sfinv installed
- assert(sfinv.enabled)
- local orig_get = sfinv.pages["sfinv:main"].get
- sfinv.override_page("sfinv:main", {
- get = function(self, player, context)
- local can_worldedit = minetest.check_player_privs(player, {worldedit=true})
- local fs = orig_get(self, player, context)
- return fs .. (can_worldedit and "image_button[0,0;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;]" or "")
- end
- })
- --show the form when the button is pressed and hide it when done
- minetest.register_on_player_receive_fields(function(player, formname, fields)
- if fields.worldedit_gui then --main page
- worldedit.show_page(player:get_player_name(), "worldedit_gui")
- return true
- elseif fields.worldedit_gui_exit then --return to original page
- sfinv.set_page(player, "sfinv:main")
- return true
- end
- return false
- end)
- worldedit.show_page = function(name, page)
- local player = minetest.get_player_by_name(name)
- if player then
- player:set_inventory_formspec(get_formspec(name, page))
- end
- end
- else
- error(
- "worldedit_gui requires a supported gui management mod to be installed.\n"..
- "To use the it you need to either:\n"..
- "* use minetest_game or another sfinv-compatible subgame\n"..
- "* install Unified Inventory, Inventory++ or Smart Inventory\n"..
- "If you don't want to use worldedit_gui, disable it by editing world.mt or from the main menu."
- )
- end
- worldedit.register_gui_function("worldedit_gui", {
- name = "WorldEdit GUI",
- privs = {interact=true},
- get_formspec = function(name)
- --create a form with all the buttons arranged in a grid
- 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 = 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)))
- index, x = index + 1, x + width
- if index == columns then --row is full
- x, y = 0, y + height
- index = 0
- end
- end
- end
- if index == 0 then --empty row
- y = y - height
- end
- return string.format("size[%g,%g]", math.max(columns * width, 5), math.max(y + 0.5, 3)) ..
- "button[0,0;2,0.5;worldedit_gui_exit;Back]" ..
- "label[2,0;WorldEdit GUI]" ..
- table.concat(buttons)
- end,
- })
- worldedit.register_gui_handler("worldedit_gui", function(name, fields)
- for identifier, entry in pairs(worldedit.pages) do --check for WorldEdit GUI main formspec button selection
- if fields[identifier] and identifier ~= "worldedit_gui" then
- --ensure player has permission to perform action
- local has_privs, missing_privs = minetest.check_player_privs(name, entry.privs)
- if not has_privs then
- worldedit.player_notify(name, "you are not allowed to use this function (missing privileges: " .. table.concat(missing_privs, ", ") .. ")")
- 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")
|