diff --git a/Example/ExampleNodeGraph.asset b/Example/ExampleNodeGraph.asset index 9650943..7b03462 100644 Binary files a/Example/ExampleNodeGraph.asset and b/Example/ExampleNodeGraph.asset differ diff --git a/Scripts/Editor/NodeEditor.cs b/Scripts/Editor/NodeEditor.cs index 9225928..cdba07d 100644 --- a/Scripts/Editor/NodeEditor.cs +++ b/Scripts/Editor/NodeEditor.cs @@ -32,7 +32,7 @@ public class NodeEditor { FieldInfo[] fields = GetInspectorFields(target); for (int i = 0; i < fields.Length; i++) { object[] fieldAttribs = fields[i].GetCustomAttributes(false); - if (fields[i].Name == "graph" || fields[i].Name == "rect") continue; + if (fields[i].Name == "graph" || fields[i].Name == "position") continue; NodeEditorGUILayout.PropertyField(target, fields[i], portPositions); } //If user changed a value, notify other scripts through onUpdateNode diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index ee2dd8d..92a93c3 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -48,7 +48,7 @@ public partial class NodeEditorWindow { } Repaint(); } else if (IsDraggingNode) { - draggedNode.rect.position = WindowToGridPosition(e.mousePosition) + dragOffset; + draggedNode.position = WindowToGridPosition(e.mousePosition) + dragOffset; Repaint(); } } else if (e.button == 1) { @@ -76,7 +76,7 @@ public partial class NodeEditorWindow { } } else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) { draggedNode = hoveredNode; - dragOffset = hoveredNode.rect.position - WindowToGridPosition(e.mousePosition); + dragOffset = hoveredNode.position - WindowToGridPosition(e.mousePosition); } break; case EventType.MouseUp: @@ -113,7 +113,7 @@ public partial class NodeEditorWindow { public void CreateNode(Type type, Vector2 position) { Node node = graph.AddNode(type); - node.rect.position = position; + node.position = position; Repaint(); } @@ -132,8 +132,8 @@ public partial class NodeEditorWindow { bool IsHoveringTitle(Node node) { Vector2 mousePos = Event.current.mousePosition; //Get node position - Vector2 nodePos = GridToWindowPosition(node.rect.position); - Rect windowRect = new Rect(nodePos, new Vector2(node.rect.size.x / zoom, 30 / zoom)); + Vector2 nodePos = GridToWindowPosition(node.position); + Rect windowRect = new Rect(nodePos, new Vector2(/*Node width*/200 / 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 38dcc00..728b2e3 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -153,8 +153,11 @@ public partial class NodeEditorWindow { Vector2 mousePos = Event.current.mousePosition; - hoveredPort = null; - hoveredNode = null; + + if (e.type != EventType.Layout) { + hoveredNode = null; + hoveredPort = null; + } foreach (Node node in graph.nodes) { if (node == null) return; @@ -162,7 +165,7 @@ public partial class NodeEditorWindow { nodeEditor.target = node; //Get node position - Vector2 nodePos = GridToWindowPositionNoClipped(node.rect.position); + Vector2 nodePos = GridToWindowPositionNoClipped(node.position); //GUIStyle style = (node == selectedNode) ? (GUIStyle)"flow node 0 on" : (GUIStyle)"flow node 0"; @@ -178,7 +181,7 @@ public partial class NodeEditorWindow { if (e.type == EventType.Repaint) { foreach (var kvp in portHandlePoints) { Vector2 portHandlePos = kvp.Value; - portHandlePos += node.rect.position; + portHandlePos += node.position; Rect rect = new Rect(portHandlePos.x - 8, portHandlePos.y - 8, 16, 16); portConnectionPoints.Add(kvp.Key, rect); } @@ -186,27 +189,29 @@ public partial class NodeEditorWindow { GUILayout.EndVertical(); - //Check if we are hovering this node - Vector2 nodeSize = GUILayoutUtility.GetLastRect().size / zoom; - Rect windowRect = new Rect(nodePos, nodeSize); - if (windowRect.Contains(mousePos)) hoveredNode = node; + if (e.type != EventType.Layout) { + //Check if we are hovering this node + Vector2 nodeSize = GUILayoutUtility.GetLastRect().size / zoom; + Rect windowRect = new Rect(nodePos, nodeSize); + if (windowRect.Contains(mousePos)) hoveredNode = node; - //Check if we are hovering any of this nodes ports - //Check input ports - for (int i = 0; i < node.InputCount; i++) { - NodePort port = node.inputs[i]; - //Check if port rect is available - if (!portConnectionPoints.ContainsKey(port)) continue; - Rect r = GridToWindowRect(portConnectionPoints[port]); - if (r.Contains(mousePos)) hoveredPort = port; - } - //Check all output ports - for (int i = 0; i < node.OutputCount; i++) { - NodePort port = node.outputs[i]; - //Check if port rect is available - if (!portConnectionPoints.ContainsKey(port)) continue; - Rect r = GridToWindowRect(portConnectionPoints[port]); - if (r.Contains(mousePos)) hoveredPort = port; + //Check if we are hovering any of this nodes ports + //Check input ports + for (int i = 0; i < node.InputCount; i++) { + NodePort port = node.inputs[i]; + //Check if port rect is available + if (!portConnectionPoints.ContainsKey(port)) continue; + Rect r = GridToWindowRect(portConnectionPoints[port]); + if (r.Contains(mousePos)) hoveredPort = port; + } + //Check all output ports + for (int i = 0; i < node.OutputCount; i++) { + NodePort port = node.outputs[i]; + //Check if port rect is available + if (!portConnectionPoints.ContainsKey(port)) continue; + Rect r = GridToWindowRect(portConnectionPoints[port]); + if (r.Contains(mousePos)) hoveredPort = port; + } } GUILayout.EndArea(); diff --git a/Scripts/Node.cs b/Scripts/Node.cs index fd6e23a..38798b7 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -10,7 +10,7 @@ public abstract class Node : ScriptableObject { /// Name of the node [SerializeField] public NodeGraph graph; - [SerializeField] public Rect rect = new Rect(0, 0, 200, 200); + [SerializeField] public Vector2 position; /// Input s. It is recommended not to modify these at hand. Instead, see [SerializeField] public List inputs = new List(); /// Output s. It is recommended not to modify these at hand. Instead, see