mirror of
https://github.com/mt-mods/home_workshop_modpack.git
synced 2024-11-15 23:20:22 +01:00
add demo formspec
This commit is contained in:
parent
f57b1642b9
commit
a844c7ec5e
|
@ -21,5 +21,5 @@ read_globals = {
|
|||
"vector", "ItemStack", "dump", "DIR_DELIM", "VoxelArea", "Settings", "PcgRandom", "VoxelManip", "PseudoRandom",
|
||||
|
||||
--mod produced
|
||||
"default", "screwdriver", "unifieddyes",
|
||||
"default", "screwdriver", "unifieddyes", "formspec_ast",
|
||||
}
|
|
@ -1,3 +1,46 @@
|
|||
--local usage
|
||||
local futil = computers.formspec
|
||||
|
||||
computers.show_formspec = computers.formspec.show_formspec
|
||||
|
||||
function computers.load_gui(pos, node, clicker)
|
||||
minetest.chat_send_all("test")
|
||||
--minetest.chat_send_all("test")
|
||||
|
||||
local formspec = {
|
||||
formspec_version = 4,
|
||||
{
|
||||
type = "size",
|
||||
w = 10,
|
||||
h = 10,
|
||||
},
|
||||
{
|
||||
type = "label",
|
||||
x = 1,
|
||||
y = 1,
|
||||
label = "test label",
|
||||
},
|
||||
{
|
||||
type = "button",
|
||||
x = 1,
|
||||
y = 2,
|
||||
w = 5,
|
||||
h = 2,
|
||||
name = "test_btn",
|
||||
label = "test btn",
|
||||
on_event = function(form, player, element)
|
||||
local cindex = futil.get_index_by_name(form, "test_btn")
|
||||
form[cindex] = {type = "label", x=1, y=3, label = "test button label"}
|
||||
|
||||
return form
|
||||
end,
|
||||
props = {
|
||||
border = false,
|
||||
bgimg = "kuto_button.png^[combine:16x16^[noalpha^[colorize:#ffffff70",
|
||||
bgimg_hovered = "kuto_button.png^[combine:16x16^[noalpha^[colorize:#ffffff90",
|
||||
bgimg_middle = "4,4",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
computers.show_formspec(clicker, "computers:gui", formspec)
|
||||
end
|
95
computers/formspec.lua
Normal file
95
computers/formspec.lua
Normal file
|
@ -0,0 +1,95 @@
|
|||
local registered_astk = {}
|
||||
computers.formspec = {}
|
||||
|
||||
computers.formspec.get_element_by_name = formspec_ast.get_element_by_name
|
||||
|
||||
function computers.formspec.get_index_by_name(tree, name)
|
||||
if type(tree) ~= "table" then return end
|
||||
|
||||
for key, element in pairs(tree) do
|
||||
if type(element) == "table" and element.name and element.name == name then
|
||||
return key
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--note this is terrible hard coded
|
||||
local function insert_styles(form, styles)
|
||||
local headers = {size = true, position = true, anchor = true, no_prepend = true, real_cordinates = true}
|
||||
local cindex = 0
|
||||
local fs = {}
|
||||
|
||||
for key, val in pairs(form) do
|
||||
if type(val) == "number" and not tonumber(key) then
|
||||
fs[key] = val
|
||||
elseif type(val) == "table" and val.type and headers[val.type] then
|
||||
table.insert(fs, val)
|
||||
cindex = key
|
||||
end
|
||||
end
|
||||
|
||||
for _, val in pairs(styles) do
|
||||
table.insert(fs, val)
|
||||
end
|
||||
|
||||
cindex = cindex+1
|
||||
for i=cindex, #form do
|
||||
table.insert(fs, form[i])
|
||||
end
|
||||
|
||||
return fs
|
||||
end
|
||||
|
||||
function computers.formspec.convert_to_ast(form)
|
||||
local styles = {}
|
||||
|
||||
|
||||
local function rfind(fs)
|
||||
for key, val in pairs(fs) do
|
||||
if type(val) == "table" and val.type and val.type:find("container") then
|
||||
rfind(val)
|
||||
elseif type(val) == "table" and val.props then
|
||||
table.insert(styles, {type = "style", selectors = val.selectors or {val.name}, props = val.props})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
rfind(form)
|
||||
|
||||
local fs = insert_styles(form, styles)
|
||||
return fs
|
||||
end
|
||||
|
||||
function computers.formspec.show_formspec(player, formname, fs)
|
||||
local playername = player
|
||||
local formspec = fs
|
||||
|
||||
if type(player) == "userdata" then
|
||||
playername = player:get_player_name()
|
||||
end
|
||||
if type(fs) == "table" then
|
||||
formspec = formspec_ast.unparse(computers.formspec.convert_to_ast(fs))
|
||||
registered_astk[playername] = fs
|
||||
end
|
||||
|
||||
minetest.show_formspec(playername, formname, formspec)
|
||||
end
|
||||
|
||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname ~= "computers:gui" then return end
|
||||
local pname = player:get_player_name()
|
||||
|
||||
if fields.quit then registered_astk[pname] = nil return end
|
||||
|
||||
local keys = {}
|
||||
for key, val in pairs(fields) do table.insert(keys, key) end
|
||||
|
||||
local element = computers.formspec.get_element_by_name(registered_astk[pname], keys[1])
|
||||
|
||||
if element and element.on_event then
|
||||
--on_event(form, player, element)
|
||||
local form = element.on_event(registered_astk[pname], player, element)
|
||||
|
||||
if form then computers.formspec.show_formspec(player, formname, form) end
|
||||
end
|
||||
end)
|
|
@ -2,5 +2,6 @@ computers = {}
|
|||
computers.modpath = minetest.get_modpath("computers")
|
||||
|
||||
dofile(computers.modpath .. "/old_stuff/init.lua")
|
||||
dofile(computers.modpath .. "/formspec.lua")
|
||||
dofile(computers.modpath .. "/api.lua")
|
||||
dofile(computers.modpath .. "/demo.lua")
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
name = computers
|
||||
depends = formspec_ast
|
||||
optional_depends = screwdriver, default, basic_materials
|
||||
|
|
BIN
computers/textures/kuto_button.png
Normal file
BIN
computers/textures/kuto_button.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 504 B |
Loading…
Reference in New Issue
Block a user