forked from MattRix/UnityDecompiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiColumnHeader.cs
More file actions
648 lines (594 loc) · 18.4 KB
/
Copy pathMultiColumnHeader.cs
File metadata and controls
648 lines (594 loc) · 18.4 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using UnityEngine;
namespace UnityEditor.IMGUI.Controls
{
public class MultiColumnHeader
{
public delegate void HeaderCallback(MultiColumnHeader multiColumnHeader);
public static class DefaultGUI
{
public static float defaultHeight
{
get
{
return 27f;
}
}
public static float minimumHeight
{
get
{
return 20f;
}
}
public static float columnContentMargin
{
get
{
return 3f;
}
}
internal static float labelSpaceFromBottom
{
get
{
return 3f;
}
}
}
public static class DefaultStyles
{
public static GUIStyle columnHeader;
public static GUIStyle columnHeaderRightAligned;
public static GUIStyle columnHeaderCenterAligned;
public static GUIStyle background;
internal static GUIStyle arrowStyle;
static DefaultStyles()
{
MultiColumnHeader.DefaultStyles.background = new GUIStyle("ProjectBrowserTopBarBg");
MultiColumnHeader.DefaultStyles.background.fixedHeight = 0f;
MultiColumnHeader.DefaultStyles.background.border = new RectOffset(3, 3, 3, 3);
MultiColumnHeader.DefaultStyles.columnHeader = new GUIStyle(EditorStyles.label);
MultiColumnHeader.DefaultStyles.columnHeader.alignment = TextAnchor.MiddleLeft;
MultiColumnHeader.DefaultStyles.columnHeader.padding = new RectOffset(4, 4, 0, 0);
Color textColor = MultiColumnHeader.DefaultStyles.columnHeader.normal.textColor;
textColor.a = 0.8f;
MultiColumnHeader.DefaultStyles.columnHeader.normal.textColor = textColor;
MultiColumnHeader.DefaultStyles.columnHeaderRightAligned = new GUIStyle(MultiColumnHeader.DefaultStyles.columnHeader);
MultiColumnHeader.DefaultStyles.columnHeaderRightAligned.alignment = TextAnchor.MiddleRight;
MultiColumnHeader.DefaultStyles.columnHeaderCenterAligned = new GUIStyle(MultiColumnHeader.DefaultStyles.columnHeader);
MultiColumnHeader.DefaultStyles.columnHeaderCenterAligned.alignment = TextAnchor.MiddleCenter;
MultiColumnHeader.DefaultStyles.arrowStyle = new GUIStyle(EditorStyles.label);
MultiColumnHeader.DefaultStyles.arrowStyle.padding = new RectOffset();
MultiColumnHeader.DefaultStyles.arrowStyle.fixedWidth = 13f;
MultiColumnHeader.DefaultStyles.arrowStyle.alignment = TextAnchor.UpperCenter;
}
}
private MultiColumnHeaderState m_State;
private float m_Height = MultiColumnHeader.DefaultGUI.defaultHeight;
private float m_DividerWidth = 6f;
private Rect m_PreviousRect;
private bool m_ResizeToFit = false;
private bool m_CanSort = true;
private GUIView m_GUIView;
private Rect[] m_ColumnRects;
public event MultiColumnHeader.HeaderCallback sortingChanged
{
add
{
MultiColumnHeader.HeaderCallback headerCallback = this.sortingChanged;
MultiColumnHeader.HeaderCallback headerCallback2;
do
{
headerCallback2 = headerCallback;
headerCallback = Interlocked.CompareExchange<MultiColumnHeader.HeaderCallback>(ref this.sortingChanged, (MultiColumnHeader.HeaderCallback)Delegate.Combine(headerCallback2, value), headerCallback);
}
while (headerCallback != headerCallback2);
}
remove
{
MultiColumnHeader.HeaderCallback headerCallback = this.sortingChanged;
MultiColumnHeader.HeaderCallback headerCallback2;
do
{
headerCallback2 = headerCallback;
headerCallback = Interlocked.CompareExchange<MultiColumnHeader.HeaderCallback>(ref this.sortingChanged, (MultiColumnHeader.HeaderCallback)Delegate.Remove(headerCallback2, value), headerCallback);
}
while (headerCallback != headerCallback2);
}
}
public event MultiColumnHeader.HeaderCallback visibleColumnsChanged
{
add
{
MultiColumnHeader.HeaderCallback headerCallback = this.visibleColumnsChanged;
MultiColumnHeader.HeaderCallback headerCallback2;
do
{
headerCallback2 = headerCallback;
headerCallback = Interlocked.CompareExchange<MultiColumnHeader.HeaderCallback>(ref this.visibleColumnsChanged, (MultiColumnHeader.HeaderCallback)Delegate.Combine(headerCallback2, value), headerCallback);
}
while (headerCallback != headerCallback2);
}
remove
{
MultiColumnHeader.HeaderCallback headerCallback = this.visibleColumnsChanged;
MultiColumnHeader.HeaderCallback headerCallback2;
do
{
headerCallback2 = headerCallback;
headerCallback = Interlocked.CompareExchange<MultiColumnHeader.HeaderCallback>(ref this.visibleColumnsChanged, (MultiColumnHeader.HeaderCallback)Delegate.Remove(headerCallback2, value), headerCallback);
}
while (headerCallback != headerCallback2);
}
}
public int sortedColumnIndex
{
get
{
return this.state.sortedColumnIndex;
}
set
{
if (value != this.state.sortedColumnIndex)
{
this.state.sortedColumnIndex = value;
this.OnSortingChanged();
}
}
}
public MultiColumnHeaderState state
{
get
{
return this.m_State;
}
set
{
if (value == null)
{
throw new ArgumentNullException("state", "MultiColumnHeader state is not allowed to be null");
}
this.m_State = value;
}
}
public float height
{
get
{
return this.m_Height;
}
set
{
this.m_Height = value;
}
}
public bool canSort
{
get
{
return this.m_CanSort;
}
set
{
this.m_CanSort = value;
this.height = this.m_Height;
}
}
public MultiColumnHeader(MultiColumnHeaderState state)
{
this.m_State = state;
}
public void SetSortingColumns(int[] columnIndices, bool[] sortAscending)
{
if (columnIndices == null)
{
throw new ArgumentNullException("columnIndices");
}
if (sortAscending == null)
{
throw new ArgumentNullException("sortAscending");
}
if (columnIndices.Length != sortAscending.Length)
{
throw new ArgumentException("Input arrays should have same length");
}
if (columnIndices.Length > this.state.maximumNumberOfSortedColumns)
{
throw new ArgumentException(string.Concat(new object[]
{
"The maximum number of sorted columns is ",
this.state.maximumNumberOfSortedColumns,
". Trying to set ",
columnIndices.Length,
" columns."
}));
}
if (columnIndices.Length != columnIndices.Distinct<int>().Count<int>())
{
throw new ArgumentException("Duplicate column indices are not allowed", "columnIndices");
}
bool flag = false;
if (!columnIndices.SequenceEqual(this.state.sortedColumns))
{
this.state.sortedColumns = columnIndices;
flag = true;
}
for (int i = 0; i < columnIndices.Length; i++)
{
MultiColumnHeaderState.Column column = this.GetColumn(columnIndices[i]);
if (column.sortedAscending != sortAscending[i])
{
column.sortedAscending = sortAscending[i];
flag = true;
}
}
if (flag)
{
this.OnSortingChanged();
}
}
public void SetSorting(int columnIndex, bool sortAscending)
{
bool flag = false;
if (this.state.sortedColumnIndex != columnIndex)
{
this.state.sortedColumnIndex = columnIndex;
flag = true;
}
MultiColumnHeaderState.Column column = this.GetColumn(columnIndex);
if (column.sortedAscending != sortAscending)
{
column.sortedAscending = sortAscending;
flag = true;
}
if (flag)
{
this.OnSortingChanged();
}
}
public void SetSortDirection(int columnIndex, bool sortAscending)
{
MultiColumnHeaderState.Column column = this.GetColumn(columnIndex);
if (column.sortedAscending != sortAscending)
{
column.sortedAscending = sortAscending;
this.OnSortingChanged();
}
}
public bool IsSortedAscending(int columnIndex)
{
return this.GetColumn(columnIndex).sortedAscending;
}
public MultiColumnHeaderState.Column GetColumn(int columnIndex)
{
if (columnIndex < 0 || columnIndex >= this.state.columns.Length)
{
throw new ArgumentOutOfRangeException("columnIndex", string.Format("columnIndex {0} is not valid when the current column count is {1}", columnIndex, this.state.columns.Length));
}
return this.state.columns[columnIndex];
}
public bool IsColumnVisible(int columnIndex)
{
return this.state.visibleColumns.Any((int t) => t == columnIndex);
}
public int GetVisibleColumnIndex(int columnIndex)
{
for (int i = 0; i < this.state.visibleColumns.Length; i++)
{
if (this.state.visibleColumns[i] == columnIndex)
{
return i;
}
}
string arg = string.Join(", ", (from t in this.state.visibleColumns
select t.ToString()).ToArray<string>());
throw new ArgumentException(string.Format("Invalid columnIndex: {0}. The index is not part of the current visible columns: {1}", columnIndex, arg), "columnIndex");
}
public Rect GetCellRect(int visibleColumnIndex, Rect rowRect)
{
Rect columnRect = this.GetColumnRect(visibleColumnIndex);
columnRect.y = rowRect.y;
columnRect.height = rowRect.height;
return columnRect;
}
public Rect GetColumnRect(int visibleColumnIndex)
{
if (visibleColumnIndex < 0 || visibleColumnIndex >= this.m_ColumnRects.Length)
{
throw new ArgumentException(string.Format("The provided visibleColumnIndex is invalid. Ensure the index ({0}) is within the number of visible columns ({1})", visibleColumnIndex, this.m_ColumnRects.Length), "visibleColumnIndex");
}
return this.m_ColumnRects[visibleColumnIndex];
}
public void ResizeToFit()
{
this.m_ResizeToFit = true;
this.Repaint();
}
private void UpdateColumnHeaderRects(Rect totalHeaderRect)
{
if (this.m_ColumnRects == null || this.m_ColumnRects.Length != this.state.visibleColumns.Length)
{
this.m_ColumnRects = new Rect[this.state.visibleColumns.Length];
}
Rect rect = totalHeaderRect;
for (int i = 0; i < this.state.visibleColumns.Length; i++)
{
int num = this.state.visibleColumns[i];
MultiColumnHeaderState.Column column = this.state.columns[num];
if (i > 0)
{
rect.x += rect.width;
}
rect.width = column.width;
this.m_ColumnRects[i] = rect;
}
}
public virtual void OnGUI(Rect rect, float xScroll)
{
Event current = Event.current;
if (this.m_GUIView == null)
{
this.m_GUIView = GUIView.current;
}
this.DetectSizeChanges(rect);
if (this.m_ResizeToFit && current.type == EventType.Repaint)
{
this.m_ResizeToFit = false;
this.ResizeColumnsWidthsProportionally(rect.width - GUI.skin.verticalScrollbar.fixedWidth - this.state.widthOfAllVisibleColumns);
}
GUIClip.Push(rect, new Vector2(-xScroll, 0f), Vector2.zero, false);
Rect totalHeaderRect = new Rect(0f, 0f, rect.width, rect.height);
float widthOfAllVisibleColumns = this.state.widthOfAllVisibleColumns;
float width = ((totalHeaderRect.width <= widthOfAllVisibleColumns) ? widthOfAllVisibleColumns : totalHeaderRect.width) + GUI.skin.verticalScrollbar.fixedWidth;
Rect position = new Rect(0f, 0f, width, totalHeaderRect.height);
GUI.Label(position, GUIContent.none, MultiColumnHeader.DefaultStyles.background);
if (current.type == EventType.ContextClick && position.Contains(current.mousePosition))
{
current.Use();
this.DoContextMenu();
}
this.UpdateColumnHeaderRects(totalHeaderRect);
for (int i = 0; i < this.state.visibleColumns.Length; i++)
{
int num = this.state.visibleColumns[i];
MultiColumnHeaderState.Column column = this.state.columns[num];
Rect headerRect = this.m_ColumnRects[i];
Rect dividerRect = new Rect(headerRect.xMax - 1f, headerRect.y + 4f, 1f, headerRect.height - 8f);
Rect position2 = new Rect(dividerRect.x - this.m_DividerWidth * 0.5f, totalHeaderRect.y, this.m_DividerWidth, totalHeaderRect.height);
bool flag;
column.width = EditorGUI.WidthResizer(position2, column.width, column.minWidth, column.maxWidth, out flag);
if (flag && current.type == EventType.Repaint)
{
this.DrawColumnResizing(headerRect, column);
}
this.DrawDivider(dividerRect, column);
this.ColumnHeaderGUI(column, headerRect, num);
}
GUIClip.Pop();
}
internal virtual void DrawColumnResizing(Rect headerRect, MultiColumnHeaderState.Column column)
{
headerRect.y += 1f;
headerRect.width -= 1f;
headerRect.height -= 2f;
EditorGUI.DrawRect(headerRect, new Color(0.5f, 0.5f, 0.5f, 0.1f));
}
internal virtual void DrawDivider(Rect dividerRect, MultiColumnHeaderState.Column column)
{
EditorGUI.DrawRect(dividerRect, new Color(0.5f, 0.5f, 0.5f, 0.5f));
}
protected virtual void ColumnHeaderClicked(MultiColumnHeaderState.Column column, int columnIndex)
{
if (this.state.sortedColumnIndex == columnIndex)
{
column.sortedAscending = !column.sortedAscending;
}
else
{
this.state.sortedColumnIndex = columnIndex;
}
this.OnSortingChanged();
}
protected virtual void OnSortingChanged()
{
if (this.sortingChanged != null)
{
this.sortingChanged(this);
}
}
protected virtual void ColumnHeaderGUI(MultiColumnHeaderState.Column column, Rect headerRect, int columnIndex)
{
if (this.canSort && column.canSort)
{
this.SortingButton(column, headerRect, columnIndex);
}
GUIStyle style = this.GetStyle(column.headerTextAlignment);
float singleLineHeight = EditorGUIUtility.singleLineHeight;
Rect position = new Rect(headerRect.x, headerRect.yMax - singleLineHeight - MultiColumnHeader.DefaultGUI.labelSpaceFromBottom, headerRect.width, singleLineHeight);
GUI.Label(position, column.headerContent, style);
}
protected void SortingButton(MultiColumnHeaderState.Column column, Rect headerRect, int columnIndex)
{
if (EditorGUI.Button(headerRect, GUIContent.none, GUIStyle.none))
{
this.ColumnHeaderClicked(column, columnIndex);
}
if (columnIndex == this.state.sortedColumnIndex && Event.current.type == EventType.Repaint)
{
Rect arrowRect = this.GetArrowRect(column, headerRect);
Matrix4x4 matrix = GUI.matrix;
if (column.sortedAscending)
{
GUIUtility.RotateAroundPivot(180f, arrowRect.center - new Vector2(0f, 1f));
}
GUI.Label(arrowRect, "▾", MultiColumnHeader.DefaultStyles.arrowStyle);
if (column.sortedAscending)
{
GUI.matrix = matrix;
}
}
}
internal virtual Rect GetArrowRect(MultiColumnHeaderState.Column column, Rect headerRect)
{
float fixedWidth = MultiColumnHeader.DefaultStyles.arrowStyle.fixedWidth;
float y = headerRect.y;
float f = 0f;
switch (column.sortingArrowAlignment)
{
case TextAlignment.Left:
f = headerRect.x + (float)MultiColumnHeader.DefaultStyles.columnHeader.padding.left;
break;
case TextAlignment.Center:
f = headerRect.x + headerRect.width * 0.5f - fixedWidth * 0.5f;
break;
case TextAlignment.Right:
f = headerRect.xMax - (float)MultiColumnHeader.DefaultStyles.columnHeader.padding.right - fixedWidth;
break;
default:
Debug.LogError("Unhandled enum");
break;
}
Rect result = new Rect(Mathf.Round(f), y, fixedWidth, 16f);
return result;
}
private GUIStyle GetStyle(TextAlignment alignment)
{
GUIStyle result;
switch (alignment)
{
case TextAlignment.Left:
result = MultiColumnHeader.DefaultStyles.columnHeader;
break;
case TextAlignment.Center:
result = MultiColumnHeader.DefaultStyles.columnHeaderCenterAligned;
break;
case TextAlignment.Right:
result = MultiColumnHeader.DefaultStyles.columnHeaderRightAligned;
break;
default:
result = MultiColumnHeader.DefaultStyles.columnHeader;
break;
}
return result;
}
private void DoContextMenu()
{
GenericMenu genericMenu = new GenericMenu();
this.AddColumnHeaderContextMenuItems(genericMenu);
genericMenu.ShowAsContext();
}
protected virtual void AddColumnHeaderContextMenuItems(GenericMenu menu)
{
menu.AddItem(new GUIContent("Resize to Fit"), false, new GenericMenu.MenuFunction(this.ResizeToFit));
menu.AddSeparator("");
for (int i = 0; i < this.state.columns.Length; i++)
{
MultiColumnHeaderState.Column column = this.state.columns[i];
string text = string.IsNullOrEmpty(column.contextMenuText) ? column.headerContent.text : column.contextMenuText;
if (column.allowToggleVisibility)
{
menu.AddItem(new GUIContent(text), this.state.visibleColumns.Contains(i), new GenericMenu.MenuFunction2(this.ToggleVisibility), i);
}
else
{
menu.AddDisabledItem(new GUIContent(text));
}
}
}
protected virtual void OnVisibleColumnsChanged()
{
if (this.visibleColumnsChanged != null)
{
this.visibleColumnsChanged(this);
}
}
private void ToggleVisibility(object userData)
{
this.ToggleVisibility((int)userData);
}
protected virtual void ToggleVisibility(int columnIndex)
{
List<int> list = new List<int>(this.state.visibleColumns);
if (list.Contains(columnIndex))
{
list.Remove(columnIndex);
}
else
{
list.Add(columnIndex);
list.Sort();
}
this.state.visibleColumns = list.ToArray();
this.Repaint();
this.OnVisibleColumnsChanged();
}
public void Repaint()
{
if (this.m_GUIView != null)
{
this.m_GUIView.Repaint();
}
}
private void DetectSizeChanges(Rect rect)
{
if (Event.current.type == EventType.Repaint)
{
if (this.m_PreviousRect.width > 0f)
{
float num = Mathf.Round(rect.width - this.m_PreviousRect.width);
if (num != 0f)
{
float fixedWidth = GUI.skin.verticalScrollbar.fixedWidth;
if (rect.width - fixedWidth > this.state.widthOfAllVisibleColumns || num < 0f)
{
this.ResizeColumnsWidthsProportionally(num);
}
}
}
this.m_PreviousRect = rect;
}
}
private void ResizeColumnsWidthsProportionally(float deltaWidth)
{
List<MultiColumnHeaderState.Column> list = null;
int[] visibleColumns = this.state.visibleColumns;
int i = 0;
while (i < visibleColumns.Length)
{
int num = visibleColumns[i];
MultiColumnHeaderState.Column column = this.state.columns[num];
if (column.autoResize)
{
if (deltaWidth <= 0f || column.width < column.maxWidth)
{
if (deltaWidth >= 0f || column.width > column.minWidth)
{
if (list == null)
{
list = new List<MultiColumnHeaderState.Column>();
}
list.Add(column);
}
}
}
IL_94:
i++;
continue;
goto IL_94;
}
if (list != null)
{
float num2 = list.Sum((MultiColumnHeaderState.Column x) => x.width);
foreach (MultiColumnHeaderState.Column current in list)
{
current.width += deltaWidth * (current.width / num2);
current.width = Mathf.Clamp(current.width, current.minWidth, current.maxWidth);
}
}
}
}
}