Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace UnityEngine.XR.Interaction.Toolkit
  6. {
  7.     /// <summary>
  8.     /// The snap turn provider is a locomotion provider that allows the user to rotate their rig using a specified 2d axis input.
  9.     /// the provider can take input from two different devices (eg: L & R hands).
  10.     /// </summary>
  11.     public sealed class SnapTurnProvider : LocomotionProvider
  12.     {
  13.         /// <summary>
  14.         /// This is the list of possible valid "InputAxes" that we allow users to read from.
  15.         /// </summary>
  16.         public enum InputAxes
  17.         {
  18.             Primary2DAxis = 0,
  19.             Secondary2DAxis = 1,  
  20.         };
  21.  
  22.         // Mapping of the above InputAxes to actual common usage values
  23.         static readonly InputFeatureUsage<Vector2>[] m_Vec2UsageList = new InputFeatureUsage<Vector2>[] {
  24.             CommonUsages.primary2DAxis,
  25.             CommonUsages.secondary2DAxis,            
  26.         };
  27.  
  28.         [SerializeField]
  29.         [Tooltip("The 2D Input Axis on the primary devices that will be used to trigger a snap turn.")]
  30.         InputAxes m_TurnUsage = InputAxes.Primary2DAxis;
  31.         /// <summary>
  32.         /// The 2D Input Axis on the primary device that will be used to trigger a snap turn.
  33.         /// </summary>
  34.         public InputAxes turnUsage { get { return m_TurnUsage; } set { m_TurnUsage = value; } }
  35.  
  36.         [SerializeField]
  37.         [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.")]
  38.         List<XRController> m_Controllers = new List<XRController>();
  39.         /// <summary>
  40.         /// The XRControllers that allow SnapTurn.  An XRController must be enabled in order to Snap Turn.
  41.         /// </summary>
  42.         public List<XRController> controllers { get { return m_Controllers; } set { m_Controllers = value; } }
  43.  
  44.         [SerializeField]
  45.         [Tooltip("The number of degrees clockwise to rotate when snap turning clockwise.")]
  46.         float m_TurnAmount = 45.0f;
  47.         /// <summary>
  48.         /// The number of degrees clockwise to rotate when snap turning clockwise.
  49.         /// </summary>
  50.         public float turnAmount {  get { return m_TurnAmount; } set { m_TurnAmount = value; } }
  51.  
  52.         [SerializeField]
  53.         [Tooltip("The amount of time that the system will wait before starting another snap turn.")]
  54.         float m_DebounceTime = 0.5f;
  55.         /// <summary>
  56.         /// The amount of time that the system will wait before starting another snap turn.
  57.         /// </summary>
  58.         public float debounceTime { get { return m_DebounceTime; } set { m_DebounceTime = value; } }
  59.  
  60.         [SerializeField]
  61.         [Tooltip("The deadzone that the controller movement will have to be above to trigger a snap turn.")]
  62.         float m_DeadZone = 0.75f;
  63.         /// <summary>
  64.         /// The deadzone that the controller movement will have to be above to trigger a snap turn.
  65.         /// </summary>
  66.         public float deadZone {  get { return m_DeadZone;  } set { m_DeadZone = value; } }
  67.  
  68.         // state data
  69.         float m_CurrentTurnAmount = 0.0f;
  70.         float m_TimeStarted = 0.0f;
  71.  
  72.         List<bool> m_ControllersWereActive = new List<bool>();
  73.  
  74.         private void Update()
  75.         {          
  76.             // wait for a certain amount of time before allowing another turn.
  77.             if (m_TimeStarted > 0.0f && (m_TimeStarted + m_DebounceTime < Time.time))
  78.             {
  79.                 m_TimeStarted = 0.0f;
  80.                 return;
  81.             }
  82.  
  83.             if(m_Controllers.Count > 0)
  84.             {
  85.                 EnsureControllerDataListSize();
  86.  
  87.                 InputFeatureUsage<Vector2> feature = m_Vec2UsageList[(int)m_TurnUsage];
  88.                 for (int i = 0; i < m_Controllers.Count; i++)
  89.                 {                    
  90.                     XRController controller = m_Controllers[i];
  91.                     if (controller != null)
  92.                     {
  93.                         if (controller.enableInputActions && m_ControllersWereActive[i])
  94.                         {
  95.                             ////MODIFIED BY TONY!
  96.                             var device = controller.inputDevice;
  97.  
  98.                             Vector2 currentState;
  99.                             if (device.TryGetFeatureValue(feature, out currentState))
  100.                             {
  101.                                 if (currentState.x > deadZone)
  102.                                 {
  103.                                     StartTurn(m_TurnAmount);
  104.                                 }
  105.                                 else if (currentState.x < -deadZone)
  106.                                 {
  107.                                     StartTurn(-m_TurnAmount);
  108.                                 }
  109.                             }
  110.                         }
  111.                         else //This adds a 1 frame delay when enabling input actions, so that the frame it's enabled doesn't trigger a snap turn.
  112.                         {
  113.                             m_ControllersWereActive[i] = controller.enableInputActions;
  114.                         }
  115.                     }
  116.                 }
  117.             }
  118.            
  119.             if (Math.Abs(m_CurrentTurnAmount) > 0.0f && BeginLocomotion())
  120.             {
  121.                 var xrRig = system.xrRig;
  122.                 if (xrRig != null)
  123.                 {
  124.                     xrRig.RotateAroundCameraUsingRigUp(m_CurrentTurnAmount);
  125.                 }
  126.                 m_CurrentTurnAmount = 0.0f;
  127.                 EndLocomotion();
  128.             }
  129.         }
  130.  
  131.         void EnsureControllerDataListSize()
  132.         {
  133.             if(m_Controllers.Count != m_ControllersWereActive.Count)
  134.             {
  135.                 while(m_ControllersWereActive.Count < m_Controllers.Count)
  136.                 {
  137.                     m_ControllersWereActive.Add(false);
  138.                 }
  139.  
  140.                 while(m_ControllersWereActive.Count < m_Controllers.Count)
  141.                 {
  142.                     m_ControllersWereActive.RemoveAt(m_ControllersWereActive.Count - 1);
  143.                 }
  144.             }
  145.         }
  146.  
  147.         internal void FakeStartTurn(bool isLeft)
  148.         {
  149.             StartTurn(isLeft ? -m_TurnAmount : m_TurnAmount);
  150.         }
  151.  
  152.         private void StartTurn(float amount)
  153.         {
  154.             if (m_TimeStarted != 0.0f)
  155.                 return;
  156.  
  157.             if (!CanBeginLocomotion())
  158.                 return;
  159.            
  160.             m_TimeStarted = Time.time;
  161.             m_CurrentTurnAmount = amount;            
  162.         }
  163.     }
  164. }
  165.