feat: basic window resizing

This commit is contained in:
Gerard Gascón 2024-04-16 01:07:26 +02:00
parent 438b16fc6e
commit ce692af862
18 changed files with 324 additions and 74 deletions

View file

@ -67,8 +67,11 @@ namespace SatorImaging.AppWindowUtility
public static bool WindowResizeSupported { get => platform?.WindowResizeSupported ?? false; }
public static void ResizeWindowRelative(int pixelX, int pixelY)
=> platform?.ResizeWindowRelative(pixelX, pixelY);
public static void ResizeWindow(int pixelX, int pixelY)
=> platform?.ResizeWindow(pixelX, pixelY);
public static void ResizeWindowRelative(float pixelX, float pixelY, Vector2 resizeDirection, float aspectRatio)
=> platform?.ResizeWindowRelative(pixelX, pixelY, resizeDirection, aspectRatio);
@ -106,9 +109,5 @@ namespace SatorImaging.AppWindowUtility
}//set
}//
}//class
}//namespace

View file

@ -1,4 +1,6 @@
namespace SatorImaging.AppWindowUtility
using UnityEngine;
namespace SatorImaging.AppWindowUtility
{
public interface IPlatformDependent
{
@ -36,8 +38,8 @@
void MoveWindow(int x, int y);
bool WindowResizeSupported { get; }
public void ResizeWindowRelative(int relativeWidth, int relativeHeight);
public void ResizeWindow(int width, int height);
void ResizeWindowRelative(float deltaX, float deltaY, Vector2 resizeDirection, float aspectRatio);
}//
}//namespace

View file

