From 2f313c6b4361f66d8060fc4f200de7698120f93a Mon Sep 17 00:00:00 2001 From: VNC <52937757+VNNCC@users.noreply.github.com> Date: Mon, 28 Dec 2020 19:05:14 +0100 Subject: [PATCH] Correct DockHintWindow drag offset if it was invalid when initializing Description: If the toMove window is unfocused the FlaxEngine.Input.MouseScreenPosition property will always return a vector that only contains zeros. #64 Fix: Perform a later update that will correct the drag offset using the current mouse position. --- Source/Editor/GUI/Docking/DockHintWindow.cs | 28 ++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/Source/Editor/GUI/Docking/DockHintWindow.cs b/Source/Editor/GUI/Docking/DockHintWindow.cs index d8228598a..06513556c 100644 --- a/Source/Editor/GUI/Docking/DockHintWindow.cs +++ b/Source/Editor/GUI/Docking/DockHintWindow.cs @@ -20,6 +20,7 @@ namespace FlaxEditor.GUI.Docking private Vector2 _mouse; private DockState _toSet; private DockPanel _toDock; + private bool _lateDragOffsetUpdate; private Rectangle _rLeft, _rRight, _rBottom, _rUpper, _rCenter; @@ -37,9 +38,14 @@ namespace FlaxEditor.GUI.Docking window.Focus(); // Calculate dragging offset and move window to the destination position - Vector2 mouse = FlaxEngine.Input.MouseScreenPosition; - Vector2 baseWinPos = window.Position; - _dragOffset = mouse - baseWinPos; + var mouseScreenPosition = FlaxEngine.Input.MouseScreenPosition; + + // If the _toMove window was not focused when initializing this window, the result vector only contains zeros + // and to prevent a failure, we need to perform an update for the drag offset at later time which will be done in the OnMouseMove event handler. + if (mouseScreenPosition != Vector2.Zero) + CalculateDragOffset(mouseScreenPosition); + else + _lateDragOffsetUpdate = true; // Get initial size _defaultWindowSize = window.Size; @@ -217,6 +223,12 @@ namespace FlaxEditor.GUI.Docking return result; } + private void CalculateDragOffset(Vector2 mouseScreenPosition) + { + Vector2 baseWinPos = this._toMove.Window.Window.Position; + _dragOffset = mouseScreenPosition - baseWinPos; + } + private void UpdateRects() { // Cache mouse position @@ -326,6 +338,16 @@ namespace FlaxEditor.GUI.Docking private void OnMouseMove(ref Vector2 mousePos) { + // Recalculate the drag offset because the current mouse screen position was invalid when we initialized the window + if (_lateDragOffsetUpdate) + { + // Calculate dragging offset and move window to the destination position + CalculateDragOffset(mousePos); + + // Reset state + _lateDragOffsetUpdate = false; + } + UpdateRects(); }