From 50669a341d060500d82e459f2115f1afd58d6424 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sat, 26 Aug 2023 09:38:33 +0200 Subject: [PATCH] Improve connections drawing for BehaviorTree and AnimGraph --- .../Surface/Archetypes/Animation.StateMachine.cs | 9 ++++++--- Source/Editor/Surface/Archetypes/BehaviorTree.cs | 2 ++ Source/Editor/Surface/Elements/Box.cs | 7 ++++++- Source/Editor/Surface/Elements/OutputBox.cs | 12 ++++++------ 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Source/Editor/Surface/Archetypes/Animation.StateMachine.cs b/Source/Editor/Surface/Archetypes/Animation.StateMachine.cs index a540ab697..621c7c25e 100644 --- a/Source/Editor/Surface/Archetypes/Animation.StateMachine.cs +++ b/Source/Editor/Surface/Archetypes/Animation.StateMachine.cs @@ -694,11 +694,14 @@ namespace FlaxEditor.Surface.Archetypes float rotation = Float2.Dot(dir, Float2.UnitY); if (endPos.X < startPos.X) rotation = 2 - rotation; - // TODO: make it look better (fix the math) - var arrowTransform = Matrix3x3.Translation2D(new Float2(-16.0f, -8.0f)) * Matrix3x3.RotationZ(rotation * Mathf.PiOverTwo) * Matrix3x3.Translation2D(endPos); + var sprite = Editor.Instance.Icons.VisjectArrowClosed32; + var arrowTransform = + Matrix3x3.Translation2D(-6.5f, -8) * + Matrix3x3.RotationZ(rotation * Mathf.PiOverTwo) * + Matrix3x3.Translation2D(endPos - dir * 8); Render2D.PushTransform(ref arrowTransform); - Render2D.DrawSprite(Editor.Instance.Icons.VisjectArrowClosed32, arrowRect, color); + Render2D.DrawSprite(sprite, arrowRect, color); Render2D.PopTransform(); endPos -= dir * 4.0f; diff --git a/Source/Editor/Surface/Archetypes/BehaviorTree.cs b/Source/Editor/Surface/Archetypes/BehaviorTree.cs index 3e6912be4..27fb62bcd 100644 --- a/Source/Editor/Surface/Archetypes/BehaviorTree.cs +++ b/Source/Editor/Surface/Archetypes/BehaviorTree.cs @@ -341,6 +341,8 @@ namespace FlaxEditor.Surface.Archetypes // Setup boxes _input = (InputBox)GetBox(0); _output = (OutputBox)GetBox(1); + _input.ConnectionOffset = new Float2(0, FlaxEditor.Surface.Constants.BoxSize * -0.5f); + _output.ConnectionOffset = new Float2(0, FlaxEditor.Surface.Constants.BoxSize * 0.5f); // Setup node type and data var flagsRoot = NodeFlags.NoRemove | NodeFlags.NoCloseButton | NodeFlags.NoSpawnViaPaste; diff --git a/Source/Editor/Surface/Elements/Box.cs b/Source/Editor/Surface/Elements/Box.cs index 8b0caab84..da081446d 100644 --- a/Source/Editor/Surface/Elements/Box.cs +++ b/Source/Editor/Surface/Elements/Box.cs @@ -675,12 +675,17 @@ namespace FlaxEditor.Surface.Elements return false; } + /// + /// Connections origin offset. + /// + public Float2 ConnectionOffset; + /// public Float2 ConnectionOrigin { get { - var center = Center; + var center = Center + ConnectionOffset; return Parent.PointToParent(ref center); } } diff --git a/Source/Editor/Surface/Elements/OutputBox.cs b/Source/Editor/Surface/Elements/OutputBox.cs index cf527402d..36355e5a5 100644 --- a/Source/Editor/Surface/Elements/OutputBox.cs +++ b/Source/Editor/Surface/Elements/OutputBox.cs @@ -63,8 +63,8 @@ namespace FlaxEditor.Surface.Elements /// The mouse position public bool IntersectsConnection(Box targetBox, ref Float2 mousePosition) { - var startPos = Parent.PointToParent(Center); - var endPos = targetBox.Parent.PointToParent(targetBox.Center); + var startPos = ConnectionOrigin; + var endPos = targetBox.ConnectionOrigin; return IntersectsConnection(ref startPos, ref endPos, ref mousePosition, MouseOverConnectionDistance); } @@ -132,12 +132,12 @@ namespace FlaxEditor.Surface.Elements // Draw all the connections var style = Surface.Style; var mouseOverDistance = MouseOverConnectionDistance; - var startPos = Parent.PointToParent(Center); + var startPos = ConnectionOrigin; var startHighlight = ConnectionsHighlightIntensity; for (int i = 0; i < Connections.Count; i++) { Box targetBox = Connections[i]; - var endPos = targetBox.Parent.PointToParent(targetBox.Center); + var endPos = targetBox.ConnectionOrigin; var highlight = 1 + Mathf.Max(startHighlight, targetBox.ConnectionsHighlightIntensity); var color = _currentTypeColor * highlight; @@ -157,8 +157,8 @@ namespace FlaxEditor.Surface.Elements public void DrawSelectedConnection(Box targetBox) { // Draw all the connections - var startPos = Parent.PointToParent(Center); - var endPos = targetBox.Parent.PointToParent(targetBox.Center); + var startPos = ConnectionOrigin; + var endPos = targetBox.ConnectionOrigin; DrawConnection(Surface.Style, ref startPos, ref endPos, ref _currentTypeColor, 2.5f); }