forked from alelievr/NodeGraphProcessor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseGraphWindow.cs
More file actions
154 lines (124 loc) · 3.56 KB
/
Copy pathBaseGraphWindow.cs
File metadata and controls
154 lines (124 loc) · 3.56 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
using System.Linq;
using System;
using UnityEngine;
using UnityEditor;
using UnityEngine.UIElements;
using UnityEngine.SceneManagement;
using UnityEditor.SceneManagement;
namespace GraphProcessor
{
[System.Serializable]
public abstract class BaseGraphWindow : EditorWindow
{
protected VisualElement rootView;
protected BaseGraphView graphView;
[SerializeField]
protected BaseGraph graph;
readonly string graphWindowStyle = "GraphProcessorStyles/BaseGraphView";
public bool isGraphLoaded
{
get { return graphView != null && graphView.graph != null; }
}
bool reloadWorkaround = false;
public event Action< BaseGraph > graphLoaded;
public event Action< BaseGraph > graphUnloaded;
/// <summary>
/// Called by Unity when the window is enabled / opened
/// </summary>
protected virtual void OnEnable()
{
InitializeRootView();
if (graph != null)
LoadGraph();
else
reloadWorkaround = true;
}
protected virtual void Update()
{
// Workaround for the Refresh option of the editor window:
// When Refresh is clicked, OnEnable is called before the serialized data in the
// editor window is deserialized, causing the graph view to not be loaded
if (reloadWorkaround && graph != null)
{
LoadGraph();
reloadWorkaround = false;
}
}
void LoadGraph()
{
// We wait for the graph to be initialized
if (graph.isEnabled)
InitializeGraph(graph);
else
graph.onEnabled += () => InitializeGraph(graph);
}
/// <summary>
/// Called by Unity when the window is disabled (happens on domain reload)
/// </summary>
protected virtual void OnDisable()
{
if (graph != null && graphView != null)
graphView.SaveGraphToDisk();
}
/// <summary>
/// Called by Unity when the window is closed
/// </summary>
protected virtual void OnDestroy() { }
void InitializeRootView()
{
rootView = base.rootVisualElement;
rootView.name = "graphRootView";
rootView.styleSheets.Add(Resources.Load<StyleSheet>(graphWindowStyle));
}
public void InitializeGraph(BaseGraph graph)
{
if (this.graph != null && graph != this.graph)
{
// Save the graph to the disk
EditorUtility.SetDirty(this.graph);
AssetDatabase.SaveAssets();
// Unload the graph
graphUnloaded?.Invoke(this.graph);
}
graphLoaded?.Invoke(graph);
this.graph = graph;
if (graphView != null)
rootView.Remove(graphView);
//Initialize will provide the BaseGraphView
InitializeWindow(graph);
graphView = rootView.Children().FirstOrDefault(e => e is BaseGraphView) as BaseGraphView;
if (graphView == null)
{
Debug.LogError("GraphView has not been added to the BaseGraph root view !");
return ;
}
graphView.Initialize(graph);
InitializeGraphView(graphView);
// TOOD: onSceneLinked...
if (graph.IsLinkedToScene())
LinkGraphWindowToScene(graph.GetLinkedScene());
else
graph.onSceneLinked += LinkGraphWindowToScene;
}
void LinkGraphWindowToScene(Scene scene)
{
EditorSceneManager.sceneClosed += CloseWindowWhenSceneIsClosed;
void CloseWindowWhenSceneIsClosed(Scene closedScene)
{
if (scene == closedScene)
{
Close();
EditorSceneManager.sceneClosed -= CloseWindowWhenSceneIsClosed;
}
}
}
public virtual void OnGraphDeleted()
{
if (graph != null && graphView != null)
rootView.Remove(graphView);
graphView = null;
}
protected abstract void InitializeWindow(BaseGraph graph);
protected virtual void InitializeGraphView(BaseGraphView view) {}
}
}