From e6dc344cdc6dc0a372ea31aa4c521483ce4f3fee Mon Sep 17 00:00:00 2001 From: Bjoern Esswein <4-Bjoern@users.noreply.git.esswe.in> Date: Sun, 18 Feb 2024 17:20:29 +0100 Subject: [PATCH] Fixed unity to browser coordinate system conversion and refactored serializer settings. --- Runtime/BrowserView.cs | 48 +++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/Runtime/BrowserView.cs b/Runtime/BrowserView.cs index e504004..f76c66b 100644 --- a/Runtime/BrowserView.cs +++ b/Runtime/BrowserView.cs @@ -2,44 +2,38 @@ using ChromeDevTools; using ChromeDevTools.Protocol.Input; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; -using System; using System.Collections; -using UnityEditor.Experimental.GraphView; +using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; -using UnityEngine.UIElements; [RequireComponent(typeof(RawImage))] public class BrowserView : MonoBehaviour, IPointerDownHandler, IPointerMoveHandler, IPointerUpHandler, IPointerEnterHandler, IDropHandler, IPointerExitHandler { #region json serializer - /// <summary> - /// JsonSerializer for the user space objects (e.g. transfering objects that have been droped on the BrowserView) - /// Users are allowed to change serializer settings to their liking. - /// </summary> - public static JsonSerializer serializer; + /// <summary> /// Json serializer settings for the user space objects (e.g. transfering objects that have been droped on the BrowserView) /// Users are allowed to change serializer settings to their liking. /// </summary> - public static JsonSerializerSettings serializerSettings; - static BrowserView() + public static JsonSerializerSettings serializerSettings = new JsonSerializerSettings { - // initialize the JsonSerializer - serializerSettings = new JsonSerializerSettings - { - ContractResolver = new CamelCasePropertyNamesContractResolver(), - Converters = new JsonConverter[] + ContractResolver = new CamelCasePropertyNamesContractResolver(), + Converters = new List<JsonConverter>() { - new ColorConverter(), - new Vector2Converter(), - new Vector3Converter(), - new Vector4Converter() + new ColorConverter(), + new Vector2Converter(), + new Vector3Converter(), + new Vector4Converter() } - }; - serializer = JsonSerializer.Create(serializerSettings); - } + }; + /// <summary> + /// JsonSerializer for the user space objects (e.g. transfering objects that have been droped on the BrowserView) + /// Users are allowed to change serializer settings to their liking. + /// </summary> + public static JsonSerializer serializer = JsonSerializer.Create(serializerSettings); + #endregion json serializer private Browser browser; @@ -184,11 +178,13 @@ public class BrowserView : MonoBehaviour, IPointerDownHandler, IPointerMoveHandl private Vector2Int toBrowserCoordinates(Vector2 eventPos) { + Vector2 localPoint; + RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventPos, null, out localPoint); + // invert y because the browser has y=0 on the top - Vector2 invertedPos = new Vector2(eventPos.x, rectTransform.rect.size.y - eventPos.y); - // TODO: fix coordinate transformation, maybe use image size instead of rectTransform, if possible - Vector2 browserCoorinate = tab.size / rectTransform.rect.size * invertedPos; - Debug.Log($"eventPos: {eventPos}, invertedPos: {invertedPos}, browserCoordinate: {browserCoorinate}"); + Vector2 invertedLocalPos = new Vector2(localPoint.x, rectTransform.rect.size.y - localPoint.y); + Vector2 browserCoorinate = tab.size / rectTransform.rect.size * invertedLocalPos; + Debug.Log($"eventPos: {eventPos}, invertedLocalPos: {invertedLocalPos}, browserCoordinate: {browserCoorinate}"); return new Vector2Int((int) browserCoorinate.x, (int) browserCoorinate.y); } -- GitLab