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