From 65af35e8d50f01c71e0f9589aaf7e66205692cd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Gasc=C3=B3n?= <52170489+GerardGascon@users.noreply.github.com> Date: Tue, 16 Apr 2024 11:44:37 +0200 Subject: [PATCH] feat: Resizing properly working without taking delta magnitude into account --- .../AppWindowUtility/Runtime/ResizeHelper.cs | 12 ++--- .../AppWindowUtility/Runtime/WindowResizer.cs | 2 +- .../Runtime/Windows/Windows.cs | 51 +++++++++++++------ Packages/AppWindowUtility/Tests/ResizeTest.cs | 12 ++--- 4 files changed, 49 insertions(+), 28 deletions(-) diff --git a/Packages/AppWindowUtility/Runtime/ResizeHelper.cs b/Packages/AppWindowUtility/Runtime/ResizeHelper.cs index e73436d..fcb7e64 100644 --- a/Packages/AppWindowUtility/Runtime/ResizeHelper.cs +++ b/Packages/AppWindowUtility/Runtime/ResizeHelper.cs @@ -12,27 +12,27 @@ namespace SatorImaging.AppWindowUtility { if (widthPercentage < marginWidthPercentage) { if (heightPercentage > 1 - marginHeightPercentage) - return new Vector2(-1, 1); - if (heightPercentage < marginHeightPercentage) return new Vector2(-1, -1); + if (heightPercentage < marginHeightPercentage) + return new Vector2(-1, 1); return new Vector2(-1, 0); } if (widthPercentage > 1 - marginWidthPercentage) { if (heightPercentage > 1 - marginHeightPercentage) - return new Vector2(1, 1); - if (heightPercentage < marginHeightPercentage) return new Vector2(1, -1); + if (heightPercentage < marginHeightPercentage) + return new Vector2(1, 1); return new Vector2(1, 0); } if (heightPercentage > 1 - marginHeightPercentage) - return new Vector2(0, 1); + return new Vector2(0, -1); if (heightPercentage < marginHeightPercentage) - return new Vector2(0, -1); + return new Vector2(0, 1); return Vector2.zero; } diff --git a/Packages/AppWindowUtility/Runtime/WindowResizer.cs b/Packages/AppWindowUtility/Runtime/WindowResizer.cs index fc40d95..de96e94 100644 --- a/Packages/AppWindowUtility/Runtime/WindowResizer.cs +++ b/Packages/AppWindowUtility/Runtime/WindowResizer.cs @@ -41,7 +41,7 @@ namespace SatorImaging.AppWindowUtility { if (_resizeDirection.x < 0) _targetPosition.x -= delta.x; - if (_resizeDirection.y < 0) + if (_resizeDirection.y > 0) _targetPosition.y -= delta.y; } } diff --git a/Packages/AppWindowUtility/Runtime/Windows/Windows.cs b/Packages/AppWindowUtility/Runtime/Windows/Windows.cs index 8fa4d64..475d68e 100644 --- a/Packages/AppWindowUtility/Runtime/Windows/Windows.cs +++ b/Packages/AppWindowUtility/Runtime/Windows/Windows.cs @@ -295,24 +295,45 @@ namespace SatorImaging.AppWindowUtility WinApi.RECT rect; WinApi.GetWindowRect(hWnd, out rect); - float resizeFactor = Mathf.Abs(resizeDirection.x) > Mathf.Abs(resizeDirection.y) - ? deltaX - : deltaY; - int x = rect.left; int y = rect.top; - int width = (int)(rect.right - rect.left + resizeFactor); - //int height = (int)(rect.bottom - rect.top + deltaY); + int width = 0; + int height = 0; - if (resizeDirection.x < 0) { - x += (int)resizeFactor; - width = rect.right - x; - } - int height = (int)(width / aspectRatio); - if (resizeDirection.y < 0) { - y += (int)resizeFactor; - height = rect.bottom - y; - width = (int)(height * aspectRatio); + if (resizeDirection.x > 0) { + if (resizeDirection.y > 0) { + width = (int)(rect.right - rect.left + deltaX); + height = (int)(width / aspectRatio); + }else if (resizeDirection.y < 0) { + width = (int)(rect.right - rect.left + deltaX); + height = (int)(width / aspectRatio); + }else { + width = (int)(rect.right - rect.left + deltaX); + height = (int)(width / aspectRatio); + } + }else if (resizeDirection.x < 0) { + if (resizeDirection.y > 0) { + x += (int)deltaX; + width = rect.right - x; + height = (int)(width / aspectRatio); + }else if (resizeDirection.y < 0) { + x += (int)deltaX; + width = rect.right - x; + height = (int)(width / aspectRatio); + }else { + x += (int)deltaX; + width = rect.right - x; + height = (int)(width / aspectRatio); + } + } else { + if (resizeDirection.y > 0) { + y += (int)deltaY; + height = rect.bottom - y; + width = (int)(height * aspectRatio); + }else if (resizeDirection.y < 0) { + height = (int)(rect.bottom - rect.top + deltaY); + width = (int)(height * aspectRatio); + } } WinApi.SetWindowPos(hWnd, IntPtr.Zero, x, y, width, height, WinApi.SetWindowPosFlags.NoFlag); diff --git a/Packages/AppWindowUtility/Tests/ResizeTest.cs b/Packages/AppWindowUtility/Tests/ResizeTest.cs index 9034334..bd1f5a1 100644 --- a/Packages/AppWindowUtility/Tests/ResizeTest.cs +++ b/Packages/AppWindowUtility/Tests/ResizeTest.cs @@ -13,7 +13,7 @@ public class ResizeTest { [Test] public void CheckTopLeft() { - Vector2 mousePos = new(0, 1920); + Vector2 mousePos = new(0, 0); Vector2 screenSize = new(1280, 1920); Assert.AreEqual(new Vector2(-1, 1), ResizeHelper.GetDirection(mousePos, screenSize)); @@ -21,7 +21,7 @@ public class ResizeTest { [Test] public void CheckTop() { - Vector2 mousePos = new(500, 1920); + Vector2 mousePos = new(500, 0); Vector2 screenSize = new(1280, 1920); Assert.AreEqual(new Vector2(0, 1), ResizeHelper.GetDirection(mousePos, screenSize)); @@ -29,7 +29,7 @@ public class ResizeTest { [Test] public void CheckTopRight() { - Vector2 mousePos = new(1280, 1920); + Vector2 mousePos = new(1280, 0); Vector2 screenSize = new(1280, 1920); Assert.AreEqual(new Vector2(1, 1), ResizeHelper.GetDirection(mousePos, screenSize)); @@ -53,7 +53,7 @@ public class ResizeTest { [Test] public void CheckBottomLeft() { - Vector2 mousePos = new(0, 0); + Vector2 mousePos = new(0, 1920); Vector2 screenSize = new(1280, 1920); Assert.AreEqual(new Vector2(-1, -1), ResizeHelper.GetDirection(mousePos, screenSize)); @@ -61,7 +61,7 @@ public class ResizeTest { [Test] public void CheckBottom() { - Vector2 mousePos = new(500, 0); + Vector2 mousePos = new(500, 1920); Vector2 screenSize = new(1280, 1920); Assert.AreEqual(new Vector2(0, -1), ResizeHelper.GetDirection(mousePos, screenSize)); @@ -69,7 +69,7 @@ public class ResizeTest { [Test] public void CheckBottomRight() { - Vector2 mousePos = new(1280, 0); + Vector2 mousePos = new(1280, 1920); Vector2 screenSize = new(1280, 1920); Assert.AreEqual(new Vector2(1, -1), ResizeHelper.GetDirection(mousePos, screenSize));