-
Notifications
You must be signed in to change notification settings - Fork 7
/
base.gfx.gui.scroller.bmx
executable file
·582 lines (436 loc) · 17.6 KB
/
base.gfx.gui.scroller.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
Rem
===========================================================
GUI Scroller
===========================================================
Scrollers have to get attached to other gui objects to get
createable.
End Rem
SuperStrict
Import "base.gfx.gui.bmx"
Import "base.gfx.gui.arrowbutton.bmx"
Import "base.gfx.gui.slider.bmx"
Type TGUIScrollerBase extends TGUIobject
Field mouseDownTime:Int = 250 'milliseconds until we react on "mousedown"
Field guiButtonMinus:TGUIArrowButton = Null
Field guiButtonPlus:TGUIArrowButton = Null
Field minValue:Double = 0
Field maxValue:Double = 100
Field begunAMouseDown:int = False
Field mouseScrollAmount:Int = 5
Field currentValue:Double
Field _orientation:int = GUI_OBJECT_ORIENTATION_VERTICAL
Method GetClassName:String()
Return "tguiscrollerbase"
End Method
Method Create:TGUIScrollerBase(parent:TGUIobject)
setParent(parent)
'create buttons
guiButtonMinus = New TGUIArrowButton.Create(Null, new TVec2D.Init(22,22), "UP", "")
guiButtonPlus = New TGUIArrowButton.Create(Null, new TVec2D.Init(22,22), "DOWN", "")
guiButtonMinus.spriteButtonBaseName = "gfx_gui_button.rounded"
guiButtonPlus.spriteButtonBaseName = "gfx_gui_button.rounded"
'set the buttons to ignore focus setting
guiButtonMinus.setOption(GUI_OBJECT_CAN_GAIN_FOCUS, False)
guiButtonPlus.setOption(GUI_OBJECT_CAN_GAIN_FOCUS, False)
'the scroller itself ignores focus too
self.setOption(GUI_OBJECT_CAN_GAIN_FOCUS, False)
'allow to keep mouse button pressed and leave the area while still
'scrolling
self.setOption(GUI_OBJECT_KEEP_ACTIVE_ON_OUTSIDE_CONTINUED_MOUSEDOWN, True)
'style myself - aligns buttons
onAppearanceChanged()
'set the parent of the buttons so they inherit visible state etc.
' guiButtonMinus.setParent(Self)
' guiButtonPlus.setParent(Self)
AddChild(guiButtonMinus)
AddChild(guiButtonPlus)
'scroller is interested in hits (not clicks) on its buttons
AddEventListener(EventManager.registerListenerFunction( "guiobject.onClick", TGUIScrollerBase.onButtonClick, guiButtonMinus ))
AddEventListener(EventManager.registerListenerFunction( "guiobject.onClick", TGUIScrollerBase.onButtonClick, guiButtonPlus ))
AddEventListener(EventManager.registerListenerFunction( "guiobject.onMouseDown", TGUIScrollerBase.onButtonDown, guiButtonMinus ))
AddEventListener(EventManager.registerListenerFunction( "guiobject.onMouseDown", TGUIScrollerBase.onButtonDown, guiButtonPlus ))
GUIManager.Add( Self)
Return Self
End Method
Method Remove:Int()
Super.Remove()
if guiButtonMinus then guiButtonMinus.Remove()
if guiButtonPlus then guiButtonPlus.Remove()
End Method
Method SetMouseScrollAmount(amount:Int)
mouseScrollAmount = amount
End Method
'override to also check buttons
Method IsAppearanceChanged:int()
if guiButtonMinus and guiButtonMinus.isAppearanceChanged() then return TRUE
if guiButtonPlus and guiButtonPlus.isAppearanceChanged() then return TRUE
return Super.isAppearanceChanged()
End Method
'override default
Method onAppearanceChanged:int()
If _parent
rect.position.setXY(_parent.rect.getW() - guiButtonMinus.rect.getW(),0)
'this also aligns the buttons
SetSize(_parent.rect.getW(), _parent.rect.getH())
Else
rect.position.setXY(rect.getW() - guiButtonMinus.rect.getW(),0)
'this also aligns the buttons
SetSize(rect.getW(), rect.getH())
EndIf
End Method
'override resize and add minSize-support
'size 0, 0 is not possible (leads to autosize)
Method SetSize(w:Float = 0, h:Float = 0)
'according the orientation we limit height or width
Select _orientation
case GUI_OBJECT_ORIENTATION_HORIZONTAL
h = guiButtonMinus.rect.getH()
'if w <= 0 then w = rect.GetW()
case GUI_OBJECT_ORIENTATION_VERTICAL
w = guiButtonMinus.rect.getW()
'if h <= 0 then h = rect.GetH()
End Select
Super.SetSize(w, h)
End Method
Method SetMinValue(value:Double)
minValue = value
'sort min max
if minValue > maxValue
local old:Double = minValue
minValue = maxValue
maxValue = minValue
endif
End Method
Method SetMaxValue(value:Double)
maxValue = value
'sort min max
if minValue > maxValue
local old:Double = minValue
minValue = maxValue
maxValue = minValue
endif
End Method
Method SetValueRange(minValue:Double, maxValue:Double)
SetMinValue(minValue)
SetMaxValue(maxValue)
End Method
Method SetCurrentValue(currentValue:Double)
TriggerBaseEvent(GUIEventKeys.GUIObject_OnChangeValue, null, self)
self.currentValue = Max(minValue, Min(maxValue, currentValue))
End Method
Method GetRelativeValue:Float()
if maxValue = minValue then return 0
return Min(1.0, Max(0.0, currentValue / (maxValue - minValue)))
End Method
Method SetRelativeValue:Double(percentage:Float)
SetCurrentValue(percentage * (maxValue - minValue))
return currentValue
End Method
Method SetOrientation:int(orientation:Int=0)
if _orientation = orientation then return FALSE
_orientation = orientation
Select _orientation
case GUI_OBJECT_ORIENTATION_HORIZONTAL
guiButtonMinus.SetDirection("LEFT")
guiButtonPlus.SetDirection("RIGHT")
'set scroller area to full WIDTH of parent
if _parent then rect.dimension.SetX(_parent.rect.getW())
default
guiButtonMinus.SetDirection("UP")
guiButtonPlus.SetDirection("DOWN")
'set scroller area to full height of parent
if _parent then rect.dimension.SetY(_parent.rect.getH())
End Select
SetSize(-1,-1)
return TRUE
End Method
Method onMouseScrollWheel:Int( triggerEvent:TEventBase ) override
Local value:Int = triggerEvent.GetData().getInt("value",0)
If value = 0 Then Return False
'emit event that the scroller position has changed
Local direction:String = ""
Select _orientation
Case GUI_OBJECT_ORIENTATION_VERTICAL
If value < 0 Then direction = "up"
If value > 0 Then direction = "down"
Case GUI_OBJECT_ORIENTATION_HORIZONTAL
If value < 0 Then direction = "left"
If value > 0 Then direction = "right"
End Select
If direction <> ""
TriggerBaseEvent(GUIEventKeys.GUIObject_OnScrollPositionChanged, New TData.AddString("direction", direction).AddInt("scrollAmount", mouseScrollAmount), self)
EndIf
'set to accepted so that nobody else receives the event
triggerEvent.SetAccepted(True)
End Method
'handle clicks on the up/down-buttons and inform others about changes
Function onButtonClick:Int( triggerEvent:TEventBase )
'only handle left/primary key clicks
If triggerEvent.GetData().GetInt("button") <> 1 Then Return False
Local sender:TGUIArrowButton = TGUIArrowButton(triggerEvent.GetSender())
If sender = Null Then Return False
Local guiScroller:TGUIScrollerBase = TGUIScrollerBase( sender._parent )
If guiScroller = Null Then Return False
'emit event that the scroller position has changed
If sender = guiScroller.guiButtonMinus or sender = guiScroller.guiButtonPlus
TriggerBaseEvent(GUIEventKeys.GUIObject_OnScrollPositionChanged, new TData.AddString("direction", sender.direction.ToLower()).AddInt("scrollAmount", 15), guiScroller)
EndIf
'handled the click
Return True
End Function
'handle mousedown on the up/down-buttons and inform others about changes
Function onButtonDown:Int( triggerEvent:TEventBase )
Local sender:TGUIArrowButton = TGUIArrowButton(triggerEvent.GetSender())
If sender = Null Then Return False
Local guiScroller:TGUIScrollerBase = TGUIScrollerBase( sender._parent )
If guiScroller = Null Then Return False
If MOUSEMANAGER.GetDownTime(1) > 0
'if we still have to wait - return without emitting events
If MOUSEMANAGER.GetDownTime(1) < guiScroller.mouseDownTime
Return False
EndIf
EndIf
guiScroller.begunAMouseDown = True
'emit event that the scroller position has changed
If sender = guiScroller.guiButtonMinus or sender = guiScroller.guiButtonPlus
TriggerBaseEvent(GUIEventKeys.GUIObject_OnScrollPositionChanged, new TData.AddString("direction", sender.direction.ToLower()), guiScroller )
EndIf
Return True
End Function
Method SetButtonStates:int(enableMinus:int = True, enablePlus:int = True)
if enableMinus <> guiButtonMinus.IsEnabled()
if enableMinus
guiButtonMinus.Enable()
else
guiButtonMinus.Disable()
endif
endif
if enablePlus <> guiButtonPlus.IsEnabled()
if enablePlus
guiButtonPlus.Enable()
else
guiButtonPlus.Disable()
endif
endif
End Method
Method ScrollerHasFocus:int()
return IsFocused() or guiButtonMinus.IsFocused() or guiButtonPlus.IsFocused()
End Method
Method Update:int()
Super.Update()
if begunAMouseDown
'skip next long click
MouseManager.SkipNextLongClick(1)
MouseManager.ResetClicked(1)
if not MouseManager.IsDown(1)
begunAMouseDown = False
'disable a potentially still active "to skip"
MouseManager.SkipNextLongClick(1, 0)
endif
MouseManager.ResetLongClicked(1)
endif
End Method
Method UpdateLayout()
'realign buttons
'move the first button to the most left and top position
guiButtonMinus.rect.position.SetXY(0, 0)
'move the second button to the most right and bottom position
'do not use "screenRect" for the button as the LimitToRect() filters it out
guiButtonPlus.rect.position.SetXY(GetScreenRect().GetW() - guiButtonPlus.rect.GetW(), GetScreenRect().GetH() - guiButtonPlus.rect.GetH())
End Method
End Type
Type TGUIScroller Extends TGUIScrollerBase
Field scrollHandle:TGUISlider
Method GetClassName:String()
Return "tguiscroller"
End Method
Method Create:TGUIScroller(parent:TGUIobject)
Super.Create(parent)
scrollHandle = New TGUISlider.Create(Null, new TVec2D.Init(22,100), "", "")
scrollHandle.setParent(Self)
scrollHandle.SetValueRange(0,100)
scrollHandle.steps = 0
scrollHandle.SetDirection(TGUISlider.DIRECTION_DOWN)
scrollHandle._showFilledGauge = False
scrollHandle._gaugeAlpha = 0.5
scrollHandle.SetOption(GUI_OBJECT_KEEP_ACTIVE_ON_OUTSIDE_CONTINUED_MOUSEDOWN)
scrollHandle.mouseScrollWheelStepSize = mouseScrollAmount
'manage (update/draw) the handle on our own
AddChild(scrollHandle)
'listen to interaction with scrollHandle elements (dragging it)
'or mouse scrolling over its "progress area"
'attention: do not listen to "guiobject.onchangevalue" as this is
' also triggered by "onScrollPositionChanged" (circlular
' triggers)
AddEventListener(EventManager.registerListenerFunction( GUIEventKeys.GUISlider_SetValueByMouse, onScrollHandleChange, scrollHandle ))
'listen to changes via scroller buttons
AddEventListener(EventManager.registerListenerFunction( GUIEventKeys.GUIObject_OnScrollPositionChanged, onScrollPositionChanged, self))
return self
End Method
Method SetMouseScrollAmount(amount:Int) override
Super.SetMouseScrollAmount(amount)
if scrollHandle then scrollHandle.mouseScrollWheelStepSize = mouseScrollAmount
End Method
Function onScrollPositionChanged:Int( triggerEvent:TEventBase )
Local sender:TGUIScroller = TGUIScroller(triggerEvent.GetSender())
If sender = Null Then Return False
'ignore if coming from our own (changeType = percentage)
if triggerEvent.GetData().Get("sendingSlider") = sender.scrollHandle then return False
' local percentage:Float = triggerEvent.GetData().GetFloat("percentage")
' print percentage + " " + sender.GetRelativeValue()
sender.scrollHandle.SetRelativeValue( sender.GetRelativeValue() )
End Function
'scroller got change
Function onScrollHandleChange:Int( triggerEvent:TEventBase )
Local sender:TGUISlider = TGUISlider(triggerEvent.GetSender())
If sender = Null Then Return False
Local guiScroller:TGUIScroller = TGUIScroller( sender._parent )
If guiScroller = Null Then Return False
guiScroller.SetRelativeValue( sender.GetRelativeValue() )
if MouseManager.IsDown(1) then guiScroller.begunAMouseDown = true
'inform others (equally to up/down-buttonclicks)
TriggerBaseEvent(GUIEventKeys.GUIObject_OnScrollPositionChanged, new TData.AddInt("isRelative", True).AddFloat("percentage", sender.GetRelativeValue()).Add("sendingSlider", sender), guiScroller )
End Function
Method SetCurrentValue(currentValue:Double)
Super.SetCurrentValue(currentValue)
'move handle accordingly
if scrollHandle then scrollHandle.SetRelativeValue( GetRelativeValue() )
End Method
'overridden
'set scroll handle too
Method SetRelativeValue:Double(percentage:Float)
Super.SetRelativeValue(percentage)
'move handle accordingly
if scrollHandle then scrollHandle.SetRelativeValue( GetRelativeValue() )
return currentValue
End Method
'override to also check handle
Method IsAppearanceChanged:int()
if scrollHandle and scrollHandle.isAppearanceChanged() then return TRUE
return Super.isAppearanceChanged()
End Method
Method ScrollerHasFocus:int()
return Super.ScrollerHasFocus() or scrollHandle.IsFocused()
End Method
Method DrawContent()
' DrawRect(GetScreenRect().GetX(), GetScreenRect().GetY(), GetScreenRect().GetW(), GetScreenRect().GetH())
End Method
Method UpdateLayout()
'realign buttons
Super.UpdateLayout()
'called after scrollHandle creation?
if scrollHandle
scrollHandle._handleDim.SetX( guiButtonMinus.rect.GetW() )
'according the orientation we limit height or width
Select _orientation
case GUI_OBJECT_ORIENTATION_HORIZONTAL
scrollHandle.rect.position.SetXY( guiButtonMinus.rect.GetX2()-3, guiButtonMinus.rect.GetY())
scrollHandle.rect.dimension.SetXY( guiButtonPlus.rect.GetX() - guiButtonMinus.rect.GetX2() + 6, guiButtonMinus.rect.GetH())
scrollHandle.SetDirection(TGUISlider.DIRECTION_RIGHT)
case GUI_OBJECT_ORIENTATION_VERTICAL
scrollHandle.rect.position.SetXY( guiButtonMinus.rect.GetX(), guiButtonMinus.rect.GetY2() -3)
scrollHandle.rect.dimension.SetXY( guiButtonPLus.rect.GetW(), guiButtonPlus.rect.GetY() - guiButtonMinus.rect.GetY2() + 6)
scrollHandle.SetDirection(TGUISlider.DIRECTION_DOWN)
End Select
endif
End Method
End Type
Type TGUIScrollerSimple Extends TGUIScrollerBase
'area showing progress / react to clicks
Field progressRect:TRectangle = new TRectangle.Init()
Field progressRectHovered:int = False
Method GetClassName:String()
Return "tguiscrollersimple"
End Method
Method Create:TGUIScrollerSimple(parent:TGUIobject)
Super.Create(parent)
return self
End Method
'override resize and add minSize-support
'size 0, 0 is not possible (leads to autosize)
Method SetSize(w:Float = 0, h:Float = 0)
Super.SetSize(w, h)
'=== PROGRESS RECT ===
'fill whole area
progressRect.SetXYWH(0,0, GetScreenRect().GetW(),GetScreenRect().GetH())
'subtract buttons
Select _orientation
case GUI_OBJECT_ORIENTATION_HORIZONTAL
progressRect.position.AddY(+guiButtonMinus.rect.GetH()/4)
progressRect.dimension.SetY(+guiButtonMinus.rect.GetH()/2)
progressRect.position.AddX(+guiButtonMinus.rect.GetW() - 2)
progressRect.dimension.AddX(-guiButtonMinus.rect.GetW() - guiButtonPlus.rect.GetW() + 2)
case GUI_OBJECT_ORIENTATION_VERTICAL
progressRect.position.AddX(+guiButtonMinus.rect.GetW()/4)
progressRect.dimension.SetX(+guiButtonMinus.rect.GetW()/2)
progressRect.position.AddY(+guiButtonMinus.rect.GetH() - 2)
progressRect.dimension.AddY(-guiButtonMinus.rect.GetH() - guiButtonPlus.rect.GetH() + 2)
End Select
End Method
'override to add progressRect-click support
Method Update:int()
Super.Update()
'check if mouse over progressRect
if not isHovered()
progressRectHovered = False
else
local overPos:TVec2D = new TVec2D.Init( MouseManager.x - GetScreenRect().GetX(), MouseManager.y - GetScreenRect().GetY())
if progressRect.ContainsVec(overPos)
progressRectHovered = True
endif
'check if mouse clicked on the progressRect
if mouseIsClicked
'convert clicked position to local widget coordinates
local clickPos:TVec2D = mouseIsClicked.Copy()
clickPos.AddXY(-GetScreenRect().GetX(), -GetScreenRect().GetY())
if progressRect.ContainsVec(clickPos)
' clickPos.AddXY(progressRect.GetX(), progressRect.GetY())
local progress:float = 0
Select _orientation
case GUI_OBJECT_ORIENTATION_HORIZONTAL
if progressRect.GetW() > 0
'subtract progress start from position
clickPos.X :- progressRect.GetX()
progress = clickPos.x / progressRect.GetW()
endif
case GUI_OBJECT_ORIENTATION_VERTICAL
if progressRect.GetH() > 0
'subtract progress start from position
clickPos.y :- progressRect.GetY()
progress = clickPos.y / progressRect.GetH()
endif
End Select
'scroll to the percentage
SetRelativeValue(progress)
'inform others
TriggerBaseEvent(GUIEventKeys.GUIObject_OnScrollPositionChanged, new TData.Addint("isRelative", True).AddFloat("percentage", GetRelativeValue()), self )
'reset clicked state and button state
mouseIsClicked = Null
'handled left click
MouseManager.SetClickHandled(1)
endif
endif
endif
End Method
Method DrawContent()
local oldCol:SColor8; GetColor(oldCol)
local oldColA:Float = GetAlpha()
SetAlpha oldColA * GetScreenAlpha() * 0.20
'draw a area to click on
if progressRectHovered
SetColor 125,0,0
else
SetColor 125,50,50
endif
DrawRect(GetScreenRect().GetX() + progressRect.GetX(), GetScreenRect().GetY() + progressRect.GetY(), progressRect.GetW(), progressRect.GetH())
SetAlpha oldColA * GetScreenAlpha() * 0.5
Select _orientation
case GUI_OBJECT_ORIENTATION_HORIZONTAL
DrawRect(GetScreenRect().GetX() + progressRect.GetX() + GetRelativeValue() * progressRect.GetW(), GetScreenRect().GetY() + progressRect.GetY(), 3, progressRect.GetH())
case GUI_OBJECT_ORIENTATION_VERTICAL
DrawRect(GetScreenRect().GetX() + progressRect.GetX(), GetScreenRect().GetY() + progressRect.GetY() + GetRelativeValue() * progressRect.GetH(), progressRect.GetW(), 3)
End Select
SetColor(oldCol)
SetAlpha(oldColA)
End Method
End Type