diff options
author | the lemons <citrons@mondecitronne.com> | 2022-02-12 21:38:19 -0600 |
---|---|---|
committer | the lemons <citrons@mondecitronne.com> | 2022-02-12 21:38:19 -0600 |
commit | 516f16fa7372f2667e829a98d2011f5f704eb690 (patch) | |
tree | a7d5fb210b093a207c0655c521718293c1fbf6dc | |
parent | 0448b0ad249f21a0eab80765e469411512dc0b31 (diff) |
formspec generation
-rw-r--r-- | mods/vzxv_inv/mod.conf | 2 | ||||
-rw-r--r-- | mods/vzxv_inv/ui.lua | 35 | ||||
-rw-r--r-- | mods/vzxv_ui/init.lua | 41 | ||||
-rw-r--r-- | mods/vzxv_ui/mod.conf | 2 |
4 files changed, 51 insertions, 29 deletions
diff --git a/mods/vzxv_inv/mod.conf b/mods/vzxv_inv/mod.conf index 1e0ca8c..82dfe70 100644 --- a/mods/vzxv_inv/mod.conf +++ b/mods/vzxv_inv/mod.conf @@ -1,3 +1,3 @@ name=vzxv_inv description = vzxv inventory system -depends = vzxv +depends = vzxv, vzxv_ui diff --git a/mods/vzxv_inv/ui.lua b/mods/vzxv_inv/ui.lua index 09f690e..f74299b 100644 --- a/mods/vzxv_inv/ui.lua +++ b/mods/vzxv_inv/ui.lua @@ -1,34 +1,13 @@ -local function formspec(template) - return function(t) - return template:gsub("([%%%$])([%w%-]+)", function(prefix,k) - if prefix == '%' then - if type(t[k]) == 'function' then - local s = {} - for v in t[k] do - table.insert(s,v) - end - return table.concat(s) - else - return t[k] - end - else - return minetest.formspec_escape(tostring(t[k])) - end - end) - end -end - -local inventory = formspec [[ - formspec_version[4] - size[2,2] - item_image[0.5,0.5;1,1;vzxv:apioform] -]] +local inventory = vzxv.formspec(function() + formspec_version(4) + size{2, 2} + item_image({0.5, 0.5}, {1, 1}, "vzxv:apioform") +end) minetest.register_on_joinplayer(function(player) - player:set_inventory_formspec(inventory { - text = "bee you" - }) + player:set_inventory_formspec(inventory()) + print(inventory()) end) minetest.register_on_player_receive_fields(function(player, formname, fields) diff --git a/mods/vzxv_ui/init.lua b/mods/vzxv_ui/init.lua new file mode 100644 index 0000000..28be04d --- /dev/null +++ b/mods/vzxv_ui/init.lua @@ -0,0 +1,41 @@ +local elements = {} +elements.elem = elements + +setmetatable(elements, { + __index = function(_, element) + return function(...) + local args = {...} + local result = {} + table.insert(result, element) + table.insert(result, '[') + for i=1,#args do + if type(args[i]) ~= 'table' then + table.insert(result, + minetest.formspec_escape(tostring(args[i]))) + else + local s = ('%f,%f'):format(unpack(args[i])) + table.insert(result, s) + end + if i ~= #args then + table.insert(result, ';') + end + end + table.insert(result, ']') + coroutine.yield(table.concat(result)) + end + end, +}) + +function vzxv.formspec(spec) + setfenv(spec, elements) + return function(...) + local co = coroutine.create(spec) + local result = {} + while coroutine.status(co) ~= "dead" do + local ok, s = coroutine.resume(co, ...) + if not ok then error(s) end + table.insert(result, s) + end + return table.concat(result) + end +end diff --git a/mods/vzxv_ui/mod.conf b/mods/vzxv_ui/mod.conf new file mode 100644 index 0000000..f3b49c6 --- /dev/null +++ b/mods/vzxv_ui/mod.conf @@ -0,0 +1,2 @@ +name=vzxv_ui +depends = vzxv |