forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryTreeList.cs
More file actions
401 lines (331 loc) · 13.8 KB
/
Copy pathMemoryTreeList.cs
File metadata and controls
401 lines (331 loc) · 13.8 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System.Collections.Generic;
using UnityEngine;
namespace UnityEditor
{
internal class MemoryTreeList
{
internal class Styles
{
public GUIStyle background = "OL Box";
public GUIStyle header = "OL title";
public GUIStyle entryEven = "OL EntryBackEven";
public GUIStyle entryOdd = "OL EntryBackOdd";
public GUIStyle numberLabel = "OL Label";
public GUIStyle foldout = "IN foldout";
}
private static Styles m_Styles;
protected static Styles styles
{
get { return m_Styles ?? (m_Styles = new Styles()); }
}
private bool m_RequiresRefresh;
public bool RequiresRefresh
{
get { return m_RequiresRefresh; }
set { m_RequiresRefresh = value; }
}
const float kIndentPx = 16;
const float kBaseIndent = 4;
protected const float kSmallMargin = 4;
protected const float kRowHeight = 16;
protected const float kNameColumnSize = 300;
protected const float kColumnSize = 70;
protected const float kFoldoutSize = 14;
public MemoryElementSelection m_MemorySelection;
protected MemoryElement m_Root = null;
protected EditorWindow m_EditorWindow;
protected SplitterState m_Splitter;
protected MemoryTreeList m_DetailView;
protected int m_ControlID;
protected Vector2 m_ScrollPosition;
protected float m_SelectionOffset;
protected float m_VisibleHeight;
public MemoryTreeList(EditorWindow editorWindow, MemoryTreeList detailview)
{
m_MemorySelection = new MemoryElementSelection();
m_EditorWindow = editorWindow;
m_DetailView = detailview;
m_ControlID = GUIUtility.GetPermanentControlID();
SetupSplitter();
}
protected virtual void SetupSplitter()
{
float[] splitterRelativeSizes = new float[1];
int[] splitterMinWidths = new int[1];
splitterRelativeSizes[0] = kNameColumnSize;
splitterMinWidths[0] = 100;
m_Splitter = new SplitterState(splitterRelativeSizes, splitterMinWidths, null);
}
public void OnGUI()
{
GUILayout.BeginVertical();
SplitterGUILayout.BeginHorizontalSplit(m_Splitter, EditorStyles.toolbar);
DrawHeader();
SplitterGUILayout.EndHorizontalSplit();
if (m_Root == null)
{
GUILayout.EndVertical();
return;
}
HandleKeyboard();
m_ScrollPosition = GUILayout.BeginScrollView(m_ScrollPosition, styles.background);
int row = 0;
foreach (MemoryElement memoryElement in m_Root.children)
{
DrawItem(memoryElement, ref row, 1);
row++;
}
GUILayoutUtility.GetRect(0f, row * kRowHeight, GUILayout.ExpandWidth(true));
if (Event.current.type == EventType.Repaint)
m_VisibleHeight = GUIClip.visibleRect.height;
GUILayout.EndScrollView();
GUILayout.EndVertical();
}
private static float Clamp(float value, float min, float max)
{
return (value < min) ? min : (value > max) ? max : value;
}
private bool FindNamedChild(string name, List<MemoryElement> list, out MemoryElement outChild)
{
foreach (var child in list)
{
if (child.name == name)
{
outChild = child;
return true;
}
}
outChild = null;
return false;
}
private void RestoreViewState(MemoryElement oldRoot, MemoryElement newRoot)
{
foreach (MemoryElement memoryElement in newRoot.children)
{
memoryElement.ExpandChildren();
if (memoryElement.ChildCount() == 0)
continue;
MemoryElement child = null;
if (FindNamedChild(memoryElement.name, oldRoot.children, out child))
{
memoryElement.expanded = child.expanded;
if (memoryElement.expanded)
{
RestoreViewState(child, memoryElement);
}
}
}
}
public void SetRoot(MemoryElement root)
{
MemoryElement oldRoot = m_Root;
m_Root = root;
if (m_Root != null)
m_Root.ExpandChildren();
if (m_DetailView != null)
m_DetailView.SetRoot(null);
// Attempt to restore the old state of things by walking the old tree
if (oldRoot != null && m_Root != null)
RestoreViewState(oldRoot, m_Root);
}
public MemoryElement GetRoot()
{
return m_Root;
}
protected static void DrawBackground(int row, bool selected)
{
var currentRect = GenerateRect(row);
var background = (row % 2 == 0 ? styles.entryEven : styles.entryOdd);
if (Event.current.type == EventType.Repaint)
background.Draw(currentRect, GUIContent.none, false, false, selected, false);
}
protected virtual void DrawHeader()
{
GUILayout.Label("Referenced By:", styles.header);
}
protected static Rect GenerateRect(int row)
{
var rect = new Rect(1, kRowHeight * row, GUIClip.visibleRect.width, kRowHeight);
return rect;
}
protected virtual void DrawData(Rect rect, MemoryElement memoryElement, int indent, int row, bool selected)
{
if (Event.current.type != EventType.Repaint)
return;
string displayName = memoryElement.name + "(" + memoryElement.memoryInfo.className + ")";
styles.numberLabel.Draw(rect, displayName, false, false, false, selected);
}
protected void DrawRecursiveData(MemoryElement element, ref int row, int indent)
{
if (element.ChildCount() == 0)
return;
element.ExpandChildren();
foreach (MemoryElement elem in element.children)
{
row++;
DrawItem(elem, ref row, indent);
}
}
protected virtual void DrawItem(MemoryElement memoryElement, ref int row, int indent)
{
bool isSelected = m_MemorySelection.isSelected(memoryElement);
DrawBackground(row, isSelected);
Rect rect = GenerateRect(row);
rect.x = kBaseIndent + indent * kIndentPx - kFoldoutSize;
Rect toggleRect = rect;
toggleRect.width = kFoldoutSize;
if (memoryElement.ChildCount() > 0)
memoryElement.expanded = GUI.Toggle(toggleRect, memoryElement.expanded, GUIContent.none, styles.foldout);
rect.x += kFoldoutSize;
if (isSelected)
m_SelectionOffset = row * kRowHeight;
if (Event.current.type == EventType.MouseDown && rect.Contains(Event.current.mousePosition))
{
RowClicked(Event.current, memoryElement);
}
DrawData(rect, memoryElement, indent, row, isSelected);
if (memoryElement.expanded)
DrawRecursiveData(memoryElement, ref row, indent + 1);
}
protected void RowClicked(Event evt, MemoryElement memoryElement)
{
m_MemorySelection.SetSelection(memoryElement);
GUIUtility.keyboardControl = m_ControlID;
if (evt.clickCount == 2 && memoryElement.memoryInfo != null && memoryElement.memoryInfo.instanceId != 0)
{
Selection.instanceIDs = new int[0];
Selection.activeInstanceID = memoryElement.memoryInfo.instanceId;
}
evt.Use();
if (memoryElement.memoryInfo != null)
{
EditorGUIUtility.PingObject(memoryElement.memoryInfo.instanceId);
}
if (m_DetailView != null)
m_DetailView.SetRoot(memoryElement.memoryInfo == null ? null : new MemoryElement(memoryElement.memoryInfo, false));
m_EditorWindow.Repaint();
}
protected void HandleKeyboard()
{
Event evt = Event.current;
if (evt.GetTypeForControl(m_ControlID) != EventType.KeyDown ||
m_ControlID != GUIUtility.keyboardControl)
return;
if (m_MemorySelection.Selected == null)
return;
int count;
switch (evt.keyCode)
{
case KeyCode.UpArrow:
m_MemorySelection.MoveUp();
break;
case KeyCode.DownArrow:
m_MemorySelection.MoveDown();
break;
case KeyCode.Home:
m_MemorySelection.MoveFirst();
break;
case KeyCode.End:
m_MemorySelection.MoveLast();
break;
case KeyCode.LeftArrow:
if (m_MemorySelection.Selected.expanded)
m_MemorySelection.Selected.expanded = false;
else
m_MemorySelection.MoveParent();
break;
case KeyCode.RightArrow:
if (m_MemorySelection.Selected.ChildCount() > 0)
m_MemorySelection.Selected.expanded = true;
break;
case KeyCode.PageUp:
count = Mathf.RoundToInt(m_VisibleHeight / kRowHeight);
for (int i = 0; i < count; i++)
m_MemorySelection.MoveUp();
break;
case KeyCode.PageDown:
count = Mathf.RoundToInt(m_VisibleHeight / kRowHeight);
for (int i = 0; i < count; i++)
m_MemorySelection.MoveDown();
break;
case KeyCode.Return:
if (m_MemorySelection.Selected.memoryInfo != null)
{
Selection.instanceIDs = new int[0];
Selection.activeInstanceID = m_MemorySelection.Selected.memoryInfo.instanceId;
}
break;
default:
return;
}
RowClicked(evt, m_MemorySelection.Selected);
EnsureVisible();
m_EditorWindow.Repaint();
}
private void RecursiveFindSelected(MemoryElement element, ref int row)
{
if (m_MemorySelection.isSelected(element))
m_SelectionOffset = row * kRowHeight;
row++;
if (!element.expanded || element.ChildCount() == 0)
return;
element.ExpandChildren();
foreach (MemoryElement elem in element.children)
RecursiveFindSelected(elem, ref row);
}
protected void EnsureVisible()
{
int row = 0;
RecursiveFindSelected(m_Root, ref row);
m_ScrollPosition.y = Clamp(m_ScrollPosition.y, m_SelectionOffset - m_VisibleHeight, m_SelectionOffset - kRowHeight);
}
}
class MemoryTreeListClickable : MemoryTreeList
{
public MemoryTreeListClickable(EditorWindow editorWindow, MemoryTreeList detailview)
: base(editorWindow, detailview)
{
}
protected override void SetupSplitter()
{
float[] splitterRelativeSizes = new float[3];
int[] splitterMinWidths = new int[3];
splitterRelativeSizes[0] = kNameColumnSize;
splitterMinWidths[0] = 100;
splitterRelativeSizes[1] = kColumnSize;
splitterMinWidths[1] = 50;
splitterRelativeSizes[2] = kColumnSize;
splitterMinWidths[2] = 50;
m_Splitter = new SplitterState(splitterRelativeSizes, splitterMinWidths, null);
}
protected override void DrawHeader()
{
GUILayout.Label("Name", styles.header);
GUILayout.Label("Memory", styles.header);
GUILayout.Label("Ref count", styles.header);
}
protected override void DrawData(Rect rect, MemoryElement memoryElement, int indent, int row, bool selected)
{
if (Event.current.type != EventType.Repaint)
return;
string displayName = memoryElement.name;
if (memoryElement.ChildCount() > 0 && indent < 3)
displayName += " (" + memoryElement.AccumulatedChildCount().ToString() + ")";
int currentColumn = 0;
rect.xMax = m_Splitter.realSizes[currentColumn];
styles.numberLabel.Draw(rect, displayName, false, false, false, selected);
rect.x = rect.xMax;
rect.width = m_Splitter.realSizes[++currentColumn] - kSmallMargin;
styles.numberLabel.Draw(rect, EditorUtility.FormatBytes(memoryElement.totalMemory), false, false, false, selected);
rect.x += m_Splitter.realSizes[currentColumn++];
rect.width = m_Splitter.realSizes[currentColumn] - kSmallMargin;
if (memoryElement.ReferenceCount() > 0)
styles.numberLabel.Draw(rect, memoryElement.ReferenceCount().ToString(), false, false, false, selected);
else if (selected)
styles.numberLabel.Draw(rect, "", false, false, false, selected);
}
}
}