-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.lua
More file actions
192 lines (160 loc) · 5.07 KB
/
Button.lua
File metadata and controls
192 lines (160 loc) · 5.07 KB
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
if not gui then
gui = {}
end
gui.button = class('gui.button')
function gui.button:created(styles)
self.entity.hitTest = true
self.entity.hoverTest = true
self.pressed = false
--self.entity.sprite = image.read(asset.builtin.UI.Grey_Button_10).slice:patch(10)
self.style =
{
normalColor = color.white,
pressedColor = color(206) ,
disabledColor = color(100),
selectedColor = color(186),
hoverColor = color(234)
}
if styles then
for styleName, styleValue in pairs(styles) do
self.style[styleName] = styleValue
end
end
self.defaultColor = nil
self.checkColor = nil
self.multiplyColor = false
self.hovered = false
self.selected = false
self.disabled = false
self.passCondition = nil
if not gui.button.scenes then
gui.button.scenes = {}
gui.button.oldSelected = {}
end
gui.button.scenes[self.scene.name] = self.scene
if not gui.button.didHandleHover then
mouse.addHandler("onHover", gui.button.handleHover)
gui.button.didHandleHover = true
end
end
function gui.button.handleHover()
for k, scen in pairs(gui.button.scenes) do
local newMouse = gui.mapMouseToScene(mouse, scen)
local buttonEnti = gui.insideTest(scen, newMouse, "hoverTest")
local oldSelected =gui.button.oldSelected[scen.name]
if oldSelected and oldSelected ~= buttonEnti and oldSelected.valid and oldSelected:has(gui.button) then
local butto = oldSelected:get(gui.button)
butto:checkHover(false)
butto:updateState()
end
if buttonEnti and buttonEnti:has(gui.button) then
local butto = buttonEnti:get(gui.button)
butto:checkHover(true)
butto:updateState()
end
gui.button.oldSelected[scen.name] = buttonEnti
end
end
function gui.button:start()
self:updateState()
end
function gui.button:select(value)
self.selected = value
self:updateState()
end
function gui.button:disable(value)
self.disabled = value
self:updateState()
end
function gui.button:update()
--self:checkHover()
--self:checkMultiply()
--self:updateState()
end
function gui.button:checkMultiply()
if self.multiplyColor and (self.defaultColor == nil or self:didChangeColor()) then
self.defaultColor = self.entity.color
end
end
function gui.button:didChangeColor()
for k, col in pairs(self.style) do
if self.defaultColor * col == self.entity.color then
return false
end
end
return true
end
function gui.button:updateState()
self:checkMultiply()
if self.entity.sprite then
local col = (self.disabled and self.style.disabledColor) or (self.selected and self.style.selectedColor) or (self.pressed and self.style.pressedColor) or (self.hovered and self.style.hoverColor) or self.style.normalColor
if self.multiplyColor then
self.entity.color = (self.defaultColor or self.entity.color) * col
else
self.entity.color = col
end
end
end
function gui.button:checkHover(didPass)
if didPass then
if not self.hovered then
self.entity:dispatch("onMouseEnter", self)
end
self.entity:dispatch('hovering', self)
self.hovered = true
else
if self.hovered then
self.entity:dispatch('onMouseExit', self)
end
self.hovered = false
end
end
function gui.button:touched(touch, hit)
if not self.disabled then
if touch.moving then
return false
end
if touch.began then
self.cantMove = nil
end
if touch.began and (mouse and touch.type ~= touch.pointer) then
local currentEntity = self.entity
repeat
currentEntity = currentEntity.parent
if currentEntity:has(gui.scrollArea) then
self.cantMove = true
break
end
until currentEntity == self.scene.canvas.entity
end
if touch.began then
self.pressed = true
self:updateState()
return true
elseif touch.moving then
if self.cantMove then
self.pressed = false
return false
end
self.pressed = hit
self:updateState()
elseif touch.ended then
self.pressed = false
self:updateState()
if hit then
self.entity:dispatch('onTapped', self)
end
end
end
return true
end
function gui.selectButton(enti)
for k, ent in ipairs(enti.parent.children) do
if ent.id ~= enti.id and ent:has(gui.button) then
ent:get(gui.button).selected = false
end
end
enti:get(gui.button).selected = true
enti:get(gui.button):updateState()
end
Profiler.wrapClass(gui.button)