-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathSoftlockViewController.cs
More file actions
513 lines (440 loc) · 18.8 KB
/
SoftlockViewController.cs
File metadata and controls
513 lines (440 loc) · 18.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
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
// 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 UnityEditor.Collaboration;
using UnityEngine.SceneManagement;
using UnityEditor.SceneManagement;
using System;
using UnityEditor.Web;
namespace UnityEditor
{
// Displays the Softlocks UI in the various areas of the Editor.
internal class SoftlockViewController
{
private static SoftlockViewController s_Instance;
public GUIStyle k_Style = null;
public GUIStyle k_StyleEmpty = GUIStyle.none; // For tooltips only.
public GUIContent k_Content = null;
// Stores UI strings for reuse and Editor (inspector) references to trigger
// a repaint when softlock data changes.
private SoftlockViewController.Cache m_Cache = null;
private const string k_TooltipHeader = "Unpublished changes by:";
private const string k_TooltipPrefabHeader = "Unpublished Prefab changes by:";
private const string k_TooltipNamePrefix = " \n \u2022 "; // u2022 displays a • (bullet point)
private SoftlockViewController() {}
~SoftlockViewController() {}
[SerializeField]
private SoftLockFilters m_SoftLockFilters = new SoftLockFilters();
public SoftLockFilters softLockFilters { get { return m_SoftLockFilters; } }
public static SoftlockViewController Instance
{
get
{
if (s_Instance == null)
{
s_Instance = new SoftlockViewController();
s_Instance.m_Cache = new Cache();
}
return s_Instance;
}
}
// Initialises dependencies.
public void TurnOn()
{
RegisterDataDelegate();
RegisterDrawDelegates();
Repaint();
}
public void TurnOff()
{
UnregisterDataDelegate();
UnregisterDrawDelegates();
}
private void UnregisterDataDelegate()
{
SoftLockData.SoftlockSubscriber -= Instance.OnSoftlockUpdate;
}
private void RegisterDataDelegate()
{
UnregisterDataDelegate();
SoftLockData.SoftlockSubscriber += Instance.OnSoftlockUpdate;
}
private void UnregisterDrawDelegates()
{
ObjectListArea.postAssetIconDrawCallback -= Instance.DrawProjectBrowserGridUI;
ObjectListArea.postAssetLabelDrawCallback -= Instance.DrawProjectBrowserListUI;
Editor.OnPostIconGUI -= Instance.DrawInspectorUI;
GameObjectTreeViewGUI.OnPostHeaderGUI -= Instance.DrawSceneUI;
}
// Connects to the areas of the Editor that display softlocks.
private void RegisterDrawDelegates()
{
UnregisterDrawDelegates();
ObjectListArea.postAssetIconDrawCallback += Instance.DrawProjectBrowserGridUI;
ObjectListArea.postAssetLabelDrawCallback += Instance.DrawProjectBrowserListUI;
AssetsTreeViewGUI.postAssetLabelDrawCallback += Instance.DrawSingleColumnProjectBrowserUI;
Editor.OnPostIconGUI += Instance.DrawInspectorUI;
GameObjectTreeViewGUI.OnPostHeaderGUI += Instance.DrawSceneUI;
}
// Returns true when the 'editor' supports Softlock UI and the
// user has Collaborate permissions.
private bool HasSoftlockSupport(Editor editor)
{
if (!Collab.instance.IsCollabEnabledForCurrentProject() || editor == null || editor.targets.Length > 1)
{
return false;
}
if (editor.target == null || !SoftLockData.AllowsSoftLocks(editor.target))
{
return false;
}
// Support Scene and Game object Inspector headers, not others like MaterialEditor.
bool hasSupport = true;
Type editorType = editor.GetType();
if (editorType != typeof(GameObjectInspector) && editorType != typeof(GenericInspector))
{
hasSupport = false;
}
return hasSupport;
}
private bool HasSoftlocks(string assetGUID)
{
if (!Collab.instance.IsCollabEnabledForCurrentProject())
{
return false;
}
bool hasSoftLocks;
bool isValid = (SoftLockData.TryHasSoftLocks(assetGUID, out hasSoftLocks) && hasSoftLocks);
return isValid;
}
// Redraws softlock UI associated with the given list of 'assetGUIDs'.
public void OnSoftlockUpdate(string[] assetGUIDs)
{
// Remove cached UI for the assetGUIDs before triggered a redraw.
m_Cache.InvalidateAssetGUIDs(assetGUIDs);
Repaint();
}
// Repaints all the areas where softlocks are displayed.
public void Repaint()
{
RepaintInspectors();
RepaintSceneHierarchy();
RepaintProjectBrowsers();
}
private void RepaintSceneHierarchy()
{
List<SceneHierarchyWindow> sceneUIs = SceneHierarchyWindow.GetAllSceneHierarchyWindows();
foreach (SceneHierarchyWindow sceneUI in sceneUIs)
{
sceneUI.Repaint();
}
}
private void RepaintInspectors()
{
foreach (Editor editor in m_Cache.GetEditors())
{
// Does not repaint when editor is not visible, but the editor's
// "DockArea" tab will redraw either way.
editor.Repaint();
}
}
private void RepaintProjectBrowsers()
{
foreach (ProjectBrowser pb in ProjectBrowser.GetAllProjectBrowsers())
{
pb.RefreshSearchIfFilterContains("s:");
pb.Repaint();
}
}
// Draws in the Hierarchy header, left of the context menu.
public float DrawSceneUI(Rect availableRect, string scenePath)
{
string assetGUID = AssetDatabase.AssetPathToGUID(scenePath);
if (!HasSoftlocks(assetGUID))
{
return availableRect.xMax;
}
int lockCount;
SoftLockData.TryGetSoftlockCount(assetGUID, out lockCount);
GUIContent content = GetGUIContent();
content.image = SoftLockUIData.GetIconForSection(SoftLockUIData.SectionEnum.Scene);
content.text = GetDisplayCount(lockCount);
content.tooltip = Instance.GetTooltip(assetGUID);
Vector2 contentSize = GetStyle().CalcSize(content);
Rect drawRect = new Rect(availableRect.position, contentSize);
const int kRightMargin = 4;
drawRect.x = (availableRect.width - drawRect.width) - kRightMargin;
EditorGUI.LabelField(drawRect, content);
return drawRect.xMin;
}
// Assigned as a callback to Editor.OnPostHeaderGUI
// Draws the Scene Inspector (Editor.cs) as well as the Game Object Inspector (GameObjectInspector.cs)
private void DrawInspectorUI(Editor editor, Rect drawRect)
{
if (!HasSoftlockSupport(editor))
{
return;
}
m_Cache.StoreEditor(editor);
string assetGUID = null;
AssetAccess.TryGetAssetGUIDFromObject(editor.target, out assetGUID);
if (!HasSoftlocks(assetGUID))
{
return;
}
Texture icon = SoftLockUIData.GetIconForSection(SoftLockUIData.SectionEnum.ProjectBrowser);
if (icon != null)
{
DrawIconWithTooltips(drawRect, icon, assetGUID);
}
}
// Assigned callback to ObjectListArea.OnPostAssetDrawDelegate.
// Draws either overtop of the project browser asset (when in grid view).
private void DrawProjectBrowserGridUI(Rect iconRect, string assetGUID, bool isListMode)
{
if (isListMode || !HasSoftlocks(assetGUID))
{
return;
}
Rect drawRect = Rect.zero;
Texture icon = SoftLockUIData.GetIconForSection(SoftLockUIData.SectionEnum.ProjectBrowser);
if (icon != null)
{
drawRect = Overlay.GetRectForBottomRight(iconRect, Overlay.k_OverlaySizeOnLargeIcon);
DrawIconWithTooltips(drawRect, icon, assetGUID);
}
}
// Should draw only in listMode and expects 'drawRect' to be the designed space for the icon,
// and not the entire row.
private bool DrawProjectBrowserListUI(Rect drawRect, string assetGUID, bool isListMode)
{
if (!isListMode || !HasSoftlocks(assetGUID))
{
return false;
}
// center icon.
Rect iconRect = drawRect;
iconRect.width = drawRect.height;
iconRect.x = (float)Math.Round(drawRect.center.x - (iconRect.width / 2F));
return DrawInProjectBrowserListMode(iconRect, assetGUID);
}
// Expects 'drawRect' to be the available width of the row.
private bool DrawSingleColumnProjectBrowserUI(Rect drawRect, string assetGUID)
{
if (ProjectBrowser.s_LastInteractedProjectBrowser.IsTwoColumns() || !HasSoftlocks(assetGUID))
{
return false;
}
Rect iconRect = drawRect;
iconRect.width = drawRect.height;
float spacingFromEnd = (iconRect.width / 2F);
iconRect.x = (float)Math.Round(drawRect.xMax - iconRect.width - spacingFromEnd);
return DrawInProjectBrowserListMode(iconRect, assetGUID);
}
private bool DrawInProjectBrowserListMode(Rect iconRect, string assetGUID)
{
Texture icon = SoftLockUIData.GetIconForSection(SoftLockUIData.SectionEnum.ProjectBrowser);
bool didDraw = false;
if (icon != null)
{
DrawIconWithTooltips(iconRect, icon, assetGUID);
didDraw = true;
}
return didDraw;
}
private void DrawIconWithTooltips(Rect iconRect, Texture icon, string assetGUID)
{
GUI.DrawTexture(iconRect, icon, ScaleMode.ScaleToFit);
DrawTooltip(iconRect, GetTooltip(assetGUID));
}
private void DrawTooltip(Rect frame, string tooltip)
{
GUIContent content = GetGUIContent();
content.tooltip = tooltip;
GUI.Label(frame, content, k_StyleEmpty);
}
#region String Helpers
// Returns a string formatted as a vertical list of names with a heading.
private string GetTooltip(string assetGUID)
{
string formattedText;
if (!m_Cache.TryGetTooltipForGUID(assetGUID, out formattedText))
{
List<string> softLockNames = SoftLockUIData.GetLocksNamesOnAsset(assetGUID);
string tooltipHeaderText = (SoftLockData.IsPrefab(assetGUID) ? k_TooltipPrefabHeader : k_TooltipHeader);
formattedText = tooltipHeaderText;
foreach (string name in softLockNames)
{
formattedText += k_TooltipNamePrefix + name + " ";
}
m_Cache.StoreTooltipForGUID(assetGUID, formattedText);
}
return formattedText;
}
// Retrieves a previously generated string from cache
// or creates a string displaying the given 'count' surrounded by brackets.
// e.g. "(0)"
private static string GetDisplayCount(int count)
{
string totalLocksText;
if (!Instance.m_Cache.TryGetDisplayCount(count, out totalLocksText))
{
totalLocksText = count.ToString();
Instance.m_Cache.StoreDisplayCount(count, totalLocksText);
}
return totalLocksText;
}
#endregion
#region GUI Content
public GUIContent GetGUIContent()
{
if (k_Content == null)
{
k_Content = new GUIContent();
}
k_Content.tooltip = string.Empty;
k_Content.text = null;
k_Content.image = null;
return k_Content;
}
public GUIStyle GetStyle()
{
if (k_Style == null)
{
k_Style = new GUIStyle(EditorStyles.label);
k_Style.normal.background = null;
}
return k_Style;
}
#endregion
// Stores UI strings for reuse and Editors as WeakReferences.
private class Cache
{
private List<WeakReference> m_EditorReferences = new List<WeakReference>();
private List<WeakReference> m_CachedWeakReferences = new List<WeakReference>();
private static Dictionary<int, string> s_CachedStringCount = new Dictionary<int, string>();
private Dictionary<string, string> m_AssetGUIDToTooltip = new Dictionary<string, string>();
private Dictionary<string, Dictionary<int, string>> m_NamesListToEllipsedNames = new Dictionary<string, Dictionary<int, string>>();
public Cache() {}
// Removes cached strings references by the given 'assetGUIDs'.
public void InvalidateAssetGUIDs(string[] assetGUIDs)
{
for (int index = 0; index < assetGUIDs.Length; index++)
{
string assetGUID = assetGUIDs[index];
m_AssetGUIDToTooltip.Remove(assetGUID);
}
}
// Failure: assigns empty string ("") to 'ellipsedNames', returns false.
// Success: assigns the cached string to 'ellipsedNames', returns true.
public bool TryGetEllipsedNames(string allNames, int characterLength, out string ellipsedNames)
{
Dictionary<int, string> ellipsedVersions;
if (m_NamesListToEllipsedNames.TryGetValue(allNames, out ellipsedVersions))
{
return ellipsedVersions.TryGetValue(characterLength, out ellipsedNames);
}
ellipsedNames = "";
return false;
}
// 'allNames' and 'characterLength' will be the keys to access the cached 'ellipsedNames'
// see TryGetEllipsedNames() for retrieval.
public void StoreEllipsedNames(string allNames, string ellipsedNames, int characterLength)
{
Dictionary<int, string> ellipsedVersions;
if (!m_NamesListToEllipsedNames.TryGetValue(allNames, out ellipsedVersions))
{
ellipsedVersions = new Dictionary<int, string>();
}
ellipsedVersions[characterLength] = ellipsedNames;
m_NamesListToEllipsedNames[allNames] = ellipsedVersions;
}
// Failure: assigns empty string ("") to 'tooltipText', returns false.
// Success: assigns the cached string to 'tooltipText', returns true.
public bool TryGetTooltipForGUID(string assetGUID, out string tooltipText)
{
return m_AssetGUIDToTooltip.TryGetValue(assetGUID, out tooltipText);
}
// 'assetGUID' will be the key to access the cached 'tooltipText'
// see TryGetTooltipForGUID() for retrieval.
public void StoreTooltipForGUID(string assetGUID, string tooltipText)
{
m_AssetGUIDToTooltip[assetGUID] = tooltipText;
}
// Failure: assigns empty string ("") to 'displayText', returns false.
// Success: assigns the cached string to 'displayText', returns true.
public bool TryGetDisplayCount(int count, out string displayText)
{
return s_CachedStringCount.TryGetValue(count, out displayText);
}
// 'count' will be the key to access the cached 'displayText'
// see TryGetDisplayCount() for retrieval.
public void StoreDisplayCount(int count, string displayText)
{
s_CachedStringCount.Add(count, displayText);
}
// Contains at most the list of all previously given Editors
// via StoreEditor(). Garbage collected Editor(s) will be missing.
public List<Editor> GetEditors()
{
List<Editor> editors = new List<Editor>();
for (int index = 0; index < m_EditorReferences.Count; index++)
{
WeakReference reference = m_EditorReferences[index];
Editor editor = reference.Target as Editor;
if (editor == null)
{
m_EditorReferences.RemoveAt(index);
m_CachedWeakReferences.Add(reference);
index--;
}
else
{
editors.Add(editor);
}
}
return editors;
}
// Stores the Editor in a WeakReference.
public void StoreEditor(Editor editor)
{
bool canAdd = true;
// Check for duplicates and purge any null targets.
for (int index = 0; canAdd && (index < m_EditorReferences.Count); index++)
{
WeakReference reference = m_EditorReferences[index];
Editor storedEditor = reference.Target as Editor;
if (storedEditor == null)
{
m_EditorReferences.RemoveAt(index);
m_CachedWeakReferences.Add(reference);
index--;
}
else if (storedEditor == editor)
{
canAdd = false;
break;
}
}
if (canAdd)
{
WeakReference editorReference;
// Reuse any old WeakReference if available.
if (m_CachedWeakReferences.Count > 0)
{
editorReference = m_CachedWeakReferences[0];
m_CachedWeakReferences.RemoveAt(0);
}
else
{
editorReference = new WeakReference(null);
}
editorReference.Target = editor;
m_EditorReferences.Add(editorReference);
}
}
}
}
}