using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; /* 23.08.2020 - first 23.10.2020 - check overlay raycaster 14.12.2020 - check EvenSystemObject existing */ namespace Mkey { [Serializable] public class TouchPadEventArgs { /// /// First selected object. /// public TouchPadMessageTarget firstSelected; /// /// The cast results. /// public Collider2D[] hits; /// /// Priority dragging direction. (0,1) or (1,0) /// public Vector2 PriorAxe { get { return priorityAxe; } } /// /// Touch delta position in screen coordinats; /// public Vector2 DragDirection { get { return touchDeltaPosRaw; } } /// /// Last drag direction. /// public Vector2 LastDragDirection { get { return lastDragDir; } } /// /// Return touch world position. /// public Vector3 WorldPos { get { return wPos; } } private Vector2 touchDeltaPosRaw; private Vector2 priorityAxe; private Vector2 lastDragDir; private Vector3 wPos; private Vector2 touchPos; /// /// Fill touch arguments from touch object; /// public void SetTouch(Touch touch, bool onlyTopCollider) { // check overlay PointerEventData pointerData = new PointerEventData(EventSystem.current); pointerData.position = touch.position; List results = new List(); if(EventSystem.current) EventSystem.current.RaycastAll(pointerData, results); // Debug.Log("results.Count: " + results.Count); if (results.Count > 0) { hits = new Collider2D[0]; return; } touchPos = touch.position; if ((Camera.main)) wPos = Camera.main.ScreenToWorldPoint(touchPos); if (onlyTopCollider) { List hl = new List(Physics2D.OverlapPointAll(new Vector2(wPos.x, wPos.y))); if (hl.Count > 0) { hits = new Collider2D[] { hl[0] }; } else { hits = new Collider2D[0]; } } else { hits = Physics2D.OverlapPointAll(new Vector2(wPos.x, wPos.y)); } touchDeltaPosRaw = touch.deltaPosition; if (touch.phase == TouchPhase.Moved) { lastDragDir = touchDeltaPosRaw; priorityAxe = GetPriorityOneDirAbs(touchDeltaPosRaw); } } /// /// Fill touch arguments. /// public void SetTouch(Vector2 position, Vector2 deltaPosition, TouchPhase touchPhase, bool onlyTopCollider) { float distRCZ = float.MaxValue; // check overlay PointerEventData pointerData = new PointerEventData(EventSystem.current); pointerData.position = position; List results = new List(); if(EventSystem.current) EventSystem.current.RaycastAll(pointerData, results); // Debug.Log("results.Count: " + results.Count); float camPosZ = (Camera.main) ? Camera.main.transform.position.z : -float.MaxValue; if (results.Count > 0) { distRCZ = (Camera.main) ? results[0].worldPosition.z - camPosZ : float.MaxValue; } touchPos = position; if (Camera.main) wPos = Camera.main.ScreenToWorldPoint(touchPos); List hl = new List(Physics2D.OverlapPointAll(new Vector2(wPos.x, wPos.y))); if (hl.Count > 0) { hl.RemoveAll((coll)=> { return (coll.transform.position.z - camPosZ > distRCZ); }); } if (onlyTopCollider) { if (hl.Count > 0) { hits = new Collider2D[] { hl[0] }; } else { hits = new Collider2D[0]; } } else { hits = Physics2D.OverlapPointAll(new Vector2(wPos.x, wPos.y)); } touchDeltaPosRaw = deltaPosition; if (touchPhase == TouchPhase.Moved) { lastDragDir = touchDeltaPosRaw; priorityAxe = GetPriorityOneDirAbs(touchDeltaPosRaw); } } /// /// Return drag icon for firs touched elment or null. /// public GameObject GetIconDrag() { if (firstSelected != null) { GameObject icon = firstSelected.GetDataIcon(); return icon; } else { return null; } } private Vector2 GetPriorityOneDirAbs(Vector2 sourceDir) { if (Mathf.Abs(sourceDir.x) > Mathf.Abs(sourceDir.y)) { float x = (sourceDir.x > 0) ? 1 : 1; return new Vector2(x, 0f); } else { float y = (sourceDir.y > 0) ? 1 : 1; return new Vector2(0f, y); } } } }