1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-03-26 22:49:02 +08:00

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.
This commit is contained in:
Simon Rodriguez 2018-12-09 21:04:37 +01:00
parent e2549808cc
commit 963638345c
2 changed files with 49 additions and 0 deletions

View File

@ -397,6 +397,48 @@ namespace XNodeEditor {
Selection.objects = newNodes;
}
/// <summary> Draw this node on top of other nodes by placing it last in the graph.nodes list </summary>
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;
}
}
}
/// <summary> Draw a connection as we are dragging it </summary>
public void DrawDraggedConnection() {
if (IsDraggingPort) {

View File

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