-
Notifications
You must be signed in to change notification settings - Fork 3
/
component.lua
318 lines (264 loc) · 6.41 KB
/
component.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
components = {}
Component = {}
function Component.new(typ, x, y, w, h)
check('nnnn', {x, y, w, h}, 5)
local self = inherit({}, Component)
self.ox = x
self.oy = y
self.x = x
self.y = y
self.w = w
self.h = h
self.offx = 0
self.offy = 0
self.type = typ
self.value = ''
self.children = {}
self.parent = nil
self.visible = true
self.enabled = true
self.focused = false
self.mouseOver = false
self.mouseDown = false
self.dragArea = nil
self.tooltip = {}
Emitter.new(self)
table.insert(components, 1, self)
return self
end
function Component:update()
if not self.visible then return end
if type(self.value) ~= 'string' then
self.value = tostring(self.value)
end
local parent = self.parent
if parent then
self.x = parent.x + self.ox + parent.offx
self.y = parent.y + self.oy + parent.offy
end
if self.draw then
self:draw()
self:emit('update')
end
for i=#self.children, 1, -1 do
self.children[i]:update()
end
if not self.enabled then
dxDrawRectangle(self.x, self.y, self.w, self.h, tocolor(0,0,0,200))
end
self.mouseOver = (self.focused or not parent) and isMouseOverPos(self.x, self.y, self.w, self.h)
self:drawTooltip()
end
function Component:drawTooltip()
if not self.tooltip.value then return end
if not self.mouseOver then return end
if not self:isOnTop() then return end
local d = self.tooltip
local val = d.value
local tw, th = d.tw, d.th
local pad = d.pad
local offset = d.offset
local fontSize = d.fontSize
local font = d.font
local mx, my = mouseX + offset, mouseY + offset
dxDrawRectangle(mx, my, tw + pad*4, th + pad*2, tocolor(0,0,0,215), true)
dxDrawText(val, mx + pad*2, my + pad, tw - pad*2, th - pad*2, tocolor(255,255,255), fontSize, font, 'left', 'top', false, false, true)
end
function Component:setTooltip(data)
check('t', {data})
if type(data.value) ~= 'string' then return end
local d = self.tooltip
d.value = data.value
d.fontSize = data.fontSize or d.fontSize or 1.5
d.pad = data.pad or d.pad or 3
d.offset = data.offset or d.offset or 10
d.font = data.font or d.font or 'default'
d.tw = dxGetTextWidth(d.value, d.fontSize, d.font)
d.th = dxGetFontHeight(d.fontSize, d.font)
end
function Component:focus()
if not self.visible then return end
for i=1, #components do
if self ~= components[i] then
components[i].focused = false
end
end
for i=1, #self.children do
self.children[i]:focus()
end
self.focused = true
return self
end
function Component:destroy()
local children = self.children
for i=#children, 1, -1 do
children[i]:destroy()
end
local parent = self.parent
if parent then
table.removeByValue(parent.children, self)
else
table.removeByValue(components, self)
end
self:emit('destroy', self)
collectgarbage()
end
function Component:setParent(parent)
assert(isComponent(parent), "the parent doesn't exist or was destroyed")
if self.parent then
self:removeParent()
end
table.removeByValue(components, self)
table.insert(parent.children, self)
self.parent = parent
return self
end
function Component:addChild(...)
local children = isComponent(arg[1]) and arg
or type(arg[1]) == 'table' and arg[1]
if children then
for i=1, #children do
if isComponent(children[i]) then
children[i]:setParent(self)
end
end
end
return self
end
function Component:getChildren(typ)
if typ then
check('s', {typ})
local children = {}
for i=1, #self.children do
local child = self.children[i]
if child.type == typ then
table.insert(children, child)
end
end
return children
else
return self.children
end
end
function Component:removeParent()
if self.parent then
table.removeByValue(self.parent.children, self)
self.parent = nil
table.insert(components, self)
end
return self
end
function Component:setOnTop()
local comps = self.parent and self.parent.children or components
if comps[1] ~= self then
table.removeByValue(comps, self)
table.insert(comps, 1, self)
end
self:focus()
return self
end
function Component:isOnTop()
local first = components[1]
if first == self.parent or first == self then
return true
end
return false
end
function Component:setToBack()
local comps = self.parent and self.parent.children or components
if comps[#comps] ~= self then
table.removeByValue(comps, self)
table.insert(comps, self)
end
return self
end
function Component:setDraggable(state)
check('b', {state})
if state then
if not isComponent(self.dragArea, 'dragarea') then
self.dragArea = DragArea():setParent(self)
end
else
if isComponent(self.dragArea, 'dragarea') then
self.dragArea:destroy()
end
end
return self
end
function Component:getEnabled()
return self.enabled
end
function Component:setEnabled(state)
self.enabled = state
return self
end
function Component:getPosition()
return self.x, self.y
end
function Component:setPosition(x, y)
x = x or self.ox
y = y or self.oy
self.ox, self.oy = x, y
self.x, self.y = x, y
return self
end
function Component:getSize()
return self.w, self.h
end
function Component:setSize(w, h)
self.w = w or self.w
self.h = h or self.h
return self
end
function Component:getVisible()
return self.visible
end
function Component:setVisible(v)
self.visible = v
return self
end
function Component:hide()
self.visible = false
return self
end
function Component:show()
self.visible = true
return self
end
function Component:getValue()
return self.value
end
function Component:setValue(v)
self.value = tostring(v)
return self
end
function Component:align(state)
if self.parent then
if state == 'center' or state == 'centerX' then
self.ox = self.parent.w/2-self.w/2
end
if state == 'center' or state == 'centerY' then
self.oy = self.parent.h/2-self.h/2
end
else
if state == 'center' or state == 'centerX' then
self.x = screenWidth/2-self.w/2
end
if state == 'center' or state == 'centerY' then
self.y = screenHeight/2-self.h/2
end
end
return self
end
function Component:center()
self:align('center')
return self
end
function Component:drawBorders(size, color)
size = size or 2
color = color or tocolor(255,55,55,200)
dxDrawLine(self.x-size, self.y-size/2, self.x+size + self.w, self.y-size/2, color, size)--top
dxDrawLine(self.x-size, self.y+size/2 + self.h, self.x+size + self.w, self.y+size/2 + self.h, color, size)--bottom
dxDrawLine(self.x-size/2, self.y, self.x-size/2, self.y + self.h, color, size)--left
dxDrawLine(self.x+size/2 + self.w, self.y, self.x+size/2 + self.w, self.y + self.h, color, size)--right
end