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);