forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphViewTemplateWindow.cs
More file actions
476 lines (415 loc) · 18.4 KB
/
Copy pathGraphViewTemplateWindow.cs
File metadata and controls
476 lines (415 loc) · 18.4 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
// 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.Threading;
using Unity.UI.Builder;
using UnityEditor.PackageManager;
using UnityEditor.PackageManager.UI;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
namespace UnityEditor.Experimental.GraphView
{
internal interface ITemplateHelper
{
string packageInfoName { get; }
string learningSampleName { get; }
string templateWindowDocUrl { get; }
string builtInTemplatePath { get; }
string builtInCategory { get; }
string assetType { get; }
string emptyTemplateName { get; }
string emptyTemplateDescription { get; }
string lastSelectedGuidKey { get; }
string createNewAssetTitle { get; }
string insertTemplateTitle { get; }
string emptyTemplateIconPath { get; }
string emptyTemplateScreenshotPath { get; }
string customTemplateIcon { get; }
GraphViewTemplateWindow.ISaveFileDialogHelper saveFileDialogHelper { get; set; }
void RaiseTemplateUsed(GraphViewTemplateDescriptor usedTemplate);
bool TryGetTemplate(string assetPath, out GraphViewTemplateDescriptor graphViewTemplate);
bool TrySetTemplate(string assetPath, GraphViewTemplateDescriptor graphViewTemplate);
}
internal interface ITemplateDescriptor
{
string header { get; }
}
internal class GraphViewTemplateWindow : EditorWindow
{
internal interface ISaveFileDialogHelper
{
string OpenSaveFileDialog();
}
private class TemplateSection : ITemplateDescriptor
{
public TemplateSection(string text)
{
header = text;
}
public string header { get; }
}
private const float PackageManagerTimeout = 5f; // 5s
private readonly List<TreeViewItemData<ITemplateDescriptor>> m_TemplatesTree = new ();
private TreeView m_ListOfTemplates;
private Texture2D m_CustomTemplateIcon;
private Image m_DetailsScreenshot;
private Label m_DetailsTitle;
private Label m_DetailsDescription;
private VisualTreeAsset m_ItemTemplate;
private Action<string> m_AssetCreationCallback;
private string m_LastSelectedTemplatePath;
private int m_LastSelectedIndex;
private CreateMode m_CurrentMode;
private Action<string, string> m_UserCallback;
private string m_LastSelectedTemplateGuid;
private GraphViewTemplateDescriptor m_SelectedTemplate;
private Button m_InstallButton;
private ITemplateHelper m_TemplateHelper;
private enum CreateMode
{
CreateNew,
Insert,
None,
}
public static void ShowCreateFromTemplate(ITemplateHelper templateHelper, Action<string, string> callback, bool showSaveDialog = true) => ShowInternal(showSaveDialog ? CreateMode.CreateNew : CreateMode.None, templateHelper, callback);
public static void ShowInsertTemplate(ITemplateHelper templateHelper, Action<string, string> callback) => ShowInternal(CreateMode.Insert, templateHelper, callback);
private static void ShowInternal(CreateMode mode, ITemplateHelper templateHelper, Action<string, string> callback)
{
var windowTitle = mode == CreateMode.Insert ? templateHelper.insertTemplateTitle : templateHelper.createNewAssetTitle;
var templateWindow = EditorWindow.GetWindow<GraphViewTemplateWindow>(true, windowTitle, false);
templateWindow.Setup(mode, templateHelper, callback);
}
private void Setup(CreateMode mode, ITemplateHelper templateHelper, Action<string, string> callback)
{
minSize = new Vector2(800, 300);
m_UserCallback = callback;
m_CurrentMode = mode;
m_TemplateHelper = templateHelper;
SetCallBack();
LoadTemplates();
// Handle the install button here because we need the template helper
m_InstallButton = rootVisualElement.Q<Button>("InstallButton");
if (!string.IsNullOrEmpty(m_TemplateHelper.learningSampleName))
{
m_InstallButton.clicked += OnInstall;
m_InstallButton.enabledSelf = TryFindSample(m_TemplateHelper.learningSampleName, out var sample) && !sample.isImported;
}
else
{
m_InstallButton.style.display = DisplayStyle.None;
}
m_CustomTemplateIcon = EditorGUIUtility.LoadIcon(m_TemplateHelper.customTemplateIcon);
}
private void CreateGUI()
{
m_ItemTemplate = (VisualTreeAsset)EditorGUIUtility.Load("UXML/GraphView/TemplateItem.uxml");
var tpl = (VisualTreeAsset)EditorGUIUtility.Load("UXML/GraphView/TemplateWindow.uxml");
tpl.CloneTree(rootVisualElement);
rootVisualElement.AddStyleSheetPath("StyleSheets/GraphView/TemplateWindow.uss");
rootVisualElement.name = "VFXTemplateWindowRoot";
rootVisualElement.Q<Button>("CreateButton").clicked += OnCreate;
rootVisualElement.Q<Button>("CancelButton").clicked += OnCancel;
m_DetailsScreenshot = rootVisualElement.Q<Image>("Screenshot");
m_DetailsScreenshot.scaleMode = ScaleMode.ScaleAndCrop;
m_DetailsTitle = rootVisualElement.Q<Label>("Title");
m_DetailsDescription = rootVisualElement.Q<Label>("Description");
var helpButton = rootVisualElement.Q<Button>("HelpButton");
helpButton.clicked += OnOpenHelp;
var helpImage = helpButton.Q<Image>("HelpImage");
helpImage.image = EditorGUIUtility.LoadIcon(EditorResources.iconsPath + "_Help.png");
m_ListOfTemplates = rootVisualElement.Q<TreeView>("ListOfTemplates");
m_ListOfTemplates.virtualizationMethod = CollectionVirtualizationMethod.DynamicHeight;
m_ListOfTemplates.makeItem = CreateTemplateItem;
m_ListOfTemplates.bindItem = BindTemplateItem;
m_ListOfTemplates.unbindItem = UnbindTemplateItem;
m_ListOfTemplates.selectionChanged += OnSelectionChanged;
}
private void SetCallBack()
{
switch (m_CurrentMode)
{
case CreateMode.CreateNew:
m_AssetCreationCallback = CreateNewAsset;
break;
case CreateMode.Insert:
m_AssetCreationCallback = InsertTemplateInVisualEffect;
break;
case CreateMode.None:
m_AssetCreationCallback = x => m_UserCallback.Invoke(x, null);
break;
default:
throw new ArgumentOutOfRangeException(nameof(m_CurrentMode), m_CurrentMode, null);
}
}
private void OnOpenHelp() => Help.Browseurl(m_TemplateHelper.templateWindowDocUrl);
private void LoadTemplates()
{
m_LastSelectedTemplateGuid = EditorPrefs.GetString(m_TemplateHelper.lastSelectedGuidKey);
CollectTemplates();
m_ListOfTemplates.ExpandAll();
}
private void OnEnable()
{
AssemblyReloadEvents.beforeAssemblyReload += OnBeforeAssemblyReload;
}
private void OnDisable()
{
AssemblyReloadEvents.beforeAssemblyReload -= OnBeforeAssemblyReload;
}
private void OnBeforeAssemblyReload()
{
Close();
}
private void OnDestroy()
{
EditorPrefs.SetString(m_TemplateHelper.lastSelectedGuidKey, m_LastSelectedTemplateGuid);
}
private void OnCancel()
{
m_LastSelectedTemplatePath = null;
m_AssetCreationCallback?.Invoke(m_LastSelectedTemplatePath);
Close();
}
private void OnInstall()
{
if (TryFindSample(m_TemplateHelper.learningSampleName, out var samplePackage))
{
m_InstallButton.enabledSelf = !samplePackage.Import(Sample.ImportOptions.HideImportWindow | Sample.ImportOptions.OverridePreviousImports);
}
}
private void OnCreate()
{
var template = m_ListOfTemplates.selectedIndex != -1 ? (GraphViewTemplateDescriptor)m_ListOfTemplates.selectedItem : m_SelectedTemplate;
m_LastSelectedTemplatePath = AssetDatabase.GUIDToAssetPath(template.assetGuid);
m_AssetCreationCallback?.Invoke(m_LastSelectedTemplatePath);
Close();
m_TemplateHelper.RaiseTemplateUsed(template);
m_AssetCreationCallback = null;
}
private bool TryFindSample(string sampleName, out Sample sample)
{
try
{
var startTime = Time.time;
var searchRequest = Client.Search(m_TemplateHelper.packageInfoName, true);
while (!searchRequest.IsCompleted && Time.time - startTime < PackageManagerTimeout)
{
Thread.Sleep(20);
}
if (searchRequest is { Result: { Length: 1 }, IsCompleted: true } && searchRequest.Result[0] is { } packageInfo)
{
// Workaround for UUM-63664
foreach (var extension in PackageManagerExtensions.Extensions)
{
try
{
extension.OnPackageSelectionChange(packageInfo);
}
catch (Exception e)
{
Debug.LogWarning($"An error occured while trying to select a package extension.\n{e.Message}");
}
}
foreach (var samplePackage in Sample.FindByPackage(m_TemplateHelper.packageInfoName, null))
{
if (string.Compare(samplePackage.displayName, sampleName, StringComparison.OrdinalIgnoreCase) ==
0)
{
sample = samplePackage;
return true;
}
}
}
else
{
Debug.LogWarning($"Could not determine if the {sampleName} package is installed");
}
}
catch (Exception ex)
{
Debug.LogWarning($"Something went wrong while trying to retrieve {sampleName} package info\n{ex.Message}");
}
sample = default;
return false;
}
private void CreateNewAsset(string templatePath)
{
if (templatePath == null)
{
return;
}
var assetPath = m_TemplateHelper.saveFileDialogHelper.OpenSaveFileDialog();
if (!string.IsNullOrEmpty(assetPath))
{
m_UserCallback?.Invoke(templatePath, assetPath);
}
}
private void InsertTemplateInVisualEffect(string templatePath)
{
if (!string.IsNullOrEmpty(templatePath))
{
this.m_UserCallback.Invoke(templatePath, null);
}
}
private void OnSelectionChanged(IEnumerable<object> newSelection)
{
foreach (var item in newSelection)
{
if (item is GraphViewTemplateDescriptor template)
{
m_SelectedTemplate = template;
m_DetailsTitle.text = template.name;
m_DetailsDescription.text = template.description;
m_LastSelectedTemplateGuid = template.assetGuid;
m_LastSelectedIndex = m_ListOfTemplates.selectedIndex;
// Maybe set a placeholder screenshot when null
m_DetailsScreenshot.image = template.thumbnail;
}
// We expect only one item to be selected
return;
}
// Reach here when the selection is empty
m_ListOfTemplates.selectedIndex = m_LastSelectedIndex;
}
private void BindTemplateItem(VisualElement item, int index)
{
var data = m_ListOfTemplates.GetItemDataForIndex<ITemplateDescriptor>(index);
var label = item.Q<Label>("TemplateName");
label.text = data.header;
string ussClass;
if (data is GraphViewTemplateDescriptor template)
{
item.Q<Image>("TemplateIcon").image = template.icon != null ? template.icon : m_CustomTemplateIcon;
if (template.assetGuid == m_LastSelectedTemplateGuid)
m_ListOfTemplates.SetSelection(index);
ussClass = "vfxtemplate-item";
item.RegisterCallback<ClickEvent>(OnClickItem);
}
else
{
// This is a hack to put the expand/collapse button above the item so that we can interact with it
var toggle = item.parent.parent.Q<Toggle>();
toggle.BringToFront();
ussClass = "vfxtemplate-section";
}
if (item.GetFirstAncestorWithClass("unity-tree-view__item") is { } parent)
{
parent.AddToClassList(ussClass);
}
}
private void UnbindTemplateItem(VisualElement item, int index)
{
if (item.GetFirstAncestorWithClass("unity-tree-view__item") is { } parent)
{
parent.RemoveFromClassList("vfxtemplate-item");
parent.RemoveFromClassList("vfxtemplate-section");
}
item.UnregisterCallback<ClickEvent>(OnClickItem);
}
private void OnClickItem(ClickEvent evt)
{
if (evt.clickCount == 2 && m_ListOfTemplates.selectedItem != null)
{
OnCreate();
}
}
private VisualElement CreateTemplateItem() => m_ItemTemplate.Instantiate();
private void CollectTemplates()
{
m_TemplatesTree.Clear();
var assetsGuid = AssetDatabase.FindAssets($"t:{m_TemplateHelper.assetType}");
var allTemplates = new List<GraphViewTemplateDescriptor>(assetsGuid.Length);
foreach (var guid in assetsGuid)
{
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
if (m_TemplateHelper.TryGetTemplate(assetPath, out var template))
{
var isBuiltIn = assetPath.StartsWith(m_TemplateHelper.builtInTemplatePath);
template.category = isBuiltIn ? m_TemplateHelper.builtInCategory : template.category;
template.order = isBuiltIn ? 0 : 1;
template.assetGuid = guid;
if (isBuiltIn)
{
template.icon = GetSkinIcon(template.icon);
}
allTemplates.Add(template);
}
}
if (m_CurrentMode != CreateMode.Insert)
{
allTemplates.Add(MakeEmptyTemplate());
}
var templatesGroupedByCategory = new Dictionary<string, List<GraphViewTemplateDescriptor>>();
foreach (var template in allTemplates)
{
if (templatesGroupedByCategory.TryGetValue(template.category, out var list))
{
list.Add(template);
}
else
{
list = new List<GraphViewTemplateDescriptor> { template };
templatesGroupedByCategory[template.category] = list;
}
}
// This is to prevent collapse/expand if there's only one category
if (templatesGroupedByCategory.Count == 1)
{
m_ListOfTemplates.AddToClassList("remove-toggle");
}
else
{
m_ListOfTemplates.RemoveFromClassList("remove-toggle");
}
var templates = new List<List<GraphViewTemplateDescriptor>>(templatesGroupedByCategory.Values);
templates.Sort((listA, listB) => listA[0].order.CompareTo(listB[0].order));
var id = 0;
var lastSelectedTemplateFound = false;
var fallBackTemplateAssetGuid = string.Empty;
foreach (var group in templates)
{
var groupId = id++;
var children = new List<TreeViewItemData<ITemplateDescriptor>>(group.Count);
foreach (var child in group)
{
if (id == 2)
fallBackTemplateAssetGuid = child.assetGuid;
if (child.assetGuid == m_LastSelectedTemplateGuid)
lastSelectedTemplateFound = true;
children.Add(new TreeViewItemData<ITemplateDescriptor>(id++, child));
}
var section = new TreeViewItemData<ITemplateDescriptor>(groupId, new TemplateSection(group[0].category), children);
m_TemplatesTree.Add(section);
}
m_ListOfTemplates.SetRootItems(m_TemplatesTree);
if (!lastSelectedTemplateFound)
{
m_LastSelectedTemplateGuid = fallBackTemplateAssetGuid;
}
}
private Texture2D GetSkinIcon(Texture2D templateIcon)
{
if (EditorGUIUtility.skinIndex == 0)
{
return templateIcon;
}
var path = AssetDatabase.GetAssetPath(templateIcon);
return EditorGUIUtility.LoadIcon(path);
}
private GraphViewTemplateDescriptor MakeEmptyTemplate()
{
return new GraphViewTemplateDescriptor
{
name = m_TemplateHelper.emptyTemplateName,
icon = EditorGUIUtility.LoadIcon(m_TemplateHelper.emptyTemplateIconPath),
thumbnail = EditorGUIUtility.LoadIcon(m_TemplateHelper.emptyTemplateScreenshotPath),
category = m_TemplateHelper.builtInCategory,
description = m_TemplateHelper.emptyTemplateDescription,
assetGuid = "empty",
};
}
}
}