This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 417
/
Copy pathRTNodeEditor.cs
108 lines (92 loc) · 2.89 KB
/
RTNodeEditor.cs
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
using UnityEngine;
using NodeEditorFramework.Utilities;
namespace NodeEditorFramework.Standard
{
/// <summary>
/// Example of displaying the Node Editor at runtime including GUI
/// </summary>
public class RTNodeEditor : MonoBehaviour
{
// Startup-canvas, cache and interface
public NodeCanvas assetSave;
public string sceneSave;
private NodeEditorUserCache canvasCache;
private NodeEditorInterface editorInterface;
// GUI rects
public bool fullscreen = false;
public Rect canvasRect = new Rect(50, 50, 900, 400);
public Rect rect { get { return fullscreen ? new Rect(0, 0, Screen.width, Screen.height) : canvasRect; } }
private void Start ()
{
NormalReInit();
}
private void Update ()
{
NodeEditor.Update ();
}
private void NormalReInit()
{
NodeEditor.ReInit(false);
AssureSetup();
if (canvasCache.nodeCanvas)
canvasCache.nodeCanvas.Validate();
}
private void AssureSetup()
{
if (canvasCache == null)
{ // Create cache and load startup-canvas
canvasCache = new NodeEditorUserCache();
if (assetSave != null)
canvasCache.SetCanvas(NodeEditorSaveManager.CreateWorkingCopy(assetSave));
else if (!string.IsNullOrEmpty (sceneSave))
canvasCache.LoadSceneNodeCanvas(sceneSave);
}
canvasCache.AssureCanvas();
if (editorInterface == null)
{ // Setup editor interface
editorInterface = new NodeEditorInterface();
editorInterface.canvasCache = canvasCache;
}
}
private void OnGUI ()
{
// Initiation
NodeEditor.checkInit(true);
if (NodeEditor.InitiationError)
{
GUILayout.Label("Node Editor Initiation failed! Check console for more information!");
return;
}
AssureSetup();
// Start Overlay GUI for popups (before any other GUI)
OverlayGUI.StartOverlayGUI("RTNodeEditor");
// Set root rect (can be any number of arbitrary groups, e.g. a nested UI, but at least one)
GUI.BeginGroup(new Rect(0, 0, Screen.width, Screen.height));
// Begin Node Editor GUI and set canvas rect
NodeEditorGUI.StartNodeGUI(false);
canvasCache.editorState.canvasRect = new Rect (rect.x, rect.y + editorInterface.toolbarHeight, rect.width, rect.height - editorInterface.toolbarHeight);
try
{ // Perform drawing with error-handling
NodeEditor.DrawCanvas (canvasCache.nodeCanvas, canvasCache.editorState);
}
catch (UnityException e)
{ // On exceptions in drawing flush the canvas to avoid locking the UI
canvasCache.NewNodeCanvas ();
NodeEditor.ReInit (true);
Debug.LogError ("Unloaded Canvas due to exception in Draw!");
Debug.LogException (e);
}
// Draw Interface
GUILayout.BeginArea(rect);
editorInterface.DrawToolbarGUI();
GUILayout.EndArea();
editorInterface.DrawModalPanel();
// End Node Editor GUI
NodeEditorGUI.EndNodeGUI();
// End root rect
GUI.EndGroup();
// End Overlay GUI and draw popups
OverlayGUI.EndOverlayGUI();
}
}
}