-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathGraphViewToolWindow.cs
More file actions
234 lines (194 loc) · 8.26 KB
/
GraphViewToolWindow.cs
File metadata and controls
234 lines (194 loc) · 8.26 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
// 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 System.Reflection;
using UnityEngine;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
namespace UnityEditor.Experimental.GraphView
{
public abstract class GraphViewToolWindow : EditorWindow
{
struct GraphViewChoice
{
public EditorWindow window;
public GraphView graphView;
public int idx;
public bool canUse;
}
const string k_DefaultSelectorName = "Select a panel";
UnityEditor.UIElements.Toolbar m_Toolbar;
protected VisualElement m_ToolbarContainer;
ToolbarMenu m_SelectorMenu;
[SerializeField]
EditorWindow m_SelectedWindow;
[SerializeField]
int m_SelectedGraphViewIdx;
protected GraphView m_SelectedGraphView;
List<GraphViewChoice> m_GraphViewChoices;
bool m_FirstUpdate;
protected abstract string ToolName { get; }
public override IEnumerable<Type> GetExtraPaneTypes()
{
return Assembly
.GetAssembly(typeof(GraphViewToolWindow))
.GetTypes()
.Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(GraphViewToolWindow)));
}
public void SelectGraphViewFromWindow(GraphViewEditorWindow window, GraphView graphView, int graphViewIndexInWindow = 0)
{
var gvChoice = new GraphViewChoice { window = window, graphView = graphView, idx = graphViewIndexInWindow };
SelectGraphView(gvChoice);
}
protected void OnEnable()
{
var root = rootVisualElement;
this.SetAntiAliasing(4);
m_Toolbar = new UnityEditor.UIElements.Toolbar();
// Register panel choice refresh on the toolbar so the event
// is received before the ToolbarPopup clickable handle it.
m_Toolbar.RegisterCallback<MouseDownEvent>(e =>
{
if (e.target == m_SelectorMenu)
RefreshPanelChoices();
}, TrickleDown.TrickleDown);
m_GraphViewChoices = new List<GraphViewChoice>();
m_SelectorMenu = new ToolbarMenu { name = "panelSelectPopup", text = "Select a panel" };
var menu = m_SelectorMenu.menu;
menu.AppendAction("None", OnSelectGraphView,
a => m_SelectedGraphView == null ?
DropdownMenuAction.Status.Checked : DropdownMenuAction.Status.Normal);
m_Toolbar.Add(m_SelectorMenu);
m_Toolbar.style.flexGrow = 1;
m_Toolbar.style.overflow = Overflow.Hidden;
m_ToolbarContainer = new VisualElement();
m_ToolbarContainer.style.flexDirection = FlexDirection.Row;
m_ToolbarContainer.Add(m_Toolbar);
root.Add(m_ToolbarContainer);
m_FirstUpdate = true;
titleContent.text = ToolName;
}
void Update()
{
// We need to wait until all the windows are created to re-assign a potential graphView.
if (m_FirstUpdate)
{
m_FirstUpdate = false;
if (m_SelectedWindow != null)
{
var graphViewEditor = m_SelectedWindow as GraphViewEditorWindow;
if (graphViewEditor != null && m_SelectedGraphViewIdx >= 0 && m_SelectedGraphView == null)
{
m_SelectedGraphView = graphViewEditor.graphViews.ElementAt(m_SelectedGraphViewIdx);
m_SelectorMenu.text = m_SelectedGraphView.name;
OnGraphViewChanged();
}
}
}
else
{
if (!m_SelectedWindow && m_SelectedGraphView != null)
SelectGraphView(null);
}
UpdateGraphViewName();
}
void RefreshPanelChoices()
{
m_GraphViewChoices.Clear();
var guiViews = new List<GUIView>();
GUIViewDebuggerHelper.GetViews(guiViews);
var usedGraphViews = new HashSet<GraphView>();
// Get all GraphViews used by existing tool windows of our type
using (var it = UIElementsUtility.GetPanelsIterator())
{
var currentWindowType = GetType();
while (it.MoveNext())
{
var dockArea = guiViews.FirstOrDefault(v => v.GetInstanceID() == it.Current.Key) as DockArea;
if (dockArea == null)
continue;
foreach (var graphViewTool in dockArea.m_Panes.Where(p => p.GetType() == currentWindowType).Cast<GraphViewToolWindow>())
{
if (graphViewTool.m_SelectedGraphView != null)
{
usedGraphViews.Add(graphViewTool.m_SelectedGraphView);
}
}
}
}
// Get all the existing GraphViewWindows we could use...
using (var it = UIElementsUtility.GetPanelsIterator())
{
while (it.MoveNext())
{
var dockArea = guiViews.FirstOrDefault(v => v.GetInstanceID() == it.Current.Key) as DockArea;
if (dockArea == null)
continue;
foreach (var graphViewWindow in dockArea.m_Panes.OfType<GraphViewEditorWindow>())
{
int idx = 0;
foreach (var graphView in graphViewWindow.graphViews.Where(IsGraphViewSupported))
{
m_GraphViewChoices.Add(new GraphViewChoice {window = graphViewWindow, idx = idx++, graphView = graphView, canUse = !usedGraphViews.Contains(graphView)});
}
}
}
}
var menu = m_SelectorMenu.menu;
var menuItemsCount = menu.MenuItems().Count;
// Clear previous items (but not the "none" one at the top of the list)
for (int i = menuItemsCount - 1; i > 0; i--)
menu.RemoveItemAt(i);
foreach (var graphView in m_GraphViewChoices)
{
menu.AppendAction(graphView.graphView.name, OnSelectGraphView,
a =>
{
var gvc = (GraphViewChoice)a.userData;
return (gvc.graphView == m_SelectedGraphView
? DropdownMenuAction.Status.Checked
: (gvc.canUse
? DropdownMenuAction.Status.Normal
: DropdownMenuAction.Status.Disabled));
},
graphView);
}
}
void OnSelectGraphView(DropdownMenuAction action)
{
var choice = (GraphViewChoice?)action.userData;
var newlySelectedGraphView = choice?.graphView;
if (newlySelectedGraphView == m_SelectedGraphView)
return;
SelectGraphView(choice);
}
void SelectGraphView(GraphViewChoice? choice)
{
OnGraphViewChanging();
m_SelectedGraphView = choice?.graphView;
m_SelectedWindow = choice?.window;
m_SelectedGraphViewIdx = choice?.idx ?? -1;
OnGraphViewChanged();
UpdateGraphViewName();
}
// Called just before the change.
protected abstract void OnGraphViewChanging();
// Called just after the change.
protected abstract void OnGraphViewChanged();
protected virtual bool IsGraphViewSupported(GraphView gv)
{
return false;
}
void UpdateGraphViewName()
{
string updatedName = k_DefaultSelectorName;
if (m_SelectedGraphView != null)
updatedName = m_SelectedGraphView.name;
if (m_SelectorMenu.text != updatedName)
m_SelectorMenu.text = updatedName;
}
}
}