-
Notifications
You must be signed in to change notification settings - Fork 671
Expand file tree
/
Copy pathGList.cs
More file actions
3108 lines (2809 loc) · 110 KB
/
Copy pathGList.cs
File metadata and controls
3108 lines (2809 loc) · 110 KB
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
using System;
using System.Collections.Generic;
using FairyGUI.Utils;
using UnityEngine;
namespace FairyGUI
{
/// <summary>
/// Callback function when an item is needed to update its look.
/// </summary>
/// <param name="index">Item index.</param>
/// <param name="item">Item object.</param>
public delegate void ListItemRenderer(int index, GObject item);
/// <summary>
///
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public delegate string ListItemProvider(int index);
/// <summary>
/// GList class.
/// </summary>
public class GList : GComponent
{
/// <summary>
/// 如果true,当item不可见时自动折叠,否则依然占位
/// </summary>
public bool foldInvisibleItems = false;
/// <summary>
/// List selection mode
/// </summary>
/// <seealso cref="ListSelectionMode"/>
public ListSelectionMode selectionMode;
/// <summary>
/// Callback function when an item is needed to update its look.
/// </summary>
public ListItemRenderer itemRenderer;
/// <summary>
/// Callback funtion to return item resource url.
/// </summary>
public ListItemProvider itemProvider;
/// <summary>
///
/// </summary>
public bool scrollItemToViewOnClick;
string _defaultItem;
ListLayoutType _layout;
int _lineCount;
int _columnCount;
int _lineGap;
int _columnGap;
AlignType _align;
VertAlignType _verticalAlign;
bool _autoResizeItem;
Controller _selectionController;
GObjectPool _pool;
int _lastSelectedIndex;
EventListener _onClickItem;
EventListener _onRightClickItem;
//Virtual List support
bool _virtual;
bool _loop;
int _numItems;
List<object> _dataList; // list自身绑定的数据
int _realNumItems;
int _firstIndex; //the top left index
int _curLineItemCount; //item count in one line
int _curLineItemCount2; //只用在页面模式,表示垂直方向的项目数
Vector2 _itemSize;
int _virtualListChanged; //1-content changed, 2-size changed
uint itemInfoVer; //用来标志item是否在本次处理中已经被重用了
int _miscFlags; //1-event locked, 2-focus events registered
class ItemInfo
{
public Vector2 size;
public GObject obj;
public uint updateFlag;
public bool selected;
}
List<ItemInfo> _virtualItems;
EventCallback1 _itemClickDelegate;
public GList()
: base()
{
_trackBounds = true;
this.opaque = true;
scrollItemToViewOnClick = true;
container = new Container();
rootContainer.AddChild(container);
rootContainer.gameObject.name = "GList";
_pool = new GObjectPool(container.cachedTransform);
_dataList = new List<object>();
_itemClickDelegate = __clickItem;
}
public override void Dispose()
{
_pool.Clear();
if (_virtualListChanged != 0)
Timers.inst.Remove(this.RefreshVirtualList);
_selectionController = null;
scrollItemToViewOnClick = false;
itemRenderer = null;
itemProvider = null;
_dataList.Clear();
_dataList = null;
base.Dispose();
}
/// <summary>
/// Dispatched when a list item being clicked.
/// </summary>
public EventListener onClickItem
{
get { return _onClickItem ?? (_onClickItem = new EventListener(this, "onClickItem")); }
}
/// <summary>
/// Dispatched when a list item being clicked with right button.
/// </summary>
public EventListener onRightClickItem
{
get { return _onRightClickItem ?? (_onRightClickItem = new EventListener(this, "onRightClickItem")); }
}
/// <summary>
/// Resource url of the default item.
/// </summary>
public string defaultItem
{
get { return _defaultItem; }
set
{
_defaultItem = UIPackage.NormalizeURL(value);
}
}
/// <summary>
/// List layout type.
/// </summary>
public ListLayoutType layout
{
get { return _layout; }
set
{
if (_layout != value)
{
_layout = value;
SetBoundsChangedFlag();
if (_virtual)
SetVirtualListChangedFlag(true);
}
}
}
/// <summary>
///
/// </summary>
public int lineCount
{
get { return _lineCount; }
set
{
if (_lineCount != value)
{
_lineCount = value;
if (_layout == ListLayoutType.FlowVertical || _layout == ListLayoutType.Pagination)
{
SetBoundsChangedFlag();
if (_virtual)
SetVirtualListChangedFlag(true);
}
}
}
}
/// <summary>
///
/// </summary>
public int columnCount
{
get { return _columnCount; }
set
{
if (_columnCount != value)
{
_columnCount = value;
if (_layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.Pagination)
{
SetBoundsChangedFlag();
if (_virtual)
SetVirtualListChangedFlag(true);
}
}
}
}
/// <summary>
///
/// </summary>
public int lineGap
{
get { return _lineGap; }
set
{
if (_lineGap != value)
{
_lineGap = value;
SetBoundsChangedFlag();
if (_virtual)
SetVirtualListChangedFlag(true);
}
}
}
/// <summary>
///
/// </summary>
public int columnGap
{
get { return _columnGap; }
set
{
if (_columnGap != value)
{
_columnGap = value;
SetBoundsChangedFlag();
if (_virtual)
SetVirtualListChangedFlag(true);
}
}
}
/// <summary>
///
/// </summary>
public AlignType align
{
get { return _align; }
set
{
if (_align != value)
{
_align = value;
SetBoundsChangedFlag();
if (_virtual)
SetVirtualListChangedFlag(true);
}
}
}
/// <summary>
///
/// </summary>
public VertAlignType verticalAlign
{
get { return _verticalAlign; }
set
{
if (_verticalAlign != value)
{
_verticalAlign = value;
SetBoundsChangedFlag();
if (_virtual)
SetVirtualListChangedFlag(true);
}
}
}
/// <summary>
/// If the item will resize itself to fit the list width/height.
/// </summary>
public bool autoResizeItem
{
get { return _autoResizeItem; }
set
{
if (_autoResizeItem != value)
{
_autoResizeItem = value;
SetBoundsChangedFlag();
if (_virtual)
SetVirtualListChangedFlag(true);
}
}
}
/// <summary>
///
/// </summary>
/// <value></value>
public Vector2 defaultItemSize
{
get { return _itemSize; }
set
{
_itemSize = value;
if (_virtual)
{
if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowHorizontal)
this.scrollPane.scrollStep = _itemSize.y;
else
this.scrollPane.scrollStep = _itemSize.x;
SetVirtualListChangedFlag(true);
}
}
}
/// <summary>
///
/// </summary>
public GObjectPool itemPool
{
get { return _pool; }
}
/// <summary>
///
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public GObject GetFromPool(string url)
{
if (string.IsNullOrEmpty(url))
url = _defaultItem;
GObject ret = _pool.GetObject(url);
if (ret != null)
ret.visible = true;
return ret;
}
void ReturnToPool(GObject obj)
{
_pool.ReturnObject(obj);
}
/// <summary>
/// Add a item to list, same as GetFromPool+AddChild
/// </summary>
/// <returns>Item object</returns>
public GObject AddItemFromPool()
{
GObject obj = GetFromPool(null);
return AddChild(obj);
}
/// <summary>
/// Add a item to list, same as GetFromPool+AddChild
/// </summary>
/// <param name="url">Item resource url</param>
/// <returns>Item object</returns>
public GObject AddItemFromPool(string url)
{
GObject obj = GetFromPool(url);
return AddChild(obj);
}
/// <summary>
///
/// </summary>
/// <param name="child"></param>
/// <param name="index"></param>
/// <returns></returns>
override public GObject AddChildAt(GObject child, int index)
{
base.AddChildAt(child, index);
if (child is GButton)
{
GButton button = (GButton)child;
button.selected = false;
button.changeStateOnClick = false;
}
child.onClick.Add(_itemClickDelegate);
child.onRightClick.Add(_itemClickDelegate);
return child;
}
/// <summary>
///
/// </summary>
/// <param name="index"></param>
/// <param name="dispose"></param>
/// <returns></returns>
override public GObject RemoveChildAt(int index, bool dispose)
{
GObject child = base.RemoveChildAt(index, dispose);
child.onClick.Remove(_itemClickDelegate);
child.onRightClick.Remove(_itemClickDelegate);
return child;
}
/// <summary>
///
/// </summary>
/// <param name="index"></param>
public void RemoveChildToPoolAt(int index)
{
GObject child = base.RemoveChildAt(index);
ReturnToPool(child);
}
/// <summary>
///
/// </summary>
/// <param name="child"></param>
public void RemoveChildToPool(GObject child)
{
base.RemoveChild(child);
ReturnToPool(child);
}
/// <summary>
///
/// </summary>
public void RemoveChildrenToPool()
{
RemoveChildrenToPool(0, -1);
}
/// <summary>
///
/// </summary>
/// <param name="beginIndex"></param>
/// <param name="endIndex"></param>
public void RemoveChildrenToPool(int beginIndex, int endIndex)
{
if (endIndex < 0 || endIndex >= _children.Count)
endIndex = _children.Count - 1;
for (int i = beginIndex; i <= endIndex; ++i)
RemoveChildToPoolAt(beginIndex);
}
/// <summary>
///
/// </summary>
public int selectedIndex
{
get
{
if (_virtual)
{
int cnt = _realNumItems;
for (int i = 0; i < cnt; i++)
{
ItemInfo ii = _virtualItems[i];
if ((ii.obj is GButton) && ((GButton)ii.obj).selected
|| ii.obj == null && ii.selected)
{
if (_loop)
return i % _numItems;
else
return i;
}
}
}
else
{
int cnt = _children.Count;
for (int i = 0; i < cnt; i++)
{
GButton obj = _children[i].asButton;
if (obj != null && obj.selected)
return i;
}
}
return -1;
}
set
{
if (value >= 0 && value < this.numItems)
{
if (selectionMode != ListSelectionMode.Single)
ClearSelection();
AddSelection(value, false);
}
else
ClearSelection();
}
}
/// <summary>
///
/// </summary>
public Controller selectionController
{
get { return _selectionController; }
set { _selectionController = value; }
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public List<int> GetSelection()
{
return GetSelection(null);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public List<int> GetSelection(List<int> result)
{
if (result == null)
result = new List<int>();
if (_virtual)
{
int cnt = _realNumItems;
for (int i = 0; i < cnt; i++)
{
ItemInfo ii = _virtualItems[i];
if ((ii.obj is GButton) && ((GButton)ii.obj).selected
|| ii.obj == null && ii.selected)
{
int j = i;
if (_loop)
{
j = i % _numItems;
if (result.Contains(j))
continue;
}
result.Add(j);
}
}
}
else
{
int cnt = _children.Count;
for (int i = 0; i < cnt; i++)
{
GButton obj = _children[i].asButton;
if (obj != null && obj.selected)
result.Add(i);
}
}
return result;
}
/// <summary>
///
/// </summary>
/// <param name="index"></param>
/// <param name="scrollItToView"></param>
public void AddSelection(int index, bool scrollItToView)
{
if (selectionMode == ListSelectionMode.None)
return;
CheckVirtualList();
if (selectionMode == ListSelectionMode.Single)
ClearSelection();
if (scrollItToView)
ScrollToView(index);
_lastSelectedIndex = index;
GButton obj = null;
if (_virtual)
{
ItemInfo ii = _virtualItems[index];
if (ii.obj != null)
obj = ii.obj.asButton;
ii.selected = true;
}
else
obj = GetChildAt(index).asButton;
if (obj != null && !obj.selected)
{
obj.selected = true;
UpdateSelectionController(index);
}
}
/// <summary>
///
/// </summary>
/// <param name="index"></param>
public void RemoveSelection(int index)
{
if (selectionMode == ListSelectionMode.None)
return;
GButton obj = null;
if (_virtual)
{
ItemInfo ii = _virtualItems[index];
if (ii.obj != null)
obj = ii.obj.asButton;
ii.selected = false;
}
else
obj = GetChildAt(index).asButton;
if (obj != null)
obj.selected = false;
}
/// <summary>
///
/// </summary>
public void ClearSelection()
{
if (_virtual)
{
int cnt = _realNumItems;
for (int i = 0; i < cnt; i++)
{
ItemInfo ii = _virtualItems[i];
if ((ii.obj is GButton))
((GButton)ii.obj).selected = false;
ii.selected = false;
}
}
else
{
int cnt = _children.Count;
for (int i = 0; i < cnt; i++)
{
GButton obj = _children[i].asButton;
if (obj != null)
obj.selected = false;
}
}
}
void ClearSelectionExcept(GObject g)
{
if (_virtual)
{
int cnt = _realNumItems;
for (int i = 0; i < cnt; i++)
{
ItemInfo ii = _virtualItems[i];
if (ii.obj != g)
{
if ((ii.obj is GButton))
((GButton)ii.obj).selected = false;
ii.selected = false;
}
}
}
else
{
int cnt = _children.Count;
for (int i = 0; i < cnt; i++)
{
GButton obj = _children[i].asButton;
if (obj != null && obj != g)
obj.selected = false;
}
}
}
/// <summary>
///
/// </summary>
public void SelectAll()
{
CheckVirtualList();
int last = -1;
if (_virtual)
{
int cnt = _realNumItems;
for (int i = 0; i < cnt; i++)
{
ItemInfo ii = _virtualItems[i];
if ((ii.obj is GButton) && !((GButton)ii.obj).selected)
{
((GButton)ii.obj).selected = true;
last = i;
}
ii.selected = true;
}
}
else
{
int cnt = _children.Count;
for (int i = 0; i < cnt; i++)
{
GButton obj = _children[i].asButton;
if (obj != null && !obj.selected)
{
obj.selected = true;
last = i;
}
}
}
if (last != -1)
UpdateSelectionController(last);
}
/// <summary>
///
/// </summary>
public void SelectNone()
{
ClearSelection();
}
/// <summary>
///
/// </summary>
public void SelectReverse()
{
CheckVirtualList();
int last = -1;
if (_virtual)
{
int cnt = _realNumItems;
for (int i = 0; i < cnt; i++)
{
ItemInfo ii = _virtualItems[i];
if ((ii.obj is GButton))
{
((GButton)ii.obj).selected = !((GButton)ii.obj).selected;
if (((GButton)ii.obj).selected)
last = i;
}
ii.selected = !ii.selected;
}
}
else
{
int cnt = _children.Count;
for (int i = 0; i < cnt; i++)
{
GButton obj = _children[i].asButton;
if (obj != null)
{
obj.selected = !obj.selected;
if (obj.selected)
last = i;
}
}
}
if (last != -1)
UpdateSelectionController(last);
}
/// <summary>
///
/// </summary>
/// <param name="enabled"></param>
public void EnableSelectionFocusEvents(bool enabled)
{
if (((_miscFlags & 2) != 0) == enabled)
return;
if (enabled)
{
_miscFlags |= 2;
this.tabStopChildren = true;
onFocusIn.Add(NotifySelection);
onFocusOut.Add(NotifySelection);
}
else
{
_miscFlags &= 0xFD;
onFocusIn.Remove(NotifySelection);
onFocusOut.Remove(NotifySelection);
}
}
void NotifySelection(EventContext context)
{
string eventType = context.type == "onFocusIn" ? "onListFocusIn" : "onListFocusOut";
int cnt = _children.Count;
for (int i = 0; i < cnt; i++)
{
GButton obj = _children[i].asButton;
if (obj != null && obj.selected)
obj.DispatchEvent(eventType);
}
}
/// <summary>
///
/// </summary>
public void EnableArrowKeyNavigation(bool enabled)
{
if (enabled)
{
this.tabStopChildren = true;
onKeyDown.Add(__keydown);
}
else
{
this.tabStopChildren = false;
onKeyDown.Remove(__keydown);
}
}
void __keydown(EventContext context)
{
int index = -1;
switch (context.inputEvent.keyCode)
{
case KeyCode.LeftArrow:
index = HandleArrowKey(7);
break;
case KeyCode.RightArrow:
index = HandleArrowKey(3);
break;
case KeyCode.UpArrow:
index = HandleArrowKey(1);
break;
case KeyCode.DownArrow:
index = HandleArrowKey(5);
break;
}
if (index != -1)
{
index = ItemIndexToChildIndex(index);
if (index != -1)
DispatchItemEvent(GetChildAt(index), context);
context.StopPropagation();
}
}
/// <summary>
///
/// </summary>
/// <param name="dir"></param>
public int HandleArrowKey(int dir)
{
int curIndex = this.selectedIndex;
if (curIndex == -1)
return -1;
int index = curIndex;
switch (dir)
{
case 1://up
if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowVertical)
{
index--;
}
else if (_layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.Pagination)
{
if (_virtual)
{
index -= _curLineItemCount;
}
else
{
GObject current = _children[index];
int k = 0;
int i;
for (i = index - 1; i >= 0; i--)
{
GObject obj = _children[i];
if (obj.y != current.y)
{
current = obj;
break;
}
k++;
}
for (; i >= 0; i--)
{
GObject obj = _children[i];
if (obj.y != current.y)
{
index = i + k + 1;
break;
}
}
}
}
break;
case 3://right
if (_layout == ListLayoutType.SingleRow || _layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.Pagination)
{
index++;
}
else if (_layout == ListLayoutType.FlowVertical)
{
if (_virtual)
{
index += _curLineItemCount;
}
else
{
GObject current = _children[index];
int k = 0;
int cnt = _children.Count;
int i;
for (i = index + 1; i < cnt; i++)
{
GObject obj = _children[i];
if (obj.x != current.x)
{
current = obj;
break;
}
k++;
}
for (; i < cnt; i++)
{
GObject obj = _children[i];
if (obj.x != current.x)
{
index = i - k - 1;
break;
}
}
}
}
break;
case 5://down
if (_layout == ListLayoutType.SingleColumn || _layout == ListLayoutType.FlowVertical)
{
index++;
}
else if (_layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.Pagination)
{
if (_virtual)
{
index += _curLineItemCount;
}
else
{
GObject current = _children[index];
int k = 0;
int cnt = _children.Count;
int i;
for (i = index + 1; i < cnt; i++)
{
GObject obj = _children[i];
if (obj.y != current.y)
{
current = obj;
break;
}
k++;
}
for (; i < cnt; i++)
{
GObject obj = _children[i];
if (obj.y != current.y)
{
index = i - k - 1;
break;
}
}
}
}
break;
case 7://left
if (_layout == ListLayoutType.SingleRow || _layout == ListLayoutType.FlowHorizontal || _layout == ListLayoutType.Pagination)
{
index--;
}
else if (_layout == ListLayoutType.FlowVertical)
{
if (_virtual)
{
index -= _curLineItemCount;
}
else