-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbase.gfx.gui.list.base.bmx
executable file
·1621 lines (1253 loc) · 49 KB
/
base.gfx.gui.list.base.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Rem
===========================================================
GUI Basic List
===========================================================
Code contains:
- TGUIListBase: basic list
- TGUIListItem: basic list item
End Rem
SuperStrict
Import "base.gfx.gui.bmx"
Import "base.gfx.gui.scroller.bmx"
Import "base.gfx.gui.panel.scrollablepanel.bmx"
Import "base.util.helper.bmx"
'=== LIST SPECIFIC CONSTANTS ===
Const GUILIST_AUTOSCROLL:Int = 2^0
'hide scroller if mouse not over parent
Const GUILIST_AUTOHIDE_SCROLLER:Int = 2^1
Const GUILIST_MULTICOLUMN:Int = 2^2
Const GUILIST_AUTOSORT_ITEMS:Int = 2^3
'flag to do a "one time" auto scroll
Const GUILIST_SCROLLER_USED:Int = 2^4
Const GUILIST_SCROLLING_ENABLED:Int = 2^5
'scroll to the very first element as soon as the scrollbars get hidden ?
Const GUILIST_SCROLL_TO_BEGIN_WITHOUT_SCROLLBARS:Int = 2^6
Const GUILIST_SCROLL_TO_NEXT_ITEM:Int = 2^7
Const GUILISTITEM_AUTOSIZE_WIDTH:Int = 2^0
Type TGUIListBase Extends TGUIobject
Field guiBackground:TGUIobject = Null
Field backgroundColor:TColor = TColor.Create(0,0,0,0)
Field backgroundColorHovered:TColor = TColor.Create(0,0,0,0)
Field guiEntriesPanel:TGUIScrollablePanel = Null
Field guiScrollerH:TGUIScrollerBase = Null
Field guiScrollerV:TGUIScrollerBase = Null
Field entriesDimension:TVec2D = New TVec2D.Init(0,0)
Field _listFlags:Int = 0
Field _autoSortFunction:Int(o1:Object,o2:Object)
Field _autoSortInAscendingOrder:Int = True
'amount (percentage) a list is scrolled based on item height
Field scrollItemHeightPercentage:Float = 0.5
Field entries:TList = CreateList()
Field entriesLimit:Int = -1
'private mouseover-field (ignoring covering child elements)
Field _mouseOverArea:Int = False
'displace each entry by (z-value is stepping)...
Field _entryDisplacement:TVec3D = New TVec3D.Init(0, 0, 1)
'displace the entriesblock by x,y...
Field _entriesBlockDisplacement:TVec2D = New TVec2D.Init(0, 0)
'orientation of the list: 0 means vertical, 1 is horizontal
Field _orientation:Int = 0
Method GetClassName:String()
Return "tguilistbase"
End Method
Method New()
setListOption(GUILIST_AUTOSCROLL, False)
setListOption(GUILIST_AUTOHIDE_SCROLLER, False)
setListOption(GUILIST_MULTICOLUMN, False)
setListOption(GUILIST_AUTOSORT_ITEMS, True)
setListOption(GUILIST_SCROLL_TO_BEGIN_WITHOUT_SCROLLBARS, True)
'by default all lists accept drop
setOption(GUI_OBJECT_ACCEPTS_DROP, True)
End Method
Method Create:TGUIListBase(position:TVec2D = Null, dimension:TVec2D = Null, limitState:String = "")
Super.CreateBase(position, dimension, limitState)
setZIndex(0)
guiScrollerH = New TGUIScroller.Create(Self)
guiScrollerV = New TGUIScroller.Create(Self)
'orientation of horizontal scroller has to get set manually
guiScrollerH.SetOrientation(GUI_OBJECT_ORIENTATION_HORIZONTAL)
guiEntriesPanel = New TGUIScrollablePanel.Create(Null, New TVec2D.Init(rect.GetW() - guiScrollerV.rect.getW(), rect.GetH() - guiScrollerH.rect.getH()), limitState)
'manage by our own
AddChild(guiEntriesPanel)
AddChild(guiScrollerH)
AddChild(guiScrollerV)
'by default all lists do not have scrollers
setScrollerState(False, False)
'the entries panel cannot be focused
guiEntriesPanel.setOption(GUI_OBJECT_CAN_GAIN_FOCUS, False)
'register events
'- we are interested in certain events from the scroller or self
AddEventListener(EventManager.registerListenerFunction( GUIEventKeys.GUIObject_OnScrollPositionChanged, onScroll, guiScrollerH ))
AddEventListener(EventManager.registerListenerFunction( GUIEventKeys.GUIObject_OnScrollPositionChanged, onScroll, guiScrollerV ))
'listening to our own scroll events redirects them to the same
'as the scrollers - but without requiring it
AddEventListener(EventManager.registerListenerFunction( GUIEventKeys.GUIObject_OnScrollPositionChanged, onScroll, self ))
'redirect the scroll event of the panel
AddEventListener(EventManager.registerListenerFunction( GUIEventKeys.GUIObject_OnMouseScrollwheel, onPanelMouseScrollWheel, guiEntriespanel ))
'is something dropping - check if it this list
SetAcceptDrop("TGUIListItem")
GUIManager.Add(Self)
Return Self
End Method
Method hasListOption:Int(option:Int)
Return (_listFlags & option) <> 0
End Method
Method setListOption(option:Int, enable:Int=True)
If enable
_listFlags :| option
Else
_listFlags :& ~option
EndIf
End Method
Method Remove:Int()
Super.Remove()
If guiEntriesPanel Then guiEntriesPanel.Remove()
If guiScrollerH Then guiScrollerH.Remove()
If guiScrollerV Then guiScrollerV.Remove()
'set all entries free
EmptyList()
End Method
Method EmptyList:Int()
'traverse a copy to avoid concurrent modification
For Local obj:TGUIobject = EachIn entries.Copy()
'call the objects cleanup-method and unsets the object
obj.remove()
GUIManager.Remove(obj)
obj = Null
Next
entries.Clear()
'also reset scroll state!
ScrollToFirstItem()
End Method
Method SetEntryDisplacement(x:Float=0.0, y:Float=0.0, stepping:Int=1)
_entryDisplacement.SetXYZ(x,y, Max(1,stepping))
End Method
Method SetEntriesBlockDisplacement(x:Float=0.0, y:Float=0.0)
_entriesBlockDisplacement.SetXY(x,y)
End Method
Method SetOrientation(orientation:Int=0)
_orientation = orientation
End Method
Method SetMultiColumn(bool:Int=False)
If hasListOption(GUILIST_MULTICOLUMN) <> bool
setListOption(GUILIST_MULTICOLUMN, bool)
'maybe now more or less elements fit into the visible
'area, so elements position need to get recalculated
InvalidateLayout()
EndIf
End Method
Method SetAutosortItems(bool:Int=False)
If hasListOption(GUILIST_AUTOSORT_ITEMS) <> bool
setListOption(GUILIST_AUTOSORT_ITEMS, bool)
EndIf
End Method
Method SetAutoscroll(bool:Int=False)
If hasListOption(GUILIST_AUTOSCROLL) <> bool
setListOption(GUILIST_AUTOSCROLL, bool)
EndIf
End Method
Method SetBackground(background:TGUIobject)
'set old background to managed again
If guiBackground Then GUIManager.add(guiBackground)
'assign new background
guiBackground = background
If guiBackground
guiBackground.SetOPTION(GUI_OBJECT_IGNORE_PARENTPADDING, True)
guiBackground.setParent(Self)
'set to unmanaged in all cases
GUIManager.remove(guiBackground)
EndIf
End Method
'when reskinned, resize to move scrollbars accordingly
Method onAppearanceChanged:Int()
Super.onAppearanceChanged()
UpdateLayout()
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) override
Super.SetSize(w,h)
' If guiBackground Then guiBackground.SetSize(w, h)
'let the children properly refresh their size
'(eg. because the scrollbars are visible now)
For Local entry:TGUIobject = EachIn entries
entry.onParentResize()
Next
End Method
Method SetItemLimit:Int(limit:Int)
entriesLimit = limit
End Method
Method ReachedItemLimit:Int()
If entriesLimit <= 0 Then Return False
Return (entries.count() >= entriesLimit)
End Method
Method GetItemByCoord:TGUIobject(coord:TVec2D)
For Local entry:TGUIobject = EachIn entries
'our entries are sorted and replaced, so we could
'quit as soon as the entry is out of range...
If entry.GetScreenRect().GetY() > GetScreenRect().GetY2() Then Return Null
If entry.GetScreenRect().GetX() > GetScreenRect().GetX2() Then Return Null
If entry.GetScreenRect().containsXY(coord.GetX(), coord.GetY()) Then Return entry
Next
Return Null
End Method
Method GetItemByListPosition:TGUIobject(pos:TVec2D)
For Local entry:TGUIobject = EachIn entries
'our entries are sorted and replaced, so we could
'quit as soon as the entry is out of range...
If entry.rect.GetY() > pos.y Then Return Null
If entry.rect.GetX() > pos.x Then Return Null
If entry.rect.containsVec(pos) Then Return entry
Next
Return Null
End Method
Method GetItemIndex:Int(item:Object)
'using "object" to avoid filtering and therefor invalid indices
'also it should be faster than loops with "ValueAtIndex"-calls in
'it
Local index:Int = 0
For Local obj:Object = EachIn entries
If item = obj Then Return index
index :+ 1
Next
Return -1
End Method
Method GetItemAtIndex:TGUIObject(index:Int)
if index >= entries.Count() or index < 0 then Return Null
Return TGUIObject(entries.ValueAtIndex(index))
End Method
Method GetItemNextToItem:TGUIobject(item:Object, distance:Int, allowDistanceReductionOnBorders:Int = True)
Local currentIndex:Int = GetItemIndex(item)
If currentIndex = -1 Then Return Null
Local nextIndex:Int = 0
'when reaching below "0" or more than "max" should we return the
'very first/last or nothing?
If currentIndex + distance < 0
If Not allowDistanceReductionOnBorders Then Return Null
nextIndex = 0
Else If currentIndex + distance >= entries.Count()
If Not allowDistanceReductionOnBorders Then Return Null
nextIndex = entries.Count() - 1
EndIf
Return GetItemAtIndex(nextIndex)
End Method
'base handling of add item
Method _AddItem:Int(item:TGUIobject, extra:Object=Null)
' if self.ReachedItemLimit() then return FALSE
'set parent of the item - so item is able to calculate position
guiEntriesPanel.addChild( item )
'recalculate dimensions as the item now knows its parent
'so a normal AddItem-handler can work with calculated dimensions from now on
'Local dimension:TVec2D = item.getDimension()
TriggerBaseEvent(GUIEventKeys.GUIList_AddItem, New TData.Add("item", item) , Self)
entries.addLast(item)
if TGUIListItem(item) then TGUIListItem(item).parentListID = self._id
'resize item
If item Then item.onParentResize()
'run the custom compare method
If hasListOption(GUILIST_AUTOSORT_ITEMS)
If _autoSortFunction
entries.sort(_autoSortInAscendingOrder, _autoSortFunction)
Else
entries.sort(_autoSortInAscendingOrder)
' entries.sort(_autoSortInAscendingOrder, SortByCompare)
' entries.sort(_autoSortInAscendingOrder, SortByValue)
EndIf
EndIf
TriggerBaseEvent(GUIEventKeys.GUIList_AddedItem, New TData.Add("item", item) , Self)
Return True
End Method
'base handling of remove item
Method _RemoveItem:Int(item:TGUIobject)
If entries.Remove(item)
TriggerBaseEvent(GUIEventKeys.GUIList_RemoveItem, New TData.Add("item", item) , Self)
if TGUIListItem(item) then TGUIListItem(item).parentListID = -1
'remove from panel and item gets managed by guimanager
guiEntriesPanel.removeChild(item)
TriggerBaseEvent(GUIEventKeys.GUIList_RemovedItem, New TData.Add("item", item) , Self)
Return True
Else
Print "not able to remove item "+item._id
Return False
EndIf
End Method
'overrideable AddItem-Handler
Method AddItem:Int(item:TGUIobject, extra:Object=Null)
If _AddItem(item, extra)
'recalculate positions, dimensions etc.
InvalidateLayout()
Return True
EndIf
Return False
End Method
'overrideable RemoveItem-Handler
Method RemoveItem:Int(item:TGUIobject)
If _RemoveItem(item)
InvalidateLayout()
Return True
EndIf
Return False
End Method
Method HasItem:Int(item:TGUIobject)
'TODO: maybe faster to just check parentListID
'if TGUIListItem(item)
' Return TGUIListItem(item).parentListID = self._id
'endif
For Local otheritem:TGUIobject = EachIn entries
If otheritem = item Then Return True
Next
Return False
End Method
Method IsAtListBottom:Int()
Local result:Int = 1 > Floor(Abs(guiEntriesPanel.scrollLimit.GetY() - guiEntriesPanel.scrollPosition.getY()))
'if all items fit on the screen, scroll limit will be
'lower than the container panel
If Abs(guiEntriesPanel.scrollLimit.GetY()) < guiEntriesPanel.GetContentScreenRect().GetH()
'if scrolled = 0 this could also mean we scrolled up to the top part
'in this case we check if the last item in the list fits into the
'panel
If guiEntriesPanel.scrollPosition.getY() = 0
result = 1
Local lastItem:TGUIListItem = GetLastItem()
If lastItem And lastItem.GetScreenRect().GetY() > guiEntriesPanel.GetContentScreenRect().GetY2()
result = 0
EndIf
EndIf
EndIf
Return result
End Method
Method GetFirstItem:TGUIListItem()
Return TGUIListItem(entries.First())
End Method
Method GetLastItem:TGUIListItem()
Return TGUIListItem(entries.Last())
End Method
Method GetLastItemY:Int()
Local i:TGUIListItem = GetLastItem()
If i Then Return i.GetScreenRect().GetY()
Return 0
End Method
'recalculate scroll maximas, item positions...
Method RecalculateElements:Int()
Local startPos:TVec2D = _entriesBlockDisplacement.copy()
Local entryNumber:Int = 1
Local nextPos:TVec2D = startPos.copy()
Local currentPos:TVec2D = New TVec2D
Local columnPadding:Int = 5
'reset current entriesDimension ...
entriesDimension.CopyFrom(_entriesBlockDisplacement)
For Local entry:TGUIobject = EachIn entries
currentPos.CopyFrom( nextPos )
'==== CALCULATE POSITION ====
Select _orientation
'only from top to bottom
Case GUI_OBJECT_ORIENTATION_VERTICAL
'MultiColumn: from left to right, if space left a
' new column on the right is started.
'advance the next position starter
If hasListOption(GUILIST_MULTICOLUMN)
'if entry does not fit, try the next line
If currentPos.GetX() + entry.rect.GetW() > GetContentScreenRect().GetW()
currentPos.SetXY(startPos.GetX(), currentPos.GetY() + entry.rect.GetH())
'new lines increase dimension of container
entriesDimension.AddXY(0, entry.rect.GetH())
EndIf
nextPos.CopyFrom(currentPos)
nextPos.AddXY(entry.rect.GetW() + columnPadding, 0)
Else
nextPos.CopyFrom(currentPos)
nextPos.AddXY(0, entry.rect.GetH())
'new lines increase dimension of container
entriesDimension.AddXY(0, entry.rect.GetH())
EndIf
Case GUI_OBJECT_ORIENTATION_HORIZONTAL
'MultiColumn: from top to bottom, if space left a
' new line below is started.
'advance the next position starter
If hasListOption(GUILIST_MULTICOLUMN)
'if entry does not fit, try the next row
If currentPos.GetY() + entry.rect.GetH() > GetContentScreenRect().GetH()
currentPos.SetXY(currentPos.GetX() + entry.rect.GetW(), startPos.GetY())
'new lines increase dimension of container
entriesDimension.AddXY(entry.rect.GetW(), 0 )
EndIf
nextPos.CopyFrom(currentPos)
nextPos.AddXY(0, entry.rect.GetH() + columnPadding)
Else
nextPos.CopyFrom(currentPos)
nextPos.AddXY(entry.rect.GetW(),0)
'new lines increase dimension of container
entriesDimension.AddXY(entry.rect.GetW(), 0 )
EndIf
End Select
'==== ADD POTENTIAL DISPLACEMENT ====
'add the displacement afterwards - so the first one is not displaced
If entryNumber Mod _entryDisplacement.z = 0 And entry <> GetLastItem()
currentPos.AddXY(_entryDisplacement.x, _entryDisplacement.y)
'increase dimension if positive displacement
entriesDimension.AddXY( Max(0,_entryDisplacement.x), Max(0, _entryDisplacement.y))
EndIf
'==== SET POSITION ====
If Not entry.rect.position.IsSame(currentPos)
entry.rect.position.CopyFrom(currentPos)
entry.InvalidateScreenRect()
EndIf
entryNumber:+1
Next
UpdateLimitsAndScrollerState()
End Method
Method UpdateLimitsAndScrollerState()
If guiEntriesPanel
'resize container panel
guiEntriesPanel.SetSize(entriesDimension.getX(), entriesDimension.getY())
'refresh scrolling limits
RefreshListLimits()
'if not all entries fit on the panel, enable scroller
SetScrollerState(..
entriesDimension.getX() > guiEntriesPanel.GetContentScreenRect().GetW(), ..
entriesDimension.getY() > guiEntriesPanel.GetContentScreenRect().GetH() ..
)
EndIf
End Method
Method RefreshListLimits:Int()
If Not guiEntriesPanel Then Return False
'if entriesDimension.GetX() = 0 then return false
Select _orientation
'===== VERTICAL ALIGNMENT =====
Case GUI_OBJECT_ORIENTATION_VERTICAL
'determine if we did not scroll the list to a middle
'position so this is true if we are at the very bottom
'of the list aka "the end"
Local atListBottom:Int = IsAtListBottom()
'set scroll limits:
If entriesDimension.getY() <= guiEntriesPanel.GetContentScreenRect().GetH()
'if there are only some elements, they might be
'"less high" than the available area - no need to
'align them at the bottom
guiEntriesPanel.SetLimits(0, -entriesDimension.getY())
Else
'maximum is at the bottom of the area, not top - so
'subtract height
'old: guiEntriesPanel.SetLimits(0, -(dimension.getY() - guiEntriesPanel.GetScreenRect().GetH()) )
Local lastItem:TGUIListItem = GetLastItem()
If Not lastItem And GetLastItemY() = 0
guiEntriesPanel.SetLimits(0, 0)
Else
' if not autoScroll
' guiEntriesPanel.SetLimits(0, - (dimension.getY() - guiEntriesPanel.GetScreenRect().GetH() - lastItem.GetScreenRect().GetH()))
' else
guiEntriesPanel.SetLimits(0, - (entriesDimension.getY() - guiEntriesPanel.GetContentScreenRect().GetH()))
' endif
EndIf
'in case of auto scrolling we should consider
'scrolling to the next visible part
If hasListOption(GUILIST_AUTOSCROLL) And (Not hasListOption(GUILIST_SCROLLER_USED) Or atListBottom) Then scrollToLastItem()
EndIf
'===== HORIZONTAL ALIGNMENT =====
Case GUI_OBJECT_ORIENTATION_HORIZONTAL
'determine if we did not scroll the list to a middle
'position so this is true if we are at the very right
'of the list aka "the end"
Local atListBottom:Int = 1 > Floor( Abs(guiEntriesPanel.scrollLimit.GetX() - guiEntriesPanel.scrollPosition.getX() ) )
'set scroll limits:
If entriesDimension.getX() < guiEntriesPanel.GetContentScreenRect().GetW()
'if there are only some elements, they might be
'"less high" than the available area - no need to
'align them at the right
guiEntriesPanel.SetLimits(-entriesDimension.getX(), 0 )
Else
'maximum is at the bottom of the area, not top - so
'subtract height
'old: guiEntriesPanel.SetLimits(-(dimension.getX() - guiEntriesPanel.GetScreenRect().GetW()), 0)
Local lastItem:TGUIListItem = GetLastItem()
If Not lastItem
guiEntriesPanel.SetLimits(0, 0)
Else
guiEntriesPanel.SetLimits(- (entriesDimension.getX() - guiEntriesPanel.GetContentScreenRect().GetW() - lastItem.GetScreenRect().GetW()), 0)
EndIf
'in case of auto scrolling we should consider
'scrolling to the next visible part
If hasListOption(GUILIST_AUTOSCROLL) And (Not hasListOption(GUILIST_SCROLLER_USED) Or atListBottom) Then scrollToLastItem()
EndIf
End Select
guiScrollerH.SetValueRange(0, entriesDimension.getX())
guiScrollerV.SetValueRange(0, entriesDimension.getY())
End Method
Method SetScrollerState:Int(boolH:Int, boolV:Int)
'set scrolling as enabled or disabled
SetListOption(GUILIST_SCROLLING_ENABLED, (boolH Or boolV))
Local changed:Int = False
If boolH <> guiScrollerH.hasOption(GUI_OBJECT_ENABLED) Then changed = True
If boolV <> guiScrollerV.hasOption(GUI_OBJECT_ENABLED) Then changed = True
'as soon as the scroller gets disabled, we scroll to the first
'item.
'ATTENTION: if you do not want this behaviour, set the variable below
' accordingly
If changed And HasListOption(GUILIST_SCROLL_TO_BEGIN_WITHOUT_SCROLLBARS)
If Not HasListOption(GUILIST_SCROLLING_ENABLED) Then ScrollToFirstItem()
End If
guiScrollerH.setOption(GUI_OBJECT_ENABLED, boolH)
guiScrollerH.setOption(GUI_OBJECT_VISIBLE, boolH)
guiScrollerV.setOption(GUI_OBJECT_ENABLED, boolV)
guiScrollerV.setOption(GUI_OBJECT_VISIBLE, boolV)
'resize everything
If changed Then UpdateLayout()
End Method
Function FindGUIListBaseParent:TGUIListBase(guiObject:TGUIObject)
If Not guiObject Then Return Null
'self is already the list?
Local guiList:TGUIListBase = TGUIListBase(guiObject)
If guiList Then Return guiList
Local obj:TGUIObject = guiObject._parent
While obj and obj <> guiObject And Not TGUIListBase(obj)
obj = obj._parent
Wend
'return the list or null (if topmost parent is of incompatible type)
Return TGUIListBase(obj)
End Function
'override default
Method onDrop:Int(triggerEvent:TEventBase) override
'we could check for dragged element here
triggerEvent.setAccepted(True)
Return True
End Method
'default handler for the case of an item being dropped back to its
'parent list
'by default it does not handly anything, so returns FALSE
Method HandleDropBack:Int(triggerEvent:TEventBase)
Return False
End Method
Method onTryDropOnTarget:Int( triggerEvent:TEventBase ) override
If not AcceptsDropObject(triggerEvent.GetSender())
triggerEvent.SetVeto()
Return False
endif
Return Super.onTryDropOnTarget(triggerEvent)
End Method
Method onDropOnTarget:Int( triggerEvent:TEventBase ) override
Local item:TGUIListItem = TGUIListItem(triggerEvent.GetSender())
If item = Null Then Return False
'ATTENTION:
'Item is still in dragged state!
'Keep this in mind when sorting the items
'only handle if coming from another list ?
Local fromList:TGUIListBase = FindGUIListBaseParent(item._parent)
'if not fromList then return FALSE
' Local toList:TGUIListBase = TGUIListBase(triggerEvent.GetReceiver())
' If Not toList Then Return False
Local data:TData = triggerEvent.getData()
If Not data Then Return False
If fromList = self
'if the handler took care of everything, we skip
'removing and adding the item
If fromList.HandleDropBack(triggerEvent)
'inform others about that dropback
TriggerBaseEvent(GUIEventKeys.GUIObject_OnDropBack, Null , item, self)
Return True
EndIf
'no drop back?
'we might have dropped it on another slot/position
EndIf
'method A
'move item if possible
If fromList Then fromList.removeItem(item)
'try to add the item, if not able, readd
If Not addItem(item, data)
If fromList And fromList.addItem(item) Then Return True
'not able to add to "toList" but also not to "fromList"
'so set veto and keep the item dragged
triggerEvent.setVeto()
Return False
EndIf
Return True
End Method
'redirect panel scrolling to parental list
Function onPanelMouseScrollWheel:Int( triggerEvent:TEventBase )
Local guiSender:TGUIObject = TGUIObject(triggerEvent.GetSender())
If Not guiSender Then Return False
Local guiList:TGUIListBase = FindGUIListBaseParent(guiSender)
If Not guiList Then Return False
If guiList
guiList.onMouseScrollWheel(triggerEvent)
EndIf
End Function
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 <> ""
Local scrollAmount:Int = 25
'try to scroll by X percent of an item height
Local item:TGUIObject
If entries.Count() > 0 Then item = TGUIObject(entries.First())
If item Then scrollAmount = item.rect.GetH() * scrollItemHeightPercentage
'instead of scrolling on our own - we just emulate scrolling
'via scrollers (which should only exist if scrolling is there)
'but IF there was no scroller
' if direction = "left" or direction = "right"
' TriggerBaseEvent(GUIEventKeys.GUIObject_OnScrollPositionChanged, New TData.AddString("direction", direction).AddNumber("scrollAmount", scrollAmount), guiScrollerH)
' else
' TriggerBaseEvent(GUIEventKeys.GUIObject_OnScrollPositionChanged, New TData.AddString("direction", direction).AddNumber("scrollAmount", scrollAmount), guiScrollerV)
' endif
TriggerBaseEvent(GUIEventKeys.GUIObject_OnScrollPositionChanged, New TData.AddString("direction", direction).AddNumber("scrollAmount", scrollAmount), self)
EndIf
'set to accepted so that nobody else receives the event
triggerEvent.SetAccepted(True)
End Method
'handle events from the connected scroller
Function onScroll:Int( triggerEvent:TEventBase )
Local guiSender:TGUIObject = TGUIObject(triggerEvent.GetSender())
If Not guiSender Then Return False
'search a TGUIListBase-parent
Local guiList:TGUIListBase = FindGUIListBaseParent(guiSender)
If Not guiList Then Return False
'do not allow scrolling if not enabled
If Not guiList.HasListOption(GUILIST_SCROLLING_ENABLED) Then Return False
Local data:TData = triggerEvent.GetData()
If Not data Then Return False
'by default scroll by 2 pixels
'the longer you pressed the mouse button, the "speedier" we get
'1px per 100ms. Start speeding up after 500ms, limit to 20px per scroll
Local baseScrollSpeed:Int = Min(20, 2 + Max(0, MOUSEMANAGER.GetDownTime(1) - 500)/100.0)
If guiList.HasListOption(GUILIST_SCROLL_TO_NEXT_ITEM)
'for now assume same height for all (as we do not know what
'item to use as "current item")
Local currentItem:TGUIListItem = guiList.GetFirstItem()
If currentItem
Local itemHeight:Int = Max(currentItem.rect.GetH(), currentItem.GetScreenRect().GetH())
If itemHeight = 0 Then itemHeight = 20
baseScrollSpeed = itemHeight * Ceil(baseScrollSpeed / Float(itemHeight))
EndIf
EndIf
Local scrollAmount:Int = data.GetInt("scrollAmount", baseScrollSpeed)
'this should be "calculate height and change amount"
If data.GetString("direction") = "up" Then guiList.ScrollEntries(0, +scrollAmount)
If data.GetString("direction") = "down" Then guiList.ScrollEntries(0, -scrollAmount)
If data.GetString("direction") = "left" Then guiList.ScrollEntries(+scrollAmount,0)
If data.GetString("direction") = "right" Then guiList.ScrollEntries(-scrollAmount,0)
'maybe data was given in percents - so something like a
'"scrollTo"-value
If data.GetInt("isRelative") = 1
Local percentage:Float = data.GetFloat("percentage", 0)
If guiSender = guiList.guiScrollerH
guiList.SetScrollPercentageX(percentage)
ElseIf guiSender = guiList.guiScrollerV
guiList.SetScrollPercentageY(percentage)
EndIf
EndIf
'from now on the user decides if he wants the end of the chat or stay inbetween
guiList.SetListOption(GUILIST_SCROLLER_USED, True)
End Function
'positive values scroll to top or left
Method ScrollEntries(dx:Float, dy:Float)
if guiEntriesPanel Then guiEntriesPanel.scrollBy(dx,dy)
'refresh scroller values (for "progress bar" on the scroller)
If dx <> 0 and guiScrollerH Then guiScrollerH.SetRelativeValue( GetScrollPercentageX() )
If dy <> 0 and guiScrollerV Then guiScrollerV.SetRelativeValue( GetScrollPercentageY() )
InvalidateLayout()
End Method
Method GetScrollPercentageY:Float()
Return guiEntriesPanel.GetScrollPercentageY()
End Method
Method GetScrollPercentageX:Float()
Return guiEntriesPanel.GetScrollPercentageX()
End Method
Method SetScrollPercentageX:Float(percentage:Float = 0.0)
if percentage = guiEntriesPanel.GetScrollPercentageX()
Return percentage
EndIf
InvalidateLayout()
rem
'scroller displays "0 - 100%" along his axis
'entries panel might be bigger than the container, to ensure
'that the panel is always visible, percentage must take
'content size and panel size into consideration
Local panelScrollValue:Float = (abs(guiEntriesPanel.scrollLimit.GetX()) - GetContentScreenRect().GetW() )
'turn negative if required (items scroll from right to left)
panelScrollValue :* sgn(guiEntriesPanel.scrollLimit.GetX())
'avoid "bottom alignment" (if list being bigger than entries panel)
panelScrollValue = Min(0, panelScrollValue)
guiEntriesPanel.ScrollToX(panelScrollValue)
endrem
guiEntriesPanel.SetScrollPercentageX(percentage)
If guiScrollerH Then guiScrollerH.SetRelativeValue(percentage)
Return guiEntriesPanel.GetScrollPercentageX()
End Method
Method SetScrollPercentageY:Float(percentage:Float = 0.0)
if percentage = guiEntriesPanel.GetScrollPercentageY()
Return percentage
EndIf
InvalidateLayout()
rem
'scroller displays "0 - 100%" along his axis
'entries panel might be bigger than the container, to ensure
'that the panel is always visible, percentage must take
'content size and panel size into consideration
Local panelScrollValue:Float = (abs(guiEntriesPanel.scrollLimit.GetY()) - GetContentScreenRect().GetH() )
'turn negative if required (items scroll from bottom to top)
panelScrollValue :* sgn(guiEntriesPanel.scrollLimit.GetY())
'avoid "bottom alignment" (if list being bigger than entries panel)
panelScrollValue = Min(0, panelScrollValue)
guiEntriesPanel.ScrollToY(panelScrollValue)
endrem
guiEntriesPanel.SetScrollPercentageY(percentage)
If guiScrollerV Then guiScrollerV.SetRelativeValue( percentage )
Return guiEntriesPanel.GetScrollPercentageY()
End Method
Method ScrollToItemIndex(index:Int, alignment:Float = 0.5)
local item:TGUIObject = GetItemAtIndex(index)
ScrollToItem(item, alignment)
End Method
'scroll to the given item
'@alignment defines the location of the item in the list
' 0.0 = top, 0.5 = center and 1.0 = bottom
Method ScrollToItem(item:TGUIobject, alignment:Float = 0.5)
if not item then return
if not HasItem(item) then return
'bottom alignment means the item must still be visible, so
'maximum space to align along is "list height minus item height"
local alignmentHeight:Int = Max(0, guiEntriesPanel.GetScreenRect().GetH() - item.rect.GetH())
local alignmentWidth:Int = Max(0, guiEntriesPanel.GetScreenRect().GetW() - item.rect.GetW())
'- move to top
'- sum heights of all items until desired one
'- scroll down to this height (item then is "on top")
' + include alignment offset (item then "where needed" )
ScrollToFirstItem()
local earlierItemsHeight:Int
local earlierItemsWidth:Int
For Local obj:TGUIObject = EachIn entries
if obj = item then exit
earlierItemsHeight :+ obj.rect.GetH()
earlierItemsWidth :+ obj.rect.GetW()
Next
Select _orientation
Case GUI_OBJECT_ORIENTATION_VERTICAL
ScrollEntries(0, -(earlierItemsHeight - alignmentHeight * alignment))
Case GUI_OBJECT_ORIENTATION_HORIZONTAL
ScrollEntries(-(earlierItemsWidth - alignmentWidth * alignment), 0)
End Select
End Method
Method ScrollToFirstItem()
Select _orientation
Case GUI_OBJECT_ORIENTATION_VERTICAL
guiEntriesPanel.SetScrollPercentageY(0)
If guiScrollerV Then guiScrollerV.SetRelativeValue(0)
Case GUI_OBJECT_ORIENTATION_HORIZONTAL
guiEntriesPanel.SetScrollPercentageX(0)
If guiScrollerH Then guiScrollerH.SetRelativeValue(0)
End Select
End Method
Method ScrollToLastItem()
Select _orientation
Case GUI_OBJECT_ORIENTATION_VERTICAL
guiEntriesPanel.SetScrollPercentageY(1.0)
If guiScrollerV Then guiScrollerV.SetRelativeValue(1.0)
Case GUI_OBJECT_ORIENTATION_HORIZONTAL
guiEntriesPanel.SetScrollPercentageX(1.0)
If guiScrollerH Then guiScrollerH.SetRelativeValue(1.0)
End Select
End Method
Function SortByCompare:Int(o1:Object,o2:Object)
If Not TGUIObject(o1) Then Return 0
Return o1.Compare(o2)
End Function
Function SortByValue:Int(o1:Object,o2:Object)
If Not TGUIObject(o1) Then Return 0
If Not TGUIObject(o2) Then Return 1
If TGUIObject(o1).GetValue() > TGUIObject(o2).GetValue() Return 1
Return 0
End Function
Method EnsureEntryIsVisible(item:TGUIObject)
If Not item Then Return
'if bottom of entry ends after bottom of list, ensure visibility
'if bottom of last entry ends before bottom of list, try to scroll up a bit