-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleEditorTableView.cs
200 lines (170 loc) · 7.15 KB
/
SimpleEditorTableView.cs
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
/***
* Copyright (c) 2024 Red Games
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLRDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USER OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
namespace RedGame.Framework.EditorTools
{
public class SimpleEditorTableView<TData>
{
private MultiColumnHeaderState _multiColumnHeaderState;
private MultiColumnHeader _multiColumnHeader;
private MultiColumnHeaderState.Column[] _columns;
private readonly Color _lighterColor = Color.white * 0.3f;
private readonly Color _darkerColor = Color.white * 0.1f;
private Vector2 _scrollPosition;
private bool _columnResized;
private bool _sortingDirty;
public delegate void DrawItem(Rect rect, TData item);
public class ColumnDef
{
internal MultiColumnHeaderState.Column column;
internal DrawItem onDraw;
internal Comparison<TData> onSort;
public ColumnDef SetMaxWidth(float maxWidth)
{
column.maxWidth = maxWidth;
return this;
}
public ColumnDef SetTooltip(string tooltip)
{
column.headerContent.tooltip = tooltip;
return this;
}
public ColumnDef SetAutoResize(bool autoResize)
{
column.autoResize = autoResize;
return this;
}
public ColumnDef SetAllowToggleVisibility(bool allow)
{
column.allowToggleVisibility = allow;
return this;
}
public ColumnDef SetSorting(Comparison<TData> onSort)
{
this.onSort = onSort;
column.canSort = true;
return this;
}
}
private readonly List<ColumnDef> _columnDefs = new List<ColumnDef>();
public void ClearColumns()
{
_columnDefs.Clear();
_columnResized = true;
}
public ColumnDef AddColumn(string title, int minWidth, DrawItem onDrawItem)
{
ColumnDef columnDef = new ColumnDef()
{
column = new MultiColumnHeaderState.Column()
{
allowToggleVisibility = false,
autoResize = true,
minWidth = minWidth,
canSort = false,
sortingArrowAlignment = TextAlignment.Right,
headerContent = new GUIContent(title),
headerTextAlignment = TextAlignment.Left,
},
onDraw = onDrawItem
};
_columnDefs.Add(columnDef);
_columnResized = true;
return columnDef;
}
private void ReBuild()
{
_columns = _columnDefs.Select((def) => def.column).ToArray();
_multiColumnHeaderState = new MultiColumnHeaderState(_columns);
_multiColumnHeader = new MultiColumnHeader(_multiColumnHeaderState);
_multiColumnHeader.visibleColumnsChanged += (multiColumnHeader) => multiColumnHeader.ResizeToFit();
_multiColumnHeader.sortingChanged += (multiColumnHeader) => _sortingDirty = true;
_multiColumnHeader.ResizeToFit();
_columnResized = false;
}
public void DrawTableGUI(TData[] data, float maxHeight = float.MaxValue, float rowHeight = -1)
{
if (_multiColumnHeader == null || _columnResized)
ReBuild();
float rowWidth = _multiColumnHeaderState.widthOfAllVisibleColumns;
if (rowHeight < 0)
rowHeight = EditorGUIUtility.singleLineHeight;
Rect headerRect = GUILayoutUtility.GetRect(rowWidth, rowHeight);
_multiColumnHeader!.OnGUI(headerRect, xScroll: 0.0f);
float sumWidth = rowWidth;
float sumHeight = rowHeight * data.Length + GUI.skin.horizontalScrollbar.fixedHeight;
UpdateSorting(data);
Rect scrollViewPos = GUILayoutUtility.GetRect(0, sumWidth, 0, maxHeight);
Rect viewRect = new Rect(0, 0, sumWidth, sumHeight);
_scrollPosition = GUI.BeginScrollView(
position: scrollViewPos,
scrollPosition: _scrollPosition,
viewRect: viewRect,
alwaysShowHorizontal: false,
alwaysShowVertical: false
);
EditorGUILayout.BeginVertical();
for (int row = 0; row < data.Length; row++)
{
Rect rowRect = new Rect(0, rowHeight * row, rowWidth, rowHeight);
EditorGUI.DrawRect(rect: rowRect, color: row % 2 == 0 ? _darkerColor : _lighterColor);
for (int col = 0; col < _columns.Length; col++)
{
if (_multiColumnHeader.IsColumnVisible(col))
{
int visibleColumnIndex = _multiColumnHeader.GetVisibleColumnIndex(col);
Rect cellRect = _multiColumnHeader.GetCellRect(visibleColumnIndex, rowRect);
_columnDefs[col].onDraw(cellRect, data[row]);
}
}
}
EditorGUILayout.EndVertical();
GUI.EndScrollView(handleScrollWheel: true);
}
private void UpdateSorting(TData[] data)
{
if (_sortingDirty)
{
int sortIndex = _multiColumnHeader.sortedColumnIndex;
if (sortIndex >= 0)
{
var sortCompare = _columnDefs[sortIndex].onSort;
bool ascending = _multiColumnHeader.IsSortedAscending(sortIndex);
Array.Sort(data, ((a, b) =>
{
int r = sortCompare(a, b);
return ascending ? r : -r;
}));
}
_sortingDirty = false;
}
}
}
}