From 432ce05bb5b2a2042a91ed8898aef250689febc4 Mon Sep 17 00:00:00 2001 From: Thor Kramer Brigsted Date: Fri, 3 Nov 2017 10:11:07 +0100 Subject: [PATCH] Fixed dynamic node width. Fixed field widths. --- Scripts/Editor/NodeEditorAction.cs | 4 +++- Scripts/Editor/NodeEditorGUI.cs | 15 ++++++++++----- Scripts/Editor/NodeEditorGUILayout.cs | 14 ++++---------- Scripts/Editor/NodeEditorWindow.cs | 2 ++ 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index f4fcdb7..8db4d10 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -137,7 +137,9 @@ public partial class NodeEditorWindow { Vector2 mousePos = Event.current.mousePosition; //Get node position Vector2 nodePos = GridToWindowPosition(node.position); - Rect windowRect = new Rect(nodePos, new Vector2(200 / zoom, 30 / zoom)); + float width = 200; + if (nodeWidths.ContainsKey(node)) width = nodeWidths[node]; + Rect windowRect = new Rect(nodePos, new Vector2(width / zoom, 30 / zoom)); return windowRect.Contains(mousePos); } } \ No newline at end of file diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 879c95c..6032fe2 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -117,7 +117,7 @@ public partial class NodeEditorWindow { //If a null node is found, return. This can happen if the nodes associated script is deleted. It is currently not possible in Unity to delete a null asset. if (node == null) continue; - foreach(NodePort output in node.Outputs) { + foreach (NodePort output in node.Outputs) { //Needs cleanup. Null checks are ugly if (!portConnectionPoints.ContainsKey(output)) continue; Vector2 from = _portConnectionPoints[output].center; @@ -136,7 +136,10 @@ public partial class NodeEditorWindow { private void DrawNodes() { Event e = Event.current; - if (e.type == EventType.Repaint) portConnectionPoints.Clear(); + if (e.type == EventType.Repaint) { + portConnectionPoints.Clear(); + nodeWidths.Clear(); + } //Selected node is hashed before and after node GUI to detect changes int nodeHash = 0; @@ -186,6 +189,8 @@ public partial class NodeEditorWindow { } if (e.type == EventType.Repaint) { + nodeWidths.Add(node, nodeEditor.GetWidth()); + foreach (var kvp in NodeEditor.portPositions) { Vector2 portHandlePos = kvp.Value; portHandlePos += node.position; @@ -204,14 +209,14 @@ public partial class NodeEditorWindow { //Check if we are hovering any of this nodes ports //Check input ports - foreach(NodePort input in node.Inputs) { + foreach (NodePort input in node.Inputs) { //Check if port rect is available if (!portConnectionPoints.ContainsKey(input)) continue; Rect r = GridToWindowRect(portConnectionPoints[input]); if (r.Contains(mousePos)) hoveredPort = input; } //Check all output ports - foreach(NodePort output in node.Outputs) { + foreach (NodePort output in node.Outputs) { //Check if port rect is available if (!portConnectionPoints.ContainsKey(output)) continue; Rect r = GridToWindowRect(portConnectionPoints[output]); @@ -274,7 +279,7 @@ public partial class NodeEditorWindow { else { string s = TypeToString(elementType); int i = s.IndexOf('['); - return s.Substring(0,i) + "["+rank+"]" + s.Substring(i); + return s.Substring(0, i) + "[" + rank + "]" + s.Substring(i); } } else return hoveredPort.ValueType.ToString(); } diff --git a/Scripts/Editor/NodeEditorGUILayout.cs b/Scripts/Editor/NodeEditorGUILayout.cs index 6c97ceb..8040942 100644 --- a/Scripts/Editor/NodeEditorGUILayout.cs +++ b/Scripts/Editor/NodeEditorGUILayout.cs @@ -14,10 +14,8 @@ public static class NodeEditorGUILayout { Node node = property.serializedObject.targetObject as Node; NodePort port = node.GetPort(property.name); - float temp_labelWidth = EditorGUIUtility.labelWidth; - // If property is not a port, display a regular property field - if (port == null) EditorGUILayout.PropertyField(property, includeChildren); + if (port == null) EditorGUILayout.PropertyField(property, includeChildren, GUILayout.MinWidth(30)); else { Rect rect = new Rect(); @@ -26,12 +24,12 @@ public static class NodeEditorGUILayout { // Display a label if port is connected if (port.IsConnected) EditorGUILayout.LabelField(property.displayName); // Display an editable property field if port is not connected - else EditorGUILayout.PropertyField(property, includeChildren); + else EditorGUILayout.PropertyField(property, includeChildren, GUILayout.MinWidth(30)); rect = GUILayoutUtility.GetLastRect(); rect.position = rect.position - new Vector2(16, 0); // If property is an output, display a text label and put a port handle on the right side } else if (port.direction == NodePort.IO.Output) { - EditorGUILayout.LabelField(property.displayName, NodeEditorResources.styles.outputPort); + EditorGUILayout.LabelField(property.displayName, NodeEditorResources.styles.outputPort, GUILayout.MinWidth(30)); rect = GUILayoutUtility.GetLastRect(); rect.position = rect.position + new Vector2(rect.width, 0); } @@ -45,14 +43,11 @@ public static class NodeEditorGUILayout { if (NodeEditor.portPositions.ContainsKey(port)) NodeEditor.portPositions[port] = portPos; else NodeEditor.portPositions.Add(port, portPos); } - EditorGUIUtility.labelWidth = temp_labelWidth; } public static void PortField(NodePort port) { if (port == null) return; - float temp_labelWidth = EditorGUIUtility.labelWidth; - - EditorGUILayout.LabelField(port.fieldName.PrettifyCamelCase()); + EditorGUILayout.LabelField(port.fieldName.PrettifyCamelCase(), GUILayout.MinWidth(30)); Rect rect = GUILayoutUtility.GetLastRect(); if (port.direction == NodePort.IO.Input) rect.position = rect.position - new Vector2(16, 0); @@ -65,7 +60,6 @@ public static class NodeEditorGUILayout { Vector2 portPos = rect.center; if (NodeEditor.portPositions.ContainsKey(port)) NodeEditor.portPositions[port] = portPos; else NodeEditor.portPositions.Add(port, portPos); - EditorGUIUtility.labelWidth = temp_labelWidth; } private static void DrawPortHandle(Rect rect, Type type) { diff --git a/Scripts/Editor/NodeEditorWindow.cs b/Scripts/Editor/NodeEditorWindow.cs index 6b8a242..3cc305e 100644 --- a/Scripts/Editor/NodeEditorWindow.cs +++ b/Scripts/Editor/NodeEditorWindow.cs @@ -13,6 +13,8 @@ public partial class NodeEditorWindow : EditorWindow { /// Stores node positions for all nodePorts. public Dictionary portConnectionPoints { get { return _portConnectionPoints; } } private Dictionary _portConnectionPoints = new Dictionary(); + public Dictionary nodeWidths { get { return _nodeWidths; } } + private Dictionary _nodeWidths = new Dictionary(); public NodeGraph graph; public Vector2 panOffset { get { return _panOffset; } set { _panOffset = value; Repaint(); } } private Vector2 _panOffset;