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

@ -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)
);
}
}
}
}