Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. ////MODIFIED BY TONY!
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. #if UNITY_STANDALONE
  7. using Valve.VR;
  8. #endif
  9.  
  10. namespace UnityEngine.XR.Interaction.Toolkit
  11. {
  12.     /// <summary>
  13.     /// Wraps an input device for the XR Interaction toolkit to add support for SteamVR Input System
  14.     /// </summary>
  15.     public struct InputDeviceWrapper
  16.     {
  17.         /// <summary>
  18.         /// The wrapped Input Device. We'll take positions and rotations from it in any case.
  19.         /// It will also provide inputs with non-SteamVR headsets
  20.         /// </summary>
  21.         private InputDevice m_inputDevice;
  22.  
  23.         /// <summary>
  24.         /// Node we must provide input from
  25.         /// </summary>
  26.         private XRNode m_deviceNode;
  27.  
  28.         /// <summary>
  29.         /// True if there is steamvr activated, false otherwise
  30.         /// </summary>
  31.         private bool m_isSteamVR;
  32.  
  33.         /// <summary>
  34.         /// Constructor
  35.         /// </summary>
  36.         /// <param name="deviceNode">Device from which take the input</param>
  37.         internal InputDeviceWrapper(XRNode deviceNode)
  38.         {
  39.             m_inputDevice = InputDevices.GetDeviceAtXRNode(deviceNode);
  40.             this.m_deviceNode = deviceNode;
  41.             this.m_isSteamVR = m_inputDevice.subsystem != null && m_inputDevice.subsystem.SubsystemDescriptor.id == "OpenVR Input";
  42.         }
  43.  
  44.         /// <summary>
  45.         ///   <para>Read Only. True if the device is currently a valid input device; otherwise false.</para>
  46.         /// </summary>
  47.         public bool isValid
  48.         {
  49.             get
  50.             {
  51.                 return m_inputDevice.isValid;
  52.             }
  53.         }
  54.  
  55.         /// <summary>
  56.         ///   <para>Read Only. The name of the device in the XR system. This is a platform provided unique identifier for the device.</para>
  57.         /// </summary>
  58.         public string name
  59.         {
  60.             get
  61.             {
  62.                 return m_inputDevice.name;
  63.             }
  64.         }
  65.  
  66.         /// <summary>
  67.         ///   <para>Read Only. The InputDeviceRole of the device in the XR system. This is a platform provided description of how the device is used.</para>
  68.         /// </summary>
  69.         [Obsolete("This API has been marked as deprecated and will be removed in future versions. Please use InputDevice.characteristics instead.")]
  70.         public InputDeviceRole role
  71.         {
  72.             get
  73.             {
  74.                 return m_inputDevice.role;
  75.             }
  76.         }
  77.  
  78.         /// <summary>
  79.         ///   <para>The manufacturer of the connected Input Device.</para>
  80.         /// </summary>
  81.         public string manufacturer
  82.         {
  83.             get
  84.             {
  85.                 return m_inputDevice.manufacturer;
  86.             }
  87.         }
  88.  
  89.         /// <summary>
  90.         ///   <para>The serial number of the connected Input Device.  Blank if no serial number is available.</para>
  91.         /// </summary>
  92.         public string serialNumber
  93.         {
  94.             get
  95.             {
  96.                 return m_inputDevice.serialNumber;
  97.             }
  98.         }
  99.  
  100.         /// <summary>
  101.         ///   <para>Read Only. A bitmask of enumerated flags describing the characteristics of this InputDevice.</para>
  102.         /// </summary>
  103.         public InputDeviceCharacteristics characteristics
  104.         {
  105.             get
  106.             {
  107.                 return m_inputDevice.characteristics;
  108.             }
  109.         }
  110.  
  111.         /// <summary>
  112.         ///   <para>Sends a haptic impulse to a device.</para>
  113.         /// </summary>
  114.         /// <param name="channel">The channel to receive the impulse.</param>
  115.         /// <param name="amplitude">The normalized (0.0 to 1.0) amplitude value of the haptic impulse to play on the device.</param>
  116.         /// <param name="duration">The duration in seconds that the haptic impulse will play. Only supported on Oculus.</param>
  117.         /// <returns>
  118.         ///   <para>Returns true if successful. Returns false otherwise.</para>
  119.         /// </returns>
  120.         public bool SendHapticImpulse(uint channel, float amplitude, float duration = 1f)
  121.         {
  122.             return m_inputDevice.SendHapticImpulse(channel, amplitude, duration);
  123.         }
  124.  
  125.         /// <summary>
  126.         ///   <para>Sends a raw buffer of haptic data to the device.</para>
  127.         /// </summary>
  128.         /// <param name="channel">The channel to receive the data.</param>
  129.         /// <param name="buffer">A raw byte buffer that contains the haptic data to send to the device.</param>
  130.         /// <returns>
  131.         ///   <para>Returns true if successful. Returns false otherwise.</para>
  132.         /// </returns>
  133.         public bool SendHapticBuffer(uint channel, byte[] buffer)
  134.         {
  135.             return m_inputDevice.SendHapticBuffer(channel, buffer);
  136.         }
  137.  
  138.         public bool TryGetHapticCapabilities(out HapticCapabilities capabilities)
  139.         {
  140.             return m_inputDevice.TryGetHapticCapabilities(out capabilities);
  141.         }
  142.  
  143.         /// <summary>
  144.         ///   <para>Stop all haptic playback for a device.</para>
  145.         /// </summary>
  146.         public void StopHaptics()
  147.         {
  148.             m_inputDevice.StopHaptics();
  149.         }
  150.  
  151.         public bool TryGetFeatureUsages(List<InputFeatureUsage> featureUsages)
  152.         {
  153.             return m_inputDevice.TryGetFeatureUsages(featureUsages);
  154.         }
  155.  
  156.         public bool TryGetFeatureValue(InputFeatureUsage<bool> usage, out bool value)
  157.         {
  158. #if UNITY_STANDALONE
  159.             if (m_isSteamVR && m_deviceNode.IsHands())
  160.             {
  161.                 if (usage == CommonUsages.triggerButton)
  162.                 {
  163.                     value = SteamVR_Actions._default.GrabPinch[m_deviceNode.ToSteamVrSource()].state;
  164.  
  165.                     return true;
  166.                 }
  167.                 else if (usage == CommonUsages.gripButton)
  168.                 {
  169.                     value = SteamVR_Actions._default.GrabGrip[m_deviceNode.ToSteamVrSource()].state;
  170.  
  171.                     return true;
  172.                 }
  173.             }
  174. #endif
  175.             return m_inputDevice.TryGetFeatureValue(usage, out value);
  176.         }
  177.  
  178.         public bool TryGetFeatureValue(InputFeatureUsage<uint> usage, out uint value)
  179.         {
  180.             return m_inputDevice.TryGetFeatureValue(usage, out value);
  181.         }
  182.  
  183.         public bool TryGetFeatureValue(InputFeatureUsage<float> usage, out float value)
  184.         {
  185. #if UNITY_STANDALONE
  186.             if (m_isSteamVR && m_deviceNode.IsHands())
  187.             {
  188.                 if (usage == CommonUsages.trigger)
  189.                 {
  190.                     value = SteamVR_Actions._default.GrabPinch[m_deviceNode.ToSteamVrSource()].state ? 1 : 0;
  191.  
  192.                     return true;
  193.                 }
  194.                 else if (usage == CommonUsages.grip)
  195.                 {
  196.                     value = SteamVR_Actions._default.Squeeze[m_deviceNode.ToSteamVrSource()].axis;
  197.  
  198.                     return true;
  199.                 }
  200.             }
  201. #endif
  202.             return m_inputDevice.TryGetFeatureValue(usage, out value);
  203.         }
  204.  
  205.         public bool TryGetFeatureValue(InputFeatureUsage<Vector2> usage, out Vector2 value)
  206.         {
  207. #if UNITY_STANDALONE
  208.             if (m_isSteamVR && m_deviceNode.IsHands())
  209.             {
  210.                 if (usage == CommonUsages.primary2DAxis)
  211.                 {
  212.                     value = new Vector2(SteamVR_Actions._default.SnapTurnLeft[m_deviceNode.ToSteamVrSource()].state ? - 1 :
  213.                                             (SteamVR_Actions._default.SnapTurnRight[m_deviceNode.ToSteamVrSource()].state ? 1 : 0),
  214.                                             SteamVR_Actions._default.Teleport[m_deviceNode.ToSteamVrSource()].state ? 1 : 0);
  215.  
  216.                     return true;
  217.                 }              
  218.             }
  219. #endif
  220.             return m_inputDevice.TryGetFeatureValue(usage, out value);
  221.         }
  222.  
  223.         public bool TryGetFeatureValue(InputFeatureUsage<Vector3> usage, out Vector3 value)
  224.         {
  225.             return m_inputDevice.TryGetFeatureValue(usage, out value);
  226.         }
  227.  
  228.         public bool TryGetFeatureValue(InputFeatureUsage<Quaternion> usage, out Quaternion value)
  229.         {
  230.             return m_inputDevice.TryGetFeatureValue(usage, out value);
  231.         }
  232.  
  233.         public bool TryGetFeatureValue(InputFeatureUsage<Hand> usage, out Hand value)
  234.         {
  235.             return m_inputDevice.TryGetFeatureValue(usage, out value);
  236.         }
  237.  
  238.         public bool TryGetFeatureValue(InputFeatureUsage<Bone> usage, out Bone value)
  239.         {
  240.             return m_inputDevice.TryGetFeatureValue(usage, out value);
  241.         }
  242.  
  243.         public bool TryGetFeatureValue(InputFeatureUsage<Eyes> usage, out Eyes value)
  244.         {
  245.             return m_inputDevice.TryGetFeatureValue(usage, out value);
  246.         }
  247.  
  248.         public bool TryGetFeatureValue(InputFeatureUsage<byte[]> usage, byte[] value)
  249.         {
  250.             return m_inputDevice.TryGetFeatureValue(usage, value);
  251.         }
  252.  
  253.         public bool TryGetFeatureValue(
  254.           InputFeatureUsage<InputTrackingState> usage,
  255.           out InputTrackingState value)
  256.         {
  257.             return m_inputDevice.TryGetFeatureValue(usage, out value);
  258.         }
  259.  
  260.         public bool TryGetFeatureValue(InputFeatureUsage<bool> usage, DateTime time, out bool value)
  261.         {
  262.             return m_inputDevice.TryGetFeatureValue(usage, time, out value);
  263.         }
  264.  
  265.         public bool TryGetFeatureValue(InputFeatureUsage<uint> usage, DateTime time, out uint value)
  266.         {
  267.             return m_inputDevice.TryGetFeatureValue(usage, time, out value);
  268.         }
  269.  
  270.         public bool TryGetFeatureValue(InputFeatureUsage<float> usage, DateTime time, out float value)
  271.         {
  272.             return m_inputDevice.TryGetFeatureValue(usage, time, out value);
  273.         }
  274.  
  275.         public bool TryGetFeatureValue(
  276.           InputFeatureUsage<Vector2> usage,
  277.           DateTime time,
  278.           out Vector2 value)
  279.         {
  280.             return m_inputDevice.TryGetFeatureValue(usage, time, out value);
  281.         }
  282.  
  283.         public bool TryGetFeatureValue(
  284.           InputFeatureUsage<Vector3> usage,
  285.           DateTime time,
  286.           out Vector3 value)
  287.         {
  288.             return m_inputDevice.TryGetFeatureValue(usage, time, out value);
  289.         }
  290.  
  291.         public bool TryGetFeatureValue(
  292.           InputFeatureUsage<Quaternion> usage,
  293.           DateTime time,
  294.           out Quaternion value)
  295.         {
  296.             return m_inputDevice.TryGetFeatureValue(usage, time, out value);
  297.         }
  298.  
  299.         public bool TryGetFeatureValue(
  300.           InputFeatureUsage<InputTrackingState> usage,
  301.           DateTime time,
  302.           out InputTrackingState value)
  303.         {
  304.             return m_inputDevice.TryGetFeatureValue(usage, time, out value);
  305.         }
  306.  
  307.     }
  308.  
  309. #if UNITY_STANDALONE
  310.  
  311.     /// <summary>
  312.     /// Helpers for use of XRNode input types with steam
  313.     /// </summary>
  314.     public static class InputXrNodeUtilities
  315.     {
  316.         /// <summary>
  317.         /// True if the node represents a hand
  318.         /// </summary>
  319.         /// <param name="node"></param>
  320.         public static bool IsHands(this XRNode node)
  321.         {            
  322.             return node == XRNode.LeftHand || node == XRNode.RightHand;
  323.         }
  324.  
  325.         /// <summary>
  326.         /// Converts between XRNode and SteamVR Input Sources
  327.         /// </summary>
  328.         /// <param name="node"></param>
  329.         /// <returns></returns>
  330.         public static SteamVR_Input_Sources ToSteamVrSource(this XRNode node)
  331.         {
  332.             //this switch should be expanded according to necessities
  333.             switch(node)
  334.             {
  335.                 case XRNode.LeftHand:
  336.                     return SteamVR_Input_Sources.LeftHand;
  337.                 case XRNode.RightHand:
  338.                     return SteamVR_Input_Sources.RightHand;
  339.                 case XRNode.Head:
  340.                     return SteamVR_Input_Sources.Head;
  341.  
  342.                 default:
  343.                     return SteamVR_Input_Sources.Any;
  344.             }
  345.         }
  346.     }
  347.  
  348. #endif
  349. }
  350.