forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiColumnHeader.cs
More file actions
497 lines (418 loc) · 18.1 KB
/
Copy pathMultiColumnHeader.cs
File metadata and controls
497 lines (418 loc) · 18.1 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace UnityEditor.IMGUI.Controls
{
public partial class MultiColumnHeader
{
MultiColumnHeaderState m_State;
float m_Height = DefaultGUI.defaultHeight;
float m_DividerWidth = 6;
Rect m_PreviousRect;
bool m_ResizeToFit = false;
bool m_CanSort = true;
GUIView m_GUIView;
Rect[] m_ColumnRects;
public delegate void HeaderCallback(MultiColumnHeader multiColumnHeader);
public event HeaderCallback sortingChanged;
public event HeaderCallback visibleColumnsChanged;
public MultiColumnHeader(MultiColumnHeaderState state)
{
m_State = state;
}
public int sortedColumnIndex
{
get { return state.sortedColumnIndex; }
set
{
if (value != state.sortedColumnIndex)
{
state.sortedColumnIndex = value;
OnSortingChanged();
}
}
}
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 > state.maximumNumberOfSortedColumns)
throw new ArgumentException("The maximum number of sorted columns is " + state.maximumNumberOfSortedColumns + ". Trying to set " + columnIndices.Length + " columns.");
if (columnIndices.Length != columnIndices.Distinct().Count())
throw new ArgumentException("Duplicate column indices are not allowed", "columnIndices");
bool changed = false;
if (!columnIndices.SequenceEqual(state.sortedColumns))
{
state.sortedColumns = columnIndices;
changed = true;
}
for (int i = 0; i < columnIndices.Length; ++i)
{
var column = GetColumn(columnIndices[i]);
if (column.sortedAscending != sortAscending[i])
{
column.sortedAscending = sortAscending[i];
changed = true;
}
}
if (changed)
OnSortingChanged();
}
public void SetSorting(int columnIndex, bool sortAscending)
{
bool changed = false;
if (state.sortedColumnIndex != columnIndex)
{
state.sortedColumnIndex = columnIndex;
changed = true;
}
var column = GetColumn(columnIndex);
if (column.sortedAscending != sortAscending)
{
column.sortedAscending = sortAscending;
changed = true;
}
if (changed)
OnSortingChanged();
}
public void SetSortDirection(int columnIndex, bool sortAscending)
{
var column = GetColumn(columnIndex);
if (column.sortedAscending != sortAscending)
{
column.sortedAscending = sortAscending;
OnSortingChanged();
}
}
public bool IsSortedAscending(int columnIndex)
{
return GetColumn(columnIndex).sortedAscending;
}
public MultiColumnHeaderState.Column GetColumn(int columnIndex)
{
if (columnIndex < 0 || columnIndex >= state.columns.Length)
throw new ArgumentOutOfRangeException("columnIndex", string.Format("columnIndex {0} is not valid when the current column count is {1}", columnIndex, state.columns.Length));
return state.columns[columnIndex];
}
public MultiColumnHeaderState state
{
get { return m_State; }
set
{
if (value == null)
throw new ArgumentNullException("state", "MultiColumnHeader state is not allowed to be null");
m_State = value;
}
}
public float height
{
get { return m_Height; }
set { m_Height = value; }
}
public bool canSort
{
get { return m_CanSort; }
set
{
m_CanSort = value;
height = m_Height;
}
}
public bool IsColumnVisible(int columnIndex)
{
return state.visibleColumns.Any(t => t == columnIndex);
}
public int GetVisibleColumnIndex(int columnIndex)
{
for (int i = 0; i < state.visibleColumns.Length; i++)
{
if (state.visibleColumns[i] == columnIndex)
return i;
}
string visibleIndices = string.Join(", ", state.visibleColumns.Select(t => t.ToString()).ToArray());
throw new ArgumentException(string.Format("Invalid columnIndex: {0}. The index is not part of the current visible columns: {1}", columnIndex, visibleIndices), "columnIndex");
}
public Rect GetCellRect(int visibleColumnIndex, Rect rowRect)
{
Rect result = GetColumnRect(visibleColumnIndex);
result.y = rowRect.y;
result.height = rowRect.height;
return result;
}
public Rect GetColumnRect(int visibleColumnIndex)
{
if (visibleColumnIndex < 0 || visibleColumnIndex >= 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, m_ColumnRects.Length), "visibleColumnIndex");
return m_ColumnRects[visibleColumnIndex];
}
public void ResizeToFit()
{
m_ResizeToFit = true;
Repaint();
}
void UpdateColumnHeaderRects(Rect totalHeaderRect)
{
if (m_ColumnRects == null || m_ColumnRects.Length != state.visibleColumns.Length)
m_ColumnRects = new Rect[state.visibleColumns.Length];
Rect curRect = totalHeaderRect;
for (int v = 0; v < state.visibleColumns.Length; v++)
{
int columnIndex = state.visibleColumns[v];
MultiColumnHeaderState.Column column = state.columns[columnIndex];
if (v > 0)
curRect.x += curRect.width;
curRect.width = column.width;
m_ColumnRects[v] = curRect;
}
}
// Virtual so clients can override header behavior and rendering entirely
public virtual void OnGUI(Rect rect, float xScroll)
{
Event evt = Event.current;
if (m_GUIView == null)
m_GUIView = GUIView.current;
DetectSizeChanges(rect);
if (m_ResizeToFit && evt.type == EventType.Repaint)
{
m_ResizeToFit = false;
ResizeColumnsWidthsProportionally(rect.width - GUI.skin.verticalScrollbar.fixedWidth - state.widthOfAllVisibleColumns);
}
// We create a guiclip to let the header be able to scroll horizontally according to the tree view's horizontal scroll
GUIClip.Push(rect, new Vector2(-xScroll, 0f), Vector2.zero, false);
{
Rect localRect = new Rect(0, 0, rect.width, rect.height);
// Background ( We always add the width of the vertical scrollbar to accomodate if this is being shown below e.g by a tree view)
float widthOfAllColumns = state.widthOfAllVisibleColumns;
float backgroundWidth = (localRect.width > widthOfAllColumns ? localRect.width : widthOfAllColumns) + GUI.skin.verticalScrollbar.fixedWidth;
Rect backgroundRect = new Rect(0, 0, backgroundWidth, localRect.height);
GUI.Label(backgroundRect, GUIContent.none, DefaultStyles.background);
// Context menu
if (evt.type == EventType.ContextClick && backgroundRect.Contains(evt.mousePosition))
{
evt.Use();
DoContextMenu();
}
// Update column rects (cached for clients to have fast access to column rects by using GetCellRect)
UpdateColumnHeaderRects(localRect);
// Columns
for (int v = 0; v < state.visibleColumns.Length; v++)
{
int columnIndex = state.visibleColumns[v];
MultiColumnHeaderState.Column column = state.columns[columnIndex];
Rect headerRect = m_ColumnRects[v];
const float limitHeightOfDivider = 4f;
Rect dividerRect = new Rect(headerRect.xMax - 1, headerRect.y + limitHeightOfDivider, 1f, headerRect.height - 2 * limitHeightOfDivider);
// Resize columns logic
Rect dragRect = new Rect(dividerRect.x - m_DividerWidth * 0.5f, localRect.y, m_DividerWidth, localRect.height);
bool hasControl;
column.width = EditorGUI.WidthResizer(dragRect, column.width, column.minWidth, column.maxWidth, out hasControl);
if (hasControl && evt.type == EventType.Repaint)
{
DrawColumnResizing(headerRect, column);
}
// Draw divider (can be overridden)
DrawDivider(dividerRect, column);
// Draw header (can be overridden)
ColumnHeaderGUI(column, headerRect, columnIndex);
}
}
GUIClip.Pop();
}
internal virtual void DrawColumnResizing(Rect headerRect, MultiColumnHeaderState.Column column)
{
const float margin = 1;
headerRect.y += margin;
headerRect.width -= margin;
headerRect.height -= 2 * margin;
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 (state.sortedColumnIndex == columnIndex)
column.sortedAscending = !column.sortedAscending;
else
state.sortedColumnIndex = columnIndex;
OnSortingChanged();
}
protected virtual void OnSortingChanged()
{
if (sortingChanged != null)
sortingChanged(this);
}
protected virtual void ColumnHeaderGUI(MultiColumnHeaderState.Column column, Rect headerRect, int columnIndex)
{
if (canSort && column.canSort)
{
SortingButton(column, headerRect, columnIndex);
}
GUIStyle style = GetStyle(column.headerTextAlignment);
float labelHeight = EditorGUI.kWindowToolbarHeight;
Rect labelRect = new Rect(headerRect.x, headerRect.yMax - labelHeight, headerRect.width, labelHeight);
GUI.Label(labelRect, column.headerContent, style);
}
protected void SortingButton(MultiColumnHeaderState.Column column, Rect headerRect, int columnIndex)
{
// Button logic
if (EditorGUI.Button(headerRect, GUIContent.none, GUIStyle.none))
{
ColumnHeaderClicked(column, columnIndex);
}
// Draw sorting arrow
if (columnIndex == state.sortedColumnIndex && Event.current.type == EventType.Repaint)
{
var arrowRect = GetArrowRect(column, headerRect);
Matrix4x4 normalMatrix = GUI.matrix;
if (column.sortedAscending)
GUIUtility.RotateAroundPivot(180, arrowRect.center - new Vector2(0, 1));
GUI.Label(arrowRect, "\u25BE", DefaultStyles.arrowStyle);
if (column.sortedAscending)
GUI.matrix = normalMatrix;
}
}
internal virtual Rect GetArrowRect(MultiColumnHeaderState.Column column, Rect headerRect)
{
float sortingArrowWidth = DefaultStyles.arrowStyle.fixedWidth;
float arrowYPos = headerRect.y;
float arrowXPos = 0f;
switch (column.sortingArrowAlignment)
{
case TextAlignment.Left:
arrowXPos = headerRect.x + DefaultStyles.columnHeader.padding.left;
break;
case TextAlignment.Center:
arrowXPos = headerRect.x + headerRect.width * 0.5f - sortingArrowWidth * 0.5f;
break;
case TextAlignment.Right:
arrowXPos = headerRect.xMax - DefaultStyles.columnHeader.padding.right - sortingArrowWidth;
break;
default:
Debug.LogError("Unhandled enum");
break;
}
Rect arrowRect = new Rect(Mathf.Round(arrowXPos), arrowYPos, sortingArrowWidth, 16f);
return arrowRect;
}
GUIStyle GetStyle(TextAlignment alignment)
{
switch (alignment)
{
case TextAlignment.Left: return DefaultStyles.columnHeader;
case TextAlignment.Center: return DefaultStyles.columnHeaderCenterAligned;
case TextAlignment.Right: return DefaultStyles.columnHeaderRightAligned;
default: return DefaultStyles.columnHeader;
}
}
void DoContextMenu()
{
var menu = new GenericMenu();
AddColumnHeaderContextMenuItems(menu);
menu.ShowAsContext();
}
protected virtual void AddColumnHeaderContextMenuItems(GenericMenu menu)
{
menu.AddItem(EditorGUIUtility.TrTextContent("Resize to Fit"), false, ResizeToFit);
menu.AddSeparator("");
for (int i = 0; i < state.columns.Length; ++i)
{
var column = state.columns[i];
var menuText = !string.IsNullOrEmpty(column.contextMenuText) ? column.contextMenuText : column.headerContent.text;
if (column.allowToggleVisibility)
menu.AddItem(new GUIContent(menuText), state.visibleColumns.Contains(i), ToggleVisibility, i);
else
menu.AddDisabledItem(new GUIContent(menuText));
}
}
protected virtual void OnVisibleColumnsChanged()
{
if (visibleColumnsChanged != null)
visibleColumnsChanged(this);
}
void ToggleVisibility(object userData)
{
ToggleVisibility((int)userData);
}
protected virtual void ToggleVisibility(int columnIndex)
{
var newVisibleColumns = new List<int>(state.visibleColumns);
if (newVisibleColumns.Contains(columnIndex))
{
newVisibleColumns.Remove(columnIndex);
}
else
{
newVisibleColumns.Add(columnIndex);
newVisibleColumns.Sort();
}
state.visibleColumns = newVisibleColumns.ToArray();
Repaint();
OnVisibleColumnsChanged();
}
public void Repaint()
{
if (m_GUIView != null)
m_GUIView.Repaint();
}
void DetectSizeChanges(Rect rect)
{
if (Event.current.type == EventType.Repaint)
{
if (m_PreviousRect.width > 0f)
{
float deltaWidth = Mathf.Round(rect.width - m_PreviousRect.width);
if (deltaWidth != 0f)
{
float tep = GUI.skin.verticalScrollbar.fixedWidth;
bool isColumnsVisible = rect.width - tep > state.widthOfAllVisibleColumns;
if (isColumnsVisible || deltaWidth < 0f)
ResizeColumnsWidthsProportionally(deltaWidth);
}
}
m_PreviousRect = rect;
}
}
void ResizeColumnsWidthsProportionally(float deltaWidth)
{
// Find auto resizing columns
List<MultiColumnHeaderState.Column> autoResizeColumns = null;
foreach (int i in state.visibleColumns)
{
MultiColumnHeaderState.Column column = state.columns[i];
if (column.autoResize)
{
// Ignore the columns that cannot expand anymore
if (deltaWidth > 0f && column.width >= column.maxWidth)
continue;
// Ignore the columns that cannot shrink anymore
if (deltaWidth < 0f && column.width <= column.minWidth)
continue;
if (autoResizeColumns == null)
autoResizeColumns = new List<MultiColumnHeaderState.Column>();
autoResizeColumns.Add(column);
}
}
// Any auto resizing columns?
if (autoResizeColumns == null)
return;
// Sum
float totalAutoResizeWidth = autoResizeColumns.Sum(x => x.width);
// Distribute
foreach (var column in autoResizeColumns)
{
column.width += deltaWidth * (column.width / totalAutoResizeWidth);
column.width = Mathf.Clamp(column.width, column.minWidth, column.maxWidth);
}
}
}
}