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 @@
1 1
 # formspeclib
2 2
 
3
-A formspec renderer for Minetest. Currently, all the documentation is in
4
-comments inside `core.lua` and `objects.lua`.
3
+A formspec renderer for Minetest.

+ 12
- 2
core.lua View File

@@ -43,6 +43,9 @@ formspeclib.render = function(formspec, safe_mode, no_iterations)
43 43
     else
44 44
         height = 0
45 45
     end
46
+    if type(no_iterations) ~= 'number' and no_iterations then
47
+        no_iterations = 1
48
+    end
46 49
     for i = 1, #formspec do
47 50
         if safe_mode and type(formspec[i]) ~= 'table' then
48 51
             return false
@@ -65,8 +68,15 @@ formspeclib.render = function(formspec, safe_mode, no_iterations)
65 68
             end
66 69
             if type(o) == 'string' then
67 70
                 compiled = compiled .. o
68
-            elseif type(o) == 'table' and not no_iterations then
69
-                o = formspeclib.render(o, safe_mode, safe_mode or no_iterations)
71
+            elseif type(o) == 'table' then
72
+                if no_iterations and no_iterations < 1 then return false end
73
+                local iter
74
+                if no_iterations then
75
+                    iter = no_iterations - 1
76
+                elseif safe_mode then
77
+                    iter = 3
78
+                end
79
+                o = formspeclib.render(o, safe_mode, iter)
70 80
                 if not o then return false end
71 81
                 compiled = compiled .. o
72 82
             else

+ 78
- 0
extras.lua View File

@@ -0,0 +1,78 @@
1
+--
2
+-- formspeclib pre-baked objects
3
+--
4
+-- Making formspecs even more fun!
5
+--
6
+
7
+--
8
+-- formspeclib:player_inventory - The current player's inventory
9
+--
10
+-- Syntax: x, y
11
+--
12
+formspeclib.register_object('formspeclib:player_inventory', function(obj, safe_mode)
13
+    if type(obj.y) ~= 'number' then return false end
14
+    return {{
15
+        type = 'inventory',
16
+        x = obj.x,
17
+        y = obj.y,
18
+        width = 8,
19
+        height = 1,
20
+        location = 'current_player',
21
+        name = 'main',
22
+    },
23
+    {
24
+        type = 'inventory',
25
+        x = obj.x,
26
+        y = obj.y + 1.23,
27
+        width = 8,
28
+        height = 3,
29
+        location = 'current_player',
30
+        name = 'main',
31
+        start_at = 8,
32
+    }}
33
+end)
34
+
35
+--
36
+-- formspeclib:node_inventory - The current node's inventory
37
+--
38
+-- Syntax: x, y, width(?), height(?), name(?)
39
+--
40
+formspeclib.register_object('formspeclib:node_inventory', function(obj, safe_mode)
41
+    return {{
42
+        type = 'inventory',
43
+        x = obj.x,
44
+        y = obj.y,
45
+        width = obj.width or 8,
46
+        height = obj.height or 4,
47
+        location = 'context',
48
+        name = obj.name or 'main',
49
+    }}
50
+end)
51
+
52
+--
53
+-- formspeclib:chest - Most useful with chest touchscreens.
54
+--
55
+-- Syntax: width(?), height(?)
56
+--
57
+formspeclib.register_object('formspeclib:chest', function(obj, safe_mode)
58
+    if type(obj.width) ~= 'number' then obj.width = 8 end
59
+    if type(obj.height) ~= 'number' then obj.height = 4 end
60
+    
61
+    return {
62
+    width = obj.width,
63
+    height = obj.height + 5,
64
+    {
65
+        type = 'formspeclib:node_inventory',
66
+        x = 0,
67
+        y = 0.3,
68
+        width = obj.width,
69
+        height = obj.height,
70
+        name = obj.name,
71
+    },
72
+    {
73
+        type = 'formspeclib:player_inventory',
74
+        x = (obj.width / 2) - 4,
75
+        y = obj.height + 0.82,
76
+    },
77
+    }
78
+end)

+ 1
- 0
init.lua View File

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

+ 1
- 0
objects.lua View File

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

Loading…
Cancel
Save