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