// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.Utilities;
using UnityEngine;
namespace Microsoft.MixedReality.Toolkit.UI
{
///
/// Utility class to control of pointers.
/// Hook up buttons to the public functions to turn rays on and off.
///
[AddComponentMenu("Scripts/MRTK/SDK/PointerBehaviorControls")]
public class PointerBehaviorControls : MonoBehaviour
{
///
/// Toggles a pointer's "enabled" behavior. If a pointer's is Default or AlwaysOn,
/// set it to AlwaysOff. Otherwise, set the pointer's behavior to Default.
/// Will set this state for all matching pointers.
///
/// Type of pointer to set
/// Input type of pointer to set
public void TogglePointerEnabled(InputSourceType inputType) where T : class, IMixedRealityPointer
{
PointerBehavior oldBehavior = PointerUtils.GetPointerBehavior(Handedness.Any, inputType);
PointerBehavior newBehavior;
if (oldBehavior == PointerBehavior.AlwaysOff)
{
newBehavior = PointerBehavior.Default;
}
else
{
newBehavior = PointerBehavior.AlwaysOff;
}
PointerUtils.SetPointerBehavior(newBehavior, inputType);
}
///
/// If hand ray is AlwaysOn or Default, set it to off.
/// Otherwise, set behavior to default
///
public void ToggleHandRayEnabled()
{
TogglePointerEnabled(InputSourceType.Hand);
}
///
/// If controller ray is AlwaysOn or Default, set it to off.
/// Otherwise, set behavior to default
///
public void ToggleControllerRayEnabled()
{
TogglePointerEnabled(InputSourceType.Controller);
}
///
/// If hand grab pointer is AlwaysOn or Default, set it to off.
/// Otherwise, set behavior to default
///
public void ToggleHandGrabEnabled()
{
TogglePointerEnabled(InputSourceType.Hand);
}
///
/// If finger poke pointer is AlwaysOn or Default, set it to off.
/// Otherwise, set behavior to default
///
public void ToggleHandPokeEnabled()
{
TogglePointerEnabled(InputSourceType.Hand);
}
///
/// Sets the for all hand rays.
/// If true, behavior will be set to Default.
/// Otherwise it will be set to AlwaysOff
///
public void SetHandRayEnabled(bool isEnabled)
{
PointerUtils.SetHandRayPointerBehavior(isEnabled ? PointerBehavior.Default : PointerBehavior.AlwaysOff,
Handedness.Any);
}
///
/// Sets the for all controller rays to be AlwaysOff
/// If true, behavior will be set to Default.
/// Otherwise it will be set to AlwaysOff
///
public void SetControllerRayEnabled(bool isEnabled)
{
PointerUtils.SetMotionControllerRayPointerBehavior(isEnabled ? PointerBehavior.Default : PointerBehavior.AlwaysOff,
Handedness.Any);
}
///
/// Sets the for the gaze pointer to be AlwaysOff
/// If true, behavior will be set to Default.
/// Otherwise it will be set to AlwaysOff
///
public void SetGazeEnabled(bool isEnabled)
{
PointerUtils.SetGazePointerBehavior(isEnabled ? PointerBehavior.Default : PointerBehavior.AlwaysOff);
}
///
/// Sets the for the grab pointer to be AlwaysOff
/// If true, behavior will be set to Default.
/// Otherwise it will be set to AlwaysOff
///
public void SetGrabEnabled(bool isEnabled)
{
PointerUtils.SetHandGrabPointerBehavior(isEnabled ? PointerBehavior.Default : PointerBehavior.AlwaysOff, Handedness.Any);
}
///
/// Sets the for the poke pointer to be AlwaysOff
/// If true, behavior will be set to Default.
/// Otherwise it will be set to AlwaysOff
///
public void SetPokeEnabled(bool isEnabled)
{
PointerUtils.SetHandPokePointerBehavior(isEnabled ? PointerBehavior.Default : PointerBehavior.AlwaysOff, Handedness.Any);
}
///
/// Sets pointer states to mimic traditional vr behavior.
/// PokePointer will be off
/// GrabPointer will be off
/// HandRayPointer will be off
/// MotionControllerRayPointer will be Default
/// GazePointef will be off
///
public void SetVR()
{
PointerUtils.SetHandPokePointerBehavior(PointerBehavior.AlwaysOff, Handedness.Any);
PointerUtils.SetHandGrabPointerBehavior(PointerBehavior.AlwaysOff, Handedness.Any);
PointerUtils.SetHandRayPointerBehavior(PointerBehavior.AlwaysOff, Handedness.Any);
PointerUtils.SetMotionControllerRayPointerBehavior(PointerBehavior.Default, Handedness.Any);
PointerUtils.SetGazePointerBehavior(PointerBehavior.AlwaysOff);
}
///
/// Sets pointer states to turn off all but the poke pointer
///
public void SetFingerOnly()
{
PointerUtils.SetHandPokePointerBehavior(PointerBehavior.Default, Handedness.Any);
PointerUtils.SetHandGrabPointerBehavior(PointerBehavior.AlwaysOff, Handedness.Any);
PointerUtils.SetHandRayPointerBehavior(PointerBehavior.AlwaysOff, Handedness.Any);
PointerUtils.SetMotionControllerRayPointerBehavior(PointerBehavior.AlwaysOff, Handedness.Any);
PointerUtils.SetGazePointerBehavior(PointerBehavior.AlwaysOff);
}
///
/// Sets pointer behavior to mimic HoloLens 1 interactions, useful
/// for using HoloLens 1 interactions on HoloLens 2.
/// PokePointer will be off
/// GrabPointer will be off
/// HandRayPointer will be off
/// MotionControllerRayPointer will be off
/// GazePointer will be Default
///
public void SetHoloLens1()
{
PointerUtils.SetHandPokePointerBehavior(PointerBehavior.AlwaysOff, Handedness.Any);
PointerUtils.SetHandGrabPointerBehavior(PointerBehavior.AlwaysOff, Handedness.Any);
PointerUtils.SetHandRayPointerBehavior(PointerBehavior.AlwaysOff, Handedness.Any);
PointerUtils.SetMotionControllerRayPointerBehavior(PointerBehavior.AlwaysOff, Handedness.Any);
PointerUtils.SetGazePointerBehavior(PointerBehavior.Default);
}
///
/// Sets pointer behavior to mimic HoloLens 2
/// PokePointer will be Default
/// GrabPointer will be Default
/// HandRayPointer will be Default
/// MotionControllerRayPointer will be off
/// GazePointer will be Off
///
public void SetHoloLens2()
{
PointerUtils.SetHandPokePointerBehavior(PointerBehavior.Default, Handedness.Any);
PointerUtils.SetHandGrabPointerBehavior(PointerBehavior.Default, Handedness.Any);
PointerUtils.SetHandRayPointerBehavior(PointerBehavior.Default, Handedness.Any);
PointerUtils.SetMotionControllerRayPointerBehavior(PointerBehavior.AlwaysOff, Handedness.Any);
PointerUtils.SetGazePointerBehavior(PointerBehavior.AlwaysOff);
}
}
}