using System;
using System.Collections.Generic;
namespace UnityEngine.XR.Interaction.Toolkit
{
///
/// A locomotion provider that allows the user to rotate their rig using a specified 2D axis input.
/// The provider can take input from multiple different devices (such as Left and Right hands).
///
[AddComponentMenu("XR/Locomotion/Legacy/Snap Turn Provider (Device-based)", 11)]
[HelpURL(XRHelpURLConstants.k_DeviceBasedSnapTurnProvider)]
[Obsolete("DeviceBasedSnapTurnProvider has been deprecated in version 3.0.0. Use SnapTurnProvider instead.")]
public class DeviceBasedSnapTurnProvider : SnapTurnProviderBase
{
///
/// Sets which input axis to use when reading from controller input.
///
///
public enum InputAxes
{
///
/// Use the primary touchpad or joystick on a device.
///
Primary2DAxis = 0,
///
/// Use the secondary touchpad or joystick on a device.
///
Secondary2DAxis = 1,
}
[SerializeField]
[Tooltip("The 2D Input Axis on the controller devices that will be used to trigger a snap turn.")]
InputAxes m_TurnUsage = InputAxes.Primary2DAxis;
///
/// The 2D Input Axis on the controller devices that will be used to trigger a snap turn.
///
public InputAxes turnUsage
{
get => m_TurnUsage;
set => m_TurnUsage = value;
}
[SerializeField]
[Tooltip("A list of controllers that allow Snap Turn. If an XRController is not enabled, or does not have input actions enabled, snap turn will not work.")]
List m_Controllers = new List();
///
/// The XRControllers that allow SnapTurn. An XRController must be enabled in order to Snap Turn.
///
public List controllers
{
get => m_Controllers;
set => m_Controllers = value;
}
[SerializeField]
[Tooltip("The deadzone that the controller movement will have to be above to trigger a snap turn.")]
float m_DeadZone = 0.75f;
///
/// The deadzone that the controller movement will have to be above to trigger a snap turn.
///
public float deadZone
{
get => m_DeadZone;
set => m_DeadZone = value;
}
///
/// Mapping of to actual common usage values.
///
static readonly InputFeatureUsage[] k_Vec2UsageList =
{
CommonUsages.primary2DAxis,
CommonUsages.secondary2DAxis,
};
///
protected override Vector2 ReadInput()
{
if (m_Controllers.Count == 0)
return Vector2.zero;
// Accumulate all the controller inputs
var input = Vector2.zero;
var feature = k_Vec2UsageList[(int)m_TurnUsage];
var sqrDeadZone = m_DeadZone * m_DeadZone;
for (var i = 0; i < m_Controllers.Count; ++i)
{
var controller = m_Controllers[i] as XRController;
if (controller != null &&
controller.enableInputActions &&
controller.inputDevice.TryGetFeatureValue(feature, out var controllerInput) &&
controllerInput.sqrMagnitude > sqrDeadZone)
{
input += controllerInput;
}
}
return input;
}
}
}