/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* Licensed under the Oculus SDK License Agreement (the "License");
* you may not use the Oculus SDK except in compliance with the License,
* which is provided at the time of installation or download, or which
* otherwise accompanies this software in either electronic or hard copy form.
*
* You may obtain a copy of the License at
*
* https://developer.oculus.com/licenses/oculussdk/
*
* Unless required by applicable law or agreed to in writing, the Oculus SDK
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine;
using UnityEngine.Assertions;
using System;
namespace Oculus.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;
[Tooltip("If true, the initial press position will be used as the drag start " +
"position, rather than the position when drag threshold is exceeded. This is used " +
"to prevent the pointer position shifting relative to the surface while dragging.")]
[SerializeField]
private bool _useInitialPressPositionForDrag = true;
private Camera _pointerEventCamera;
private static PointableCanvasModule _instance = null;
private static PointableCanvasModule Instance
{
get
{
return _instance;
}
}
public static void RegisterPointableCanvas(IPointableCanvas pointerCanvas)
{
Assert.IsNotNull(Instance, $"A {nameof(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 List _pointersForDeletion = new List();
private Dictionary> _pointerCanvasActionMap =
new Dictionary>();
private Pointer[] _pointersToProcessScratch = Array.Empty();
private void AddPointerCanvas(IPointableCanvas pointerCanvas)
{
Action pointerCanvasAction = (args) => HandlePointerEvent(pointerCanvas.Canvas, args);
_pointerCanvasActionMap.Add(pointerCanvas, pointerCanvasAction);
pointerCanvas.WhenPointerEventRaised += pointerCanvasAction;
}
private void RemovePointerCanvas(IPointableCanvas pointerCanvas)
{
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.Add(pointer);
_pointerMap.Remove(pointerID);
}
}
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);
break;
case PointerEventType.Unhover:
pointer = _pointerMap[evt.Identifier];
_pointerMap.Remove(evt.Identifier);
pointer.MarkForDeletion();
_pointersForDeletion.Add(pointer);
break;
case PointerEventType.Select:
pointer = _pointerMap[evt.Identifier];
pointer.SetPosition(evt.Pose.position);
pointer.Press();
break;
case PointerEventType.Unselect:
pointer = _pointerMap[evt.Identifier];
pointer.SetPosition(evt.Pose.position);
pointer.Release();
break;
case PointerEventType.Move:
pointer = _pointerMap[evt.Identifier];
pointer.SetPosition(evt.Pose.position);
break;
case PointerEventType.Cancel:
pointer = _pointerMap[evt.Identifier];
_pointerMap.Remove(evt.Identifier);
ClearPointerSelection(pointer.PointerEventData);
pointer.MarkForDeletion();
_pointersForDeletion.Add(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 Awake()
{
base.Awake();
Assert.IsNull(_instance, "There must be at most one PointableCanvasModule in the scene");
_instance = this;
}
protected override void OnDestroy()
{
// Must unset _instance prior to calling the base.OnDestroy, otherwise error is thrown:
// Can't add component to object that is being destroyed.
// UnityEngine.EventSystems.BaseInputModule:get_input ()
_instance = null;
base.OnDestroy();
}
protected bool _started = false;
protected override void Start()
{
this.BeginStart(ref _started, () => base.Start());
this.EndStart(ref _started);
}
protected override void OnEnable()
{
base.OnEnable();
if (_started)
{
_pointerEventCamera = gameObject.AddComponent();
_pointerEventCamera.nearClipPlane = 0.1f;
// We do not need this camera to be enabled to serve this module's purposes:
// as a dependency for Canvases and for its WorldToScreenPoint functionality
_pointerEventCamera.enabled = false;
}
}
protected override void OnDisable()
{
if (_started)
{
Destroy(_pointerEventCamera);
_pointerEventCamera = null;
}
base.OnDisable();
}
// 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