-
Notifications
You must be signed in to change notification settings - Fork 1
/
Switcher.lua
58 lines (50 loc) · 1.44 KB
/
Switcher.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
local ContainerBase = require("ContainerBase")
local WidgetBase = require("WidgetBase")
local Switcher = ContainerBase:extend()
Switcher.baseStyle = {
gap = 0,
}
setmetatable(Switcher.baseStyle, {__index=ContainerBase.baseStyle})
Switcher.type = "Switcher"
function Switcher:new(...)
self.switcherWidgets = {}
self.selected = nil
ContainerBase.new(self, ...)
for _, W in ipairs(self.items) do
self.switcherWidgets[W.id] = W
self.selected = W.id
end
end
function Switcher:getContentDimensions()
local maxW = 0
local maxH = 0
for _, W in ipairs(self.items) do
local w, h = W:getMinDimensions()
maxW = math.max(maxW, w)
maxH = math.max(maxH, h)
end
return maxW, maxH
end
function Switcher:calculateGeometry(x, y, w, h)
ContainerBase.calculateGeometry(self, x, y, w, h)
local xB, yB, wB, hB = self:getContentBox()
for _, W in ipairs(self.items) do
W:calculateGeometry(xB, yB, wB, hB)
end
end
function Switcher:draw()
WidgetBase.draw(self)
self.switcherWidgets[self.selected]:draw()
end
function Switcher:handleMouse(x, y)
WidgetBase.handleMouse(self, x, y)
self.switcherWidgets[self.selected]:handleMouse(x, y)
end
function Switcher:handleClick(x, y, button)
if not self:inside(x, y) then
return
end
WidgetBase.handleClick(self, x, y, button)
self.switcherWidgets[self.selected]:handleClick(x, y, button)
end
return Switcher