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
|
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using NodeEditorFramework.Utilities;
using UNEB.Utility;
namespace UNEB
{
public class NodeEditorWindow : EditorWindow
{
[MenuItem("Window/Node Editor")]
static void Init()
{
var w = EditorWindow.CreateInstance<NodeEditorWindow>();
w.titleContent = new GUIContent("Node Editor");
w.Show();
}
public const float kToolbarHeight = 20f;
public const float kToolbarButtonWidth = 50f;
[SerializeField]
public NodeGraph graph;
public NodeEditor editor;
public ActionManager actions;
public ActionTriggerSystem triggers;
public NodeEditorState state;
private SaveManager _saveManager;
public enum Mode { Edit, View };
private Mode _mode = Mode.Edit;
void OnEnable()
{
GUIScaleUtility.CheckInit();
TextureLib.LoadStandardTextures();
actions = new ActionManager(this);
editor = new NodeEditor(this);
triggers = new ActionTriggerSystem(actions);
state = new NodeEditorState();
_saveManager = new SaveManager(this);
editor.graph = graph;
// Make sure that changes from the undo system are immediately
// updated in the window. If not, the undo changes will be
// visually delayed.
actions.OnUndo += Repaint;
actions.OnRedo += Repaint;
// Always start in edit mode.
// The only way it can be in view mode is if the window is
// already opened and the user selects a some graph.
_mode = Mode.Edit;
editor.HomeView();
}
void OnDisable()
{
_saveManager.Cleanup();
}
void OnDestroy()
{
cleanup();
}
void OnGUI()
{
// Asset removed.
if (!graph && !_saveManager.IsInNographState()) {
_saveManager.InitState();
}
editor.Draw();
drawToolbar();
// This must go after draw calls or there can be
// GUI layout errors.
triggers.Update();
}
public void SetGraph(NodeGraph g, Mode mode = Mode.Edit)
{
graph = g;
editor.graph = g;
// Reset Undo and Redo buffers.
actions.Reset();
_mode = mode;
}
private void cleanup()
{
if (actions != null) {
actions.OnUndo -= Repaint;
actions.OnRedo -= Repaint;
}
actions.Reset();
_saveManager.Cleanup();
}
private void drawToolbar()
{
EditorGUILayout.BeginHorizontal("Toolbar");
if (DropdownButton("File", kToolbarButtonWidth)) {
createFileMenu();
}
if (DropdownButton("Edit", kToolbarButtonWidth)) {
createEditMenu();
}
if (DropdownButton("View", kToolbarButtonWidth)) {
createViewMenu();
}
if (DropdownButton("Settings", kToolbarButtonWidth + 10f)) {
createSettingsMenu();
}
if (DropdownButton("Tools", kToolbarButtonWidth)) {
createToolsMenu();
}
// Make the toolbar extend all throughout the window extension.
GUILayout.FlexibleSpace();
drawGraphName();
EditorGUILayout.EndHorizontal();
}
private void drawGraphName()
{
string graphName = "None";
if (graph != null) {
graphName = graph.name;
}
GUILayout.Label(graphName);
}
private void createFileMenu()
{
var menu = new GenericMenu();
menu.AddItem(new GUIContent("Create New"), false, _saveManager.RequestNew);
menu.AddItem(new GUIContent("Load"), false, _saveManager.RequestLoad);
menu.AddSeparator("");
menu.AddItem(new GUIContent("Save"), false, _saveManager.RequestSave);
menu.AddItem(new GUIContent("Save As"), false, _saveManager.RequestSaveAs);
menu.DropDown(new Rect(5f, kToolbarHeight, 0f, 0f));
}
private void createEditMenu()
{
var menu = new GenericMenu();
menu.AddItem(new GUIContent("Undo"), false, actions.UndoAction);
menu.AddItem(new GUIContent("Redo"), false, actions.RedoAction);
menu.DropDown(new Rect(55f, kToolbarHeight, 0f, 0f));
}
private void createViewMenu()
{
var menu = new GenericMenu();
menu.AddItem(new GUIContent("Home"), false, editor.HomeView);
menu.AddItem(new GUIContent("Zoom In"), false, () => { editor.Zoom(-1); });
menu.AddItem(new GUIContent("Zoom Out"), false, () => { editor.Zoom(1); });
menu.DropDown(new Rect(105f, kToolbarHeight, 0f, 0f));
}
private void createSettingsMenu()
{
var menu = new GenericMenu();
menu.AddItem(new GUIContent("Show Guide"), editor.bDrawGuide, editor.ToggleDrawGuide);
menu.DropDown(new Rect(155f, kToolbarHeight, 0f, 0f));
}
private void createToolsMenu()
{
var menu = new GenericMenu();
menu.AddItem(new GUIContent("Add Test Nodes"), false, addTestNodes);
menu.AddItem(new GUIContent("Clear Nodes"), false, clearNodes);
menu.DropDown(new Rect(215f, kToolbarHeight, 0f, 0f));
}
public bool DropdownButton(string name, float width)
{
return GUILayout.Button(name, EditorStyles.toolbarDropDown, GUILayout.Width(width));
}
private void addTestNodes()
{
if (graph) {
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) {
var node = SaveManager.CreateNode<BasicNode>(graph);
float xpos = x * Node.kDefaultSize.x * 1.5f;
float ypos = y * Node.kDefaultSize.y * 1.5f;
node.bodyRect.position = new Vector2(xpos, ypos);
}
}
}
}
private void clearNodes()
{
if (graph) {
foreach (var node in graph.nodes) {
ScriptableObject.DestroyImmediate(node, true);
}
actions.Reset();
graph.nodes.Clear();
}
}
/// <summary>
/// The size of the window.
/// </summary>
public Rect Size
{
get { return new Rect(Vector2.zero, position.size); }
}
/// <summary>
/// The rect used to filter input.
/// This is so the toolbar is not ignored by editor inputs.
/// </summary>
public Rect InputRect
{
get
{
var rect = Size;
rect.y += kToolbarHeight;
rect.height -= kToolbarHeight;
return rect;
}
}
public Mode GetMode()
{
return _mode;
}
/// <summary>
/// Opens up the node editor window from asset selection.
/// </summary>
/// <param name="instanceID"></param>
/// <param name="line"></param>
/// <returns></returns>
[OnOpenAsset(1)]
private static bool OpenGraphAsset(int instanceID, int line)
{
var graphSelected = EditorUtility.InstanceIDToObject(instanceID) as NodeGraph;
if (graphSelected != null) {
NodeEditorWindow windowToUse = null;
// Try to find an editor window without a graph...
var windows = Resources.FindObjectsOfTypeAll<NodeEditorWindow>();
foreach (var w in windows) {
// The canvas is already opened
if (w.graph == graphSelected) {
return false;
}
// Found a window with no active canvas.
if (w.graph == null) {
windowToUse = w;
break;
}
}
// No windows available...just make a new one.
if (!windowToUse) {
windowToUse = EditorWindow.CreateInstance<NodeEditorWindow>();
windowToUse.titleContent = new GUIContent("Node Editor");
windowToUse.Show();
}
windowToUse.SetGraph(graphSelected);
windowToUse._saveManager.InitState();
windowToUse.Repaint();
return true;
}
return false;
}
}
}
|