From 79a65674cbdae485d7cbceeaf2d68bbaa78c1c13 Mon Sep 17 00:00:00 2001 From: Seneral Date: Tue, 4 Jul 2017 22:39:37 +0200 Subject: [PATCH] Added Node and Canvas Inspector GUI - Node Inspector calls DrawNodePropertyEditor and defaults to imitating the NodeGUI - Adjusted selected objects after click to either select a node or the canvas itself --- Editor/Node_Editor/CanvasInspector.cs | 73 +++++++++++++++++++ Editor/Node_Editor/NodeInspector.cs | 72 ++++++++++++++++++ Node_Editor/Framework/Core/ConnectionKnob.cs | 6 +- Node_Editor/Framework/Core/Node.cs | 18 ++++- .../Interface/NodeEditorInputSystem.cs | 10 ++- 5 files changed, 173 insertions(+), 6 deletions(-) create mode 100644 Editor/Node_Editor/CanvasInspector.cs create mode 100644 Editor/Node_Editor/NodeInspector.cs diff --git a/Editor/Node_Editor/CanvasInspector.cs b/Editor/Node_Editor/CanvasInspector.cs new file mode 100644 index 00000000..79de2264 --- /dev/null +++ b/Editor/Node_Editor/CanvasInspector.cs @@ -0,0 +1,73 @@ +using UnityEngine; +using UnityEditor; + +namespace NodeEditorFramework.Standard +{ + [CustomEditor(typeof(NodeCanvas), true)] + public class CanvasInspector : Editor + { + public static GUIStyle titleStyle; + public static GUIStyle subTitleStyle; + public static GUIStyle boldLabelStyle; + + public NodeCanvas canvas; + + public void OnEnable() + { + canvas = (NodeCanvas)target; + canvas.Validate(); + } + + public override void OnInspectorGUI() + { + if (canvas == null) + canvas = (NodeCanvas)target; + if (canvas == null) + return; + if (titleStyle == null) + { + titleStyle = new GUIStyle(GUI.skin.label); + titleStyle.fontStyle = FontStyle.Bold; + titleStyle.alignment = TextAnchor.MiddleCenter; + titleStyle.fontSize = 16; + } + if (subTitleStyle == null) + { + subTitleStyle = new GUIStyle(GUI.skin.label); + subTitleStyle.fontStyle = FontStyle.Bold; + subTitleStyle.alignment = TextAnchor.MiddleCenter; + subTitleStyle.fontSize = 12; + } + if (boldLabelStyle == null) + { + boldLabelStyle = new GUIStyle(GUI.skin.label); + boldLabelStyle.fontStyle = FontStyle.Bold; + } + + EditorGUI.BeginChangeCheck(); + + GUILayout.Space(10); + + GUILayout.Label(new GUIContent(canvas.saveName, canvas.savePath), titleStyle); + GUILayout.Label(canvas.livesInScene? "Scene Save" : "Asset Save", subTitleStyle); + GUILayout.Label("Type: " + canvas.canvasName, subTitleStyle); + + GUILayout.Space(10); + + if (GUILayout.Button("Open")) + { + string NodeCanvasPath = AssetDatabase.GetAssetPath(canvas); + NodeEditorWindow.OpenNodeEditor().canvasCache.LoadNodeCanvas(NodeCanvasPath); + } + + GUILayout.Space(10); + + GUILayout.Label("Nodes", boldLabelStyle); + foreach (Node node in canvas.nodes) + { + string label = node.Title; + EditorGUILayout.ObjectField(label, node, node.GetType(), true); + } + } + } +} diff --git a/Editor/Node_Editor/NodeInspector.cs b/Editor/Node_Editor/NodeInspector.cs new file mode 100644 index 00000000..fdf12ec2 --- /dev/null +++ b/Editor/Node_Editor/NodeInspector.cs @@ -0,0 +1,72 @@ +using UnityEngine; +using UnityEditor; +using NodeEditorFramework.Utilities; + +namespace NodeEditorFramework.Standard +{ + [CustomEditor(typeof(Node), true)] + public class NodeInspector : Editor + { + public static GUIStyle titleStyle; + public static GUIStyle boldLabelStyle; + public Node node; + + public void OnEnable() + { + node = (Node)target; + } + + public override void OnInspectorGUI() + { + if (node == null) + node = (Node)target; + if (node == null) + return; + if (titleStyle == null) + { + titleStyle = new GUIStyle(GUI.skin.label); + titleStyle.fontStyle = FontStyle.Bold; + titleStyle.alignment = TextAnchor.MiddleCenter; + titleStyle.fontSize = 16; + } + if (boldLabelStyle == null) + { + boldLabelStyle = new GUIStyle(GUI.skin.label); + boldLabelStyle.fontStyle = FontStyle.Bold; + } + + OverlayGUI.StartOverlayGUI("NodeInspector"); + + EditorGUI.BeginChangeCheck(); + + GUILayout.Space(10); + + GUILayout.Label(node.Title, titleStyle); + + GUILayout.Space(10); + + GUILayout.Label("Rect: " + node.rect.ToString()); + node.backgroundColor = EditorGUILayout.ColorField("Color", node.backgroundColor); + + GUILayout.Space(10); + + GUILayout.Label("Connection Ports", boldLabelStyle); + foreach (ConnectionPort port in node.connectionPorts) + { + string labelPrefix = port.direction == Direction.In ? "Input " : (port.direction == Direction.Out ? "Output " : ""); + string label = labelPrefix + port.styleID + " '" + port.name + "'"; + EditorGUILayout.ObjectField(label, port, port.GetType(), true); + } + + GUILayout.Space(10); + + GUILayout.Label("Property Editor", boldLabelStyle); + node.DrawNodePropertyEditor(); + + if (EditorGUI.EndChangeCheck()) + NodeEditor.RepaintClients(); + + OverlayGUI.EndOverlayGUI(); + } + } +} diff --git a/Node_Editor/Framework/Core/ConnectionKnob.cs b/Node_Editor/Framework/Core/ConnectionKnob.cs index 9fc0518f..834cd1ec 100644 --- a/Node_Editor/Framework/Core/ConnectionKnob.cs +++ b/Node_Editor/Framework/Core/ConnectionKnob.cs @@ -247,7 +247,7 @@ public void DisplayLayout (GUIContent content, GUIStyle style) /// public void SetPosition () { - if (Event.current.type != EventType.Repaint) + if (Event.current.type != EventType.Repaint || body.ignoreGUIKnobPlacement) return; Vector2 pos = GUILayoutUtility.GetLastRect ().center + body.contentOffset; SetPosition (side == NodeSide.Bottom || side == NodeSide.Top? pos.x : pos.y); @@ -258,6 +258,8 @@ public void SetPosition () /// public void SetPosition(float position, NodeSide nodeSide) { + if (body.ignoreGUIKnobPlacement) + return; if (side != nodeSide) { side = nodeSide; @@ -271,6 +273,8 @@ public void SetPosition(float position, NodeSide nodeSide) /// public void SetPosition(float position) { + if (body.ignoreGUIKnobPlacement) + return; sidePosition = position; } diff --git a/Node_Editor/Framework/Core/Node.cs b/Node_Editor/Framework/Core/Node.cs index 25058427..7d539eaa 100644 --- a/Node_Editor/Framework/Core/Node.cs +++ b/Node_Editor/Framework/Core/Node.cs @@ -29,6 +29,7 @@ public abstract partial class Node : ScriptableObject // Internal internal Vector2 contentOffset = Vector2.zero; internal Vector2 nodeGUIHeight; + internal bool ignoreGUIKnobPlacement; // Style public Color backgroundColor = Color.white; @@ -115,7 +116,22 @@ public virtual void NodeGUI () /// Used to display a custom node property editor in the GUI. /// By default shows the standard NodeGUI. /// - public virtual void DrawNodePropertyEditor () { NodeGUI (); } + public virtual void DrawNodePropertyEditor () + { + try + { // Draw Node GUI without disturbing knob placement + ignoreGUIKnobPlacement = true; + NodeEditorGUI.StartNodeGUI(false); + GUILayout.BeginVertical(GUI.skin.box); + NodeGUI(); + GUILayout.EndVertical(); + NodeEditorGUI.EndNodeGUI(); + } + finally + { // Be sure to always reset the state to not mess up other GUI code + ignoreGUIKnobPlacement = false; + } + } /// /// Calculates the outputs of this Node depending on the inputs. diff --git a/Node_Editor/Framework/Interface/NodeEditorInputSystem.cs b/Node_Editor/Framework/Interface/NodeEditorInputSystem.cs index 46b8a8a0..be6d81c4 100644 --- a/Node_Editor/Framework/Interface/NodeEditorInputSystem.cs +++ b/Node_Editor/Framework/Interface/NodeEditorInputSystem.cs @@ -251,11 +251,13 @@ private static void HandleSelecting (NodeEditorInputInfo inputInfo) unfocusControlsForState = state; state.selectedNode = state.focusedNode; NodeEditor.RepaintClients (); - #if UNITY_EDITOR - if (state.selectedNode != null) - UnityEditor.Selection.activeObject = state.selectedNode; - #endif } +#if UNITY_EDITOR + if (state.selectedNode != null) + UnityEditor.Selection.activeObject = state.selectedNode; + else + UnityEditor.Selection.activeObject = state.canvas; +#endif } // CONTEXT CLICKS