-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbadr.lua
138 lines (123 loc) · 3.57 KB
/
badr.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
--
-- Badr
--
-- Copyright (c) 2024 Nabeel20
--
-- This library is free software; you can redistribute it and/or modify it
-- under the terms of the MIT license. See LICENSE for details.
--
local badr = {}
badr.__index = badr
function badr:new(t)
t = t or {}
local _default = {
x = 0,
y = 0,
height = 0,
width = 0,
parent = { x = 0, y = 0, visible = true },
id = tostring(love.timer.getTime()),
visible = true,
children = {},
}
for key, value in pairs(t) do
_default[key] = value
end
return setmetatable(_default, badr)
end
function badr.__add(self, component)
if type(component) ~= "table" or component == nil then return end
component.parent = self
component.x = self.x + component.x
component.y = self.y + component.y
local childrenSize = { width = 0, hight = 0 }
for _, child in ipairs(self.children) do
childrenSize.width = childrenSize.width + child.width;
childrenSize.hight = childrenSize.hight + child.height
end
local gap = self.gap or 0
local lastChild = self.children[#self.children] or {}
if self.column then
component.y = (lastChild.height or 0) + (lastChild.y or self.y)
if #self.children > 0 then
component.y = component.y + gap
end
self.height = math.max(self.height, childrenSize.hight + component.height + gap * #self.children)
self.width = math.max(self.width, component.width)
end
if self.row then
component.x = (lastChild.width or 0) + (lastChild.x or self.x)
if #self.children > 0 then
component.x = component.x + gap
end
self.width = math.max(self.width, childrenSize.width + component.width + gap * #self.children)
self.height = math.max(self.height, component.height)
end
if #component.children > 0 then
for _, child in ipairs(component.children) do
child:updatePosition(component.x, component.y)
end
end
table.insert(self.children, component)
return self
end
-- Remove child
function badr.__sub(self, component)
if self % component.id then
for index, child in ipairs(self.children) do
if child.id == component.id then
table.remove(self.children, index)
end
end
end
return self
end
-- Returns child with specific id
function badr.__mod(self, id)
assert(type(id) == "string", 'Badar; Provided id must be a string.')
for _, child in ipairs(self.children) do
if child.id == id then
return child
end
end
end
function badr:isMouseInside()
local mouseX, mouseY = love.mouse.getPosition()
return mouseX >= self.x and mouseX <= self.x + self.width and
mouseY >= self.y and
mouseY <= self.y + self.height
end
function badr:draw()
if not self.visible then return end;
if #self.children > 0 then
for _, child in ipairs(self.children) do
child:draw()
end
end
end
function badr:updatePosition(x, y)
self.x = self.x + x
self.y = self.y + y
for _, child in ipairs(self.children) do
child:updatePosition(x, y)
end
end
function badr:animate(props)
props(self)
for _, child in ipairs(self.children) do
child:animate(props)
end
end
function badr:update()
if self.onUpdate then
self:onUpdate()
end
for _, child in ipairs(self.children) do
child:update()
end
end
return setmetatable({ new = badr.new }, {
__call = function(t, ...)
return badr:new(...)
end,
})