This repository has been archived by the owner on Jan 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
flxbutton.monkey
313 lines (228 loc) · 5.77 KB
/
flxbutton.monkey
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
Strict
Import reflection
Import flxextern
Import flxsprite
Import flxtext
Import flxpoint
Import flxsound
Import flxg
Import flxcamera
Import system.flxassetsmanager
Import "data/button_flx.png"
Import "data/beep_flx.mp3"
Class FlxButton Extends FlxSprite
Global __CLASS__:Object
Const NORMAL:Int = 0
Const HIGHLIGHT:Int = 1
Const PRESSED:Int = 2
Field label:FlxText
Field labelOffset:FlxPoint
Field onUp:FlxButtonClickListener
Field onDown:FlxButtonDownListener
Field onOver:FlxButtonOverListener
Field onOut:FlxButtonOutListener
Field status:Int
Field soundOver:FlxSound
Field soundOut:FlxSound
Field soundDown:FlxSound
Field soundUp:FlxSound
Private
Field _onToggle:Bool
Field _pressed:Bool
Field _initialized:Bool
Public
Method New(x:Float = 0, y:Float = 0, label:String = "", onClick:FlxButtonClickListener = Null)
Super.New(x, y)
If (label.Length() > 0) Then
Self.label = New FlxText(0, 0, 80, label)
Self.label.SetFormat(FlxText.SYSTEM_FONT, 8, $FF333333, FlxText.ALIGN_CENTER)
labelOffset = New FlxPoint(-1, 3)
End If
LoadGraphic("button" + FlxG.DATA_SUFFIX, True, False, 80, 20)
onUp = onClick
onDown = Null
onOut = Null
onOver = Null
soundOver = Null
soundOut = Null
soundDown = Null
soundUp = Null
status = NORMAL
_onToggle = False
_pressed = False
End Method
Method Destroy:Void()
If (label <> Null) Then
label.Destroy()
label = Null
End If
onUp = Null
onDown = Null
onOut = Null
onOver = Null
If (soundOver <> Null) Then
soundOver.Destroy()
soundOver = Null
End If
If (soundOut <> Null) Then
soundOut.Destroy()
soundOut = Null
End If
If (soundDown <> Null) Then
soundDown.Destroy()
soundDown = Null
End If
If (soundUp <> Null) Then
soundUp.Destroy()
soundUp = Null
End If
Super.Destroy()
End Method
Method Update:Void()
_UpdateButton()
If (label = Null) Return
Select (Frame)
Case HIGHLIGHT
label.Alpha = 1.0
Case PRESSED
label.Alpha = .5
label.y += 1
Default
label.Alpha = .8
End Select
End Method
Method Draw:Void()
Super.Draw()
If (label <> Null) Then
label.scrollFactor = scrollFactor
label.Draw()
End If
End Method
Method ResetHelpers:Void()
Super.ResetHelpers()
If (label <> Null) Then
label.SetWidth(width)
End If
End Method
Method SetSounds:Void(soundOver:String = "", soundOverVolume:Float = 1.0, soundOut:String = "", soundOutVolume:Float = 1.0, soundDown:String = "", soundDownVolume:Float = 1.0, soundUp:String = "", soundUpVolume:Float = 1.0)
If (soundOver.Length() > 0) Then
Self.soundOver = FlxG.LoadSound(soundOver, soundOverVolume)
End If
If (soundOut.Length() > 0) Then
Self.soundOut = FlxG.LoadSound(soundOut, soundOutVolume)
End If
If (soundDown.Length() > 0) Then
Self.soundDown = FlxG.LoadSound(soundDown, soundDownVolume)
End If
If (soundUp.Length() > 0) Then
Self.soundUp = FlxG.LoadSound(soundUp, soundUpVolume)
End If
End Method
Method On:Bool() Property
Return _onToggle
End Method
Method On:Void(on:Bool) Property
_onToggle = on
End Method
Private
Method _UpdateButton:Void()
If (FlxG.Mobile Or FlxG.Mouse.Visible Or FlxG._Game.useSystemCursor) Then
Local cameras:Stack<FlxCamera> = FlxG.Cameras
Local camera:FlxCamera
Local touchInput:TouchInput
Local i:Int = 0
Local l:Int = cameras.Length()
Local offAll:Bool = True
Local click:Bool = False
While (i < l)
camera = cameras.Get(i)
#If FLX_MULTITOUCH_ENABLED
Local tc:Int = FlxG.TouchCount()
#Else
Local tc:Int = 1
#End
For Local ti:Int = 0 Until tc
touchInput = FlxG.Touch(ti)
If (ti > 0 And Not touchInput.Pressed() And Not touchInput.JustReleased()) Then
Exit
End If
If (_cameras <> Null And Not _cameras.Contains(camera)) Then
i += 1
Continue
End If
touchInput.GetWorldPosition(camera, _point)
If (OverlapsPoint(_point, True, camera)) Then
offAll = False
If (touchInput.JustPressed()) Then
status = PRESSED
If (onDown <> Null) Then
onDown.OnButtonDown(Self)
End If
If (soundDown <> Null) Then
soundDown.Play(True)
End If
End If
If (touchInput.JustReleased() And status = PRESSED) Then
status = NORMAL
click = True
End If
If (status = NORMAL) Then
status = HIGHLIGHT
If (onOver <> Null) Then
onOver.OnButtonOver(Self)
End If
If (soundOver <> Null) Then
soundOver.Play(True)
End If
End If
End If
Next
i += 1
Wend
If(offAll) Then
If (status <> NORMAL) Then
If (onOut <> Null) Then
onOut.OnButtonOut(Self)
End If
If (soundOut <> Null) Then
soundOut.Play(True)
End If
End If
status = NORMAL
End If
If(click) Then
If (onUp <> Null) Then
onUp.OnButtonClick(Self)
End If
If (soundUp <> Null) Then
soundUp.Play(True)
End If
End If
End If
If (label <> Null) Then
label.x = x
label.y = y
If (labelOffset <> Null) Then
label.x += labelOffset.x
label.y += labelOffset.y
End If
End If
If (status = HIGHLIGHT And _onToggle) Then
Frame = NORMAL
Else
Frame = status
End If
End Method
End Class
Interface FlxButtonClickListener
Method OnButtonClick:Void(button:FlxButton)
End Interface
Interface FlxButtonDownListener
Method OnButtonDown:Void(button:FlxButton)
End Interface
Interface FlxButtonOverListener
Method OnButtonOver:Void(button:FlxButton)
End Interface
Interface FlxButtonOutListener
Method OnButtonOut:Void(button:FlxButton)
End Interface