-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathProBuilderToolsContexts.cs
More file actions
449 lines (392 loc) · 16.2 KB
/
ProBuilderToolsContexts.cs
File metadata and controls
449 lines (392 loc) · 16.2 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
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.ProBuilder;
using UnityEngine.UIElements;
using UnityEditor.EditorTools;
using UnityEditor.ProBuilder.Actions;
using UnityEditor.ProBuilder.Overlays;
using UnityEditor.Actions;
using UnityEditor.ShortcutManagement;
namespace UnityEditor.ProBuilder
{
static class ProBuilderToolManager
{
public static Tool activeTool => Tools.current;
}
[Icon("Packages/com.unity.probuilder/Content/Icons/EditableMesh/EditMeshContext.png")]
[EditorToolContext("ProBuilder", typeof(ProBuilderMesh))]
public class PositionToolContext : EditorToolContext
{
internal class ProBuilderShortcutContext : IShortcutContext
{
public bool active
=> EditorWindow.focusedWindow is SceneView
&& ProBuilderEditor.instance != null;
}
ProBuilderEditor m_Editor;
ProBuilderEditor editor => m_Editor ??= new ProBuilderEditor();
ProBuilderShortcutContext m_ShortcutContext;
SceneInformationOverlay m_SceneInfoOverlay;
protected override Type GetEditorToolType(Tool tool)
{
switch(tool)
{
case Tool.Move:
return typeof(ProbuilderMoveTool);
case Tool.Rotate:
return typeof(ProbuilderRotateTool);
case Tool.Scale:
return typeof(ProbuilderScaleTool);
default:
return null;
}
}
static DropdownMenuAction.Status GetStatus(MenuAction action)
{
if(action.hidden)
return DropdownMenuAction.Status.Hidden;
if (action.enabled)
return DropdownMenuAction.Status.Normal;
return DropdownMenuAction.Status.Disabled;
}
static readonly HashSet<Type> k_ContextMenuBlacklist = new HashSet<Type>()
{
typeof(Actions.OpenSmoothingEditor),
typeof(Actions.OpenMaterialEditor),
typeof(Actions.OpenUVEditor),
typeof(Actions.OpenVertexColorEditor),
typeof(Actions.ToggleSelectBackFaces),
typeof(Actions.ToggleHandleOrientation),
typeof(Actions.ToggleDragRectMode),
typeof(Actions.ToggleXRay)
};
public override void PopulateMenu(DropdownMenu menu)
{
var actions = EditorToolbarLoader.GetActions();
var group = ToolbarGroup.Tool;
ContextMenuUtility.AddMenuItem(menu, "Edit/Select All","Select/Select All");
ContextMenuUtility.AddMenuItem(menu, "Edit/Deselect All","Select/Deselect All");
ContextMenuUtility.AddMenuItem(menu, "Edit/Invert Selection","Select/Invert Selection");
group = actions[0].group;
// grouping and filtering is bespoke for demo reasons
foreach (var action in actions)
{
if (k_ContextMenuBlacklist.Contains(action.GetType()))
continue;
if (action.group == ToolbarGroup.Entity || action.group == ToolbarGroup.Object || action.group == ToolbarGroup.Tool)
continue;
if (action.group != group)
{
menu.AppendSeparator();
group = action.group;
}
var title = action.menuTitle;
if (GetStatus(action) == DropdownMenuAction.Status.Normal || GetStatus(action) == DropdownMenuAction.Status.Disabled)
{
if (action.hasFileMenuEntry)
{
string path = EditorToolbarMenuItem.k_MenuPrefix + action.group + "/" + title;
ContextMenuUtility.AddMenuItem(menu, path, GetMenuTitle(action, title));
}
else if (action.optionsEnabled)
{
title = GetMenuTitle(action, title);
menu.AppendAction(title, _ => EditorAction.Start(new MenuActionSettings(action, HasPreview(action))), GetStatus(action));
}
else
menu.AppendAction(GetMenuTitle(action, title), _ => action.PerformAction());
}
}
var trs = Selection.transforms;
if (trs.GetComponents<MeshFilter>().Length > trs.GetComponents<ProBuilderMesh>().Length)
ContextMenuUtility.AddMenuItemsForType(menu, typeof(MeshFilter), targets, "Mesh Filter");
}
static bool HasPreview(MenuAction action)
{
return !(action is DetachFaces || action is DuplicateFaces);
}
string GetMenuTitle(MenuAction action, string title)
{
// Geometry and Tool groups are not displayed in the menu
if (action.group != ToolbarGroup.Geometry && action.group != ToolbarGroup.Tool)
{
//STO-3001: For a better UX, Selection group is renamed to Select so that users don't think this is
//acting on the current selection
var groupName = action.group == ToolbarGroup.Selection ? "ProBuilder Select" : action.group.ToString();
title = $"{groupName}/{action.menuTitle}";
}
return title;
}
public override void OnActivated()
{
m_Editor = new ProBuilderEditor();
ProBuilderSettings.instance.afterSettingsSaved += UpdateSceneInfoOverlay;
UpdateSceneInfoOverlay();
ShortcutManager.RegisterContext(m_ShortcutContext ??= new ProBuilderShortcutContext());
}
public override void OnWillBeDeactivated()
{
m_Editor.Dispose();
ProBuilderSettings.instance.afterSettingsSaved -= UpdateSceneInfoOverlay;
if(m_SceneInfoOverlay != null)
SceneView.RemoveOverlayFromActiveView(m_SceneInfoOverlay);
ShortcutManager.UnregisterContext(m_ShortcutContext);
}
void UpdateSceneInfoOverlay()
{
if (ProBuilderEditor.s_ShowSceneInfo)
{
if(m_SceneInfoOverlay == null)
m_SceneInfoOverlay = new SceneInformationOverlay();
SceneView.AddOverlayToActiveView(m_SceneInfoOverlay);
}
else if(m_SceneInfoOverlay != null)
{
SceneView.RemoveOverlayFromActiveView(m_SceneInfoOverlay);
}
}
public override void OnToolGUI(EditorWindow window)
{
if (!(window is SceneView view))
return;
editor.OnSceneGUI(view);
}
// This boolean allows to call the action only once in case of multi-selection as PB actions
// are called on the entire selection and not per element.
static bool s_ActionAlreadyTriggered = false;
[MenuItem("CONTEXT/ProBuilderMesh/Conform Normals", true)]
static bool ValidateConformObjectNormalsAction()
{
return MeshSelection.selectedObjectCount > 0;
}
[MenuItem("CONTEXT/ProBuilderMesh/Conform Normals", false, 11)]
static void ConformObjectNormalsAction(MenuCommand command)
{
if (!s_ActionAlreadyTriggered)
{
s_ActionAlreadyTriggered = true;
//Once again, delayCall is necessary to prevent multiple call in case of multi-selection
EditorApplication.delayCall += () =>
{
EditorToolbarLoader.GetInstance<ConformObjectNormals>().PerformAction();
s_ActionAlreadyTriggered = false;
};
}
}
[MenuItem("CONTEXT/ProBuilderMesh/Export", true)]
public static bool ValidateExportAction()
{
return MeshSelection.selectedObjectCount > 0;
}
[MenuItem("CONTEXT/ProBuilderMesh/Export", false, 12)]
public static void ExportAction(MenuCommand command)
{
EditorToolbarLoader.GetInstance<Export>().PerformAltAction();
}
[MenuItem("CONTEXT/ProBuilderMesh/Triangulate", true)]
public static bool ValidateTriangulateObjectAction()
{
return MeshSelection.selectedObjectCount > 0;
}
[MenuItem("CONTEXT/ProBuilderMesh/Triangulate", false, 13)]
public static void TriangulateObjectAction(MenuCommand command)
{
if (!s_ActionAlreadyTriggered)
{
s_ActionAlreadyTriggered = true;
//Once again, delayCall is necessary to prevent multiple call in case of multi-selection
EditorApplication.delayCall += () =>
{
EditorToolbarLoader.GetInstance<TriangulateObject>().PerformAction();
s_ActionAlreadyTriggered = false;
};
}
}
[MenuItem("CONTEXT/ProBuilderMesh/Center Pivot", true)]
static bool ValidateCenterPivotAction()
{
return MeshSelection.selectedObjectCount > 0;
}
[MenuItem("CONTEXT/ProBuilderMesh/Center Pivot", false, 14)]
static void CenterPivotAction(MenuCommand command)
{
if (!s_ActionAlreadyTriggered)
{
s_ActionAlreadyTriggered = true;
//Once again, delayCall is necessary to prevent multiple call in case of multi-selection
EditorApplication.delayCall += () =>
{
EditorToolbarLoader.GetInstance<CenterPivot>().PerformAction();
s_ActionAlreadyTriggered = false;
};
}
}
[MenuItem("CONTEXT/ProBuilderMesh/Flip Normals", true)]
static bool ValidateFlipNormalsAction()
{
return MeshSelection.selectedObjectCount > 0;
}
[MenuItem("CONTEXT/ProBuilderMesh/Flip Normals", false, 16)]
static void FlipNormalsAction(MenuCommand command)
{
if (!s_ActionAlreadyTriggered)
{
s_ActionAlreadyTriggered = true;
//Once again, delayCall is necessary to prevent multiple call in case of multi-selection
EditorApplication.delayCall += () =>
{
EditorToolbarLoader.GetInstance<FlipObjectNormals>().PerformAction();
s_ActionAlreadyTriggered = false;
};
}
}
[MenuItem("CONTEXT/ProBuilderMesh/Subdivide Object", true)]
public static bool ValidateSubdivideObjectAction()
{
return MeshSelection.selectedObjectCount > 0;
}
[MenuItem("CONTEXT/ProBuilderMesh/Subdivide Object", false, 15)]
public static void SubdivideObjectAction(MenuCommand command)
{
if (!s_ActionAlreadyTriggered)
{
s_ActionAlreadyTriggered = true;
//Once again, delayCall is necessary to prevent multiple call in case of multi-selection
EditorApplication.delayCall += () =>
{
EditorToolbarLoader.GetInstance<SubdivideObject>().PerformAction();
s_ActionAlreadyTriggered = false;
};
}
}
[MenuItem("CONTEXT/ProBuilderMesh/Mirror Objects", true)]
static bool ValidateMirrorObjectAction()
{
return MeshSelection.selectedObjectCount > 0;
}
[MenuItem("CONTEXT/ProBuilderMesh/Mirror Objects", false, 17)]
static void MirrorObjectAction(MenuCommand command)
{
if (!s_ActionAlreadyTriggered)
{
s_ActionAlreadyTriggered = true;
//Once again, delayCall is necessary to prevent multiple call in case of multi-selection
EditorApplication.delayCall += () =>
{
EditorAction.Start(new MenuActionSettings(EditorToolbarLoader.GetInstance<MirrorObjects>(), true));
s_ActionAlreadyTriggered = false;
};
}
}
[MenuItem("CONTEXT/ProBuilderMesh/Merge Objects", true)]
static bool ValidateMergeObjectsAction()
{
return MeshSelection.selectedObjectCount > 1 && MeshSelection.activeMesh != null;
}
[MenuItem("CONTEXT/ProBuilderMesh/Merge Objects", false, 18)]
static void MergeObjectsAction(MenuCommand command)
{
if (!s_ActionAlreadyTriggered)
{
s_ActionAlreadyTriggered = true;
//Once again, delayCall is necessary to prevent multiple call in case of multi-selection
EditorApplication.delayCall += () =>
{
EditorToolbarLoader.GetInstance<MergeObjects>().PerformAction();
s_ActionAlreadyTriggered = false;
};
}
}
[MenuItem("CONTEXT/ProBuilderMesh/Freeze Transform", true)]
static bool ValidateFreezeTransformAction()
{
return MeshSelection.selectedObjectCount > 0;
}
[MenuItem("CONTEXT/ProBuilderMesh/Freeze Transform", false, 19)]
static void FreezeTransformAction(MenuCommand command)
{
if (!s_ActionAlreadyTriggered)
{
s_ActionAlreadyTriggered = true;
//Once again, delayCall is necessary to prevent multiple call in case of multi-selection
EditorApplication.delayCall += () =>
{
EditorToolbarLoader.GetInstance<FreezeTransform>().PerformAction();
s_ActionAlreadyTriggered = false;
};
}
}
[MenuItem("CONTEXT/ProBuilderMesh/Set Trigger", true)]
static bool ValidateSetTriggerAction()
{
return MeshSelection.selectedObjectCount > 0;
}
[MenuItem("CONTEXT/ProBuilderMesh/Set Trigger", false, 20)]
static void SetTriggerAction(MenuCommand command)
{
if (!s_ActionAlreadyTriggered)
{
s_ActionAlreadyTriggered = true;
//Once again, delayCall is necessary to prevent multiple call in case of multi-selection
EditorApplication.delayCall += () =>
{
EditorToolbarLoader.GetInstance<SetTrigger>().PerformAction();
s_ActionAlreadyTriggered = false;
};
}
}
[MenuItem("CONTEXT/ProBuilderMesh/Set Collider", true)]
static bool ValidateSetColliderAction()
{
return MeshSelection.selectedObjectCount > 0;
}
[MenuItem("CONTEXT/ProBuilderMesh/Set Collider", false, 21)]
static void SetColliderAction(MenuCommand command)
{
if (!s_ActionAlreadyTriggered)
{
s_ActionAlreadyTriggered = true;
//Once again, delayCall is necessary to prevent multiple call in case of multi-selection
EditorApplication.delayCall += () =>
{
EditorToolbarLoader.GetInstance<SetCollider>().PerformAction();
s_ActionAlreadyTriggered = false;
};
}
}
}
public class TextureToolContext : EditorToolContext
{
ProBuilderEditor m_Editor;
TextureToolContext() { }
protected override Type GetEditorToolType(Tool tool)
{
switch(tool)
{
case Tool.Move:
return typeof(TextureMoveTool);
case Tool.Rotate:
return typeof(TextureRotateTool);
case Tool.Scale:
return typeof(TextureScaleTool);
default:
return null;
}
}
public override void OnActivated()
{
m_Editor = new ProBuilderEditor();
}
public override void OnWillBeDeactivated()
{
m_Editor.Dispose();
}
public override void OnToolGUI(EditorWindow window)
{
if (!(window is SceneView view))
return;
m_Editor.OnSceneGUI(view);
}
}
}