// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.MixedReality.Toolkit.UI;
using UnityEngine;
namespace Microsoft.MixedReality.Toolkit
{
///
/// This class exists to route events through to .
/// The result is being able to have physical touch call Interactable.OnPointerClicked.
///
[AddComponentMenu("Scripts/MRTK/SDK/PhysicalPressEventRouter")]
public class PhysicalPressEventRouter : MonoBehaviour
{
[Tooltip("Interactable to which the press events are being routed. Defaults to the object of the component.")]
public Interactable routingTarget;
/// Enum specifying which button event causes a Click to be raised.
public enum PhysicalPressEventBehavior
{
EventOnClickCompletion = 0,
EventOnPress,
EventOnTouch
}
public PhysicalPressEventBehavior InteractableOnClick = PhysicalPressEventBehavior.EventOnClickCompletion;
private void Awake()
{
if (routingTarget == null)
{
routingTarget = GetComponent();
}
}
private bool CanRouteInput()
{
return routingTarget != null && routingTarget.IsEnabled;
}
///
/// Gets called when the TouchBegin event is invoked within the default PressableButton and
/// PressableButtonHoloLens2 components. When the physical touch with a
/// hand has begun, set physical touch state within Interactable.
///
public void OnHandPressTouched()
{
if (CanRouteInput())
{
routingTarget.HasPhysicalTouch = true;
if (InteractableOnClick == PhysicalPressEventBehavior.EventOnTouch)
{
routingTarget.HasPress = true;
routingTarget.TriggerOnClick();
routingTarget.HasPress = false;
}
}
}
///
/// Gets called when the TouchEnd event is invoked within the default PressableButton and
/// PressableButtonHoloLens2 components. Once the physical touch with a hand is removed, set
/// the physical touch and possibly press state within Interactable.
///
public void OnHandPressUntouched()
{
if (CanRouteInput())
{
routingTarget.HasPhysicalTouch = false;
if (InteractableOnClick == PhysicalPressEventBehavior.EventOnTouch)
{
routingTarget.HasPress = true;
}
}
}
///
/// Gets called when the ButtonPressed event is invoked within the default PressableButton and
/// PressableButtonHoloLens2 components. When the physical press with a hand is triggered, set
/// the physical touch and press state within Interactable.
///
public void OnHandPressTriggered()
{
if (CanRouteInput())
{
routingTarget.HasPhysicalTouch = true;
routingTarget.HasPress = true;
if (InteractableOnClick == PhysicalPressEventBehavior.EventOnPress)
{
routingTarget.TriggerOnClick();
}
}
}
///
/// Gets called when the ButtonReleased event is invoked within the default PressableButton and
/// PressableButtonHoloLens2 components. Once the physical press with a hand is completed, set
/// the press and physical touch states within Interactable
///
public void OnHandPressCompleted()
{
if (CanRouteInput())
{
routingTarget.HasPhysicalTouch = true;
routingTarget.HasPress = true;
if (InteractableOnClick == PhysicalPressEventBehavior.EventOnClickCompletion)
{
routingTarget.TriggerOnClick();
}
routingTarget.HasPress = false;
routingTarget.HasPhysicalTouch = false;
}
}
}
}