From c16ad1fff06c7354a72af788c7267ee74a7ae2be Mon Sep 17 00:00:00 2001 From: Simon Rodriguez Date: Tue, 3 Sep 2019 22:18:30 +0200 Subject: [PATCH] Added functionality that focuses on selected nodes. (#173) Resets view if no nodes are selected. --- Scripts/Editor/NodeEditorAction.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index f0ebec0..1caecd9 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -335,10 +335,17 @@ namespace XNodeEditor { } } - /// Puts all nodes in focus. If no nodes are present, resets view to + /// Puts all selected nodes in focus. If no nodes are present, resets view and zoom to to origin public void Home() { - zoom = 2; - panOffset = Vector2.zero; + var nodes = Selection.objects.Where(o => o is XNode.Node).Cast().ToList(); + if (nodes.Count > 0) { + Vector2 minPos = nodes.Select(x => x.position).Aggregate((x, y) => new Vector2(Mathf.Min(x.x, y.x), Mathf.Min(x.y, y.y))); + Vector2 maxPos = nodes.Select(x => x.position + (nodeSizes.ContainsKey(x) ? nodeSizes[x] : Vector2.zero)).Aggregate((x, y) => new Vector2(Mathf.Max(x.x, y.x), Mathf.Max(x.y, y.y))); + panOffset = -(minPos + (maxPos - minPos) / 2f); + } else { + zoom = 2; + panOffset = Vector2.zero; + } } /// Remove nodes in the graph in Selection.objects