-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContainerBase.lua
89 lines (79 loc) · 2.06 KB
/
ContainerBase.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
local WidgetBase = require("WidgetBase")
local ContainerBase = WidgetBase:extend()
ContainerBase.baseStyle = {
gap = 0,
slots = 0,
}
setmetatable(ContainerBase.baseStyle, {__index=WidgetBase.baseStyle})
ContainerBase.type = "ContainerBase"
function ContainerBase:new(...)
local args = {...}
self.items = {}
self.widgets = {}
local style = {}
local id = nil
local i = 1
for _, W in ipairs(args) do
if type(W)=="string" then
id = W
else
if W.type then
self.items[i] = W
i = i + 1
else
style = W
end
end
end
WidgetBase.new(self, style, id)
for _, W in ipairs(self.items) do
W:setParent(self)
end
self:addWidgetLookup(self.id, self)
end
function ContainerBase:addWidgetLookup(key, widget)
assert(not self.widgets[key], "ID duplication in container \""..self.id.."\": "..key)
self.widgets[key] = widget
end
function ContainerBase:getWidget(id)
if self.widgets then
return assert(self.widgets[id], "Widget ID not found in "..self.id..": "..id)
end
return self.parent:getWidget(id)
end
function ContainerBase:setParent(parent)
self.parent = parent
for k, v in pairs(self.widgets) do
parent:addWidgetLookup(k, v)
end
self.widgets = nil
end
function ContainerBase:draw()
WidgetBase.draw(self)
for i=1, #self.items do
self.items[i]:draw()
end
end
function ContainerBase:handleMouse(x, y)
WidgetBase.handleMouse(self, x, y)
for _, W in ipairs(self.items) do
W:handleMouse(x, y)
end
end
function ContainerBase:handleClick(x, y, button)
if not self:inside(x, y) then
return
end
WidgetBase.handleClick(self, x, y, button)
for _, W in ipairs(self.items) do
W:handleClick(x, y, button)
end
end
function ContainerBase:getSlots()
local slots = 0
for _, W in ipairs(self.items) do
slots = slots + W.style.span
end
return math.max(slots, self.style.slots)
end
return ContainerBase