From 963638345cd6140776ca81048d466910f0e37368 Mon Sep 17 00:00:00 2001 From: Simon Rodriguez Date: Sun, 9 Dec 2018 21:04:37 +0100 Subject: [PATCH] Suggestion! Added context menu options to align selected nodes to the far left/right/top/bottom edge of the selected nodes. Creates prettier looking graphs. --- Scripts/Editor/NodeEditorAction.cs | 42 ++++++++++++++++++++++++++++++ Scripts/Editor/NodeEditorGUI.cs | 7 +++++ 2 files changed, 49 insertions(+) diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index 404b8af..180cb01 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -397,6 +397,48 @@ namespace XNodeEditor { Selection.objects = newNodes; } + /// Draw this node on top of other nodes by placing it last in the graph.nodes list + public void AlignNodesTo(bool left, bool right, bool top, bool bottom) { + float positionLeft = float.MaxValue; + float positionRight = float.MinValue; + float positionTop = float.MaxValue; + float positionBottom = float.MinValue; + + for (int i = 0; i < Selection.objects.Length; i++) { + if (Selection.objects[i] is XNode.Node) { + XNode.Node node = Selection.objects[i] as XNode.Node; + float width = 200; + float height = 200; + Vector2 size; + if (nodeSizes.TryGetValue(node, out size)) { + width = size.x; + height = size.y; + } + if (node.position.x < positionLeft) positionLeft = node.position.x; + if (node.position.y < positionTop) positionTop = node.position.y; + if (node.position.x + width > positionRight) positionRight = node.position.x + width; + if (node.position.y + height > positionBottom) positionBottom = node.position.y + height; + } + } + + for (int i = 0; i < Selection.objects.Length; i++) { + if (Selection.objects[i] is XNode.Node) { + XNode.Node node = Selection.objects[i] as XNode.Node; + float width = 200; + float height = 200; + Vector2 size; + if (nodeSizes.TryGetValue(node, out size)) { + width = size.x; + height = size.y; + } + if (left) node.position.x = positionLeft; + if (top) node.position.y = positionTop; + if (right) node.position.x = positionRight - width; + if (bottom) node.position.y = positionBottom - height; + } + } + } + /// Draw a connection as we are dragging it public void DrawDraggedConnection() { if (IsDraggingPort) { diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 34fa6a7..d388975 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -126,6 +126,13 @@ namespace XNodeEditor { contextMenu.AddItem(new GUIContent("Rename"), false, RenameSelectedNode); } + if(Selection.objects.Length > 1) { + contextMenu.AddItem(new GUIContent("Align To/Left"), false, () => AlignNodesTo(true, false, false, false)); + contextMenu.AddItem(new GUIContent("Align To/Right"), false, () => AlignNodesTo(false, true, false, false)); + contextMenu.AddItem(new GUIContent("Align To/Top"), false, () => AlignNodesTo(false, false, true, false)); + contextMenu.AddItem(new GUIContent("Align To/Bottom"), false, () => AlignNodesTo(false, false, false, true)); + } + contextMenu.AddItem(new GUIContent("Duplicate"), false, DublicateSelectedNodes); contextMenu.AddItem(new GUIContent("Remove"), false, RemoveSelectedNodes);