feat: rounded corners for display

This commit is contained in:
Gerard Gascón 2024-04-18 11:16:43 +02:00
parent a47aee1561
commit e4ad9cc2f1
23 changed files with 337 additions and 282 deletions

View file

@ -1,40 +0,0 @@
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

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

View file

@ -7,7 +7,6 @@ namespace SatorImaging.AppWindowUtility {
private bool _isDragging;
private Vector2 _targetPosition = Vector2.zero;
private Vector2 _resizeDirection = Vector2.zero;
private void Update() {
#if UNITY_EDITOR
@ -20,12 +19,10 @@ namespace SatorImaging.AppWindowUtility {
if (Input.GetMouseButtonDown((int)mouseButton)) {
_targetPosition = Event.current.mousePosition;
_resizeDirection =
ResizeHelper.GetDirection(_targetPosition, new Vector2(Screen.width, Screen.height));
_isDragging = true;
}
if (_isDragging && Input.GetMouseButton((int)mouseButton) && _resizeDirection == Vector2.zero) {
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.

View file

@ -1,49 +0,0 @@
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

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

View file

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 95bed7a6ca15f8045b8a333ad25d61f5
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -1,77 +0,0 @@
using NUnit.Framework;
using SatorImaging.AppWindowUtility;
using UnityEngine;
public class ResizeTest {
[Test]
public void CheckCenter() {
Vector2 mousePos = new(500, 500);
Vector2 screenSize = new(1280, 1920);
Assert.AreEqual(new Vector2(0, 0), ResizeHelper.GetDirection(mousePos, screenSize));
}
[Test]
public void CheckTopLeft() {
Vector2 mousePos = new(0, 0);
Vector2 screenSize = new(1280, 1920);
Assert.AreEqual(new Vector2(-1, 1), ResizeHelper.GetDirection(mousePos, screenSize));
}
[Test]
public void CheckTop() {
Vector2 mousePos = new(500, 0);
Vector2 screenSize = new(1280, 1920);
Assert.AreEqual(new Vector2(0, 1), ResizeHelper.GetDirection(mousePos, screenSize));
}
[Test]
public void CheckTopRight() {
Vector2 mousePos = new(1280, 0);
Vector2 screenSize = new(1280, 1920);
Assert.AreEqual(new Vector2(1, 1), ResizeHelper.GetDirection(mousePos, screenSize));
}
[Test]
public void CheckLeft() {
Vector2 mousePos = new(0, 500);
Vector2 screenSize = new(1280, 1920);
Assert.AreEqual(new Vector2(-1, 0), ResizeHelper.GetDirection(mousePos, screenSize));
}
[Test]
public void CheckRight() {
Vector2 mousePos = new(1280, 500);
Vector2 screenSize = new(1280, 1920);
Assert.AreEqual(new Vector2(1, 0), ResizeHelper.GetDirection(mousePos, screenSize));
}
[Test]
public void CheckBottomLeft() {
Vector2 mousePos = new(0, 1920);
Vector2 screenSize = new(1280, 1920);
Assert.AreEqual(new Vector2(-1, -1), ResizeHelper.GetDirection(mousePos, screenSize));
}
[Test]
public void CheckBottom() {
Vector2 mousePos = new(500, 1920);
Vector2 screenSize = new(1280, 1920);
Assert.AreEqual(new Vector2(0, -1), ResizeHelper.GetDirection(mousePos, screenSize));
}
[Test]
public void CheckBottomRight() {
Vector2 mousePos = new(1280, 1920);
Vector2 screenSize = new(1280, 1920);
Assert.AreEqual(new Vector2(1, -1), ResizeHelper.GetDirection(mousePos, screenSize));
}
}

View file

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: ec4a69b0e49f65c4c98675104812c31b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -1,24 +0,0 @@
{
"name": "SatorImaging.AppWindowUtility.Tests",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"SatorImaging.AppWindowUtility"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}

View file

@ -1,7 +0,0 @@
fileFormatVersion: 2
guid: 39a22fcf88b04a24eb3566b752f8d676
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: