-
Notifications
You must be signed in to change notification settings - Fork 7
/
base.gfx.gui.window.modal.bmx
executable file
·357 lines (279 loc) · 10.7 KB
/
base.gfx.gui.window.modal.bmx
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
Rem
====================================================================
GUI Modal Window
====================================================================
====================================================================
If not otherwise stated, the following code is available under the
following licence:
LICENCE: zlib/libpng
Copyright (C) 2015 Ronny Otto, digidea.de
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
====================================================================
End Rem
SuperStrict
Import "base.util.virtualgraphics.bmx"
Import "base.util.interpolation.bmx"
Import "base.gfx.gui.window.base.bmx"
Import "base.gfx.gui.button.bmx"
Type TGUIModalWindow Extends TGUIWindowBase
Field DarkenedArea:TRectangle = Null
Field darkenedAreaAlpha:Float = 0.5
'the area the window centers to
Field screenArea:TRectangle = Null
Field buttons:TGUIButton[]
Field buttonCallbacks:Int(index:Int, sender:TGUIObject)[]
'0 = centered, -1 = left aligned, 1 = right aligned
Field buttonAlignment:Int = 0
'templates for button positions
Field buttonPositionTemplate:Int = 0
Field autoAdjustHeight:Int = True
Field isOpen:Int
'=== CLOSING VARIABLES ===
'indicator if currently closing
Field closeActionStarted:Long = 0
'the time a close action started
Field closeActionTime:Long = 0
'the time a close action runs
Field closeActionDuration:int = 650
'the position of the widget when closing
Field closeActionStartPosition:TVec2D = new TVec2D
Method Create:TGUIModalWindow(pos:TVec2D, dimension:TVec2D, limitState:String = "")
Super.Create(pos, dimension, limitState)
setZIndex(10000)
'by default just a "ok" button
SetDialogueType(1)
'set another panel background
GUIManager.Remove(guiBackground)
guiBackground.spriteBaseName = "gfx_gui_modalWindow"
'we want to know if one clicks on a windows buttons
AddEventListener(EventManager.registerListenerMethod(GUIEventKeys.GUIObject_OnClick, Self, "onButtonClick"))
'fire event so others know that the window is created
TriggerBaseEvent(GUIEventKeys.GUIModalWindow_OnCreate, Null, Self)
Return Self
End Method
'override to delete children too (not really needed)
Method Remove:Int() override
Super.Remove()
'buttons are normally removed by "Super.Remove()" already
'as they are added as "children" and are iterated on its own
For Local o:TGUIObject = EachIn buttons
o.Remove()
Next
End Method
Method Initialize:int()
closeActionStarted = 0
closeActionTime = 0
End Method
'easier setter for dialogue types
Method SetDialogueType:Int(typeID:Int)
For Local button:TGUIobject = EachIn Self.buttons
button.remove()
'RemoveChild(button)
Next
buttons = New TGUIButton[0] '0 sized array
Select typeID
'a default button
Case 1
buttons = buttons[..1]
buttonCallbacks = buttonCallbacks[..1]
buttons[0] = New TGUIButton.Create(new TVec2D.Init(0, 0), new TVec2D.Init(120, -1), GetLocale("OK"))
buttonCallbacks[0] = onClickCallback_Close
AddChild(buttons[0])
'set to ignore parental padding (so it starts at 0,0)
buttons[0].SetOption(GUI_OBJECT_IGNORE_PARENTPADDING, True)
'yes and no button
Case 2
buttons = buttons[..2]
buttonCallbacks = buttonCallbacks[..2]
buttons[0] = New TGUIButton.Create(new TVec2D.Init(0, 0), new TVec2D.Init(90, -1), GetLocale("YES"))
buttons[1] = New TGUIButton.Create(new TVec2D.Init(0, 0), new TVec2D.Init(90, -1), GetLocale("NO"))
buttonCallbacks[0] = onClickCallback_Close
buttonCallbacks[1] = onClickCallback_Close
AddChild(buttons[0])
AddChild(buttons[1])
'set to ignore parental padding (so it starts at 0,0)
buttons[0].SetOption(GUI_OBJECT_IGNORE_PARENTPADDING, True)
buttons[1].SetOption(GUI_OBJECT_IGNORE_PARENTPADDING, True)
End Select
End Method
Function onClickCallback_Close:Int(index:Int, sender:TGUIObject)
local window:TGUIModalWindow = TGUIModalWindow(sender)
if not window then return False
window.Close(index)
End Function
'size 0, 0 is not possible (leads to autosize)
Method SetSize(w:Float = 0, h:Float = 0)
Super.SetSize(w, h)
'move button
If buttons.length = 1
Select buttonAlignment
Case -1
buttons[0].SetPosition(0, GetScreenRect().GetH() - 49)
Case 1
buttons[0].SetPosition(rect.GetW() - buttons[0].rect.GetW(), GetScreenRect().GetH() - 49)
Default
buttons[0].SetPosition(rect.GetW()/2 - buttons[0].rect.GetW()/2, GetScreenRect().GetH() - 49)
End Select
ElseIf buttons.length = 2
Select buttonAlignment
Case -1
buttons[0].SetPosition(0, GetScreenRect().GetH() - 49)
buttons[1].SetPosition(buttons[0].rect.GetW() + 10, GetScreenRect().GetH() - 49)
Case 1
buttons[0].SetPosition(rect.GetW() - buttons[1].rect.GetW() - 10 - buttons[0].rect.GetW(), GetScreenRect().GetH() - 49)
buttons[1].SetPosition(rect.GetW() - buttons[0].rect.GetW(), GetScreenRect().GetH() - 49)
Default
buttons[0].SetPosition(rect.GetW()/2 - buttons[0].rect.GetW() - 10, GetScreenRect().GetH() - 49)
buttons[1].SetPosition(rect.GetW()/2 + 10, GetScreenRect().GetH() - 49)
End Select
EndIf
'move some buttons according to a template (here: left or right)
'while keeping position of others intact
If buttonPositionTemplate = 1
if buttons.length > 1
buttons[0].SetPositionX(rect.GetW()/2 - buttons[0].rect.GetW()/2)
buttons[1].SetPositionX(GetContentX())
endif
ElseIf buttonPositionTemplate = 2
if buttons.length > 1
buttons[0].SetPositionX(rect.GetW()/2 - buttons[0].rect.GetW()/2)
buttons[1].SetPositionX(GetContentX() - buttons[1].rect.GetW())
endif
EndIf
Recenter()
End Method
'overwrite windowBase-method to recenter after appearance change
Method onAppearanceChanged:int()
Super.onAppearanceChanged()
Recenter()
End Method
Method Recenter:Int(moveByX:Float = 0, moveByY:Float = 0)
'center the window
Local centerX:Float=0.0
Local centerY:Float=0.0
If Not screenArea
centerX = VirtualWidth()/2
centerY = VirtualHeight()/2
Else
centerX = screenArea.getX() + screenArea.GetW()/2
centerY = screenArea.getY() + screenArea.GetH()/2
EndIf
SetPosition(centerX - rect.getW()/2 + moveByX,centerY - rect.getH()/2 + moveByY)
End Method
Method Open:Int()
TriggerBaseEvent(GUIEventKeys.GUIModalWindow_OnOpen, Null, Self)
isOpen = True
End Method
'close the window (eg. with an animation)
Method Close:Int(closeButton:Int=-1)
'only close once :D
if closeActionStarted then return False
isOpen = False
closeActionStarted = True
closeActionTime = Time.GetAppTimeGone()
closeActionStartPosition = rect.position.copy()
'fire event so others know that the window is closed
'and what button was used
TriggerBaseEvent(GUIEventKeys.GUIModalWindow_OnClose, new TData.AddNumber("closeButton", closeButton) , Self)
End Method
Method canClose:Int()
'is there an animation active?
If closeActionStarted
If closeActionTime + closeActionDuration < Time.GetAppTimeGone()
Return True
Else
Return False
EndIf
Else
Return True
EndIf
End Method
Method IsClosed:int()
return closeActionStarted and closeActionTime + closeActionDuration < Time.GetAppTimeGone()
End Method
Method IsClosing:int()
return closeActionStarted
End Method
'handle clicks on the various close buttons
Method onButtonClick:Int( triggerEvent:TEventBase )
Local sender:TGUIButton = TGUIButton(triggerEvent.GetSender())
If sender = Null Then Return False
For Local i:Int = 0 To Self.buttons.length - 1
If Self.buttons[i] <> sender Then Continue
If Self.buttonCallbacks[i]
'run callback with window as param
Self.buttonCallbacks[i](i, self)
EndIf
Next
End Method
'override default update-method
Method Update:Int()
'maybe children intercept clicks...
'so call Super.Update as it calls UpdateChildren already
Super.Update()
if closeActionStarted
local yUntilScreenLeft:int = VirtualHeight() - (closeActionStartPosition.y + GetScreenRect().GetH())
Recenter(0, Float(- yUntilScreenLeft * TInterpolation.BackIn(0.0, 1.0, Min(closeActionDuration, Time.GetAppTimeGone() - closeActionTime), closeActionDuration)))
endif
if Not GuiManager.GetKeyboardInputReceiver() and KeyManager.IsHit(KEY_ESCAPE)
'do not allow another ESC-press for X ms
KeyManager.blockKey(KEY_ESCAPE, 250)
self.Close()
endif
'remove the window as soon as there is no animation active
'until then: play the animation
If IsClosed()
Self.remove()
Return False
EndIf
'we manage drawing and updating our background
If guiBackground and guiBackground.IsEnabled() then guiBackground.Update()
'deactivate mousehandling for other underlying objects
GUIManager._ignoreMouse = True
End Method
Method DrawBackground()
Local oldCol:SColor8; GetColor(oldCol)
Local oldColA:Float = GetAlpha()
local newAlpha:Float = 1.0
if closeActionStarted
newAlpha = 1.0 - TInterpolation.Linear(0.0, 1.0, Min(closeActionDuration, Time.GetAppTimeGone() - closeActionTime), closeActionDuration)
endif
self.alpha = newAlpha
SetAlpha(oldColA * alpha * darkenedAreaAlpha)
SetColor(0, 0, 0)
If Not DarkenedArea
DrawRect(0, 0, GetGraphicsManager().GetWidth(), GetGraphicsManager().GetHeight())
Else
DrawRect(DarkenedArea.getX(), DarkenedArea.getY(), DarkenedArea.getW(), DarkenedArea.getH())
EndIf
SetColor(oldCol)
SetAlpha(oldColA)
'we manage drawing and updating our background
If guiBackground and guiBackground.IsEnabled() then guiBackground.Draw()
Super.DrawBackground()
End Method
Method DrawContent()
if closeActionStarted
local newAlpha:Float = 1.0 - TInterpolation.Linear(0.0, 1.0, Min(closeActionDuration, Time.GetAppTimeGone() - closeActionTime), closeActionDuration)
'as text "wobbles" (drawn at INT position while sprites draw
'with floats - so they seem to change offsets) we fade them
'out earlier
if guiCaptionTextBox then guiCaptionTextBox.alpha = 0.5 * newAlpha
if guiTextBox then guiTextBox.alpha = 0.5 * newAlpha
endif
Super.DrawContent()
End Method
End Type