forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchWindow.cs
More file actions
626 lines (537 loc) · 22.5 KB
/
Copy pathSearchWindow.cs
File metadata and controls
626 lines (537 loc) · 22.5 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
// 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 UnityEditor;
using UnityEngine;
namespace UnityEditor.Experimental.GraphView
{
[Serializable]
public struct SearchWindowContext
{
public Vector2 screenMousePosition { get; private set; }
public float requestedWidth { get; private set; }
public float requestedHeight { get; private set; }
public SearchWindowContext(Vector2 screenMousePosition, float requestedWidth = 0.0f, float requestedHeight = 0.0f)
{
this.screenMousePosition = screenMousePosition;
this.requestedWidth = requestedWidth;
this.requestedHeight = requestedHeight;
}
}
public interface ISearchWindowProvider
{
List<SearchTreeEntry> CreateSearchTree(SearchWindowContext context);
bool OnSelectEntry(SearchTreeEntry SearchTreeEntry, SearchWindowContext context);
}
[InitializeOnLoad]
public class SearchWindow : EditorWindow
{
// Styles
class Styles
{
public GUIStyle header = "AC BoldHeader";
public GUIStyle componentButton = "AC ComponentButton";
public GUIStyle groupButton = "AC GroupButton";
public GUIStyle background = "grey_border";
public GUIStyle rightArrow = "ArrowNavigationRight";
public GUIStyle leftArrow = "ArrowNavigationLeft";
}
// Constants
private const float k_DefaultWidth = 240f;
private const float k_DefaultHeight = 320f;
private const int k_HeaderHeight = 30;
private const int k_WindowYOffset = 16;
private const string kSearchHeader = "Search";
// Static variables
private static Styles s_Styles;
private static SearchWindow s_FilterWindow = null;
private static long s_LastClosedTime;
private static bool s_DirtyList = false;
// Member variables
ScriptableObject m_Owner;
SearchWindowContext m_Context;
private ISearchWindowProvider provider { get { return m_Owner as ISearchWindowProvider; } }
private SearchTreeEntry[] m_Tree;
private SearchTreeEntry[] m_SearchResultTree;
private List<SearchTreeGroupEntry> m_SelectionStack = new List<SearchTreeGroupEntry>();
private float m_Anim = 1;
private int m_AnimTarget = 1;
private long m_LastTime = 0;
private bool m_ScrollToSelected = false;
private string m_DelayedSearch = null;
private string m_Search = "";
// Properties
private bool hasSearch { get { return !string.IsNullOrEmpty(m_Search); } }
private SearchTreeGroupEntry activeParent
{
get
{
int index = m_SelectionStack.Count - 2 + m_AnimTarget;
if (index < 0 || index >= m_SelectionStack.Count)
return null;
return m_SelectionStack[index];
}
}
private SearchTreeEntry[] activeTree { get { return hasSearch ? m_SearchResultTree : m_Tree; } }
private SearchTreeEntry activeSearchTreeEntry
{
get
{
if (activeTree == null)
return null;
List<SearchTreeEntry> children = GetChildren(activeTree, activeParent);
if (activeParent == null || activeParent.selectedIndex < 0 || activeParent.selectedIndex >= children.Count)
return null;
return children[activeParent.selectedIndex];
}
}
private bool isAnimating { get { return m_Anim != m_AnimTarget; } }
// Methods
static SearchWindow()
{
s_DirtyList = true;
}
void OnEnable()
{
s_FilterWindow = this;
}
void OnDisable()
{
s_LastClosedTime = System.DateTime.Now.Ticks / System.TimeSpan.TicksPerMillisecond;
s_FilterWindow = null;
}
public static bool Open<T>(SearchWindowContext context, T provider) where T : ScriptableObject, ISearchWindowProvider
{
// If the window is already open, close it instead.
UnityEngine.Object[] wins = Resources.FindObjectsOfTypeAll(typeof(SearchWindow));
if (wins.Length > 0)
{
try
{
((EditorWindow)wins[0]).Close();
return false;
}
catch (Exception)
{
s_FilterWindow = null;
}
}
// We could not use realtimeSinceStartUp since it is set to 0 when entering/exitting playmode, we assume an increasing time when comparing time.
long nowMilliSeconds = System.DateTime.Now.Ticks / System.TimeSpan.TicksPerMillisecond;
bool justClosed = nowMilliSeconds < s_LastClosedTime + 50;
if (!justClosed)
{
if (s_FilterWindow == null)
{
s_FilterWindow = ScriptableObject.CreateInstance<SearchWindow>();
s_FilterWindow.hideFlags = HideFlags.HideAndDontSave;
}
s_FilterWindow.Init(context, provider);
return true;
}
return false;
}
void Init(SearchWindowContext context, ScriptableObject provider)
{
m_Owner = provider;
m_Context = context;
float width = Math.Max(context.requestedWidth, k_DefaultWidth);
float height = Math.Max(context.requestedHeight, k_DefaultHeight);
Rect buttonRect = new Rect(context.screenMousePosition.x - width / 2, context.screenMousePosition.y - k_WindowYOffset, width, 1);
CreateSearchTree();
ShowAsDropDown(buttonRect, new Vector2(buttonRect.width, height));
Focus();
wantsMouseMove = true;
}
private void CreateSearchTree()
{
List<SearchTreeEntry> tree = provider.CreateSearchTree(m_Context);
if (tree != null)
m_Tree = tree.ToArray();
else
m_Tree = new SearchTreeEntry[0];
// Rebuild stack
if (m_SelectionStack.Count == 0)
m_SelectionStack.Add(m_Tree[0] as SearchTreeGroupEntry);
else
{
// The root is always the match for level 0
SearchTreeGroupEntry match = m_Tree[0] as SearchTreeGroupEntry;
int level = 0;
while (true)
{
// Assign the match for the current level
SearchTreeGroupEntry oldSearchTreeEntry = m_SelectionStack[level];
m_SelectionStack[level] = match;
m_SelectionStack[level].selectedIndex = oldSearchTreeEntry.selectedIndex;
m_SelectionStack[level].scroll = oldSearchTreeEntry.scroll;
// See if we reached last SearchTreeEntry of stack
level++;
if (level == m_SelectionStack.Count)
break;
// Try to find a child of the same name as we had before
List<SearchTreeEntry> children = GetChildren(activeTree, match);
SearchTreeEntry childMatch = children.FirstOrDefault(c => c.name == m_SelectionStack[level].name);
if (childMatch != null && childMatch is SearchTreeGroupEntry)
{
match = childMatch as SearchTreeGroupEntry;
}
else
{
// If we couldn't find the child, remove all further SearchTreeEntrys from the stack
m_SelectionStack.RemoveRange(level, m_SelectionStack.Count - level);
}
}
}
s_DirtyList = false;
RebuildSearch();
}
internal void OnGUI()
{
if (s_Styles == null)
s_Styles = new Styles();
GUI.Label(new Rect(0, 0, position.width, position.height), GUIContent.none, s_Styles.background);
if (s_DirtyList)
CreateSearchTree();
// Keyboard
HandleKeyboard();
GUILayout.Space(7);
// Search
EditorGUI.FocusTextInControl("ComponentSearch");
Rect searchRect = GUILayoutUtility.GetRect(10, 20);
searchRect.x += 8;
searchRect.width -= 16;
GUI.SetNextControlName("ComponentSearch");
EditorGUI.BeginChangeCheck();
string newSearch = EditorGUI.SearchField(searchRect, m_DelayedSearch ?? m_Search);
if (EditorGUI.EndChangeCheck() && (newSearch != m_Search || m_DelayedSearch != null))
{
if (!isAnimating)
{
m_Search = m_DelayedSearch ?? newSearch;
RebuildSearch();
m_DelayedSearch = null;
}
else
{
m_DelayedSearch = newSearch;
}
}
// Show lists
ListGUI(activeTree, m_Anim, GetSearchTreeEntryRelative(0), GetSearchTreeEntryRelative(-1));
if (m_Anim < 1)
ListGUI(activeTree, m_Anim + 1, GetSearchTreeEntryRelative(-1), GetSearchTreeEntryRelative(-2));
// Animate
if (isAnimating && Event.current.type == EventType.Repaint)
{
long now = System.DateTime.Now.Ticks;
float deltaTime = (now - m_LastTime) / (float)System.TimeSpan.TicksPerSecond;
m_LastTime = now;
m_Anim = Mathf.MoveTowards(m_Anim, m_AnimTarget, deltaTime * 4);
if (m_AnimTarget == 0 && m_Anim == 0)
{
m_Anim = 1;
m_AnimTarget = 1;
m_SelectionStack.RemoveAt(m_SelectionStack.Count - 1);
}
Repaint();
}
}
private void HandleKeyboard()
{
Event evt = Event.current;
if (evt.type == EventType.KeyDown)
{
// Always do these
if (evt.keyCode == KeyCode.DownArrow)
{
activeParent.selectedIndex++;
activeParent.selectedIndex = Mathf.Min(activeParent.selectedIndex, GetChildren(activeTree, activeParent).Count - 1);
m_ScrollToSelected = true;
evt.Use();
}
if (evt.keyCode == KeyCode.UpArrow)
{
activeParent.selectedIndex--;
activeParent.selectedIndex = Mathf.Max(activeParent.selectedIndex, 0);
m_ScrollToSelected = true;
evt.Use();
}
if (evt.keyCode == KeyCode.Return || evt.keyCode == KeyCode.KeypadEnter)
{
if (activeSearchTreeEntry != null)
{
SelectEntry(activeSearchTreeEntry, true);
evt.Use();
}
}
// Do these if we're not in search mode
if (!hasSearch)
{
if (evt.keyCode == KeyCode.LeftArrow || evt.keyCode == KeyCode.Backspace)
{
GoToParent();
evt.Use();
}
if (evt.keyCode == KeyCode.RightArrow)
{
if (activeSearchTreeEntry != null)
{
SelectEntry(activeSearchTreeEntry, false);
evt.Use();
}
}
if (evt.keyCode == KeyCode.Escape)
{
Close();
evt.Use();
}
}
}
}
private void RebuildSearch()
{
if (!hasSearch)
{
m_SearchResultTree = null;
if (m_SelectionStack[m_SelectionStack.Count - 1].name == kSearchHeader)
{
m_SelectionStack.Clear();
m_SelectionStack.Add(m_Tree[0] as SearchTreeGroupEntry);
}
m_AnimTarget = 1;
m_LastTime = System.DateTime.Now.Ticks;
return;
}
// Support multiple search words separated by spaces.
string[] searchWords = m_Search.ToLower().Split(' ');
// We keep two lists. Matches that matches the start of an item always get first priority.
List<SearchTreeEntry> matchesStart = new List<SearchTreeEntry>();
List<SearchTreeEntry> matchesWithin = new List<SearchTreeEntry>();
foreach (SearchTreeEntry e in m_Tree)
{
if ((e is SearchTreeGroupEntry))
continue;
string name = e.name.ToLower().Replace(" ", "");
bool didMatchAll = true;
bool didMatchStart = false;
// See if we match ALL the seaarch words.
for (int w = 0; w < searchWords.Length; w++)
{
string search = searchWords[w];
if (name.Contains(search))
{
// If the start of the item matches the first search word, make a note of that.
if (w == 0 && name.StartsWith(search))
didMatchStart = true;
}
else
{
// As soon as any word is not matched, we disregard this item.
didMatchAll = false;
break;
}
}
// We always need to match all search words.
// If we ALSO matched the start, this item gets priority.
if (didMatchAll)
{
if (didMatchStart)
matchesStart.Add(e);
else
matchesWithin.Add(e);
}
}
matchesStart.Sort();
matchesWithin.Sort();
// Create search tree
List<SearchTreeEntry> tree = new List<SearchTreeEntry>();
// Add parent
tree.Add(new SearchTreeGroupEntry(new GUIContent(kSearchHeader)));
// Add search results
tree.AddRange(matchesStart);
tree.AddRange(matchesWithin);
// Create search result tree
m_SearchResultTree = tree.ToArray();
m_SelectionStack.Clear();
m_SelectionStack.Add(m_SearchResultTree[0] as SearchTreeGroupEntry);
// Always select the first search result when search is changed (e.g. a character was typed in or deleted),
// because it's usually the best match.
if (GetChildren(activeTree, activeParent).Count >= 1)
activeParent.selectedIndex = 0;
else
activeParent.selectedIndex = -1;
}
private SearchTreeGroupEntry GetSearchTreeEntryRelative(int rel)
{
int i = m_SelectionStack.Count + rel - 1;
if (i < 0 || i >= m_SelectionStack.Count)
return null;
return m_SelectionStack[i] as SearchTreeGroupEntry;
}
private void GoToParent()
{
if (m_SelectionStack.Count > 1)
{
m_AnimTarget = 0;
m_LastTime = System.DateTime.Now.Ticks;
}
}
private void ListGUI(SearchTreeEntry[] tree, float anim, SearchTreeGroupEntry parent, SearchTreeGroupEntry grandParent)
{
// Smooth the fractional part of the anim value
anim = Mathf.Floor(anim) + Mathf.SmoothStep(0, 1, Mathf.Repeat(anim, 1));
// Calculate rect for animated area
Rect animRect = position;
animRect.x = position.width * (1 - anim) + 1;
animRect.y = k_HeaderHeight;
animRect.height -= k_HeaderHeight;
animRect.width -= 2;
// Start of animated area (the part that moves left and right)
GUILayout.BeginArea(animRect);
// Header
Rect headerRect = GUILayoutUtility.GetRect(10, 25);
string name = parent.name;
GUI.Label(headerRect, name, s_Styles.header);
// Back button
if (grandParent != null)
{
float yOffset = (headerRect.height - s_Styles.leftArrow.fixedHeight) / 2;
Rect arrowRect = new Rect(
headerRect.x + s_Styles.leftArrow.margin.left,
headerRect.y + yOffset,
s_Styles.leftArrow.fixedWidth,
s_Styles.leftArrow.fixedHeight);
if (Event.current.type == EventType.Repaint)
s_Styles.leftArrow.Draw(arrowRect, false, false, false, false);
if (Event.current.type == EventType.MouseDown && headerRect.Contains(Event.current.mousePosition))
{
GoToParent();
Event.current.Use();
}
}
ListGUI(tree, parent);
GUILayout.EndArea();
}
private void SelectEntry(SearchTreeEntry e, bool shouldInvokeCallback)
{
if (e is SearchTreeGroupEntry)
{
if (!hasSearch)
{
m_LastTime = System.DateTime.Now.Ticks;
if (m_AnimTarget == 0)
m_AnimTarget = 1;
else if (m_Anim == 1)
{
m_Anim = 0;
m_SelectionStack.Add(e as SearchTreeGroupEntry);
}
}
}
else if (shouldInvokeCallback && provider.OnSelectEntry(e, m_Context))
Close();
}
private void ListGUI(SearchTreeEntry[] tree, SearchTreeGroupEntry parent)
{
// Start of scroll view list
parent.scroll = GUILayout.BeginScrollView(parent.scroll);
EditorGUIUtility.SetIconSize(new Vector2(16, 16));
List<SearchTreeEntry> children = GetChildren(tree, parent);
Rect selectedRect = new Rect();
// Iterate through the children
for (int i = 0; i < children.Count; i++)
{
SearchTreeEntry e = children[i];
Rect r = GUILayoutUtility.GetRect(16, 20, GUILayout.ExpandWidth(true));
// Select the SearchTreeEntry the mouse cursor is over.
// Only do it on mouse move - keyboard controls are allowed to overwrite this until the next time the mouse moves.
if (Event.current.type == EventType.MouseMove || Event.current.type == EventType.MouseDown)
{
if (parent.selectedIndex != i && r.Contains(Event.current.mousePosition))
{
parent.selectedIndex = i;
Repaint();
}
}
bool selected = false;
// Handle selected item
if (i == parent.selectedIndex)
{
selected = true;
selectedRect = r;
}
// Draw SearchTreeEntry
if (Event.current.type == EventType.Repaint)
{
GUIStyle labelStyle = (e is SearchTreeGroupEntry) ? s_Styles.groupButton : s_Styles.componentButton;
labelStyle.Draw(r, e.content, false, false, selected, selected);
if ((e is SearchTreeGroupEntry))
{
float yOffset = (r.height - s_Styles.rightArrow.fixedHeight) / 2;
Rect arrowRect = new Rect(
r.xMax - s_Styles.rightArrow.fixedWidth - s_Styles.rightArrow.margin.right,
r.y + yOffset,
s_Styles.rightArrow.fixedWidth,
s_Styles.rightArrow.fixedHeight);
s_Styles.rightArrow.Draw(arrowRect, false, false, false, false);
}
}
if (Event.current.type == EventType.MouseDown && r.Contains(Event.current.mousePosition))
{
Event.current.Use();
parent.selectedIndex = i;
SelectEntry(e, true);
}
}
EditorGUIUtility.SetIconSize(Vector2.zero);
GUILayout.EndScrollView();
// Scroll to show selected
if (m_ScrollToSelected && Event.current.type == EventType.Repaint)
{
m_ScrollToSelected = false;
Rect scrollRect = GUILayoutUtility.GetLastRect();
if (selectedRect.yMax - scrollRect.height > parent.scroll.y)
{
parent.scroll.y = selectedRect.yMax - scrollRect.height;
Repaint();
}
if (selectedRect.y < parent.scroll.y)
{
parent.scroll.y = selectedRect.y;
Repaint();
}
}
}
private List<SearchTreeEntry> GetChildren(SearchTreeEntry[] tree, SearchTreeEntry parent)
{
List<SearchTreeEntry> children = new List<SearchTreeEntry>();
int level = -1;
int i = 0;
for (i = 0; i < tree.Length; i++)
{
if (tree[i] == parent)
{
level = parent.level + 1;
i++;
break;
}
}
if (level == -1)
return children;
for (; i < tree.Length; i++)
{
SearchTreeEntry e = tree[i];
if (e.level < level)
break;
if (e.level > level && !hasSearch)
continue;
children.Add(e);
}
return children;
}
}
}