forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphViewToolWindow.cs
More file actions
186 lines (153 loc) · 6.34 KB
/
Copy pathGraphViewToolWindow.cs
File metadata and controls
186 lines (153 loc) · 6.34 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
// 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;
//using Toolbar = UnityEditor.UIElements.Toolbar;
namespace UnityEditor.Experimental.GraphView
{
public abstract class GraphViewToolWindow : EditorWindow
{
struct GraphViewChoice
{
public EditorWindow window;
public GraphView graphView;
public int idx;
}
protected UnityEditor.UIElements.Toolbar m_Toolbar;
ToolbarMenu m_SelectorMenu;
[SerializeField]
EditorWindow m_SelectedWindow;
[SerializeField]
int m_SelectedGraphViewIdx;
protected GraphView m_SelectedGraphView;
List<GraphViewChoice> m_GraphViewChoices;
bool m_FirstUpdate;
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);
root.Add(m_Toolbar);
m_FirstUpdate = true;
}
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);
}
}
void RefreshPanelChoices()
{
m_GraphViewChoices.Clear();
List<GUIView> guiViews = new List<GUIView>();
GUIViewDebuggerHelper.GetViews(guiViews);
var it = UIElementsUtility.GetPanelsIterator();
while (it.MoveNext())
{
GUIView view = guiViews.FirstOrDefault(v => v.GetInstanceID() == it.Current.Key);
if (view == null)
continue;
DockArea dockArea = view as DockArea;
if (dockArea == null)
continue;
foreach (var graphViewEditor in dockArea.m_Panes.OfType<GraphViewEditorWindow>())
{
int idx = 0;
foreach (var graphView in graphViewEditor.graphViews.Where(IsGraphViewSupported))
{
m_GraphViewChoices.Add(new GraphViewChoice {window = graphViewEditor, idx = idx++, graphView = 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 => ((GraphViewChoice)a.userData).graphView == m_SelectedGraphView ?
DropdownMenuAction.Status.Checked : DropdownMenuAction.Status.Normal,
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;
if (m_SelectedGraphView != null)
m_SelectorMenu.text = m_SelectedGraphView.name;
else
m_SelectorMenu.text = "Select a panel";
m_SelectedWindow = choice?.window;
m_SelectedGraphViewIdx = choice?.idx ?? -1;
OnGraphViewChanged();
}
// 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;
}
}
}