From 0b54691f07771a737f6efa3aaf26a84f8a2dcf99 Mon Sep 17 00:00:00 2001 From: stefnotch Date: Wed, 17 Feb 2021 22:36:11 +0100 Subject: [PATCH] Basic Visject box arrow key navigation --- Source/Editor/Surface/SurfaceNode.cs | 19 ++-- Source/Editor/Surface/VisjectSurface.Input.cs | 95 +++++++++++++++++++ 2 files changed, 106 insertions(+), 8 deletions(-) diff --git a/Source/Editor/Surface/SurfaceNode.cs b/Source/Editor/Surface/SurfaceNode.cs index bdce94513..303324870 100644 --- a/Source/Editor/Surface/SurfaceNode.cs +++ b/Source/Editor/Surface/SurfaceNode.cs @@ -553,19 +553,22 @@ namespace FlaxEditor.Surface internal Box GetNextBox(Box box) { - int i = 0; - for (; i < Elements.Count; i++) + // Get the one after it + for (int i = box.IndexInParent + 1; i < Elements.Count; i++) { - if (Elements[i] == box) + if (Elements[i] is Box b) { - // We found the box - break; + return b; } } - // Get the one after it - i++; - for (; i < Elements.Count; i++) + return null; + } + + internal Box GetPreviousBox(Box box) + { + // Get the one before it + for (int i = box.IndexInParent - 1; i >= 0; i--) { if (Elements[i] is Box b) { diff --git a/Source/Editor/Surface/VisjectSurface.Input.cs b/Source/Editor/Surface/VisjectSurface.Input.cs index dec89e143..28cff42b8 100644 --- a/Source/Editor/Surface/VisjectSurface.Input.cs +++ b/Source/Editor/Surface/VisjectSurface.Input.cs @@ -527,6 +527,101 @@ namespace FlaxEditor.Surface } return true; } + + if (key == KeyboardKeys.ArrowUp) + { + Box selectedBox = GetSelectedBox(SelectedNodes); + Box toSelect = selectedBox?.ParentNode.GetPreviousBox(selectedBox); + + if (toSelect != null) + { + if (toSelect.Connections.Count > 1) + { + // Box has multiple connections + } + + if (toSelect.IsOutput != selectedBox.IsOutput) + { + // Jump up (nodes) + } + else + { + Select(toSelect.ParentNode); + toSelect.ParentNode.SelectBox(toSelect); + } + } + + return true; + } + if (key == KeyboardKeys.ArrowDown) + { + Box selectedBox = GetSelectedBox(SelectedNodes); + Box toSelect = selectedBox?.ParentNode.GetNextBox(selectedBox); + + if (toSelect != null) + { + if (toSelect.Connections.Count > 1) + { + // Box has multiple connections + } + + if (toSelect.IsOutput != selectedBox.IsOutput) + { + // Jump down (nodes) + } + else + { + Select(toSelect.ParentNode); + toSelect.ParentNode.SelectBox(toSelect); + } + } + + return true; + } + + if (key == KeyboardKeys.ArrowRight || key == KeyboardKeys.ArrowLeft) + { + Box selectedBox = GetSelectedBox(SelectedNodes); + if (selectedBox == null) return false; + + Box toSelect = null; + + if (key == KeyboardKeys.ArrowRight && selectedBox.IsOutput || key == KeyboardKeys.ArrowLeft && !selectedBox.IsOutput) + { + if (selectedBox.Connections.Count > 1) + { + // Box has multiple connections + } + else if (selectedBox.Connections.Count == 1) + { + toSelect = selectedBox.Connections[0]; + } + } + else + { + // Use the node with the closest Y-level + // Since there are cases like 3 nodes on one side and only 1 node on the other side + + var elements = selectedBox?.ParentNode.Elements; + float distance = float.PositiveInfinity; + for (int i = 0; i < elements.Count; i++) + { + if (elements[i] is Box box && box.IsOutput != selectedBox.IsOutput && Mathf.Abs(box.Y - selectedBox.Y) < distance) + { + toSelect = box; + distance = Mathf.Abs(box.Y - selectedBox.Y); + } + } + } + + if (toSelect != null) + { + Select(toSelect.ParentNode); + toSelect.ParentNode.SelectBox(toSelect); + + } + return true; + } } return false;