forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToggleTreeView.cs
More file actions
309 lines (272 loc) · 10.3 KB
/
Copy pathToggleTreeView.cs
File metadata and controls
309 lines (272 loc) · 10.3 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
// 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.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
internal abstract class ToggleTreeViewItem : TreeViewItem
{
public abstract bool nodeState { get; set; }
}
internal class ToggleTreeView<T> : TreeView where T : ToggleTreeViewItem, new()
{
static class Styles
{
public static readonly GUIContent toggleAll = EditorGUIUtility.TrTextContent("Toggle All");
public static readonly GUIContent expandAll = EditorGUIUtility.TrTextContent("Expand All");
public static readonly GUIContent collapseAll = EditorGUIUtility.TrTextContent("Collapse All");
public static readonly GUIContent toggle = EditorGUIUtility.TrTextContent("", "Maintain Alt/Option key to enable or disable all children");
public static readonly GUIContent filterSelected = new GUIContent(EditorGUIUtility.FindTexture("FilterSelectedOnly"), "Filter selected only");
}
static string s_Regex = "(?:(.*) |^)(s:)(false|true)(?: (.*)|$)";
Func<T> m_RebuildRoot;
List<TreeViewItem> m_DefaultRows;
public enum Column
{
Enabled,
Name,
}
public ToggleTreeView(TreeViewState state, MultiColumnHeader multiColumnHeader, Func<T> rebuildRoot)
: base(state, multiColumnHeader)
{
m_RebuildRoot = rebuildRoot;
columnIndexForTreeFoldouts = 1;
useScrollView = false;
multiColumnHeader.canSort = false;
multiColumnHeader.height = 18f;
showBorder = true;
showAlternatingRowBackgrounds = true;
foldoutOverride = DoFoldoutButtonOverride;
Reload();
}
bool hasNodes => rootItem != null && rootItem.hasChildren;
public float totalHeightIncludingSearchBarAndBottomBar => totalHeight + 18f + EditorGUI.kSingleLineHeight;
protected override IList<TreeViewItem> BuildRows(TreeViewItem root)
{
// Reuse cached list (for capacity)
if (m_DefaultRows == null)
m_DefaultRows = new List<TreeViewItem>(100);
m_DefaultRows.Clear();
if (hasSearch)
SearchFullTree(m_DefaultRows);
else
AddExpandedRows(root, m_DefaultRows);
return m_DefaultRows;
}
void SearchFullTree(List<TreeViewItem> rows)
{
if (rows == null)
throw new ArgumentException("Invalid list: cannot be null", nameof(rows));
var search = searchString;
bool searchEnabledState = false;
bool searchedEnabledState = false;
var match = Regex.Match(search, s_Regex);
if (match.Success)
{
search = match.Groups[1].Value + match.Groups[4].Value;
searchEnabledState = true;
searchedEnabledState = match.Groups[3].Value == "true";
}
var stack = new Stack<TreeViewItem>();
stack.Push(rootItem);
while (stack.Count > 0)
{
TreeViewItem current = stack.Pop();
if (current.children != null)
{
foreach (var child in current.children)
{
if (child != null)
{
if (!searchEnabledState || ((ToggleTreeViewItem)child).nodeState == searchedEnabledState)
if (DoesItemMatchSearch(child, search))
rows.Add(child);
stack.Push(child);
}
}
}
}
rows.Sort((x, y) => EditorUtility.NaturalCompare(x.displayName, y.displayName));
}
protected override bool DoesItemMatchSearch(TreeViewItem item, string search)
{
return string.IsNullOrEmpty(search) || base.DoesItemMatchSearch(item, search);
}
public override void OnGUI(Rect rect)
{
using (new EditorGUILayout.VerticalScope())
{
DrawSearchField();
DrawTreeViewGUI();
BottomGUI();
}
}
void DrawTreeViewGUI()
{
base.OnGUI(GUILayoutUtility.GetRect(0, totalHeight));
if (HasFocus() && Event.current.type == EventType.KeyDown
&& (Event.current.keyCode == KeyCode.Space || Event.current.keyCode == KeyCode.Return || Event.current.keyCode == KeyCode.KeypadEnter))
{
Event.current.Use();
var nodes = GetSelection();
if (nodes.Count > 0)
{
var firstNode = (T)FindItem(nodes[0], rootItem);
bool value = !firstNode.nodeState;
firstNode.nodeState = value;
for (var i = 1; i < nodes.Count; i++)
{
var node = (T)FindItem(nodes[i], rootItem);
node.nodeState = value;
}
}
}
}
void DrawSearchField()
{
using (new EditorGUILayout.HorizontalScope(EditorStyles.helpBox, GUILayout.MaxHeight(EditorGUI.kWindowToolbarHeight)))
{
EditorGUI.BeginChangeCheck();
var search = EditorGUILayout.ToolbarSearchField(searchString);
if (EditorGUI.EndChangeCheck())
{
bool wasSearching = hasSearch;
searchString = search;
if (wasSearching && !hasSearch)
{
foreach (var item in GetSelection())
{
FrameItem(item);
}
}
}
using (new EditorGUI.DisabledScope(Regex.IsMatch(searchString, s_Regex)))
{
if (GUILayout.Button(Styles.filterSelected, EditorStyles.miniButton, GUILayout.MaxWidth(24f)))
searchString = "s:true " + searchString;
}
}
}
protected virtual void BottomGUI()
{
using (new EditorGUILayout.HorizontalScope())
using (new EditorGUI.DisabledScope(!hasNodes))
{
if (GUILayout.Button(Styles.toggleAll, EditorStyles.miniButton))
ToggleAll();
var enabledGUI = GUI.enabled;
GUI.enabled = hasNodes;
if (GUILayout.Button(Styles.collapseAll, EditorStyles.miniButton))
CollapseAll();
if (GUILayout.Button(Styles.expandAll, EditorStyles.miniButton))
ExpandAll();
GUI.enabled = enabledGUI;
}
}
protected virtual void ToggleAll()
{
if (!hasNodes) return;
bool value = !((T)rootItem.children[0]).nodeState;
PropagateValue((T)rootItem, value);
}
protected override TreeViewItem BuildRoot()
{
return m_RebuildRoot == null ? new T() { depth = -1, id = 0, displayName = "", children = new List<TreeViewItem>() } : m_RebuildRoot();
}
protected override void RowGUI(RowGUIArgs args)
{
if (!hasNodes)
{
base.RowGUI(args);
return;
}
for (var i = 0; i < args.GetNumVisibleColumns(); ++i)
{
CellGUI(args.GetCellRect(i), (Column)args.GetColumn(i), (T)args.item, ref args);
}
}
void CellGUI(Rect cellRect, Column column, T node, ref RowGUIArgs args)
{
CenterRectUsingSingleLineHeight(ref cellRect);
switch (column)
{
case Column.Enabled:
EnabledGUI(cellRect, node, ref args);
break;
case Column.Name:
NameGUI(cellRect, node, ref args);
break;
}
}
protected virtual void EnabledGUI(Rect cellRect, T node, ref RowGUIArgs args)
{
using (var change = new EditorGUI.ChangeCheckScope())
{
bool isActive = node.nodeState;
// center the toggle button in the cell
cellRect.xMin = cellRect.xMin + cellRect.width / 2f - 8f;
isActive = GUI.Toggle(cellRect, isActive, Styles.toggle);
if (change.changed)
{
var selection = GetSelection();
if (selection.Contains(node.id))
{
var propagate = selection.Count == 1 && Event.current.alt;
foreach (var i in selection)
{
var item = (T)FindItem(i, rootItem);
item.nodeState = isActive;
if (propagate)
{
PropagateValue(item, isActive);
}
}
}
else
{
node.nodeState = isActive;
if (Event.current.alt)
{
PropagateValue(node, isActive);
}
}
}
}
}
protected static void PropagateValue(T node, bool value)
{
if (node.children == null)
return;
foreach (var treeViewItem in node.children)
{
var child = (T)treeViewItem;
child.nodeState = value;
PropagateValue(child, value);
}
}
private static bool DoFoldoutButtonOverride(Rect foldoutRect, bool expandedState, GUIStyle foldoutStyle)
{
// We need to make sure hierarchyMode is false before drawing the Foldout
// or the foldout will be offsetted if the TreeView is inside an inspector.
var hierarchy = EditorGUIUtility.hierarchyMode;
EditorGUIUtility.hierarchyMode = false;
// We need to make sure the indentLevel is 0 before drawing the Foldout
// because we want it to ignore current indent level as it was already calculated by the TreeView.
var indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
// Using EditorGUI.Foldout to keep the foldout active even when the GUI is disabled.
var newExpandedValue = EditorGUI.Foldout(foldoutRect, expandedState, GUIContent.none, foldoutStyle);
// Restore previous values after the Foldout has been draw.
EditorGUI.indentLevel = indent;
EditorGUIUtility.hierarchyMode = hierarchy;
return newExpandedValue;
}
protected virtual void NameGUI(Rect position, T node, ref RowGUIArgs args)
{
args.rowRect = position;
base.RowGUI(args);
}
}