Zepha/assets/base/script/modules/gui.lua

35 lines
971 B
Lua
Raw Normal View History

2020-04-13 16:33:15 -07:00
zepha.__builtin.gui_env = { Gui = {}}
local env = zepha.__builtin.gui_env
setmetatable(env, {__index = _G})
2020-04-11 16:15:41 -07:00
-- create_element
-- Build a GUI Element with the provided constructor data, apply the metatable.
local function create_element(elem_type, data)
2020-04-13 16:33:15 -07:00
local elem = GuiElement.new(elem_type, data)
return elem
end
2020-04-11 16:15:41 -07:00
-- register_element
-- Add an element to the Gui namespace.
local function register_element(key)
if type(key) == "table" then
for _,v in pairs(key) do register_element(v) end
return
end
2020-04-13 16:33:15 -07:00
env.Gui[key] = function(data) return create_element(key, data) end
2020-04-11 16:15:41 -07:00
end
register_element({"Body", "Rect", "Text", "Model", "Button", "InventoryList"})
2020-04-11 16:15:41 -07:00
-- pc
-- Formats a number to be a percent string.
env.pc = function(num)
return tostring(num) .. "%"
end
2020-04-13 16:33:15 -07:00
-- zepha.build_gui
2020-04-11 16:15:41 -07:00
-- Allows you to Build UI Elements with the GUI namespace outside of a callback.
zepha.build_gui = function(fn)
setfenv(fn, env)
return fn()
end