using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine;
using UnityEngine.Assertions;
using System;
using UnityEngine.UI;
namespace Rokid.UXR.Interaction
{
public class PointableCanvasEventArgs
{
public readonly Canvas Canvas;
public readonly GameObject Hovered;
public readonly bool Dragging;
public PointableCanvasEventArgs(Canvas canvas, GameObject hovered, bool dragging)
{
Canvas = canvas;
Hovered = hovered;
Dragging = dragging;
}
}
///
/// IPointerInteractableModule manages all InteractableCanvas events in
/// the scene and translates them into pointer events for Unity Canvas UIs.
///
public class PointableCanvasModule : PointerInputModule
{
public static event Action WhenSelected;
public static event Action WhenUnselected;
public static event Action WhenSelectableHovered;
public static event Action WhenSelectableUnhovered;
[SerializeField]
private bool _useInitialPressPositionForDrag = true;
private Camera _pointerEventCamera;
private static PointableCanvasModule _instance = null;
[SerializeField]
private GameObject m_PreOverGo;
[SerializeField]
private Text eventLogText;
private List draggingPointerData = new List();
private static PointableCanvasModule Instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType();
}
return _instance;
}
}
public static void RegisterPointableCanvas(IPointableCanvas pointerCanvas)
{
RKLog.Info($"====PointableCanvasModule====: +RegisterPointableCanvas");
Assert.IsNotNull(Instance, "A PointableCanvasModule is required in the scene.");
Instance.AddPointerCanvas(pointerCanvas);
}
public static void UnregisterPointableCanvas(IPointableCanvas pointerCanvas)
{
Instance?.RemovePointerCanvas(pointerCanvas);
}
private Dictionary _pointerMap = new Dictionary();
private List _raycastResultCache = new List();
private Dictionary _pointersForDeletion = new Dictionary();
private Dictionary> _pointerCanvasActionMap =
new Dictionary>();
private void AddPointerCanvas(IPointableCanvas pointerCanvas)
{
//1.New A PoitnerToCanvasAction
//2.Add PointerCanvasAction To Map
//3.Regist PointerCanvasAction To pointerCanvas.WhenPointerEventRaised
Action pointerCanvasAction = (args) =>
{
HandlePointerEvent(pointerCanvas.Canvas, args);
};
_pointerCanvasActionMap.Add(pointerCanvas, pointerCanvasAction);
pointerCanvas.WhenPointerEventRaised += pointerCanvasAction;
}
private void RemovePointerCanvas(IPointableCanvas pointerCanvas)
{
RKLog.Info("====PointableCanvasModule==== : RemovePointerCanvas");
Action pointerCanvasAction = _pointerCanvasActionMap[pointerCanvas];
_pointerCanvasActionMap.Remove(pointerCanvas);
pointerCanvas.WhenPointerEventRaised -= pointerCanvasAction;
List pointerIDs = new List(_pointerMap.Keys);
foreach (int pointerID in pointerIDs)
{
Pointer pointer = _pointerMap[pointerID];
if (pointer.Canvas != pointerCanvas.Canvas)
{
continue;
}
ClearPointerSelection(pointer.PointerEventData);
pointer.MarkForDeletion();
_pointersForDeletion[pointerID] = pointer;
}
}
private void HandlePointerEvent(Canvas canvas, PointerEvent evt)
{
Pointer pointer;
switch (evt.Type)
{
case PointerEventType.Hover:
pointer = new Pointer(canvas);
pointer.PointerEventData = new PointerEventData(eventSystem);
pointer.SetPosition(evt.Pose.position);
_pointerMap.Add(evt.Identifier, pointer);
// RKLog.Info($"====PointableCanvasModule====: Hover AddPointer:{evt.Identifier},{_pointerMap.Count}");
pointer.PointerEventData.pointerId = evt.Identifier;
if (!m_PointerData.ContainsKey(evt.Identifier))
m_PointerData.Add(evt.Identifier, pointer.PointerEventData);
RKPointerLisener.OnPointerEnter?.Invoke(pointer.PointerEventData);
break;
case PointerEventType.Unhover:
if (_pointerMap.ContainsKey(evt.Identifier))
{
pointer = _pointerMap[evt.Identifier];
_pointerMap.Remove(evt.Identifier);
pointer.MarkForDeletion();
_pointersForDeletion[evt.Identifier] = pointer;
}
break;
case PointerEventType.Select:
if (_pointerMap.ContainsKey(evt.Identifier))
{
pointer = _pointerMap[evt.Identifier];
pointer.SetPosition(evt.Pose.position);
pointer.Press();
}
break;
case PointerEventType.Unselect:
if (_pointerMap.ContainsKey(evt.Identifier))
{
pointer = _pointerMap[evt.Identifier];
pointer.SetPosition(evt.Pose.position);
pointer.Release();
}
break;
case PointerEventType.Move:
if (_pointerMap.ContainsKey(evt.Identifier))
{
pointer = _pointerMap[evt.Identifier];
pointer.SetPosition(evt.Pose.position);
// RKLog.Debug($"====PointableCanvasModule==== Move: {evt.Pose.position}");
}
break;
case PointerEventType.Cancel:
if (_pointerMap.ContainsKey(evt.Identifier))
{
pointer = _pointerMap[evt.Identifier];
_pointerMap.Remove(evt.Identifier);
ClearPointerSelection(pointer.PointerEventData);
pointer.MarkForDeletion();
_pointersForDeletion[evt.Identifier] = pointer;
}
break;
}
}
///
/// Pointer class that is used for state associated with IPointables that are currently
/// tracked by any IPointableCanvases in the scene.
///
private class Pointer
{
public PointerEventData PointerEventData { get; set; }
public bool MarkedForDeletion { get; private set; }
private Canvas _canvas;
public Canvas Canvas => _canvas;
private Vector3 _position;
public Vector3 Position => _position;
private GameObject _hoveredSelectable;
public GameObject HoveredSelectable => _hoveredSelectable;
private bool _pressing = false;
private bool _pressed;
private bool _released;
public Pointer(Canvas canvas)
{
_canvas = canvas;
_pressed = _released = false;
}
public void Press()
{
if (_pressing) return;
_pressing = true;
_pressed = true;
}
public void Release()
{
if (!_pressing) return;
_pressing = false;
_released = true;
}
public void ReadAndResetPressedReleased(out bool pressed, out bool released)
{
pressed = _pressed;
released = _released;
_pressed = _released = false;
}
public void MarkForDeletion()
{
MarkedForDeletion = true;
Release();
}
public void SetPosition(Vector3 position)
{
_position = position;
}
public void SetHoveredSelectable(GameObject hoveredSelectable)
{
_hoveredSelectable = hoveredSelectable;
}
}
protected override void OnEnable()
{
base.OnEnable();
if (_pointerEventCamera == null)
{
_pointerEventCamera = gameObject.AddComponent();
_pointerEventCamera.nearClipPlane = 0.1f;
_pointerEventCamera.enabled = false;
}
RKPointerLisener.OnPointerDragBegin += OnPointerDragBegin;
RKPointerLisener.OnPointerDragEnd += OnPointerDragEnd;
}
private void OnPointerDragBegin(PointerEventData eventData)
{
RKLog.KeyInfo($" ===PointableCanvas=== {eventData.pointerId} is begin dragging");
draggingPointerData.Add(eventData.pointerId);
}
private void OnPointerDragEnd(PointerEventData eventData)
{
RKLog.KeyInfo($" ===PointableCanvas=== {eventData.pointerId} is end dragging");
draggingPointerData.Remove(eventData.pointerId);
}
protected override void OnDisable()
{
ProcessOnDisable();
Destroy(_pointerEventCamera);
_pointerEventCamera = null;
base.OnDisable();
RKPointerLisener.OnPointerDragBegin -= OnPointerDragBegin;
RKPointerLisener.OnPointerDragEnd -= OnPointerDragEnd;
draggingPointerData.Clear();
}
// Based On FindFirstRaycast
protected static RaycastResult FindFirstRaycastWithinCanvas(List candidates, Canvas canvas)
{
GameObject candidateGameObject;
Canvas candidateCanvas;
for (var i = 0; i < candidates.Count; ++i)
{
candidateGameObject = candidates[i].gameObject;
if (candidateGameObject == null) continue;
candidateCanvas = candidateGameObject.GetComponentInParent