red_glazed_terracotta/mods/rgt_ui/init.lua
2025-10-27 22:05:23 -04:00

62 lines
2.2 KiB
Lua

-- The purpose of this module is to abstract away element theming in formspecs.
-- While formspecs themselves are a good UI format, custom theming can become
-- troublesome, especially when it needs to be added into every single formspec.
-- This module also abstracts away the construction of some more complex widgets.
ui = {}
local ns = ui
function ns.headers(w, h)
return "formspec_version[10]\
size["..w..","..h.."]\
"
end
function ns.container(x --[[Float]], y --[[Float]], body --[[String]]) --> String
return string.format("container[%f,%f]\
%s\
container_end[]\
", x, y, body)
end
-- Note: This automatically creates the container's associated scrollbar, just in case scrollbar theming is ever added.
function ns.scroll_container(x, y, width, height, name, orientation, factor, padding, body)
return string.format("scroll_container[%f,%f;%f,%f;%s;%s;%s;%s]\
%s\
scroll_container_end[]\
%s\
", x, y, width, height, name or "", orientation or "", factor and tostring(factor) or "", padding or "", ns.scrollbar(x +width -0.25, y, 0.25, height, name, orientation))
end
function ns.scrollbar(x, y, width, height, name, value, main, max, smallstep, largestep, thumbsize, arrows)
local out = string.format("scrollbar[%f,%f;%f,%f;%s;%s]", x, y, width, height, name, value and tostring(value) or "")
return out
end
function ns.label(x --[[FLoat]], y --[[Float]], text --[[String]]) --> String
return string.format("label[%f,%f;%s]\n", x, y, text)
end
function ns.button(x --[[Float]], y --[[Float]], width --[[Float]], height --[[Float]], name --[[String?]], label --[[String?]]) --> String
return string.format("button[%f,%f;%f,%f;%s;%s]\n", x, y, name or "", label or "")
end
function ns.list(location, list, x, y, w, h)
local out = {}
for a = 0, w -1 do
for b = 0, h -1 do
out[#out +1] = "\
image["
out[#out +1] = a *1.25 +x -0.0625
out[#out +1] = ","
out[#out +1] = b *1.25 +y -0.0625
out[#out +1] = ";1.14,1.14;rgt_other_button_bg.png;8,8]"
end
end
out[#out +1] = string.format("list[%s;%s;%f,%f;%i,%i]", location, list, x, y, w, h)
return table.concat(out)
end
local function show_test_view()
ui.begin()
end