-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbase.gfx.gui.accordeon.bmx
executable file
·439 lines (315 loc) · 9.13 KB
/
base.gfx.gui.accordeon.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
Rem
===========================================================
GUI Button
===========================================================
End Rem
SuperStrict
Import "base.gfx.gui.bmx"
Type TGUIAccordeonPanel Extends TGUIObject
Field isOpen:Int = False
Field fixedSize:TVec2D = New TVec2D.Init(-1,-1)
Method GetClassName:String()
Return "tguiaccordeonpanel"
End Method
Method Create:TGUIAccordeonPanel(pos:TVec2D, dimension:TVec2D, value:String, State:String = "")
'setup base widget
Super.CreateBase(pos, dimension, State)
SetValue(value)
GUIManager.Add(Self)
Return Self
End Method
Method New()
' setOption(GUI_OBJECT_CLICKABLE, False)
setOption(GUI_OBJECT_CAN_GAIN_FOCUS, False)
End Method
Method Close:Int()
If isOpen = False Then Return True
isOpen = False
SetAppearanceChanged(True)
'adjust parental accordeon
If TGUIAccordeon(_parent) Then TGUIAccordeon(_parent).onClosePanel(Self)
TriggerBaseEvent(GUIEventKeys.GUIAccordeonPanel_OnClose, Null, Self)
Return True
End Method
Method Open:Int()
If isOpen = True Then Return True
isOpen = True
SetAppearanceChanged(True)
'adjust parental accordeon
If TGUIAccordeon(_parent) Then TGUIAccordeon(_parent).onOpenPanel(Self)
TriggerBaseEvent(GUIEventKeys.GUIAccordeonPanel_OnOpen, Null, Self)
Return True
End Method
Method GetHeaderHeight:Float()
Return 16
End Method
Method GetHeaderScreenHeight:Float()
Return GetHeaderHeight()
End Method
Method GetBodyY:Float()
'displace by header
Return GetHeaderHeight()
End Method
Method GetBodyHeight:Float()
If isOpen
If fixedSize.y = -1
Return Max(0, GetScreenRect().GetH() - GetHeaderScreenHeight())
Else
Return Max(0, fixedSize.y - GetHeaderScreenHeight())
EndIf
Else
Return 0
EndIf
End Method
Method GetHeight:Int()
If isOpen
If fixedSize.y = -1
Return Max(Super.GetHeight(), GetHeaderHeight())
Else
Return Max(fixedSize.y, GetHeaderHeight())
EndIf
EndIf
Return GetHeaderHeight()
End Method
Method GetWidth:Int()
If fixedSize.x = -1 Then Return Super.GetWidth()
Return fixedSize.x
End Method
Method _UpdateScreenH:Float()
_screenRect.SetH( GetHeight() )
return _screenRect.GetH()
End Method
Method DrawHeader()
SetColor 80, Abs(255 - (25 * _id)) Mod 255, (35 * _id) Mod 255
DrawRect( GetScreenRect().GetX(), GetScreenRect().GetY(), GetScreenRect().GetW(), GetScreenRect().GetH() )
If IsHovered()
SetColor 250,250,250
Else
SetColor 0, 0, 0
EndIf
Local openStr:String = Chr(9654)
If isOpen Then openStr = Chr(9660)
If TGUIAccordeon(_parent)
DrawText(openStr + " Panel #" + TGUIAccordeon(_parent).GetPanelIndex(Self), GetScreenRect().GetX(), GetScreenRect().GetY())
Else
DrawText(openStr + " Panel [id=" + _id+"]", GetScreenRect().GetX(), GetScreenRect().GetY())
EndIf
SetColor 255,255,255
End Method
Method DrawBody()
SetColor 120,120,120
DrawRect( GetScreenRect().GetX(), GetScreenRect().GetY() + GetHeaderScreenHeight(), GetScreenRect().GetW(), GetBodyHeight() )
SetColor 255,255,255
End Method
Method DrawContent()
'draw header after the body so potential "shadows" are drawn
'correctly
DrawBody()
DrawHeader()
End Method
'override to toggle open/close
Method onClick:Int(triggerEvent:TEventBase)
Local coord:TVec2D = TVec2D(triggerEvent.GetData().Get("coord"))
Local headerScreenRect:TRectangle = New TRectangle.Init(GetScreenRect().GetX(), GetScreenRect().GetY(), GetScreenRect().GetW(), GetHeaderScreenHeight())
If headerScreenRect.containsVec( coord )
If isOpen
Close()
Else
Open()
EndIf
EndIf
Return Super.onClick(triggerEvent)
End Method
Method UpdateLayout()
End Method
End Type
Type TGUIAccordeon Extends TGUIObject
Field panels:TGUIAccordeonPanel[]
Field _panelsFillAccordeon:Int = True
Field _allowMultipleOpenPanels:Int = False
Method GetClassName:String()
Return "tguiaccordeon"
End Method
Method Create:TGUIAccordeon(pos:TVec2D, dimension:TVec2D, value:String, State:String = "")
'setup base widget
Super.CreateBase(pos, dimension, State)
SetValue(value)
GUIManager.Add(Self)
Return Self
End Method
Method onOpenPanel:Int(panel:TGUIAccordeonPanel)
'close all other open panels
If Not _allowMultipleOpenPanels
For Local i:Int = 0 Until panels.length
If panels[i] = panel Then Continue
If panels[i].isOpen Then panels[i].Close()
Next
EndIf
InvalidateLayout()
TriggerBaseEvent(GUIEventKeys.GUIAccordeon_OnOpenPanel, New TData.Add("panel", panel), Self)
Return True
End Method
Method onClosePanel:Int(panel:TGUIAccordeonPanel)
'if no panel is open now, keep the closing one opened
If GetOpenPanelCount() = 0
panel.Open()
Return False
EndIf
InvalidateLayout()
TriggerBaseEvent(GUIEventKeys.GUIAccordeon_OnClosePanel, New TData.Add("panel", panel), Self)
Return True
End Method
Method OpenPanel:Int(index:Int)
Local child:TGUIAccordeonPanel = TGUIAccordeonPanel( GetPanelAtIndex(index) )
If Not child Then Return False
Return child.Open()
End Method
Method ClosePanel:Int(index:Int)
Local child:TGUIAccordeonPanel = TGUIAccordeonPanel( GetPanelAtIndex(index) )
If Not child Then Return False
Return child.Close()
End Method
'override, remove panels too
Method RemoveChild:Int(child:TGUIobject, giveBackToManager:Int=False)
If TGUIAccordeonPanel(child)
RemovePanel(TGUIAccordeonPanel(child))
EndIf
Return Super.RemoveChild(child, giveBackToManager)
End Method
Method AddPanel:Int(panel:TGUIAccordeonPanel, index:Int = -1)
If Not panel Then Return False
'already added ?
If GetPanelIndex(panel) >= 0 Then Return False
If Not panels
panels = [panel]
Else
'within existing or "next" index
index = Min(panels.length, index)
If index = panels.length
panels :+ [panel]
ElseIf index = 0
panels = [panel] + panels
Else
panels = panels[.. index] + [panel] + panels[index ..]
EndIf
EndIf
panel.SetParent(Self)
'accordeon manages the panel
GUIManager.remove(panel)
InvalidateLayout()
Return True
End Method
Method RemovePanel:Int(panel:TGUIAccordeonPanel)
If Not panel Or Not panels Then Return False
Local newPanels:TGUIAccordeonPanel[] = New TGUIAccordeonPanel[0]
Local removedSomething:Int = False
For Local p:TGUIAccordeonPanel = EachIn panels
If p = panel
p.SetParent(Null)
removedSomething = True
Continue
EndIf
newPanels :+ [p]
Next
panels = newPanels
' If removedSomething Then RefitPanelSizes()
If removedSomething Then InvalidateLayout()
Return removedSomething
End Method
Method GetPanelAtIndex:TGUIAccordeonPanel(index:Int)
If index < 0 Or Not panels Or index >= panels.length Then Return Null
Return panels[index]
End Method
Method GetPanelIndex:Int(panel:TGUIAccordeonPanel)
If Not panels Or panels.length = 0 Then Return -1
For Local i:Int = 0 Until panels.length
If panels[i] = panel Then Return i
Next
Return -1
End Method
Method GetOpenPanelCount:Int()
Local openPanels:Int = 0
For Local i:Int = 0 Until panels.length
If panels[i].isOpen Then openPanels :+ 1
Next
Return openPanels
End Method
Method GetPanelCount:Int()
If Not panels Then Return 0
Return panels.length
End Method
Method GetTotalPanelHeadersHeight:Int()
If Not panels Then Return 0
Local panelHeadersHeight:Int = 0
For Local p:TGUIAccordeonPanel = EachIn panels
panelHeadersHeight :+ p.GetHeaderHeight()
Next
Return panelHeadersHeight
End Method
'returns the maximum height for an open panel's body
Method GetMaxPanelBodyHeight:Int()
Return Max(0, GetHeight() - GetTotalPanelHeadersHeight())
End Method
'override to add panels
Method UpdateChildren:Int()
If panels
For Local p:TGUIAccordeonPanel = EachIn panels
p.Update()
Next
EndIf
End Method
'override to add panels
Method DrawChildren:Int()
If panels
For Local p:TGUIAccordeonPanel = EachIn panels
p.Draw()
Next
EndIf
Super.DrawChildren()
End Method
'override to add panels
Method DrawTooltips:Int()
If panels
For Local p:TGUIAccordeonPanel = EachIn panels
p.DrawTooltips()
Next
EndIf
Super.DrawTooltips()
End Method
Method DrawContent()
'DrawRect( GetScreenRect().GetX(), GetScreenRect().GetY(), GetScreenRect().GetW(), GetScreenRect().GetH() )
End Method
Method onAppearanceChanged:Int()
Super.onAppearanceChanged()
if panels
For Local p:TGUIAccordeonPanel = EachIn panels
p.InvalidateLayout()
p.SetAppearanceChanged(True)
Next
endif
End Method
Method UpdateLayout()
If panels
InvalidateScreenRect()
Local panelX:Int = 0
Local panelY:Int = 0
For Local p:TGUIAccordeonPanel = EachIn panels
p.SetPosition(panelX, panelY)
If Not p.isOpen
p.SetSize(GetContentWidth(), -1)
panelY :+ p.GetHeaderHeight()
Else
'auto height
If Not p.fixedSize Or p.fixedSize.y = -1
If _panelsFillAccordeon And Not _allowMultipleOpenPanels
p.SetSize(GetContentWidth(), p.GetHeaderHeight() + GetMaxPanelBodyHeight())
Else
p.SetSize(GetContentWidth(), p.GetHeaderHeight() + p.GetBodyHeight())
EndIf
EndIf
panelY :+ p.GetHeight()
EndIf
Next
EndIf
End Method
End Type