]> git.lizzy.rs Git - xdecor.git/blob - handlers/nodeboxes.lua
63da57e385433f0d6f4b5f7813f9592be931c36e
[xdecor.git] / handlers / nodeboxes.lua
1 xdecor.box = {
2         slab_y = function(height, shift)
3                 return {-0.5, -0.5 + (shift or 0), -0.5, 0.5, -0.5 + height +
4                         (shift or 0), 0.5}
5         end,
6         slab_z = function(depth)
7                 return {-0.5, -0.5, -0.5 + depth, 0.5, 0.5, 0.5}
8         end,
9         bar_y = function(radius)
10                 return {-radius, -0.5, -radius, radius, 0.5, radius}
11         end,
12         cuboid = function(radius_x, radius_y, radius_z)
13                 return {-radius_x, -radius_y, -radius_z, radius_x, radius_y, radius_z}
14         end
15 }
16
17 xdecor.nodebox = {
18         regular = {type="regular"},
19         null = {type="fixed", fixed={0,0,0,0,0,0}}
20 }
21
22 xdecor.pixelbox = function(size, boxes)
23         local fixed = {}
24         for _, box in pairs(boxes) do
25                 -- `unpack` has been changed to `table.unpack` in newest Lua versions.
26                 local x, y, z, w, h, l = unpack(box)
27                 fixed[#fixed+1] = {
28                         (x / size) - 0.5,
29                         (y / size) - 0.5,
30                         (z / size) - 0.5,
31                         ((x + w) / size) - 0.5,
32                         ((y + h) / size) - 0.5,
33                         ((z + l) / size) - 0.5
34                 }
35         end
36         return {type="fixed", fixed=fixed}
37 end
38
39 local mt = {}
40 mt.__index = function(table, key)
41         local ref = xdecor.box[key]
42         local ref_type = type(ref)
43
44         if ref_type == "function" then
45                 return function(...)
46                         return {type="fixed", fixed=ref(...)}
47                 end
48         elseif ref_type == "table" then
49                 return {type="fixed", fixed=ref}
50         elseif ref_type == "nil" then
51                 error(key.."could not be found among nodebox presets and functions")
52         end
53
54         error("unexpected datatype "..tostring(type(ref)).." while looking for "..key)
55 end
56
57 setmetatable(xdecor.nodebox, mt)
58