Browse Source

Change how works, create pre-baked formspecs

master
luk3yx 1 year ago
parent
commit
b058e21b77
5 changed files with 93 additions and 4 deletions
  1. 1
    2
      README.md
  2. 12
    2
      core.lua
  3. 78
    0
      extras.lua
  4. 1
    0
      init.lua
  5. 1
    0
      objects.lua

+ 1
- 2
README.md View File

@@ -1,4 +1,3 @@
# formspeclib

A formspec renderer for Minetest. Currently, all the documentation is in
comments inside `core.lua` and `objects.lua`.
A formspec renderer for Minetest.

+ 12
- 2
core.lua View File

@@ -43,6 +43,9 @@ formspeclib.render = function(formspec, safe_mode, no_iterations)
else
height = 0
end
if type(no_iterations) ~= 'number' and no_iterations then
no_iterations = 1
end
for i = 1, #formspec do
if safe_mode and type(formspec[i]) ~= 'table' then
return false
@@ -65,8 +68,15 @@ formspeclib.render = function(formspec, safe_mode, no_iterations)
end
if type(o) == 'string' then
compiled = compiled .. o
elseif type(o) == 'table' and not no_iterations then
o = formspeclib.render(o, safe_mode, safe_mode or no_iterations)
elseif type(o) == 'table' then
if no_iterations and no_iterations < 1 then return false end
local iter
if no_iterations then
iter = no_iterations - 1
elseif safe_mode then
iter = 3
end
o = formspeclib.render(o, safe_mode, iter)
if not o then return false end
compiled = compiled .. o
else

+ 78
- 0
extras.lua View File

@@ -0,0 +1,78 @@
--
-- formspeclib pre-baked objects
--
-- Making formspecs even more fun!
--

--
-- formspeclib:player_inventory - The current player's inventory
--
-- Syntax: x, y
--
formspeclib.register_object('formspeclib:player_inventory', function(obj, safe_mode)
if type(obj.y) ~= 'number' then return false end
return {{
type = 'inventory',
x = obj.x,
y = obj.y,
width = 8,
height = 1,
location = 'current_player',
name = 'main',
},
{
type = 'inventory',
x = obj.x,
y = obj.y + 1.23,
width = 8,
height = 3,
location = 'current_player',
name = 'main',
start_at = 8,
}}
end)

--
-- formspeclib:node_inventory - The current node's inventory
--
-- Syntax: x, y, width(?), height(?), name(?)
--
formspeclib.register_object('formspeclib:node_inventory', function(obj, safe_mode)
return {{
type = 'inventory',
x = obj.x,
y = obj.y,
width = obj.width or 8,
height = obj.height or 4,
location = 'context',
name = obj.name or 'main',
}}
end)

--
-- formspeclib:chest - Most useful with chest touchscreens.
--
-- Syntax: width(?), height(?)
--
formspeclib.register_object('formspeclib:chest', function(obj, safe_mode)
if type(obj.width) ~= 'number' then obj.width = 8 end
if type(obj.height) ~= 'number' then obj.height = 4 end
return {
width = obj.width,
height = obj.height + 5,
{
type = 'formspeclib:node_inventory',
x = 0,
y = 0.3,
width = obj.width,
height = obj.height,
name = obj.name,
},
{
type = 'formspeclib:player_inventory',
x = (obj.width / 2) - 4,
y = obj.height + 0.82,
},
}
end)

+ 1
- 0
init.lua View File

@@ -8,3 +8,4 @@
local path = minetest.get_modpath('formspeclib')
dofile(path .. '/core.lua') -- formspeclib core
dofile(path .. '/objects.lua') -- Default formspeclib objects
dofile(path .. '/extras.lua') -- Easy to use pre-baked formspec chunks

+ 1
- 0
objects.lua View File

@@ -183,7 +183,7 @@ end)
--
-- A combo box
--
-- Parameters: x, y, width, height(?), name, items, label(?), default(?)
--
formspeclib.register_object('combobox', function(obj, safe_mode)
if not obj.x or not obj.y or not obj.width or not obj.name or not obj.items then

Loading…
Cancel
Save