forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchFilter.cs
More file actions
308 lines (257 loc) · 10.9 KB
/
Copy pathSearchFilter.cs
File metadata and controls
308 lines (257 loc) · 10.9 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEditor.Collaboration;
using UnityEditor.Connect;
namespace UnityEditor
{
[System.Serializable]
internal class SearchFilter
{
public enum SearchArea
{
AllAssets,
InAssetsOnly,
InPackagesOnly,
SelectedFolders,
AssetStore
}
public enum State
{
EmptySearchFilter,
FolderBrowsing,
SearchingInAllAssets,
SearchingInAssetsOnly,
SearchingInPackagesOnly,
SearchingInFolders,
SearchingInAssetStore
}
// Searching
[SerializeField]
private string m_NameFilter = "";
[SerializeField]
private string[] m_ClassNames = new string[0];
[SerializeField]
private string[] m_AssetLabels = new string[0];
[SerializeField]
private string[] m_AssetBundleNames = new string[0];
[SerializeField]
private string[] m_VersionControlStates = new string[0];
[SerializeField]
private string[] m_SoftLockControlStates = new string[0];
[SerializeField]
private int[] m_ReferencingInstanceIDs = new int[0];
[SerializeField]
private string[] m_ScenePaths;
[SerializeField]
private bool m_ShowAllHits = false; // If true then just one filter must match to show an object, if false then all filters must match to show an object
[SerializeField]
SearchArea m_SearchArea = SearchArea.InAssetsOnly;
// Folder browsing
[SerializeField]
private string[] m_Folders = new string[0];
// Interface
public string nameFilter {get {return m_NameFilter; } set { m_NameFilter = value; } }
public string[] classNames { get {return m_ClassNames; } set {m_ClassNames = value; }}
public string[] assetLabels {get {return m_AssetLabels; } set {m_AssetLabels = value; }}
public string[] versionControlStates { get { return m_VersionControlStates; } set { m_VersionControlStates = value; } }
public string[] softLockControlStates { get { return m_SoftLockControlStates; } set { m_SoftLockControlStates = value; } }
public string[] assetBundleNames {get {return m_AssetBundleNames; } set {m_AssetBundleNames = value; }}
public int[] referencingInstanceIDs { get { return m_ReferencingInstanceIDs; } set { m_ReferencingInstanceIDs = value; } }
public string[] scenePaths { get { return m_ScenePaths; } set { m_ScenePaths = value; } }
public bool showAllHits { get { return m_ShowAllHits; } set { m_ShowAllHits = value; }}
public string[] folders { get {return m_Folders; } set {m_Folders = value; }}
public SearchArea searchArea { get { return m_SearchArea; } set { m_SearchArea = value; }}
public void ClearSearch()
{
m_NameFilter = "";
m_ClassNames = new string[0];
m_AssetLabels = new string[0];
m_AssetBundleNames = new string[0];
m_ReferencingInstanceIDs = new int[0];
m_ScenePaths = new string[0];
m_VersionControlStates = new string[0];
m_SoftLockControlStates = new string[0];
m_ShowAllHits = false;
}
bool IsNullOrEmpty<T>(T[] list)
{
return (list == null || list.Length == 0);
}
public State GetState()
{
bool isSearchActive = !string.IsNullOrEmpty(m_NameFilter) ||
!IsNullOrEmpty(m_AssetLabels) ||
!IsNullOrEmpty(m_ClassNames) ||
!IsNullOrEmpty(m_AssetBundleNames) ||
!IsNullOrEmpty(m_ReferencingInstanceIDs);
isSearchActive = isSearchActive || !IsNullOrEmpty(m_VersionControlStates);
isSearchActive = isSearchActive || !IsNullOrEmpty(m_SoftLockControlStates);
bool foldersActive = !IsNullOrEmpty(m_Folders);
if (isSearchActive)
{
if (m_SearchArea == SearchArea.AssetStore)
return State.SearchingInAssetStore;
if (foldersActive && m_SearchArea == SearchArea.SelectedFolders)
return State.SearchingInFolders;
if (m_SearchArea == SearchArea.InAssetsOnly)
return State.SearchingInAssetsOnly;
if (m_SearchArea == SearchArea.InPackagesOnly)
return State.SearchingInPackagesOnly;
return State.SearchingInAllAssets;
}
else if (foldersActive)
{
return State.FolderBrowsing;
}
return State.EmptySearchFilter;
}
public bool IsSearching()
{
State state = GetState();
return (state == State.SearchingInAllAssets ||
state == State.SearchingInAssetsOnly ||
state == State.SearchingInPackagesOnly ||
state == State.SearchingInFolders ||
state == State.SearchingInAssetStore);
}
public bool SetNewFilter(SearchFilter newFilter)
{
bool changed = false;
if (newFilter.m_NameFilter != m_NameFilter)
{
m_NameFilter = newFilter.m_NameFilter;
changed = true;
}
if (newFilter.m_ClassNames != m_ClassNames)
{
m_ClassNames = newFilter.m_ClassNames;
changed = true;
}
if (newFilter.m_Folders != m_Folders)
{
m_Folders = newFilter.m_Folders;
changed = true;
}
if (newFilter.m_VersionControlStates != m_VersionControlStates)
{
m_VersionControlStates = newFilter.m_VersionControlStates;
changed = true;
}
if (newFilter.m_SoftLockControlStates != m_SoftLockControlStates)
{
m_SoftLockControlStates = newFilter.m_SoftLockControlStates;
changed = true;
}
if (newFilter.m_AssetLabels != m_AssetLabels)
{
m_AssetLabels = newFilter.m_AssetLabels;
changed = true;
}
if (newFilter.m_AssetBundleNames != m_AssetBundleNames)
{
m_AssetBundleNames = newFilter.m_AssetBundleNames;
changed = true;
}
if (newFilter.m_ReferencingInstanceIDs != m_ReferencingInstanceIDs)
{
m_ReferencingInstanceIDs = newFilter.m_ReferencingInstanceIDs;
changed = true;
}
if (newFilter.m_ScenePaths != m_ScenePaths)
{
m_ScenePaths = newFilter.m_ScenePaths;
changed = true;
}
if (newFilter.m_SearchArea != m_SearchArea)
{
m_SearchArea = newFilter.m_SearchArea;
changed = true;
}
m_ShowAllHits = newFilter.m_ShowAllHits;
return changed;
}
// Debug
public override string ToString()
{
string result = "SearchFilter: ";
result += string.Format("[Area: {0}, State: {1}]", m_SearchArea, GetState());
if (!string.IsNullOrEmpty(m_NameFilter))
result += "[Name: " + m_NameFilter + "]";
if (m_AssetLabels != null && m_AssetLabels.Length > 0)
result += "[Labels: " + m_AssetLabels[0] + "]";
if (m_VersionControlStates != null && m_VersionControlStates.Length > 0)
result += "[VersionStates: " + m_VersionControlStates[0] + "]";
if (m_SoftLockControlStates != null && m_SoftLockControlStates.Length > 0)
result += "[SoftLockStates: " + m_SoftLockControlStates[0] + "]";
if (m_AssetBundleNames != null && m_AssetBundleNames.Length > 0)
result += "[AssetBundleNames: " + m_AssetBundleNames[0] + "]";
if (m_ClassNames != null && m_ClassNames.Length > 0)
result += "[Types: " + m_ClassNames[0] + " (" + m_ClassNames.Length + ")]";
if (m_ReferencingInstanceIDs != null && m_ReferencingInstanceIDs.Length > 0)
result += "[RefIDs: " + m_ReferencingInstanceIDs[0] + "]";
if (m_Folders != null && m_Folders.Length > 0)
result += "[Folders: " + m_Folders[0] + "]";
result += "[ShowAllHits: " + showAllHits + "]";
return result;
}
internal string FilterToSearchFieldString()
{
string result = "";
if (!string.IsNullOrEmpty(m_NameFilter))
result += m_NameFilter;
// See SearchUtility.cs for search tokens
AddToString("t:", m_ClassNames, ref result);
AddToString("l:", m_AssetLabels, ref result);
AddToString("v:", m_VersionControlStates, ref result);
AddToString("s:", m_SoftLockControlStates, ref result);
AddToString("b:", m_AssetBundleNames, ref result);
return result;
}
void AddToString<T>(string prefix, T[] list, ref string result)
{
if (list == null)
return;
if (result == null)
result = "";
foreach (T item in list)
{
if (!string.IsNullOrEmpty(result))
result += " ";
result += prefix + item;
}
}
// Keeps current SearchArea
internal void SearchFieldStringToFilter(string searchString)
{
ClearSearch();
if (string.IsNullOrEmpty(searchString))
return;
SearchUtility.ParseSearchString(searchString, this);
}
internal static SearchFilter CreateSearchFilterFromString(string searchText)
{
SearchFilter searchFilter = new SearchFilter();
SearchUtility.ParseSearchString(searchText, searchFilter);
return searchFilter;
}
// Split text into words separated by whitespace but handle quotes
// E.g 'one man' becomes: 'one', 'man'
// E.g '"one man' becomes: 'one', 'man'
// E.g '"one man"' becomes: 'one man'
public static string[] Split(string text)
{
if (string.IsNullOrEmpty(text))
return new string[0];
List<string> words = new List<string>();
foreach (Match m in Regex.Matches(text, "\".+?\"|\\S+"))
{
words.Add(m.Value.Replace("\"", "")); // remove quotes
}
return words.ToArray();
}
} // end of class SearchFilter
}