@ -0,0 +1,7 @@
namespace SatorImaging.AppWindowUtility {
public enum MouseButton {
Left = 0,
Right = 1,
Middle = 2,
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d4a99a35048e47dd945227eb9f0d0c0b
timeCreated: 1713211044

View file

@ -0,0 +1,40 @@
using System.Runtime.CompilerServices;
using UnityEngine;
namespace SatorImaging.AppWindowUtility {
public static class ResizeHelper {
public static Vector2 GetDirection(Vector2 mousePosition, Vector2 screenSize) {
float widthPercentage = mousePosition.x / screenSize.x;
float heightPercentage = mousePosition.y / screenSize.y;
const float marginHeightPercentage = 30f / 1920f;
const float marginWidthPercentage = 30f / 1280f;
if (widthPercentage < marginWidthPercentage) {
if (heightPercentage > 1 - 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);
return new Vector2(1, 0);
}
if (heightPercentage > 1 - marginHeightPercentage)
return new Vector2(0, 1);
if (heightPercentage < marginHeightPercentage)
return new Vector2(0, -1);
return Vector2.zero;
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4f5b1e31614e4b458c18c903a3934198
timeCreated: 1713212603

View file

@ -1,66 +1,39 @@
using UnityEngine;
using UnityEngine.EventSystems;
namespace SatorImaging.AppWindowUtility {
public class WindowGrabber : MonoBehaviour {
public MouseButton mouseButton;
private bool _isDragging;
private Vector2 _targetPosition = Vector2.zero;
private Vector2 _resizeDirection = Vector2.zero;
namespace SatorImaging.AppWindowUtility
{
public class WindowGrabber : MonoBehaviour
{
public enum MouseButton
{
Left = 0,
Right = 1,
Middle = 2,
}
public MouseButton mouseButton;
public KeyCode modifierKey = KeyCode.None;
public KeyCode[] temporarilyDisableIfKeyPressed;
private bool isDragging = false;
Vector2 targetPosition = Vector2.zero;
void Update()
{
private void Update() {
#if UNITY_EDITOR
if(isDragging.Equals(isDragging)) return; // to avoid CS0162 warning
if (_isDragging.Equals(_isDragging)) return; // to avoid CS0162 warning
#endif
// do nothing if any uGUI is in use.
if (EventSystem.current?.currentSelectedGameObject) return;
if (EventSystem.current?.currentSelectedGameObject) return;
if (Input.GetMouseButtonUp((int)mouseButton)) _isDragging = false;
// initialize dragging state. don't check modifier key.
if (Input.GetMouseButtonUp((int)mouseButton)) isDragging = false;
if (Input.GetMouseButtonDown((int)mouseButton)) {
_targetPosition = Event.current.mousePosition;
_resizeDirection =
ResizeHelper.GetDirection(_targetPosition, new Vector2(Screen.width, Screen.height));
_isDragging = true;
}
// key check.
foreach (var k in temporarilyDisableIfKeyPressed) if (Input.GetKey(k)) return;
if (modifierKey != KeyCode.None && !Input.GetKey(modifierKey)) return;
if (Input.GetMouseButtonDown((int)mouseButton))
{
targetPosition = Event.current.mousePosition;
isDragging = true;
}
if (isDragging && Input.GetMouseButton((int)mouseButton))
{
// do NOT use Event.current.delta. it's sampled in local window coordinate.
// and moving window while mouse dragging changes coordinate sample by sample.
// just remove the gap between current mouse position and drag starting position.
AppWindowUtility.MoveWindowRelative(
(int)(Event.current.mousePosition.x - targetPosition.x),
(int)(Event.current.mousePosition.y - targetPosition.y)
);
}
}//
}//class
}//namespace
if (_isDragging && Input.GetMouseButton((int)mouseButton) && _resizeDirection == Vector2.zero) {
// do NOT use Event.current.delta. it's sampled in local window coordinate.
// and moving window while mouse dragging changes coordinate sample by sample.
// just remove the gap between current mouse position and drag starting position.
AppWindowUtility.MoveWindowRelative(
(int)(Event.current.mousePosition.x - _targetPosition.x),
(int)(Event.current.mousePosition.y - _targetPosition.y)
);
}
}
}
}

View file

@ -0,0 +1,49 @@
using UnityEngine;
using UnityEngine.EventSystems;
namespace SatorImaging.AppWindowUtility {
public class WindowResizer : MonoBehaviour {
public MouseButton mouseButton;
private bool _isResizing;
private Vector2 _targetPosition = Vector2.zero;
private Vector2 _resizeDirection = Vector2.zero;
[SerializeField] private Vector2 aspectRatio = new(10, 15);
private void Update() {
#if UNITY_EDITOR
if (_isResizing.Equals(_isResizing)) return; // to avoid CS0162 warning
#endif
if (EventSystem.current?.currentSelectedGameObject) return;
if (Input.GetMouseButtonUp((int)mouseButton)) _isResizing = false;
if (Input.GetMouseButtonDown((int)mouseButton)) {
_targetPosition = Event.current.mousePosition;
_resizeDirection =
ResizeHelper.GetDirection(_targetPosition, new Vector2(Screen.width, Screen.height));
_isResizing = true;
}
if (_isResizing && Input.GetMouseButton((int)mouseButton) && _resizeDirection != Vector2.zero) {
float ratio = aspectRatio.x / aspectRatio.y;
Vector2 delta = new(
Event.current.mousePosition.x - _targetPosition.x,
Event.current.mousePosition.y - _targetPosition.y
);
AppWindowUtility.ResizeWindowRelative(delta.x, delta.y, _resizeDirection, ratio);
_targetPosition = Event.current.mousePosition;
if (_resizeDirection.x < 0)
_targetPosition.x -= delta.x;
if (_resizeDirection.y < 0)
_targetPosition.y -= delta.y;
}
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0d779b53ae5c4644b516e84fdc31fcdc
timeCreated: 1713211016

View file

@ -273,7 +273,7 @@ namespace SatorImaging.AppWindowUtility
public void ResizeWindowRelative(int relativeWidth, int relativeHeight)
public void ResizeWindow(int width, int height)
{
if (AppWindowUtility.FullScreen) return;
@ -282,12 +282,41 @@ namespace SatorImaging.AppWindowUtility
WinApi.SetWindowPos(hWnd, IntPtr.Zero,
rect.left,
rect.top,
relativeWidth,
relativeHeight,
width,
height,
WinApi.SetWindowPosFlags.NoFlag //ApiWindows.SetWindowPosFlags.IgnoreMove
);
}//
}
public void ResizeWindowRelative(float deltaX, float deltaY, Vector2 resizeDirection, float aspectRatio) {
if (AppWindowUtility.FullScreen) return;
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);
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);
}
WinApi.SetWindowPos(hWnd, IntPtr.Zero, x, y, width, height, WinApi.SetWindowPosFlags.NoFlag);
}