/*
* 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
{
///
/// Arguments from events emitted by
/// containing data about the state of the event system.
///
public class PointableCanvasEventArgs
{
///
/// The canvas that's being interacted with.
///
public readonly Canvas Canvas;
///
/// The GameObject that's being hovered, if any.
///
public readonly GameObject Hovered;
///
/// Whether the user is dragging at the time of this event.
/// Corresponds to
///
public readonly bool Dragging;
///
/// Create a new
///
/// The canvas that's being interacted with.
/// The GameObject that's being hovered, if any.
/// Whether the user is dragging at the time of this event.
public PointableCanvasEventArgs(Canvas canvas, GameObject hovered, bool dragging)
{
Canvas = canvas;
Hovered = hovered;
Dragging = dragging;
}
}
///
/// PointableCanvasModule is a context-like object which exists in the scene and handles routing for certain types of
/// s, translating them into Unity pointer events which can be routed to and consumed by Unity Canvases.
/// requires that the scene contain a PointableCanvasModule.
///
public class PointableCanvasModule : PointerInputModule
{
///
/// Global event invoked in response to a on an .
/// Though this event itself is static, it is invoked by the PointableCanvasModule instance in the scene as part of
/// .
///
public static event Action WhenSelected;
///
/// Global event invoked in response to a on an .
/// Though this event itself is static, it is invoked by the PointableCanvasModule instance in the scene as part of
/// .
///
public static event Action WhenUnselected;
///
/// Global event invoked in response to a on an .
/// Though this event itself is static, it is invoked by the PointableCanvasModule instance in the scene as part of
/// .
///
public static event Action WhenSelectableHovered;
///
/// Global event invoked in response to a on an .
/// Though this event itself is static, it is invoked by the PointableCanvasModule instance in the scene as part of
/// .
///
public static event Action WhenSelectableUnhovered;
///
/// Global event invoked whenever a new is started. Though this event itself is static, it is
/// invoked by the PointableCanvasModule instance in the scene as part of .
///
public static event Action WhenPointerStarted;
[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;
[Tooltip("If true, this module will disable other input modules in the event system " +
"and will be the only input module used in the scene.")]
[SerializeField]
private bool _exclusiveMode = false;
///
/// If true, this module will disable other input modules in the event system and will be the only input module used in the
/// scene.
///
public bool ExclusiveMode { get => _exclusiveMode; set => _exclusiveMode = value; }
private Camera _pointerEventCamera;
private static PointableCanvasModule _instance = null;
private static PointableCanvasModule Instance
{
get
{
return _instance;
}
}
///
/// Registers an with the PointableCanvasModule in the scene so that its
/// s can be correctly handled, converted, and forwarded.
///
/// The to register
public static void RegisterPointableCanvas(IPointableCanvas pointerCanvas)
{
Assert.IsNotNull(Instance, $"A {nameof(PointableCanvasModule)} is required in the scene.");
Instance.AddPointerCanvas(pointerCanvas);
}
///
/// Unregisters an with the PointableCanvasModule in the scene. s
/// from that canvas will no longer be propagated to the Unity Canvas.
///
/// The to unregister
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 List _inputModules = new List();
private PointerImpl[] _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)
{
PointerImpl 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)
{
PointerImpl pointer;
switch (evt.Type)
{
case PointerEventType.Hover:
pointer = new PointerImpl(evt.Identifier, canvas);
pointer.PointerEventData = new PointerEventData(eventSystem);
pointer.SetPosition(evt.Pose.position);
_pointerMap.Add(evt.Identifier, pointer);
WhenPointerStarted?.Invoke(pointer);
break;
case PointerEventType.Unhover:
if (_pointerMap.TryGetValue(evt.Identifier, out pointer))
{
_pointerMap.Remove(evt.Identifier);
pointer.MarkForDeletion();
_pointersForDeletion.Add(pointer);
}
break;
case PointerEventType.Select:
if (_pointerMap.TryGetValue(evt.Identifier, out pointer))
{
pointer.SetPosition(evt.Pose.position);
pointer.Press();
}
break;
case PointerEventType.Unselect:
if (_pointerMap.TryGetValue(evt.Identifier, out pointer))
{
pointer.SetPosition(evt.Pose.position);
pointer.Release();
}
break;
case PointerEventType.Move:
if (_pointerMap.TryGetValue(evt.Identifier, out pointer))
{
pointer.SetPosition(evt.Pose.position);
}
break;
case PointerEventType.Cancel:
if (_pointerMap.TryGetValue(evt.Identifier, out pointer))
{
_pointerMap.Remove(evt.Identifier);
ClearPointerSelection(pointer.PointerEventData);
pointer.MarkForDeletion();
_pointersForDeletion.Add(pointer);
}
break;
}
}
///
/// Representation of an ongoing interaction between a
/// and the s at which it points. Instance of this
/// can be observed and leveraged to add pointer interaction functionality no built into PointableCanvasModule,
/// such as scrolling based on controller thumbsticks.
///
public class Pointer
{
internal Pointer(int identifier)
{
Identifier = identifier;
}
///
/// The of the s which control the state of
/// this Pointer. Canonically, this will be the same as the of the
/// actuating interactor.
///
public int Identifier { get; }
internal PointerEventData PointerEventData { get; set; }
///
/// Invoked when the Pointer has been updated, exposing the PointerEventData representing what
/// PointableCanvasModule does and knows about this Pointer's behavior.
///
///
/// PointerEventData is exposed here in order to be leveraged in extending functionality, such as scrolling
/// support. However, modifying this data is not supported and can result in unexpected behavior. Rather than
/// modifying this data, consumers should copy relevant information to their own PointerEventData instances
/// and use those to execute events. For similar reasons, references to this PointerEventData should not be
/// cached off or accessed except during the WhenUpdated callback itself.
///
public event Action WhenUpdated = _ => { };
///
/// Invoked when the pointer is being disposed. Observers can use this signal to clean up any references or
/// resources they may have retained related to this Pointer.
///
public event Action WhenDisposed = () => { };
internal void InvokeWhenUpdated()
{
WhenUpdated(PointerEventData);
}
internal void InvokeWhenDisposed()
{
WhenDisposed();
}
}
///
/// Pointer class that is used for state associated with IPointables that are currently
/// tracked by any IPointableCanvases in the scene.
///
private class PointerImpl : Pointer
{
public bool MarkedForDeletion { get; private set; }
private Canvas _canvas;
public Canvas Canvas => _canvas;
private Vector3 _position;
public Vector3 Position => _position;
private Vector3 _targetPosition;
private GameObject _hoveredSelectable;
public GameObject HoveredSelectable => _hoveredSelectable;
private bool _pressing = false;
private bool _pressed;
private bool _released;
public PointerImpl(int identifier, Canvas canvas) : base(identifier)
{
_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;
_position = _targetPosition;
}
public void MarkForDeletion()
{
MarkedForDeletion = true;
Release();
}
public void SetPosition(Vector3 position)
{
_targetPosition = position;
if (!_released)
{
_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;
}
#if UNITY_EDITOR
protected override void Reset()
{
base.Reset();
_exclusiveMode = true;
}
#endif
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());
if (_exclusiveMode)
{
DisableOtherModules();
}
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();
}
private void DisableOtherModules()
{
GetComponents(_inputModules);
foreach (var module in _inputModules)
{
if (module != this && module.enabled)
{
module.enabled = false;
Debug.Log($"PointableCanvasModule: Disabling {module.GetType().Name}.");
}
}
}
///
/// This is an internal API which is invoked to update the PointableCanvasModule. This overrides the UpdateModule() method of
/// Unity's BaseInputModule, from which PointableCanvasModule is descended, and should not be invoked directly.
///
public override void UpdateModule()
{
base.UpdateModule();
if (_exclusiveMode)
{
if (eventSystem.currentInputModule != null &&
eventSystem.currentInputModule != this)
{
DisableOtherModules();
}
}
}
// 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