Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3807 lines
155 KiB

  1. /************************************************************************************
  2. Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
  4. the Utilities SDK except in compliance with the License, which is provided at the time of installation
  5. or download, or which otherwise accompanies this software in either electronic or hard copy form.
  6. You may obtain a copy of the License at
  7. https://developer.oculus.com/licenses/utilities-1.31
  8. Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
  9. under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  10. ANY KIND, either express or implied. See the License for the specific language governing
  11. permissions and limitations under the License.
  12. ************************************************************************************/
  13. using System;
  14. using System.Collections;
  15. using System.Collections.Generic;
  16. using System.Runtime.InteropServices;
  17. using UnityEngine;
  18. #if UNITY_2017_2_OR_NEWER
  19. using InputTracking = UnityEngine.XR.InputTracking;
  20. using Node = UnityEngine.XR.XRNode;
  21. using Settings = UnityEngine.XR.XRSettings;
  22. #else
  23. using InputTracking = UnityEngine.VR.InputTracking;
  24. using Node = UnityEngine.VR.VRNode;
  25. using Settings = UnityEngine.VR.VRSettings;
  26. #endif
  27. /// <summary>
  28. /// Provides a unified input system for Oculus controllers and gamepads.
  29. /// </summary>
  30. public static class OVRInput
  31. {
  32. [Flags]
  33. /// Virtual button mappings that allow the same input bindings to work across different controllers.
  34. public enum Button
  35. {
  36. None = 0, ///< Maps to RawButton: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  37. One = 0x00000001, ///< Maps to RawButton: [Gamepad, Touch, RTouch: A], [LTouch: X], [LTrackedRemote: LTouchpad], [RTrackedRemote: RTouchpad], [Touchpad, Remote: Start]
  38. Two = 0x00000002, ///< Maps to RawButton: [Gamepad, Touch, RTouch: B], [LTouch: Y], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: Back]
  39. Three = 0x00000004, ///< Maps to RawButton: [Gamepad, Touch: X], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  40. Four = 0x00000008, ///< Maps to RawButton: [Gamepad, Touch: Y], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  41. Start = 0x00000100, ///< Maps to RawButton: [Gamepad: Start], [Touch, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: Start], [RTouch: None]
  42. Back = 0x00000200, ///< Maps to RawButton: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: Back], [Touch, LTouch, RTouch: None]
  43. PrimaryShoulder = 0x00001000, ///< Maps to RawButton: [Gamepad: LShoulder], [Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  44. PrimaryIndexTrigger = 0x00002000, ///< Maps to RawButton: [Gamepad, Touch, LTouch, LTrackedRemote: LIndexTrigger], [RTouch, RTrackedRemote: RIndexTrigger], [Touchpad, Remote: None]
  45. PrimaryHandTrigger = 0x00004000, ///< Maps to RawButton: [Touch, LTouch: LHandTrigger], [RTouch: RHandTrigger], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  46. PrimaryThumbstick = 0x00008000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstick], [RTouch: RThumbstick], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  47. PrimaryThumbstickUp = 0x00010000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickUp], [RTouch: RThumbstickUp], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  48. PrimaryThumbstickDown = 0x00020000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickDown], [RTouch: RThumbstickDown], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  49. PrimaryThumbstickLeft = 0x00040000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickLeft], [RTouch: RThumbstickLeft], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  50. PrimaryThumbstickRight = 0x00080000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickRight], [RTouch: RThumbstickRight], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  51. PrimaryTouchpad = 0x00000400, ///< Maps to RawButton: [LTrackedRemote, Touchpad: LTouchpad], [RTrackedRemote: RTouchpad], [Gamepad, Touch, LTouch, RTouch, Remote: None]
  52. SecondaryShoulder = 0x00100000, ///< Maps to RawButton: [Gamepad: RShoulder], [Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  53. SecondaryIndexTrigger = 0x00200000, ///< Maps to RawButton: [Gamepad, Touch: RIndexTrigger], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  54. SecondaryHandTrigger = 0x00400000, ///< Maps to RawButton: [Touch: RHandTrigger], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  55. SecondaryThumbstick = 0x00800000, ///< Maps to RawButton: [Gamepad, Touch: RThumbstick], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  56. SecondaryThumbstickUp = 0x01000000, ///< Maps to RawButton: [Gamepad, Touch: RThumbstickUp], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  57. SecondaryThumbstickDown = 0x02000000, ///< Maps to RawButton: [Gamepad, Touch: RThumbstickDown], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  58. SecondaryThumbstickLeft = 0x04000000, ///< Maps to RawButton: [Gamepad, Touch: RThumbstickLeft], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  59. SecondaryThumbstickRight = 0x08000000, ///< Maps to RawButton: [Gamepad, Touch: RThumbstickRight], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  60. SecondaryTouchpad = 0x00000800, ///< Maps to RawButton: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  61. DpadUp = 0x00000010, ///< Maps to RawButton: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadUp], [Touch, LTouch, RTouch: None]
  62. DpadDown = 0x00000020, ///< Maps to RawButton: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadDown], [Touch, LTouch, RTouch: None]
  63. DpadLeft = 0x00000040, ///< Maps to RawButton: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadLeft], [Touch, LTouch, RTouch: None]
  64. DpadRight = 0x00000080, ///< Maps to RawButton: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadRight], [Touch, LTouch, RTouch: None]
  65. Up = 0x10000000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickUp], [RTouch: RThumbstickUp], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadUp]
  66. Down = 0x20000000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickDown], [RTouch: RThumbstickDown], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadDown]
  67. Left = 0x40000000, ///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickLeft], [RTouch: RThumbstickLeft], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadLeft]
  68. Right = unchecked((int)0x80000000),///< Maps to RawButton: [Gamepad, Touch, LTouch: LThumbstickRight], [RTouch: RThumbstickRight], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadRight]
  69. Any = ~None, ///< Maps to RawButton: [Gamepad, Touch, LTouch, RTouch: Any]
  70. }
  71. [Flags]
  72. /// Raw button mappings that can be used to directly query the state of a controller.
  73. public enum RawButton
  74. {
  75. None = 0, ///< Maps to Physical Button: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  76. A = 0x00000001, ///< Maps to Physical Button: [Gamepad, Touch, RTouch: A], [LTrackedRemote: LIndexTrigger], [RTrackedRemote: RIndexTrigger], [LTouch, Touchpad, Remote: None]
  77. B = 0x00000002, ///< Maps to Physical Button: [Gamepad, Touch, RTouch: B], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  78. X = 0x00000100, ///< Maps to Physical Button: [Gamepad, Touch, LTouch: X], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  79. Y = 0x00000200, ///< Maps to Physical Button: [Gamepad, Touch, LTouch: Y], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  80. Start = 0x00100000, ///< Maps to Physical Button: [Gamepad, Touch, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: Start], [RTouch: None]
  81. Back = 0x00200000, ///< Maps to Physical Button: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: Back], [Touch, LTouch, RTouch: None]
  82. LShoulder = 0x00000800, ///< Maps to Physical Button: [Gamepad: LShoulder], [Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  83. LIndexTrigger = 0x10000000, ///< Maps to Physical Button: [Gamepad, Touch, LTouch, LTrackedRemote: LIndexTrigger], [RTouch, RTrackedRemote, Touchpad, Remote: None]
  84. LHandTrigger = 0x20000000, ///< Maps to Physical Button: [Touch, LTouch: LHandTrigger], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  85. LThumbstick = 0x00000400, ///< Maps to Physical Button: [Gamepad, Touch, LTouch: LThumbstick], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  86. LThumbstickUp = 0x00000010, ///< Maps to Physical Button: [Gamepad, Touch, LTouch: LThumbstickUp], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  87. LThumbstickDown = 0x00000020, ///< Maps to Physical Button: [Gamepad, Touch, LTouch: LThumbstickDown], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  88. LThumbstickLeft = 0x00000040, ///< Maps to Physical Button: [Gamepad, Touch, LTouch: LThumbstickLeft], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  89. LThumbstickRight = 0x00000080, ///< Maps to Physical Button: [Gamepad, Touch, LTouch: LThumbstickRight], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  90. LTouchpad = 0x40000000, ///< Maps to Physical Button: [LTrackedRemote: LTouchpad], [Gamepad, Touch, LTouch, RTouch, RTrackedRemote, Touchpad, Remote: None]
  91. RShoulder = 0x00000008, ///< Maps to Physical Button: [Gamepad: RShoulder], [Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  92. RIndexTrigger = 0x04000000, ///< Maps to Physical Button: [Gamepad, Touch, RTouch, RTrackedRemote: RIndexTrigger], [LTouch, LTrackedRemote, Touchpad, Remote: None]
  93. RHandTrigger = 0x08000000, ///< Maps to Physical Button: [Touch, RTouch: RHandTrigger], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  94. RThumbstick = 0x00000004, ///< Maps to Physical Button: [Gamepad, Touch, RTouch: RThumbstick], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  95. RThumbstickUp = 0x00001000, ///< Maps to Physical Button: [Gamepad, Touch, RTouch: RThumbstickUp], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  96. RThumbstickDown = 0x00002000, ///< Maps to Physical Button: [Gamepad, Touch, RTouch: RThumbstickDown], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  97. RThumbstickLeft = 0x00004000, ///< Maps to Physical Button: [Gamepad, Touch, RTouch: RThumbstickLeft], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  98. RThumbstickRight = 0x00008000, ///< Maps to Physical Button: [Gamepad, Touch, RTouch: RThumbstickRight], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  99. RTouchpad = unchecked((int)0x80000000),///< Maps to Physical Button: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  100. DpadUp = 0x00010000, ///< Maps to Physical Button: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadUp], [Touch, LTouch, RTouch: None]
  101. DpadDown = 0x00020000, ///< Maps to Physical Button: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadDown], [Touch, LTouch, RTouch: None]
  102. DpadLeft = 0x00040000, ///< Maps to Physical Button: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadLeft], [Touch, LTouch, RTouch: None]
  103. DpadRight = 0x00080000, ///< Maps to Physical Button: [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: DpadRight], [Touch, LTouch, RTouch: None]
  104. Any = ~None, ///< Maps to Physical Button: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: Any]
  105. }
  106. [Flags]
  107. /// Virtual capacitive touch mappings that allow the same input bindings to work across different controllers with capacitive touch support.
  108. public enum Touch
  109. {
  110. None = 0, ///< Maps to RawTouch: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  111. One = Button.One, ///< Maps to RawTouch: [Touch, RTouch: A], [LTouch: X], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  112. Two = Button.Two, ///< Maps to RawTouch: [Touch, RTouch: B], [LTouch: Y], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  113. Three = Button.Three, ///< Maps to RawTouch: [Touch: X], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  114. Four = Button.Four, ///< Maps to RawTouch: [Touch: Y], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  115. PrimaryIndexTrigger = Button.PrimaryIndexTrigger, ///< Maps to RawTouch: [Touch, LTouch: LIndexTrigger], [RTouch: RIndexTrigger], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  116. PrimaryThumbstick = Button.PrimaryThumbstick, ///< Maps to RawTouch: [Touch, LTouch: LThumbstick], [RTouch: RThumbstick], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  117. PrimaryThumbRest = 0x00001000, ///< Maps to RawTouch: [Touch, LTouch: LThumbRest], [RTouch: RThumbRest], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  118. PrimaryTouchpad = Button.PrimaryTouchpad, ///< Maps to RawTouch: [LTrackedRemote, Touchpad: LTouchpad], [RTrackedRemote: RTouchpad], [Gamepad, Touch, LTouch, RTouch, Remote: None]
  119. SecondaryIndexTrigger = Button.SecondaryIndexTrigger, ///< Maps to RawTouch: [Touch: RIndexTrigger], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  120. SecondaryThumbstick = Button.SecondaryThumbstick, ///< Maps to RawTouch: [Touch: RThumbstick], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  121. SecondaryThumbRest = 0x00100000, ///< Maps to RawTouch: [Touch: RThumbRest], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  122. SecondaryTouchpad = Button.SecondaryTouchpad, ///< Maps to RawTouch: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  123. Any = ~None, ///< Maps to RawTouch: [Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad: Any], [Gamepad, Remote: None]
  124. }
  125. [Flags]
  126. /// Raw capacitive touch mappings that can be used to directly query the state of a controller.
  127. public enum RawTouch
  128. {
  129. None = 0, ///< Maps to Physical Touch: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  130. A = RawButton.A, ///< Maps to Physical Touch: [Touch, RTouch: A], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  131. B = RawButton.B, ///< Maps to Physical Touch: [Touch, RTouch: B], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  132. X = RawButton.X, ///< Maps to Physical Touch: [Touch, LTouch: X], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  133. Y = RawButton.Y, ///< Maps to Physical Touch: [Touch, LTouch: Y], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  134. LIndexTrigger = 0x00001000, ///< Maps to Physical Touch: [Touch, LTouch: LIndexTrigger], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  135. LThumbstick = RawButton.LThumbstick, ///< Maps to Physical Touch: [Touch, LTouch: LThumbstick], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  136. LThumbRest = 0x00000800, ///< Maps to Physical Touch: [Touch, LTouch: LThumbRest], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  137. LTouchpad = RawButton.LTouchpad, ///< Maps to Physical Touch: [LTrackedRemote, Touchpad: LTouchpad], [Gamepad, Touch, LTouch, RTouch, RTrackedRemote, Remote: None]
  138. RIndexTrigger = 0x00000010, ///< Maps to Physical Touch: [Touch, RTouch: RIndexTrigger], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  139. RThumbstick = RawButton.RThumbstick, ///< Maps to Physical Touch: [Touch, RTouch: RThumbstick], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  140. RThumbRest = 0x00000008, ///< Maps to Physical Touch: [Touch, RTouch: RThumbRest], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  141. RTouchpad = RawButton.RTouchpad, ///< Maps to Physical Touch: [RTrackedRemote: RTouchpad], [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, Touchpad, Remote: None]
  142. Any = ~None, ///< Maps to Physical Touch: [Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad: Any], [Gamepad, Remote: None]
  143. }
  144. [Flags]
  145. /// Virtual near touch mappings that allow the same input bindings to work across different controllers with near touch support.
  146. /// A near touch uses the capacitive touch sensors of a controller to detect approximate finger proximity prior to a full touch being reported.
  147. public enum NearTouch
  148. {
  149. None = 0, ///< Maps to RawNearTouch: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  150. PrimaryIndexTrigger = 0x00000001, ///< Maps to RawNearTouch: [Touch, LTouch: LIndexTrigger], [RTouch: RIndexTrigger], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  151. PrimaryThumbButtons = 0x00000002, ///< Maps to RawNearTouch: [Touch, LTouch: LThumbButtons], [RTouch: RThumbButtons], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  152. SecondaryIndexTrigger = 0x00000004, ///< Maps to RawNearTouch: [Touch: RIndexTrigger], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  153. SecondaryThumbButtons = 0x00000008, ///< Maps to RawNearTouch: [Touch: RThumbButtons], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  154. Any = ~None, ///< Maps to RawNearTouch: [Touch, LTouch, RTouch: Any], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  155. }
  156. [Flags]
  157. /// Raw near touch mappings that can be used to directly query the state of a controller.
  158. public enum RawNearTouch
  159. {
  160. None = 0, ///< Maps to Physical NearTouch: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  161. LIndexTrigger = 0x00000001, ///< Maps to Physical NearTouch: [Touch, LTouch: Implies finger is in close proximity to LIndexTrigger.], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  162. LThumbButtons = 0x00000002, ///< Maps to Physical NearTouch: [Touch, LTouch: Implies thumb is in close proximity to LThumbstick OR X/Y buttons.], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  163. RIndexTrigger = 0x00000004, ///< Maps to Physical NearTouch: [Touch, RTouch: Implies finger is in close proximity to RIndexTrigger.], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  164. RThumbButtons = 0x00000008, ///< Maps to Physical NearTouch: [Touch, RTouch: Implies thumb is in close proximity to RThumbstick OR A/B buttons.], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  165. Any = ~None, ///< Maps to Physical NearTouch: [Touch, LTouch, RTouch: Any], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  166. }
  167. [Flags]
  168. /// Virtual 1-dimensional axis (float) mappings that allow the same input bindings to work across different controllers.
  169. public enum Axis1D
  170. {
  171. None = 0, ///< Maps to RawAxis1D: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  172. PrimaryIndexTrigger = 0x01, ///< Maps to RawAxis1D: [Gamepad, Touch, LTouch: LIndexTrigger], [RTouch: RIndexTrigger], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  173. PrimaryHandTrigger = 0x04, ///< Maps to RawAxis1D: [Touch, LTouch: LHandTrigger], [RTouch: RHandTrigger], [Gamepad, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  174. SecondaryIndexTrigger = 0x02, ///< Maps to RawAxis1D: [Gamepad, Touch: RIndexTrigger], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  175. SecondaryHandTrigger = 0x08, ///< Maps to RawAxis1D: [Touch: RHandTrigger], [Gamepad, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  176. Any = ~None, ///< Maps to RawAxis1D: [Gamepad, Touch, LTouch, RTouch: Any], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  177. }
  178. [Flags]
  179. /// Raw 1-dimensional axis (float) mappings that can be used to directly query the state of a controller.
  180. public enum RawAxis1D
  181. {
  182. None = 0, ///< Maps to Physical Axis1D: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  183. LIndexTrigger = 0x01, ///< Maps to Physical Axis1D: [Gamepad, Touch, LTouch: LIndexTrigger], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  184. LHandTrigger = 0x04, ///< Maps to Physical Axis1D: [Touch, LTouch: LHandTrigger], [Gamepad, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  185. RIndexTrigger = 0x02, ///< Maps to Physical Axis1D: [Gamepad, Touch, RTouch: RIndexTrigger], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  186. RHandTrigger = 0x08, ///< Maps to Physical Axis1D: [Touch, RTouch: RHandTrigger], [Gamepad, LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  187. Any = ~None, ///< Maps to Physical Axis1D: [Gamepad, Touch, LTouch, RTouch: Any], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  188. }
  189. [Flags]
  190. /// Virtual 2-dimensional axis (Vector2) mappings that allow the same input bindings to work across different controllers.
  191. public enum Axis2D
  192. {
  193. None = 0, ///< Maps to RawAxis2D: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  194. PrimaryThumbstick = 0x01, ///< Maps to RawAxis2D: [Gamepad, Touch, LTouch: LThumbstick], [RTouch: RThumbstick], [LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  195. PrimaryTouchpad = 0x04, ///< Maps to RawAxis2D: [LTrackedRemote, Touchpad: LTouchpad], RTrackedRemote: RTouchpad], [Gamepad, Touch, LTouch, RTouch, Remote: None]
  196. SecondaryThumbstick = 0x02, ///< Maps to RawAxis2D: [Gamepad, Touch: RThumbstick], [LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  197. SecondaryTouchpad = 0x08, ///< Maps to RawAxis2D: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  198. Any = ~None, ///< Maps to RawAxis2D: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad: Any], [Remote: None]
  199. }
  200. [Flags]
  201. /// Raw 2-dimensional axis (Vector2) mappings that can be used to directly query the state of a controller.
  202. public enum RawAxis2D
  203. {
  204. None = 0, ///< Maps to Physical Axis2D: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  205. LThumbstick = 0x01, ///< Maps to Physical Axis2D: [Gamepad, Touch, LTouch: LThumbstick], [RTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  206. LTouchpad = 0x04, ///< Maps to Physical Axis2D: [LTrackedRemote, Touchpad: LTouchpad], [Gamepad, Touch, LTouch, RTouch, RTrackedRemote, Remote: None]
  207. RThumbstick = 0x02, ///< Maps to Physical Axis2D: [Gamepad, Touch, RTouch: RThumbstick], [LTouch, LTrackedRemote, RTrackedRemote, Touchpad, Remote: None]
  208. RTouchpad = 0x08, ///< Maps to Physical Axis2D: [RTrackedRemote: RTouchpad], [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, Touchpad, Remote: None]
  209. Any = ~None, ///< Maps to Physical Axis2D: [Gamepad, Touch, LTouch, RTouch, LTrackedRemote, RTrackedRemote: Any], [Touchpad, Remote: None]
  210. }
  211. [Flags]
  212. /// OpenVR Controller State Enum
  213. public enum OpenVRButton : ulong
  214. {
  215. None = 0,
  216. Two = 0x0002,
  217. Thumbstick = 0x100000000,
  218. Grip = 0x0004,
  219. }
  220. [Flags]
  221. /// Identifies a controller which can be used to query the virtual or raw input state.
  222. public enum Controller
  223. {
  224. None = OVRPlugin.Controller.None, ///< Null controller.
  225. LTouch = OVRPlugin.Controller.LTouch, ///< Left Oculus Touch controller. Virtual input mapping differs from the combined L/R Touch mapping.
  226. RTouch = OVRPlugin.Controller.RTouch, ///< Right Oculus Touch controller. Virtual input mapping differs from the combined L/R Touch mapping.
  227. Touch = OVRPlugin.Controller.Touch, ///< Combined Left/Right pair of Oculus Touch controllers.
  228. Remote = OVRPlugin.Controller.Remote, ///< Oculus Remote controller.
  229. Gamepad = OVRPlugin.Controller.Gamepad, ///< Xbox 360 or Xbox One gamepad on PC. Generic gamepad on Android.
  230. Hands = OVRPlugin.Controller.Hands, ///< Left Hand provided by hand-tracking.
  231. LHand = OVRPlugin.Controller.LHand, ///< Left Hand provided by hand-tracking.
  232. RHand = OVRPlugin.Controller.RHand, ///< Right Hand provided by hand-tracking.
  233. Touchpad = OVRPlugin.Controller.Touchpad, ///< GearVR touchpad on Android.
  234. LTrackedRemote = OVRPlugin.Controller.LTrackedRemote, ///< Left GearVR tracked remote on Android.
  235. RTrackedRemote = OVRPlugin.Controller.RTrackedRemote, ///< Right GearVR tracked remote on Android.
  236. Active = OVRPlugin.Controller.Active, ///< Default controller. Represents the controller that most recently registered a button press from the user.
  237. All = OVRPlugin.Controller.All, ///< Represents the logical OR of all controllers.
  238. }
  239. public enum Handedness
  240. {
  241. Unsupported = OVRPlugin.Handedness.Unsupported,
  242. LeftHanded = OVRPlugin.Handedness.LeftHanded,
  243. RightHanded = OVRPlugin.Handedness.RightHanded,
  244. }
  245. private static readonly float AXIS_AS_BUTTON_THRESHOLD = 0.5f;
  246. private static readonly float AXIS_DEADZONE_THRESHOLD = 0.2f;
  247. private static List<OVRControllerBase> controllers;
  248. private static Controller activeControllerType = Controller.None;
  249. private static Controller connectedControllerTypes = Controller.None;
  250. private static OVRPlugin.Step stepType = OVRPlugin.Step.Render;
  251. private static int fixedUpdateCount = 0;
  252. private static bool _pluginSupportsActiveController = false;
  253. private static bool _pluginSupportsActiveControllerCached = false;
  254. private static System.Version _pluginSupportsActiveControllerMinVersion = new System.Version(1, 9, 0);
  255. private static bool pluginSupportsActiveController
  256. {
  257. get
  258. {
  259. if (!_pluginSupportsActiveControllerCached)
  260. {
  261. bool isSupportedPlatform = true;
  262. #if (UNITY_ANDROID && !UNITY_EDITOR) || UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
  263. isSupportedPlatform = false;
  264. #endif
  265. _pluginSupportsActiveController = isSupportedPlatform && (OVRPlugin.version >= _pluginSupportsActiveControllerMinVersion);
  266. _pluginSupportsActiveControllerCached = true;
  267. }
  268. return _pluginSupportsActiveController;
  269. }
  270. }
  271. /// <summary>
  272. /// Creates an instance of OVRInput.
  273. /// </summary>
  274. static OVRInput()
  275. {
  276. controllers = new List<OVRControllerBase>
  277. {
  278. #if UNITY_ANDROID && !UNITY_EDITOR
  279. new OVRControllerGamepadAndroid(),
  280. new OVRControllerTouchpad(),
  281. new OVRControllerLTrackedRemote(),
  282. new OVRControllerRTrackedRemote(),
  283. new OVRControllerTouch(),
  284. new OVRControllerLTouch(),
  285. new OVRControllerRTouch(),
  286. new OVRControllerHands(),
  287. new OVRControllerLHand(),
  288. new OVRControllerRHand(),
  289. #elif UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
  290. new OVRControllerGamepadMac(),
  291. #else
  292. new OVRControllerGamepadPC(),
  293. new OVRControllerTouch(),
  294. new OVRControllerLTouch(),
  295. new OVRControllerRTouch(),
  296. new OVRControllerHands(),
  297. new OVRControllerLHand(),
  298. new OVRControllerRHand(),
  299. new OVRControllerRemote(),
  300. #endif
  301. };
  302. InitHapticInfo();
  303. }
  304. /// <summary>
  305. /// Updates the internal state of OVRInput. Must be called manually if used independently from OVRManager.
  306. /// </summary>
  307. public static void Update()
  308. {
  309. connectedControllerTypes = Controller.None;
  310. stepType = OVRPlugin.Step.Render;
  311. fixedUpdateCount = 0;
  312. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR)
  313. {
  314. UpdateXRControllerNodeIds();
  315. UpdateXRControllerHaptics();
  316. }
  317. for (int i = 0; i < controllers.Count; i++)
  318. {
  319. OVRControllerBase controller = controllers[i];
  320. connectedControllerTypes |= controller.Update();
  321. if ((connectedControllerTypes & controller.controllerType) != 0)
  322. {
  323. RawButton rawButtonMask = RawButton.Any;
  324. RawTouch rawTouchMask = RawTouch.Any;
  325. if (Get(rawButtonMask, controller.controllerType)
  326. || Get(rawTouchMask, controller.controllerType))
  327. {
  328. activeControllerType = controller.controllerType;
  329. }
  330. }
  331. }
  332. if ((activeControllerType == Controller.LTouch) || (activeControllerType == Controller.RTouch))
  333. {
  334. if ((connectedControllerTypes & Controller.Touch) == Controller.Touch)
  335. {
  336. // If either Touch controller is Active and both Touch controllers are connected, set both to Active.
  337. activeControllerType = Controller.Touch;
  338. }
  339. }
  340. if ((activeControllerType == Controller.LHand) || (activeControllerType == Controller.RHand))
  341. {
  342. if ((connectedControllerTypes & Controller.Hands) == Controller.Hands)
  343. {
  344. // If either Hand controller is Active and both Hand controllers are connected, set both to Active.
  345. activeControllerType = Controller.Hands;
  346. }
  347. }
  348. if ((connectedControllerTypes & activeControllerType) == 0)
  349. {
  350. activeControllerType = Controller.None;
  351. }
  352. // Promote TrackedRemote to Active if one is connected and no other controller is active
  353. if (activeControllerType == Controller.None)
  354. {
  355. if ((connectedControllerTypes & Controller.RTrackedRemote) != 0)
  356. {
  357. activeControllerType = Controller.RTrackedRemote;
  358. }
  359. else if ((connectedControllerTypes & Controller.LTrackedRemote) != 0)
  360. {
  361. activeControllerType = Controller.LTrackedRemote;
  362. }
  363. }
  364. if ( OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus && pluginSupportsActiveController)
  365. {
  366. Controller localActiveController = activeControllerType;
  367. // override locally derived active and connected controllers if plugin provides more accurate data
  368. connectedControllerTypes = (OVRInput.Controller)OVRPlugin.GetConnectedControllers();
  369. activeControllerType = (OVRInput.Controller)OVRPlugin.GetActiveController();
  370. // unless the plugin reports none and we locally detected hands as the active controller
  371. if (activeControllerType == Controller.None && ((localActiveController & Controller.Hands) != 0))
  372. {
  373. activeControllerType = localActiveController;
  374. }
  375. }
  376. else if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR)
  377. {
  378. activeControllerType = connectedControllerTypes;
  379. }
  380. }
  381. /// <summary>
  382. /// Updates the internal physics state of OVRInput. Must be called manually if used independently from OVRManager.
  383. /// </summary>
  384. public static void FixedUpdate()
  385. {
  386. stepType = OVRPlugin.Step.Physics;
  387. double predictionSeconds = (double)fixedUpdateCount * Time.fixedDeltaTime / Mathf.Max(Time.timeScale, 1e-6f);
  388. fixedUpdateCount++;
  389. OVRPlugin.UpdateNodePhysicsPoses(0, predictionSeconds);
  390. }
  391. /// <summary>
  392. /// Returns true if the given Controller's orientation is currently tracked.
  393. /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return false.
  394. /// </summary>
  395. public static bool GetControllerOrientationTracked(OVRInput.Controller controllerType)
  396. {
  397. switch (controllerType)
  398. {
  399. case Controller.LTouch:
  400. case Controller.LTrackedRemote:
  401. case Controller.LHand:
  402. return OVRPlugin.GetNodeOrientationTracked(OVRPlugin.Node.HandLeft);
  403. case Controller.RTouch:
  404. case Controller.RTrackedRemote:
  405. case Controller.RHand:
  406. return OVRPlugin.GetNodeOrientationTracked(OVRPlugin.Node.HandRight);
  407. default:
  408. return false;
  409. }
  410. }
  411. /// <summary>
  412. /// Returns true if the given Controller's orientation is currently valid.
  413. /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return false.
  414. /// </summary>
  415. public static bool GetControllerOrientationValid(OVRInput.Controller controllerType)
  416. {
  417. switch (controllerType)
  418. {
  419. case Controller.LTouch:
  420. case Controller.LTrackedRemote:
  421. case Controller.LHand:
  422. return OVRPlugin.GetNodeOrientationValid(OVRPlugin.Node.HandLeft);
  423. case Controller.RTouch:
  424. case Controller.RTrackedRemote:
  425. case Controller.RHand:
  426. return OVRPlugin.GetNodeOrientationValid(OVRPlugin.Node.HandRight);
  427. default:
  428. return false;
  429. }
  430. }
  431. /// <summary>
  432. /// Returns true if the given Controller's position is currently tracked.
  433. /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return false.
  434. /// </summary>
  435. public static bool GetControllerPositionTracked(OVRInput.Controller controllerType)
  436. {
  437. switch (controllerType)
  438. {
  439. case Controller.LTouch:
  440. case Controller.LTrackedRemote:
  441. case Controller.LHand:
  442. return OVRPlugin.GetNodePositionTracked(OVRPlugin.Node.HandLeft);
  443. case Controller.RTouch:
  444. case Controller.RTrackedRemote:
  445. case Controller.RHand:
  446. return OVRPlugin.GetNodePositionTracked(OVRPlugin.Node.HandRight);
  447. default:
  448. return false;
  449. }
  450. }
  451. /// <summary>
  452. /// Returns true if the given Controller's position is currently valid.
  453. /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return false.
  454. /// </summary>
  455. public static bool GetControllerPositionValid(OVRInput.Controller controllerType)
  456. {
  457. switch (controllerType)
  458. {
  459. case Controller.LTouch:
  460. case Controller.LTrackedRemote:
  461. case Controller.LHand:
  462. return OVRPlugin.GetNodePositionValid(OVRPlugin.Node.HandLeft);
  463. case Controller.RTouch:
  464. case Controller.RTrackedRemote:
  465. case Controller.RHand:
  466. return OVRPlugin.GetNodePositionValid(OVRPlugin.Node.HandRight);
  467. default:
  468. return false;
  469. }
  470. }
  471. /// <summary>
  472. /// Gets the position of the given Controller local to its tracking space.
  473. /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return Vector3.zero.
  474. /// </summary>
  475. public static Vector3 GetLocalControllerPosition(OVRInput.Controller controllerType)
  476. {
  477. switch (controllerType)
  478. {
  479. case Controller.LTouch:
  480. case Controller.LTrackedRemote:
  481. case Controller.LHand:
  482. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  483. return OVRPlugin.GetNodePose(OVRPlugin.Node.HandLeft, stepType).ToOVRPose().position;
  484. else if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR)
  485. return openVRControllerDetails[0].localPosition;
  486. else
  487. {
  488. Vector3 retVec;
  489. if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.LeftHand, NodeStatePropertyType.Position, OVRPlugin.Node.HandLeft, stepType, out retVec))
  490. return retVec;
  491. return Vector3.zero; //Will never be hit, but is a final fallback.
  492. }
  493. case Controller.RTouch:
  494. case Controller.RTrackedRemote:
  495. case Controller.RHand:
  496. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  497. return OVRPlugin.GetNodePose(OVRPlugin.Node.HandRight, stepType).ToOVRPose().position;
  498. else if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR)
  499. return openVRControllerDetails[1].localPosition;
  500. else
  501. {
  502. Vector3 retVec;
  503. if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.RightHand, NodeStatePropertyType.Position, OVRPlugin.Node.HandRight, stepType, out retVec))
  504. return retVec;
  505. return Vector3.zero;
  506. }
  507. default:
  508. return Vector3.zero;
  509. }
  510. }
  511. /// <summary>
  512. /// Gets the linear velocity of the given Controller local to its tracking space.
  513. /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return Vector3.zero.
  514. /// </summary>
  515. public static Vector3 GetLocalControllerVelocity(OVRInput.Controller controllerType)
  516. {
  517. Vector3 velocity = Vector3.zero;
  518. switch (controllerType)
  519. {
  520. case Controller.LTouch:
  521. case Controller.LTrackedRemote:
  522. case Controller.LHand:
  523. if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.LeftHand, NodeStatePropertyType.Velocity, OVRPlugin.Node.HandLeft, stepType, out velocity))
  524. {
  525. return velocity;
  526. }
  527. else
  528. {
  529. return Vector3.zero;
  530. }
  531. case Controller.RTouch:
  532. case Controller.RTrackedRemote:
  533. case Controller.RHand:
  534. if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.RightHand, NodeStatePropertyType.Velocity, OVRPlugin.Node.HandRight, stepType, out velocity))
  535. {
  536. return velocity;
  537. }
  538. else
  539. {
  540. return Vector3.zero;
  541. }
  542. default:
  543. return Vector3.zero;
  544. }
  545. }
  546. /// <summary>
  547. /// Gets the linear acceleration of the given Controller local to its tracking space.
  548. /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return Vector3.zero.
  549. /// </summary>
  550. public static Vector3 GetLocalControllerAcceleration(OVRInput.Controller controllerType)
  551. {
  552. Vector3 accel = Vector3.zero;
  553. switch (controllerType)
  554. {
  555. case Controller.LTouch:
  556. case Controller.LTrackedRemote:
  557. case Controller.LHand:
  558. if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.LeftHand, NodeStatePropertyType.Acceleration, OVRPlugin.Node.HandLeft, stepType, out accel))
  559. {
  560. return accel;
  561. }
  562. else
  563. {
  564. return Vector3.zero;
  565. }
  566. case Controller.RTouch:
  567. case Controller.RTrackedRemote:
  568. case Controller.RHand:
  569. if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.RightHand, NodeStatePropertyType.Acceleration, OVRPlugin.Node.HandRight, stepType, out accel))
  570. {
  571. return accel;
  572. }
  573. else
  574. {
  575. return Vector3.zero;
  576. }
  577. default:
  578. return Vector3.zero;
  579. }
  580. }
  581. /// <summary>
  582. /// Gets the rotation of the given Controller local to its tracking space.
  583. /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return Quaternion.identity.
  584. /// </summary>
  585. public static Quaternion GetLocalControllerRotation(OVRInput.Controller controllerType)
  586. {
  587. switch (controllerType)
  588. {
  589. case Controller.LTouch:
  590. case Controller.LTrackedRemote:
  591. case Controller.LHand:
  592. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  593. return OVRPlugin.GetNodePose(OVRPlugin.Node.HandLeft, stepType).ToOVRPose().orientation;
  594. else if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR)
  595. return openVRControllerDetails[0].localOrientation;
  596. else
  597. {
  598. Quaternion retQuat;
  599. if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.LeftHand, NodeStatePropertyType.Orientation, OVRPlugin.Node.HandLeft, stepType, out retQuat))
  600. return retQuat;
  601. return Quaternion.identity;
  602. }
  603. case Controller.RTouch:
  604. case Controller.RTrackedRemote:
  605. case Controller.RHand:
  606. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  607. return OVRPlugin.GetNodePose(OVRPlugin.Node.HandRight, stepType).ToOVRPose().orientation;
  608. else if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR)
  609. return openVRControllerDetails[1].localOrientation;
  610. else
  611. {
  612. Quaternion retQuat;
  613. if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.RightHand, NodeStatePropertyType.Orientation, OVRPlugin.Node.HandRight, stepType, out retQuat))
  614. return retQuat;
  615. return Quaternion.identity;
  616. }
  617. default:
  618. return Quaternion.identity;
  619. }
  620. }
  621. /// <summary>
  622. /// Gets the angular velocity of the given Controller local to its tracking space in radians per second around each axis.
  623. /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return Vector3.zero.
  624. /// </summary>
  625. public static Vector3 GetLocalControllerAngularVelocity(OVRInput.Controller controllerType)
  626. {
  627. Vector3 velocity = Vector3.zero;
  628. switch (controllerType)
  629. {
  630. case Controller.LTouch:
  631. case Controller.LTrackedRemote:
  632. case Controller.LHand:
  633. if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.LeftHand, NodeStatePropertyType.AngularVelocity, OVRPlugin.Node.HandLeft, stepType, out velocity))
  634. {
  635. return velocity;
  636. }
  637. else
  638. {
  639. return Vector3.zero;
  640. }
  641. case Controller.RTouch:
  642. case Controller.RTrackedRemote:
  643. case Controller.RHand:
  644. if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.RightHand, NodeStatePropertyType.AngularVelocity, OVRPlugin.Node.HandRight, stepType, out velocity))
  645. {
  646. return velocity;
  647. }
  648. else
  649. {
  650. return Vector3.zero;
  651. }
  652. default:
  653. return Vector3.zero;
  654. }
  655. }
  656. /// <summary>
  657. /// Gets the angular acceleration of the given Controller local to its tracking space in radians per second per second around each axis.
  658. /// Only supported for Oculus LTouch and RTouch controllers. Non-tracked controllers will return Vector3.zero.
  659. /// </summary>
  660. public static Vector3 GetLocalControllerAngularAcceleration(OVRInput.Controller controllerType)
  661. {
  662. Vector3 accel = Vector3.zero;
  663. switch (controllerType)
  664. {
  665. case Controller.LTouch:
  666. case Controller.LTrackedRemote:
  667. case Controller.LHand:
  668. if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.LeftHand, NodeStatePropertyType.AngularAcceleration, OVRPlugin.Node.HandLeft, stepType, out accel))
  669. {
  670. return accel;
  671. }
  672. else
  673. {
  674. return Vector3.zero;
  675. }
  676. case Controller.RTouch:
  677. case Controller.RTrackedRemote:
  678. case Controller.RHand:
  679. if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.RightHand, NodeStatePropertyType.AngularAcceleration, OVRPlugin.Node.HandRight, stepType, out accel))
  680. {
  681. return accel;
  682. }
  683. else
  684. {
  685. return Vector3.zero;
  686. }
  687. default:
  688. return Vector3.zero;
  689. }
  690. }
  691. /// <summary>
  692. /// Gets the dominant hand that the user has specified in settings, for mobile devices.
  693. /// </summary>
  694. public static Handedness GetDominantHand()
  695. {
  696. return (Handedness) OVRPlugin.GetDominantHand();
  697. }
  698. /// <summary>
  699. /// Gets the current state of the given virtual button mask with the given controller mask.
  700. /// Returns true if any masked button is down on any masked controller.
  701. /// </summary>
  702. public static bool Get(Button virtualMask, Controller controllerMask = Controller.Active)
  703. {
  704. return GetResolvedButton(virtualMask, RawButton.None, controllerMask);
  705. }
  706. /// <summary>
  707. /// Gets the current state of the given raw button mask with the given controller mask.
  708. /// Returns true if any masked button is down on any masked controllers.
  709. /// </summary>
  710. public static bool Get(RawButton rawMask, Controller controllerMask = Controller.Active)
  711. {
  712. return GetResolvedButton(Button.None, rawMask, controllerMask);
  713. }
  714. private static bool GetResolvedButton(Button virtualMask, RawButton rawMask, Controller controllerMask)
  715. {
  716. if ((controllerMask & Controller.Active) != 0)
  717. controllerMask |= activeControllerType;
  718. for (int i = 0; i < controllers.Count; i++)
  719. {
  720. OVRControllerBase controller = controllers[i];
  721. if (ShouldResolveController(controller.controllerType, controllerMask))
  722. {
  723. RawButton resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask);
  724. if (((RawButton)controller.currentState.Buttons & resolvedMask) != 0)
  725. {
  726. return true;
  727. }
  728. }
  729. }
  730. return false;
  731. }
  732. /// <summary>
  733. /// Gets the current down state of the given virtual button mask with the given controller mask.
  734. /// Returns true if any masked button was pressed this frame on any masked controller and no masked button was previously down last frame.
  735. /// </summary>
  736. public static bool GetDown(Button virtualMask, Controller controllerMask = Controller.Active)
  737. {
  738. return GetResolvedButtonDown(virtualMask, RawButton.None, controllerMask);
  739. }
  740. /// <summary>
  741. /// Gets the current down state of the given raw button mask with the given controller mask.
  742. /// Returns true if any masked button was pressed this frame on any masked controller and no masked button was previously down last frame.
  743. /// </summary>
  744. public static bool GetDown(RawButton rawMask, Controller controllerMask = Controller.Active)
  745. {
  746. return GetResolvedButtonDown(Button.None, rawMask, controllerMask);
  747. }
  748. private static bool GetResolvedButtonDown(Button virtualMask, RawButton rawMask, Controller controllerMask)
  749. {
  750. bool down = false;
  751. if ((controllerMask & Controller.Active) != 0)
  752. controllerMask |= activeControllerType;
  753. for (int i = 0; i < controllers.Count; i++)
  754. {
  755. OVRControllerBase controller = controllers[i];
  756. if (ShouldResolveController(controller.controllerType, controllerMask))
  757. {
  758. RawButton resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask);
  759. if (((RawButton)controller.previousState.Buttons & resolvedMask) != 0)
  760. {
  761. return false;
  762. }
  763. if ((((RawButton)controller.currentState.Buttons & resolvedMask) != 0)
  764. && (((RawButton)controller.previousState.Buttons & resolvedMask) == 0))
  765. {
  766. down = true;
  767. }
  768. }
  769. }
  770. return down;
  771. }
  772. /// <summary>
  773. /// Gets the current up state of the given virtual button mask with the given controller mask.
  774. /// Returns true if any masked button was released this frame on any masked controller and no other masked button is still down this frame.
  775. /// </summary>
  776. public static bool GetUp(Button virtualMask, Controller controllerMask = Controller.Active)
  777. {
  778. return GetResolvedButtonUp(virtualMask, RawButton.None, controllerMask);
  779. }
  780. /// <summary>
  781. /// Gets the current up state of the given raw button mask with the given controller mask.
  782. /// Returns true if any masked button was released this frame on any masked controller and no other masked button is still down this frame.
  783. /// </summary>
  784. public static bool GetUp(RawButton rawMask, Controller controllerMask = Controller.Active)
  785. {
  786. return GetResolvedButtonUp(Button.None, rawMask, controllerMask);
  787. }
  788. private static bool GetResolvedButtonUp(Button virtualMask, RawButton rawMask, Controller controllerMask)
  789. {
  790. bool up = false;
  791. if ((controllerMask & Controller.Active) != 0)
  792. controllerMask |= activeControllerType;
  793. for (int i = 0; i < controllers.Count; i++)
  794. {
  795. OVRControllerBase controller = controllers[i];
  796. if (ShouldResolveController(controller.controllerType, controllerMask))
  797. {
  798. RawButton resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask);
  799. if (((RawButton)controller.currentState.Buttons & resolvedMask) != 0)
  800. {
  801. return false;
  802. }
  803. if ((((RawButton)controller.currentState.Buttons & resolvedMask) == 0)
  804. && (((RawButton)controller.previousState.Buttons & resolvedMask) != 0))
  805. {
  806. up = true;
  807. }
  808. }
  809. }
  810. return up;
  811. }
  812. /// <summary>
  813. /// Gets the current state of the given virtual touch mask with the given controller mask.
  814. /// Returns true if any masked touch is down on any masked controller.
  815. /// </summary>
  816. public static bool Get(Touch virtualMask, Controller controllerMask = Controller.Active)
  817. {
  818. return GetResolvedTouch(virtualMask, RawTouch.None, controllerMask);
  819. }
  820. /// <summary>
  821. /// Gets the current state of the given raw touch mask with the given controller mask.
  822. /// Returns true if any masked touch is down on any masked controllers.
  823. /// </summary>
  824. public static bool Get(RawTouch rawMask, Controller controllerMask = Controller.Active)
  825. {
  826. return GetResolvedTouch(Touch.None, rawMask, controllerMask);
  827. }
  828. private static bool GetResolvedTouch(Touch virtualMask, RawTouch rawMask, Controller controllerMask)
  829. {
  830. if ((controllerMask & Controller.Active) != 0)
  831. controllerMask |= activeControllerType;
  832. for (int i = 0; i < controllers.Count; i++)
  833. {
  834. OVRControllerBase controller = controllers[i];
  835. if (ShouldResolveController(controller.controllerType, controllerMask))
  836. {
  837. RawTouch resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask);
  838. if (((RawTouch)controller.currentState.Touches & resolvedMask) != 0)
  839. {
  840. return true;
  841. }
  842. }
  843. }
  844. return false;
  845. }
  846. /// <summary>
  847. /// Gets the current down state of the given virtual touch mask with the given controller mask.
  848. /// Returns true if any masked touch was pressed this frame on any masked controller and no masked touch was previously down last frame.
  849. /// </summary>
  850. public static bool GetDown(Touch virtualMask, Controller controllerMask = Controller.Active)
  851. {
  852. return GetResolvedTouchDown(virtualMask, RawTouch.None, controllerMask);
  853. }
  854. /// <summary>
  855. /// Gets the current down state of the given raw touch mask with the given controller mask.
  856. /// Returns true if any masked touch was pressed this frame on any masked controller and no masked touch was previously down last frame.
  857. /// </summary>
  858. public static bool GetDown(RawTouch rawMask, Controller controllerMask = Controller.Active)
  859. {
  860. return GetResolvedTouchDown(Touch.None, rawMask, controllerMask);
  861. }
  862. private static bool GetResolvedTouchDown(Touch virtualMask, RawTouch rawMask, Controller controllerMask)
  863. {
  864. bool down = false;
  865. if ((controllerMask & Controller.Active) != 0)
  866. controllerMask |= activeControllerType;
  867. for (int i = 0; i < controllers.Count; i++)
  868. {
  869. OVRControllerBase controller = controllers[i];
  870. if (ShouldResolveController(controller.controllerType, controllerMask))
  871. {
  872. RawTouch resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask);
  873. if (((RawTouch)controller.previousState.Touches & resolvedMask) != 0)
  874. {
  875. return false;
  876. }
  877. if ((((RawTouch)controller.currentState.Touches & resolvedMask) != 0)
  878. && (((RawTouch)controller.previousState.Touches & resolvedMask) == 0))
  879. {
  880. down = true;
  881. }
  882. }
  883. }
  884. return down;
  885. }
  886. /// <summary>
  887. /// Gets the current up state of the given virtual touch mask with the given controller mask.
  888. /// Returns true if any masked touch was released this frame on any masked controller and no other masked touch is still down this frame.
  889. /// </summary>
  890. public static bool GetUp(Touch virtualMask, Controller controllerMask = Controller.Active)
  891. {
  892. return GetResolvedTouchUp(virtualMask, RawTouch.None, controllerMask);
  893. }
  894. /// <summary>
  895. /// Gets the current up state of the given raw touch mask with the given controller mask.
  896. /// Returns true if any masked touch was released this frame on any masked controller and no other masked touch is still down this frame.
  897. /// </summary>
  898. public static bool GetUp(RawTouch rawMask, Controller controllerMask = Controller.Active)
  899. {
  900. return GetResolvedTouchUp(Touch.None, rawMask, controllerMask);
  901. }
  902. private static bool GetResolvedTouchUp(Touch virtualMask, RawTouch rawMask, Controller controllerMask)
  903. {
  904. bool up = false;
  905. if ((controllerMask & Controller.Active) != 0)
  906. controllerMask |= activeControllerType;
  907. for (int i = 0; i < controllers.Count; i++)
  908. {
  909. OVRControllerBase controller = controllers[i];
  910. if (ShouldResolveController(controller.controllerType, controllerMask))
  911. {
  912. RawTouch resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask);
  913. if (((RawTouch)controller.currentState.Touches & resolvedMask) != 0)
  914. {
  915. return false;
  916. }
  917. if ((((RawTouch)controller.currentState.Touches & resolvedMask) == 0)
  918. && (((RawTouch)controller.previousState.Touches & resolvedMask) != 0))
  919. {
  920. up = true;
  921. }
  922. }
  923. }
  924. return up;
  925. }
  926. /// <summary>
  927. /// Gets the current state of the given virtual near touch mask with the given controller mask.
  928. /// Returns true if any masked near touch is down on any masked controller.
  929. /// </summary>
  930. public static bool Get(NearTouch virtualMask, Controller controllerMask = Controller.Active)
  931. {
  932. return GetResolvedNearTouch(virtualMask, RawNearTouch.None, controllerMask);
  933. }
  934. /// <summary>
  935. /// Gets the current state of the given raw near touch mask with the given controller mask.
  936. /// Returns true if any masked near touch is down on any masked controllers.
  937. /// </summary>
  938. public static bool Get(RawNearTouch rawMask, Controller controllerMask = Controller.Active)
  939. {
  940. return GetResolvedNearTouch(NearTouch.None, rawMask, controllerMask);
  941. }
  942. private static bool GetResolvedNearTouch(NearTouch virtualMask, RawNearTouch rawMask, Controller controllerMask)
  943. {
  944. if ((controllerMask & Controller.Active) != 0)
  945. controllerMask |= activeControllerType;
  946. for (int i = 0; i < controllers.Count; i++)
  947. {
  948. OVRControllerBase controller = controllers[i];
  949. if (ShouldResolveController(controller.controllerType, controllerMask))
  950. {
  951. RawNearTouch resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask);
  952. if (((RawNearTouch)controller.currentState.NearTouches & resolvedMask) != 0)
  953. {
  954. return true;
  955. }
  956. }
  957. }
  958. return false;
  959. }
  960. /// <summary>
  961. /// Gets the current down state of the given virtual near touch mask with the given controller mask.
  962. /// Returns true if any masked near touch was pressed this frame on any masked controller and no masked near touch was previously down last frame.
  963. /// </summary>
  964. public static bool GetDown(NearTouch virtualMask, Controller controllerMask = Controller.Active)
  965. {
  966. return GetResolvedNearTouchDown(virtualMask, RawNearTouch.None, controllerMask);
  967. }
  968. /// <summary>
  969. /// Gets the current down state of the given raw near touch mask with the given controller mask.
  970. /// Returns true if any masked near touch was pressed this frame on any masked controller and no masked near touch was previously down last frame.
  971. /// </summary>
  972. public static bool GetDown(RawNearTouch rawMask, Controller controllerMask = Controller.Active)
  973. {
  974. return GetResolvedNearTouchDown(NearTouch.None, rawMask, controllerMask);
  975. }
  976. private static bool GetResolvedNearTouchDown(NearTouch virtualMask, RawNearTouch rawMask, Controller controllerMask)
  977. {
  978. bool down = false;
  979. if ((controllerMask & Controller.Active) != 0)
  980. controllerMask |= activeControllerType;
  981. for (int i = 0; i < controllers.Count; i++)
  982. {
  983. OVRControllerBase controller = controllers[i];
  984. if (ShouldResolveController(controller.controllerType, controllerMask))
  985. {
  986. RawNearTouch resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask);
  987. if (((RawNearTouch)controller.previousState.NearTouches & resolvedMask) != 0)
  988. {
  989. return false;
  990. }
  991. if ((((RawNearTouch)controller.currentState.NearTouches & resolvedMask) != 0)
  992. && (((RawNearTouch)controller.previousState.NearTouches & resolvedMask) == 0))
  993. {
  994. down = true;
  995. }
  996. }
  997. }
  998. return down;
  999. }
  1000. /// <summary>
  1001. /// Gets the current up state of the given virtual near touch mask with the given controller mask.
  1002. /// Returns true if any masked near touch was released this frame on any masked controller and no other masked near touch is still down this frame.
  1003. /// </summary>
  1004. public static bool GetUp(NearTouch virtualMask, Controller controllerMask = Controller.Active)
  1005. {
  1006. return GetResolvedNearTouchUp(virtualMask, RawNearTouch.None, controllerMask);
  1007. }
  1008. /// <summary>
  1009. /// Gets the current up state of the given raw near touch mask with the given controller mask.
  1010. /// Returns true if any masked near touch was released this frame on any masked controller and no other masked near touch is still down this frame.
  1011. /// </summary>
  1012. public static bool GetUp(RawNearTouch rawMask, Controller controllerMask = Controller.Active)
  1013. {
  1014. return GetResolvedNearTouchUp(NearTouch.None, rawMask, controllerMask);
  1015. }
  1016. private static bool GetResolvedNearTouchUp(NearTouch virtualMask, RawNearTouch rawMask, Controller controllerMask)
  1017. {
  1018. bool up = false;
  1019. if ((controllerMask & Controller.Active) != 0)
  1020. controllerMask |= activeControllerType;
  1021. for (int i = 0; i < controllers.Count; i++)
  1022. {
  1023. OVRControllerBase controller = controllers[i];
  1024. if (ShouldResolveController(controller.controllerType, controllerMask))
  1025. {
  1026. RawNearTouch resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask);
  1027. if (((RawNearTouch)controller.currentState.NearTouches & resolvedMask) != 0)
  1028. {
  1029. return false;
  1030. }
  1031. if ((((RawNearTouch)controller.currentState.NearTouches & resolvedMask) == 0)
  1032. && (((RawNearTouch)controller.previousState.NearTouches & resolvedMask) != 0))
  1033. {
  1034. up = true;
  1035. }
  1036. }
  1037. }
  1038. return up;
  1039. }
  1040. /// <summary>
  1041. /// Gets the current state of the given virtual 1-dimensional axis mask on the given controller mask.
  1042. /// Returns the value of the largest masked axis across all masked controllers. Values range from 0 to 1.
  1043. /// </summary>
  1044. public static float Get(Axis1D virtualMask, Controller controllerMask = Controller.Active)
  1045. {
  1046. return GetResolvedAxis1D(virtualMask, RawAxis1D.None, controllerMask);
  1047. }
  1048. /// <summary>
  1049. /// Gets the current state of the given raw 1-dimensional axis mask on the given controller mask.
  1050. /// Returns the value of the largest masked axis across all masked controllers. Values range from 0 to 1.
  1051. /// </summary>
  1052. public static float Get(RawAxis1D rawMask, Controller controllerMask = Controller.Active)
  1053. {
  1054. return GetResolvedAxis1D(Axis1D.None, rawMask, controllerMask);
  1055. }
  1056. private static float GetResolvedAxis1D(Axis1D virtualMask, RawAxis1D rawMask, Controller controllerMask)
  1057. {
  1058. float maxAxis = 0.0f;
  1059. if ((controllerMask & Controller.Active) != 0)
  1060. controllerMask |= activeControllerType;
  1061. for (int i = 0; i < controllers.Count; i++)
  1062. {
  1063. OVRControllerBase controller = controllers[i];
  1064. if (OVRManager.loadedXRDevice != OVRManager.XRDevice.Oculus)
  1065. controller.shouldApplyDeadzone = false;
  1066. if (ShouldResolveController(controller.controllerType, controllerMask))
  1067. {
  1068. RawAxis1D resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask);
  1069. if ((RawAxis1D.LIndexTrigger & resolvedMask) != 0)
  1070. {
  1071. float axis = controller.currentState.LIndexTrigger;
  1072. if (controller.shouldApplyDeadzone)
  1073. axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD);
  1074. maxAxis = CalculateAbsMax(maxAxis, axis);
  1075. }
  1076. if ((RawAxis1D.RIndexTrigger & resolvedMask) != 0)
  1077. {
  1078. float axis = controller.currentState.RIndexTrigger;
  1079. if (controller.shouldApplyDeadzone)
  1080. axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD);
  1081. maxAxis = CalculateAbsMax(maxAxis, axis);
  1082. }
  1083. if ((RawAxis1D.LHandTrigger & resolvedMask) != 0)
  1084. {
  1085. float axis = controller.currentState.LHandTrigger;
  1086. if (controller.shouldApplyDeadzone)
  1087. axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD);
  1088. maxAxis = CalculateAbsMax(maxAxis, axis);
  1089. }
  1090. if ((RawAxis1D.RHandTrigger & resolvedMask) != 0)
  1091. {
  1092. float axis = controller.currentState.RHandTrigger;
  1093. if (controller.shouldApplyDeadzone)
  1094. axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD);
  1095. maxAxis = CalculateAbsMax(maxAxis, axis);
  1096. }
  1097. }
  1098. }
  1099. return maxAxis;
  1100. }
  1101. /// <summary>
  1102. /// Gets the current state of the given virtual 2-dimensional axis mask on the given controller mask.
  1103. /// Returns the vector of the largest masked axis across all masked controllers. Values range from -1 to 1.
  1104. /// </summary>
  1105. public static Vector2 Get(Axis2D virtualMask, Controller controllerMask = Controller.Active)
  1106. {
  1107. return GetResolvedAxis2D(virtualMask, RawAxis2D.None, controllerMask);
  1108. }
  1109. /// <summary>
  1110. /// Gets the current state of the given raw 2-dimensional axis mask on the given controller mask.
  1111. /// Returns the vector of the largest masked axis across all masked controllers. Values range from -1 to 1.
  1112. /// </summary>
  1113. public static Vector2 Get(RawAxis2D rawMask, Controller controllerMask = Controller.Active)
  1114. {
  1115. return GetResolvedAxis2D(Axis2D.None, rawMask, controllerMask);
  1116. }
  1117. private static Vector2 GetResolvedAxis2D(Axis2D virtualMask, RawAxis2D rawMask, Controller controllerMask)
  1118. {
  1119. Vector2 maxAxis = Vector2.zero;
  1120. if ((controllerMask & Controller.Active) != 0)
  1121. controllerMask |= activeControllerType;
  1122. for (int i = 0; i < controllers.Count; i++)
  1123. {
  1124. OVRControllerBase controller = controllers[i];
  1125. if (OVRManager.loadedXRDevice != OVRManager.XRDevice.Oculus)
  1126. controller.shouldApplyDeadzone = false;
  1127. if (ShouldResolveController(controller.controllerType, controllerMask))
  1128. {
  1129. RawAxis2D resolvedMask = rawMask | controller.ResolveToRawMask(virtualMask);
  1130. if ((RawAxis2D.LThumbstick & resolvedMask) != 0)
  1131. {
  1132. Vector2 axis = new Vector2(
  1133. controller.currentState.LThumbstick.x,
  1134. controller.currentState.LThumbstick.y);
  1135. if (controller.shouldApplyDeadzone)
  1136. axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD);
  1137. maxAxis = CalculateAbsMax(maxAxis, axis);
  1138. }
  1139. if ((RawAxis2D.LTouchpad & resolvedMask) != 0)
  1140. {
  1141. Vector2 axis = new Vector2(
  1142. controller.currentState.LTouchpad.x,
  1143. controller.currentState.LTouchpad.y);
  1144. //if (controller.shouldApplyDeadzone)
  1145. // axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD);
  1146. maxAxis = CalculateAbsMax(maxAxis, axis);
  1147. }
  1148. if ((RawAxis2D.RThumbstick & resolvedMask) != 0)
  1149. {
  1150. Vector2 axis = new Vector2(
  1151. controller.currentState.RThumbstick.x,
  1152. controller.currentState.RThumbstick.y);
  1153. if (controller.shouldApplyDeadzone)
  1154. axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD);
  1155. maxAxis = CalculateAbsMax(maxAxis, axis);
  1156. }
  1157. if ((RawAxis2D.RTouchpad & resolvedMask) != 0)
  1158. {
  1159. Vector2 axis = new Vector2(
  1160. controller.currentState.RTouchpad.x,
  1161. controller.currentState.RTouchpad.y);
  1162. //if (controller.shouldApplyDeadzone)
  1163. // axis = CalculateDeadzone(axis, AXIS_DEADZONE_THRESHOLD);
  1164. maxAxis = CalculateAbsMax(maxAxis, axis);
  1165. }
  1166. }
  1167. }
  1168. return maxAxis;
  1169. }
  1170. /// <summary>
  1171. /// Returns a mask of all currently connected controller types.
  1172. /// </summary>
  1173. public static Controller GetConnectedControllers()
  1174. {
  1175. return connectedControllerTypes;
  1176. }
  1177. /// <summary>
  1178. /// Returns true if the specified controller type is currently connected.
  1179. /// </summary>
  1180. public static bool IsControllerConnected(Controller controller)
  1181. {
  1182. return (connectedControllerTypes & controller) == controller;
  1183. }
  1184. /// <summary>
  1185. /// Returns the current active controller type.
  1186. /// </summary>
  1187. public static Controller GetActiveController()
  1188. {
  1189. return activeControllerType;
  1190. }
  1191. private static void StartVibration(float amplitude, float duration, Node controllerNode)
  1192. {
  1193. int index = (controllerNode == Node.LeftHand) ? 0 : 1;
  1194. hapticInfos[index].hapticsDurationPlayed = 0.0f;
  1195. hapticInfos[index].hapticAmplitude = amplitude;
  1196. hapticInfos[index].hapticsDuration = duration;
  1197. hapticInfos[index].playingHaptics = (amplitude != 0.0f);
  1198. hapticInfos[index].node = controllerNode;
  1199. if (amplitude <= 0.0f || duration <= 0.0f)
  1200. {
  1201. hapticInfos[index].playingHaptics = false;
  1202. }
  1203. }
  1204. private static int NUM_HAPTIC_CHANNELS = 2;
  1205. private static HapticInfo[] hapticInfos;
  1206. private static float OPENVR_MAX_HAPTIC_AMPLITUDE = 4000.0f;
  1207. private static float HAPTIC_VIBRATION_DURATION_SECONDS = 2.0f;
  1208. private static String OPENVR_TOUCH_NAME = "oculus_touch";
  1209. private static String OPENVR_VIVE_CONTROLLER_NAME = "vive_controller";
  1210. private static String OPENVR_WINDOWSMR_CONTROLLER_NAME = "holographic_controller";
  1211. [Flags]
  1212. /// OpenVR Controller Enum
  1213. public enum OpenVRController : ulong
  1214. {
  1215. Unknown = 0,
  1216. OculusTouch = 1,
  1217. ViveController = 2,
  1218. WindowsMRController = 3
  1219. }
  1220. [StructLayout(LayoutKind.Sequential)]
  1221. public struct OpenVRControllerDetails
  1222. {
  1223. public OVR.OpenVR.VRControllerState_t state;
  1224. public OpenVRController controllerType;
  1225. public uint deviceID;
  1226. public Vector3 localPosition; //Position relative to Tracking Space
  1227. public Quaternion localOrientation; //Orientation relative to Tracking Space
  1228. }
  1229. public static OpenVRControllerDetails[] openVRControllerDetails = new OpenVRControllerDetails[2];
  1230. private class HapticInfo
  1231. {
  1232. public bool playingHaptics;
  1233. public float hapticsDurationPlayed;
  1234. public float hapticsDuration;
  1235. public float hapticAmplitude;
  1236. public Node node;
  1237. }
  1238. /// <summary>
  1239. /// Sets OpenVR left and right controller poses local to tracking space
  1240. /// </summary>
  1241. public static void SetOpenVRLocalPose(Vector3 leftPos, Vector3 rightPos, Quaternion leftRot, Quaternion rightRot)
  1242. {
  1243. openVRControllerDetails[0].localPosition = leftPos;
  1244. openVRControllerDetails[0].localOrientation = leftRot;
  1245. openVRControllerDetails[1].localPosition = rightPos;
  1246. openVRControllerDetails[1].localOrientation = rightRot;
  1247. }
  1248. /// <summary>
  1249. /// Accesses OpenVR properties about a given deviceID. Especially useful for differentiating per type of OpenVR device (i.e. Oculus, Vive)
  1250. /// </summary>
  1251. public static string GetOpenVRStringProperty(OVR.OpenVR.ETrackedDeviceProperty prop, uint deviceId = OVR.OpenVR.OpenVR.k_unTrackedDeviceIndex_Hmd)
  1252. {
  1253. OVR.OpenVR.ETrackedPropertyError error = OVR.OpenVR.ETrackedPropertyError.TrackedProp_Success;
  1254. OVR.OpenVR.CVRSystem system = OVR.OpenVR.OpenVR.System;
  1255. if (system != null)
  1256. {
  1257. uint capacity = system.GetStringTrackedDeviceProperty(deviceId, prop, null, 0, ref error);
  1258. if (capacity > 1)
  1259. {
  1260. var result = new System.Text.StringBuilder((int)capacity);
  1261. system.GetStringTrackedDeviceProperty(deviceId, prop, result, capacity, ref error);
  1262. return result.ToString();
  1263. }
  1264. return (error != OVR.OpenVR.ETrackedPropertyError.TrackedProp_Success) ? error.ToString() : "<unknown>";
  1265. }
  1266. return "";
  1267. }
  1268. /// <summary>
  1269. /// Associates OpenVR device IDs with left and right motion controllers, for later haptic playback.
  1270. /// </summary>
  1271. private static void UpdateXRControllerNodeIds()
  1272. {
  1273. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR)
  1274. {
  1275. openVRControllerDetails[0].deviceID = OVR.OpenVR.OpenVR.k_unMaxTrackedDeviceCount;
  1276. openVRControllerDetails[1].deviceID = OVR.OpenVR.OpenVR.k_unMaxTrackedDeviceCount;
  1277. OVR.OpenVR.CVRSystem system = OVR.OpenVR.OpenVR.System;
  1278. if (system != null)
  1279. {
  1280. for (uint id = 0; id < OVR.OpenVR.OpenVR.k_unMaxTrackedDeviceCount; id++)
  1281. {
  1282. OVR.OpenVR.ETrackedDeviceClass deviceClass = system.GetTrackedDeviceClass(id);
  1283. if (deviceClass == OVR.OpenVR.ETrackedDeviceClass.Controller && system.IsTrackedDeviceConnected(id))
  1284. {
  1285. OpenVRController controllerType;
  1286. String controllerName = GetOpenVRStringProperty(OVR.OpenVR.ETrackedDeviceProperty.Prop_ControllerType_String, id);
  1287. if (controllerName == OPENVR_TOUCH_NAME)
  1288. controllerType = OpenVRController.OculusTouch;
  1289. else if (controllerName == OPENVR_VIVE_CONTROLLER_NAME)
  1290. controllerType = OpenVRController.ViveController;
  1291. else if (controllerName == OPENVR_WINDOWSMR_CONTROLLER_NAME)
  1292. controllerType = OpenVRController.WindowsMRController;
  1293. else
  1294. controllerType = OpenVRController.Unknown;
  1295. OVR.OpenVR.ETrackedControllerRole role = system.GetControllerRoleForTrackedDeviceIndex(id);
  1296. if (role == OVR.OpenVR.ETrackedControllerRole.LeftHand)
  1297. {
  1298. system.GetControllerState(id, ref openVRControllerDetails[0].state, (uint)Marshal.SizeOf(typeof(OVR.OpenVR.VRControllerState_t)));
  1299. openVRControllerDetails[0].deviceID = id;
  1300. openVRControllerDetails[0].controllerType = controllerType;
  1301. connectedControllerTypes |= Controller.LTouch;
  1302. }
  1303. else if (role == OVR.OpenVR.ETrackedControllerRole.RightHand)
  1304. {
  1305. system.GetControllerState(id, ref openVRControllerDetails[1].state, (uint)Marshal.SizeOf(typeof(OVR.OpenVR.VRControllerState_t)));
  1306. openVRControllerDetails[1].deviceID = id;
  1307. openVRControllerDetails[1].controllerType = controllerType;
  1308. connectedControllerTypes |= Controller.RTouch;
  1309. }
  1310. }
  1311. }
  1312. }
  1313. }
  1314. }
  1315. /// <summary>
  1316. /// Runs once a frame to update cross-platform haptic playback
  1317. /// </summary>
  1318. private static void UpdateXRControllerHaptics()
  1319. {
  1320. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR)
  1321. {
  1322. for (int i = 0; i < NUM_HAPTIC_CHANNELS; i++)
  1323. {
  1324. if (hapticInfos[i].playingHaptics)
  1325. {
  1326. hapticInfos[i].hapticsDurationPlayed += Time.deltaTime;
  1327. PlayHapticImpulse(hapticInfos[i].hapticAmplitude, hapticInfos[i].node);
  1328. if (hapticInfos[i].hapticsDurationPlayed >= hapticInfos[i].hapticsDuration)
  1329. {
  1330. hapticInfos[i].playingHaptics = false;
  1331. }
  1332. }
  1333. }
  1334. }
  1335. }
  1336. private static void InitHapticInfo()
  1337. {
  1338. hapticInfos = new HapticInfo[NUM_HAPTIC_CHANNELS];
  1339. for (int i = 0; i < NUM_HAPTIC_CHANNELS; i++)
  1340. {
  1341. hapticInfos[i] = new HapticInfo();
  1342. }
  1343. }
  1344. private static void PlayHapticImpulse(float amplitude, Node deviceNode)
  1345. {
  1346. OVR.OpenVR.CVRSystem system = OVR.OpenVR.OpenVR.System;
  1347. if (system != null && amplitude != 0.0f)
  1348. {
  1349. uint controllerId = (deviceNode == Node.LeftHand) ? openVRControllerDetails[0].deviceID : openVRControllerDetails[1].deviceID;
  1350. if (IsValidOpenVRDevice(controllerId))
  1351. system.TriggerHapticPulse(controllerId, 0, (char)(OPENVR_MAX_HAPTIC_AMPLITUDE * amplitude));
  1352. }
  1353. }
  1354. private static bool IsValidOpenVRDevice(uint deviceId)
  1355. {
  1356. return (deviceId >= 0 && deviceId < OVR.OpenVR.OpenVR.k_unMaxTrackedDeviceCount);
  1357. }
  1358. /// <summary>
  1359. /// Activates vibration with the given frequency and amplitude with the given controller mask.
  1360. /// Ignored on controllers that do not support vibration. Expected values range from 0 to 1.
  1361. /// </summary>
  1362. public static void SetControllerVibration(float frequency, float amplitude, Controller controllerMask = Controller.Active)
  1363. {
  1364. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus)
  1365. {
  1366. if ((controllerMask & Controller.Active) != 0)
  1367. controllerMask |= activeControllerType;
  1368. for (int i = 0; i < controllers.Count; i++)
  1369. {
  1370. OVRControllerBase controller = controllers[i];
  1371. if (ShouldResolveController(controller.controllerType, controllerMask))
  1372. {
  1373. controller.SetControllerVibration(frequency, amplitude);
  1374. }
  1375. }
  1376. }
  1377. else if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR)
  1378. {
  1379. if (controllerMask == Controller.LTouch || controllerMask == Controller.RTouch)
  1380. {
  1381. Node controllerNode = (controllerMask == Controller.LTouch) ? Node.LeftHand : Node.RightHand;
  1382. StartVibration(amplitude, HAPTIC_VIBRATION_DURATION_SECONDS, controllerNode);
  1383. }
  1384. }
  1385. }
  1386. /// <summary>
  1387. /// Triggers a recenter to realign the specified controller's virtual pose with the user's real-world pose.
  1388. /// Only applicable to controllers that require recentering, such as the GearVR Controller.
  1389. /// Ignored for controllers that do not require recentering.
  1390. /// </summary>
  1391. public static void RecenterController(Controller controllerMask = Controller.Active)
  1392. {
  1393. if ((controllerMask & Controller.Active) != 0)
  1394. controllerMask |= activeControllerType;
  1395. for (int i = 0; i < controllers.Count; i++)
  1396. {
  1397. OVRControllerBase controller = controllers[i];
  1398. if (ShouldResolveController(controller.controllerType, controllerMask))
  1399. {
  1400. controller.RecenterController();
  1401. }
  1402. }
  1403. }
  1404. /// <summary>
  1405. /// Returns true if the specified controller was recentered this frame.
  1406. /// Only applicable to controllers that require recentering, such as the GearVR Controller.
  1407. /// Returns false for controllers that do not require recentering.
  1408. /// </summary>
  1409. public static bool GetControllerWasRecentered(Controller controllerMask = Controller.Active)
  1410. {
  1411. if ((controllerMask & Controller.Active) != 0)
  1412. controllerMask |= activeControllerType;
  1413. bool wasRecentered = false;
  1414. for (int i = 0; i < controllers.Count; i++)
  1415. {
  1416. OVRControllerBase controller = controllers[i];
  1417. if (ShouldResolveController(controller.controllerType, controllerMask))
  1418. {
  1419. wasRecentered |= controller.WasRecentered();
  1420. }
  1421. }
  1422. return wasRecentered;
  1423. }
  1424. /// <summary>
  1425. /// Returns the number of times the controller has been recentered this session.
  1426. /// Useful for detecting recenter events and resetting state such as arm model simulations, etc.
  1427. /// Wraps around to 0 after 255.
  1428. /// Only applicable to controllers that require recentering, such as the GearVR Controller.
  1429. /// Returns 0 for controllers that do not require recentering.
  1430. /// </summary>
  1431. public static byte GetControllerRecenterCount(Controller controllerMask = Controller.Active)
  1432. {
  1433. if ((controllerMask & Controller.Active) != 0)
  1434. controllerMask |= activeControllerType;
  1435. byte recenterCount = 0;
  1436. for (int i = 0; i < controllers.Count; i++)
  1437. {
  1438. OVRControllerBase controller = controllers[i];
  1439. if (ShouldResolveController(controller.controllerType, controllerMask))
  1440. {
  1441. recenterCount = controller.GetRecenterCount();
  1442. break;
  1443. }
  1444. }
  1445. return recenterCount;
  1446. }
  1447. /// <summary>
  1448. /// Returns the battery percentage remaining for the specified controller. Values range from 0 to 100.
  1449. /// Only applicable to controllers that report battery level, such as the GearVR Controller.
  1450. /// Returns 0 for controllers that do not report battery level.
  1451. /// </summary>
  1452. public static byte GetControllerBatteryPercentRemaining(Controller controllerMask = Controller.Active)
  1453. {
  1454. if ((controllerMask & Controller.Active) != 0)
  1455. controllerMask |= activeControllerType;
  1456. byte battery = 0;
  1457. for (int i = 0; i < controllers.Count; i++)
  1458. {
  1459. OVRControllerBase controller = controllers[i];
  1460. if (ShouldResolveController(controller.controllerType, controllerMask))
  1461. {
  1462. battery = controller.GetBatteryPercentRemaining();
  1463. break;
  1464. }
  1465. }
  1466. return battery;
  1467. }
  1468. private static Vector2 CalculateAbsMax(Vector2 a, Vector2 b)
  1469. {
  1470. float absA = a.sqrMagnitude;
  1471. float absB = b.sqrMagnitude;
  1472. if (absA >= absB)
  1473. return a;
  1474. return b;
  1475. }
  1476. private static float CalculateAbsMax(float a, float b)
  1477. {
  1478. float absA = (a >= 0) ? a : -a;
  1479. float absB = (b >= 0) ? b : -b;
  1480. if (absA >= absB)
  1481. return a;
  1482. return b;
  1483. }
  1484. private static Vector2 CalculateDeadzone(Vector2 a, float deadzone)
  1485. {
  1486. if (a.sqrMagnitude <= (deadzone * deadzone))
  1487. return Vector2.zero;
  1488. a *= ((a.magnitude - deadzone) / (1.0f - deadzone));
  1489. if (a.sqrMagnitude > 1.0f)
  1490. return a.normalized;
  1491. return a;
  1492. }
  1493. private static float CalculateDeadzone(float a, float deadzone)
  1494. {
  1495. float mag = (a >= 0) ? a : -a;
  1496. if (mag <= deadzone)
  1497. return 0.0f;
  1498. a *= (mag - deadzone) / (1.0f - deadzone);
  1499. if ((a * a) > 1.0f)
  1500. return (a >= 0) ? 1.0f : -1.0f;
  1501. return a;
  1502. }
  1503. private static bool ShouldResolveController(Controller controllerType, Controller controllerMask)
  1504. {
  1505. bool isValid = false;
  1506. if ((controllerType & controllerMask) == controllerType)
  1507. {
  1508. isValid = true;
  1509. }
  1510. // If the mask requests both Touch controllers, reject the individual touch controllers.
  1511. if (((controllerMask & Controller.Touch) == Controller.Touch)
  1512. && ((controllerType & Controller.Touch) != 0)
  1513. && ((controllerType & Controller.Touch) != Controller.Touch))
  1514. {
  1515. isValid = false;
  1516. }
  1517. // If the mask requests both Hands, reject the individual hands.
  1518. if (((controllerMask & Controller.Hands) == Controller.Hands)
  1519. && ((controllerType & Controller.Hands) != 0)
  1520. && ((controllerType & Controller.Hands) != Controller.Hands))
  1521. {
  1522. isValid = false;
  1523. }
  1524. return isValid;
  1525. }
  1526. private abstract class OVRControllerBase
  1527. {
  1528. public class VirtualButtonMap
  1529. {
  1530. public RawButton None = RawButton.None;
  1531. public RawButton One = RawButton.None;
  1532. public RawButton Two = RawButton.None;
  1533. public RawButton Three = RawButton.None;
  1534. public RawButton Four = RawButton.None;
  1535. public RawButton Start = RawButton.None;
  1536. public RawButton Back = RawButton.None;
  1537. public RawButton PrimaryShoulder = RawButton.None;
  1538. public RawButton PrimaryIndexTrigger = RawButton.None;
  1539. public RawButton PrimaryHandTrigger = RawButton.None;
  1540. public RawButton PrimaryThumbstick = RawButton.None;
  1541. public RawButton PrimaryThumbstickUp = RawButton.None;
  1542. public RawButton PrimaryThumbstickDown = RawButton.None;
  1543. public RawButton PrimaryThumbstickLeft = RawButton.None;
  1544. public RawButton PrimaryThumbstickRight = RawButton.None;
  1545. public RawButton PrimaryTouchpad = RawButton.None;
  1546. public RawButton SecondaryShoulder = RawButton.None;
  1547. public RawButton SecondaryIndexTrigger = RawButton.None;
  1548. public RawButton SecondaryHandTrigger = RawButton.None;
  1549. public RawButton SecondaryThumbstick = RawButton.None;
  1550. public RawButton SecondaryThumbstickUp = RawButton.None;
  1551. public RawButton SecondaryThumbstickDown = RawButton.None;
  1552. public RawButton SecondaryThumbstickLeft = RawButton.None;
  1553. public RawButton SecondaryThumbstickRight = RawButton.None;
  1554. public RawButton SecondaryTouchpad = RawButton.None;
  1555. public RawButton DpadUp = RawButton.None;
  1556. public RawButton DpadDown = RawButton.None;
  1557. public RawButton DpadLeft = RawButton.None;
  1558. public RawButton DpadRight = RawButton.None;
  1559. public RawButton Up = RawButton.None;
  1560. public RawButton Down = RawButton.None;
  1561. public RawButton Left = RawButton.None;
  1562. public RawButton Right = RawButton.None;
  1563. public RawButton ToRawMask(Button virtualMask)
  1564. {
  1565. RawButton rawMask = 0;
  1566. if (virtualMask == Button.None)
  1567. return RawButton.None;
  1568. if ((virtualMask & Button.One) != 0)
  1569. rawMask |= One;
  1570. if ((virtualMask & Button.Two) != 0)
  1571. rawMask |= Two;
  1572. if ((virtualMask & Button.Three) != 0)
  1573. rawMask |= Three;
  1574. if ((virtualMask & Button.Four) != 0)
  1575. rawMask |= Four;
  1576. if ((virtualMask & Button.Start) != 0)
  1577. rawMask |= Start;
  1578. if ((virtualMask & Button.Back) != 0)
  1579. rawMask |= Back;
  1580. if ((virtualMask & Button.PrimaryShoulder) != 0)
  1581. rawMask |= PrimaryShoulder;
  1582. if ((virtualMask & Button.PrimaryIndexTrigger) != 0)
  1583. rawMask |= PrimaryIndexTrigger;
  1584. if ((virtualMask & Button.PrimaryHandTrigger) != 0)
  1585. rawMask |= PrimaryHandTrigger;
  1586. if ((virtualMask & Button.PrimaryThumbstick) != 0)
  1587. rawMask |= PrimaryThumbstick;
  1588. if ((virtualMask & Button.PrimaryThumbstickUp) != 0)
  1589. rawMask |= PrimaryThumbstickUp;
  1590. if ((virtualMask & Button.PrimaryThumbstickDown) != 0)
  1591. rawMask |= PrimaryThumbstickDown;
  1592. if ((virtualMask & Button.PrimaryThumbstickLeft) != 0)
  1593. rawMask |= PrimaryThumbstickLeft;
  1594. if ((virtualMask & Button.PrimaryThumbstickRight) != 0)
  1595. rawMask |= PrimaryThumbstickRight;
  1596. if ((virtualMask & Button.PrimaryTouchpad) != 0)
  1597. rawMask |= PrimaryTouchpad;
  1598. if ((virtualMask & Button.SecondaryShoulder) != 0)
  1599. rawMask |= SecondaryShoulder;
  1600. if ((virtualMask & Button.SecondaryIndexTrigger) != 0)
  1601. rawMask |= SecondaryIndexTrigger;
  1602. if ((virtualMask & Button.SecondaryHandTrigger) != 0)
  1603. rawMask |= SecondaryHandTrigger;
  1604. if ((virtualMask & Button.SecondaryThumbstick) != 0)
  1605. rawMask |= SecondaryThumbstick;
  1606. if ((virtualMask & Button.SecondaryThumbstickUp) != 0)
  1607. rawMask |= SecondaryThumbstickUp;
  1608. if ((virtualMask & Button.SecondaryThumbstickDown) != 0)
  1609. rawMask |= SecondaryThumbstickDown;
  1610. if ((virtualMask & Button.SecondaryThumbstickLeft) != 0)
  1611. rawMask |= SecondaryThumbstickLeft;
  1612. if ((virtualMask & Button.SecondaryThumbstickRight) != 0)
  1613. rawMask |= SecondaryThumbstickRight;
  1614. if ((virtualMask & Button.SecondaryTouchpad) != 0)
  1615. rawMask |= SecondaryTouchpad;
  1616. if ((virtualMask & Button.DpadUp) != 0)
  1617. rawMask |= DpadUp;
  1618. if ((virtualMask & Button.DpadDown) != 0)
  1619. rawMask |= DpadDown;
  1620. if ((virtualMask & Button.DpadLeft) != 0)
  1621. rawMask |= DpadLeft;
  1622. if ((virtualMask & Button.DpadRight) != 0)
  1623. rawMask |= DpadRight;
  1624. if ((virtualMask & Button.Up) != 0)
  1625. rawMask |= Up;
  1626. if ((virtualMask & Button.Down) != 0)
  1627. rawMask |= Down;
  1628. if ((virtualMask & Button.Left) != 0)
  1629. rawMask |= Left;
  1630. if ((virtualMask & Button.Right) != 0)
  1631. rawMask |= Right;
  1632. return rawMask;
  1633. }
  1634. }
  1635. public class VirtualTouchMap
  1636. {
  1637. public RawTouch None = RawTouch.None;
  1638. public RawTouch One = RawTouch.None;
  1639. public RawTouch Two = RawTouch.None;
  1640. public RawTouch Three = RawTouch.None;
  1641. public RawTouch Four = RawTouch.None;
  1642. public RawTouch PrimaryIndexTrigger = RawTouch.None;
  1643. public RawTouch PrimaryThumbstick = RawTouch.None;
  1644. public RawTouch PrimaryThumbRest = RawTouch.None;
  1645. public RawTouch PrimaryTouchpad = RawTouch.None;
  1646. public RawTouch SecondaryIndexTrigger = RawTouch.None;
  1647. public RawTouch SecondaryThumbstick = RawTouch.None;
  1648. public RawTouch SecondaryThumbRest = RawTouch.None;
  1649. public RawTouch SecondaryTouchpad = RawTouch.None;
  1650. public RawTouch ToRawMask(Touch virtualMask)
  1651. {
  1652. RawTouch rawMask = 0;
  1653. if (virtualMask == Touch.None)
  1654. return RawTouch.None;
  1655. if ((virtualMask & Touch.One) != 0)
  1656. rawMask |= One;
  1657. if ((virtualMask & Touch.Two) != 0)
  1658. rawMask |= Two;
  1659. if ((virtualMask & Touch.Three) != 0)
  1660. rawMask |= Three;
  1661. if ((virtualMask & Touch.Four) != 0)
  1662. rawMask |= Four;
  1663. if ((virtualMask & Touch.PrimaryIndexTrigger) != 0)
  1664. rawMask |= PrimaryIndexTrigger;
  1665. if ((virtualMask & Touch.PrimaryThumbstick) != 0)
  1666. rawMask |= PrimaryThumbstick;
  1667. if ((virtualMask & Touch.PrimaryThumbRest) != 0)
  1668. rawMask |= PrimaryThumbRest;
  1669. if ((virtualMask & Touch.PrimaryTouchpad) != 0)
  1670. rawMask |= PrimaryTouchpad;
  1671. if ((virtualMask & Touch.SecondaryIndexTrigger) != 0)
  1672. rawMask |= SecondaryIndexTrigger;
  1673. if ((virtualMask & Touch.SecondaryThumbstick) != 0)
  1674. rawMask |= SecondaryThumbstick;
  1675. if ((virtualMask & Touch.SecondaryThumbRest) != 0)
  1676. rawMask |= SecondaryThumbRest;
  1677. if ((virtualMask & Touch.SecondaryTouchpad) != 0)
  1678. rawMask |= SecondaryTouchpad;
  1679. return rawMask;
  1680. }
  1681. }
  1682. public class VirtualNearTouchMap
  1683. {
  1684. public RawNearTouch None = RawNearTouch.None;
  1685. public RawNearTouch PrimaryIndexTrigger = RawNearTouch.None;
  1686. public RawNearTouch PrimaryThumbButtons = RawNearTouch.None;
  1687. public RawNearTouch SecondaryIndexTrigger = RawNearTouch.None;
  1688. public RawNearTouch SecondaryThumbButtons = RawNearTouch.None;
  1689. public RawNearTouch ToRawMask(NearTouch virtualMask)
  1690. {
  1691. RawNearTouch rawMask = 0;
  1692. if (virtualMask == NearTouch.None)
  1693. return RawNearTouch.None;
  1694. if ((virtualMask & NearTouch.PrimaryIndexTrigger) != 0)
  1695. rawMask |= PrimaryIndexTrigger;
  1696. if ((virtualMask & NearTouch.PrimaryThumbButtons) != 0)
  1697. rawMask |= PrimaryThumbButtons;
  1698. if ((virtualMask & NearTouch.SecondaryIndexTrigger) != 0)
  1699. rawMask |= SecondaryIndexTrigger;
  1700. if ((virtualMask & NearTouch.SecondaryThumbButtons) != 0)
  1701. rawMask |= SecondaryThumbButtons;
  1702. return rawMask;
  1703. }
  1704. }
  1705. public class VirtualAxis1DMap
  1706. {
  1707. public RawAxis1D None = RawAxis1D.None;
  1708. public RawAxis1D PrimaryIndexTrigger = RawAxis1D.None;
  1709. public RawAxis1D PrimaryHandTrigger = RawAxis1D.None;
  1710. public RawAxis1D SecondaryIndexTrigger = RawAxis1D.None;
  1711. public RawAxis1D SecondaryHandTrigger = RawAxis1D.None;
  1712. public RawAxis1D ToRawMask(Axis1D virtualMask)
  1713. {
  1714. RawAxis1D rawMask = 0;
  1715. if (virtualMask == Axis1D.None)
  1716. return RawAxis1D.None;
  1717. if ((virtualMask & Axis1D.PrimaryIndexTrigger) != 0)
  1718. rawMask |= PrimaryIndexTrigger;
  1719. if ((virtualMask & Axis1D.PrimaryHandTrigger) != 0)
  1720. rawMask |= PrimaryHandTrigger;
  1721. if ((virtualMask & Axis1D.SecondaryIndexTrigger) != 0)
  1722. rawMask |= SecondaryIndexTrigger;
  1723. if ((virtualMask & Axis1D.SecondaryHandTrigger) != 0)
  1724. rawMask |= SecondaryHandTrigger;
  1725. return rawMask;
  1726. }
  1727. }
  1728. public class VirtualAxis2DMap
  1729. {
  1730. public RawAxis2D None = RawAxis2D.None;
  1731. public RawAxis2D PrimaryThumbstick = RawAxis2D.None;
  1732. public RawAxis2D PrimaryTouchpad = RawAxis2D.None;
  1733. public RawAxis2D SecondaryThumbstick = RawAxis2D.None;
  1734. public RawAxis2D SecondaryTouchpad = RawAxis2D.None;
  1735. public RawAxis2D ToRawMask(Axis2D virtualMask)
  1736. {
  1737. RawAxis2D rawMask = 0;
  1738. if (virtualMask == Axis2D.None)
  1739. return RawAxis2D.None;
  1740. if ((virtualMask & Axis2D.PrimaryThumbstick) != 0)
  1741. rawMask |= PrimaryThumbstick;
  1742. if ((virtualMask & Axis2D.PrimaryTouchpad) != 0)
  1743. rawMask |= PrimaryTouchpad;
  1744. if ((virtualMask & Axis2D.SecondaryThumbstick) != 0)
  1745. rawMask |= SecondaryThumbstick;
  1746. if ((virtualMask & Axis2D.SecondaryTouchpad) != 0)
  1747. rawMask |= SecondaryTouchpad;
  1748. return rawMask;
  1749. }
  1750. }
  1751. public Controller controllerType = Controller.None;
  1752. public VirtualButtonMap buttonMap = new VirtualButtonMap();
  1753. public VirtualTouchMap touchMap = new VirtualTouchMap();
  1754. public VirtualNearTouchMap nearTouchMap = new VirtualNearTouchMap();
  1755. public VirtualAxis1DMap axis1DMap = new VirtualAxis1DMap();
  1756. public VirtualAxis2DMap axis2DMap = new VirtualAxis2DMap();
  1757. public OVRPlugin.ControllerState4 previousState = new OVRPlugin.ControllerState4();
  1758. public OVRPlugin.ControllerState4 currentState = new OVRPlugin.ControllerState4();
  1759. public bool shouldApplyDeadzone = true;
  1760. public OVRControllerBase()
  1761. {
  1762. ConfigureButtonMap();
  1763. ConfigureTouchMap();
  1764. ConfigureNearTouchMap();
  1765. ConfigureAxis1DMap();
  1766. ConfigureAxis2DMap();
  1767. }
  1768. public virtual Controller Update()
  1769. {
  1770. OVRPlugin.ControllerState4 state;
  1771. if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR && ( (controllerType & Controller.Touch) != 0) )
  1772. state = GetOpenVRControllerState(controllerType);
  1773. else
  1774. state = OVRPlugin.GetControllerState4((uint)controllerType);
  1775. if (state.LIndexTrigger >= AXIS_AS_BUTTON_THRESHOLD)
  1776. state.Buttons |= (uint)RawButton.LIndexTrigger;
  1777. if (state.LHandTrigger >= AXIS_AS_BUTTON_THRESHOLD)
  1778. state.Buttons |= (uint)RawButton.LHandTrigger;
  1779. if (state.LThumbstick.y >= AXIS_AS_BUTTON_THRESHOLD)
  1780. state.Buttons |= (uint)RawButton.LThumbstickUp;
  1781. if (state.LThumbstick.y <= -AXIS_AS_BUTTON_THRESHOLD)
  1782. state.Buttons |= (uint)RawButton.LThumbstickDown;
  1783. if (state.LThumbstick.x <= -AXIS_AS_BUTTON_THRESHOLD)
  1784. state.Buttons |= (uint)RawButton.LThumbstickLeft;
  1785. if (state.LThumbstick.x >= AXIS_AS_BUTTON_THRESHOLD)
  1786. state.Buttons |= (uint)RawButton.LThumbstickRight;
  1787. if (state.RIndexTrigger >= AXIS_AS_BUTTON_THRESHOLD)
  1788. state.Buttons |= (uint)RawButton.RIndexTrigger;
  1789. if (state.RHandTrigger >= AXIS_AS_BUTTON_THRESHOLD)
  1790. state.Buttons |= (uint)RawButton.RHandTrigger;
  1791. if (state.RThumbstick.y >= AXIS_AS_BUTTON_THRESHOLD)
  1792. state.Buttons |= (uint)RawButton.RThumbstickUp;
  1793. if (state.RThumbstick.y <= -AXIS_AS_BUTTON_THRESHOLD)
  1794. state.Buttons |= (uint)RawButton.RThumbstickDown;
  1795. if (state.RThumbstick.x <= -AXIS_AS_BUTTON_THRESHOLD)
  1796. state.Buttons |= (uint)RawButton.RThumbstickLeft;
  1797. if (state.RThumbstick.x >= AXIS_AS_BUTTON_THRESHOLD)
  1798. state.Buttons |= (uint)RawButton.RThumbstickRight;
  1799. previousState = currentState;
  1800. currentState = state;
  1801. return ((Controller)currentState.ConnectedControllers & controllerType);
  1802. }
  1803. private OVRPlugin.ControllerState4 GetOpenVRControllerState(Controller controllerType)
  1804. {
  1805. OVRPlugin.ControllerState4 state = new OVRPlugin.ControllerState4();
  1806. if ((controllerType & Controller.LTouch) == Controller.LTouch && IsValidOpenVRDevice(openVRControllerDetails[0].deviceID))
  1807. {
  1808. OVR.OpenVR.VRControllerState_t leftControllerState = openVRControllerDetails[0].state;
  1809. if ((leftControllerState.ulButtonPressed & ((ulong)OpenVRButton.Two)) == (ulong)OpenVRButton.Two)
  1810. state.Buttons |= (uint)RawButton.Y;
  1811. if ((leftControllerState.ulButtonPressed & ((ulong)OpenVRButton.Thumbstick)) == (ulong)OpenVRButton.Thumbstick)
  1812. state.Buttons |= (uint)RawButton.LThumbstick;
  1813. state.LIndexTrigger = leftControllerState.rAxis1.x;
  1814. if (openVRControllerDetails[0].controllerType == OpenVRController.OculusTouch || openVRControllerDetails[0].controllerType == OpenVRController.ViveController)
  1815. {
  1816. state.LThumbstick.x = leftControllerState.rAxis0.x;
  1817. state.LThumbstick.y = leftControllerState.rAxis0.y;
  1818. }
  1819. else if (openVRControllerDetails[0].controllerType == OpenVRController.WindowsMRController)
  1820. {
  1821. state.LThumbstick.x = leftControllerState.rAxis2.x;
  1822. state.LThumbstick.y = leftControllerState.rAxis2.y;
  1823. }
  1824. if (openVRControllerDetails[0].controllerType == OpenVRController.OculusTouch)
  1825. state.LHandTrigger = leftControllerState.rAxis2.x;
  1826. else if (openVRControllerDetails[0].controllerType == OpenVRController.ViveController || openVRControllerDetails[0].controllerType == OpenVRController.WindowsMRController)
  1827. state.LHandTrigger = ((leftControllerState.ulButtonPressed & ((ulong)OpenVRButton.Grip)) == ((ulong)OpenVRButton.Grip)) ? 1 : 0;
  1828. }
  1829. if ((controllerType & Controller.RTouch) == Controller.RTouch && IsValidOpenVRDevice(openVRControllerDetails[1].deviceID))
  1830. {
  1831. OVR.OpenVR.VRControllerState_t rightControllerState = openVRControllerDetails[1].state;
  1832. if ((rightControllerState.ulButtonPressed & ((ulong)OpenVRButton.Two)) == (ulong)OpenVRButton.Two)
  1833. state.Buttons |= (uint)RawButton.B;
  1834. if ((rightControllerState.ulButtonPressed & ((ulong)OpenVRButton.Thumbstick)) == (ulong)OpenVRButton.Thumbstick)
  1835. state.Buttons |= (uint)RawButton.RThumbstick;
  1836. state.RIndexTrigger = rightControllerState.rAxis1.x;
  1837. if (openVRControllerDetails[1].controllerType == OpenVRController.OculusTouch || openVRControllerDetails[1].controllerType == OpenVRController.ViveController)
  1838. {
  1839. state.RThumbstick.x = rightControllerState.rAxis0.x;
  1840. state.RThumbstick.y = rightControllerState.rAxis0.y;
  1841. }
  1842. else if (openVRControllerDetails[1].controllerType == OpenVRController.WindowsMRController)
  1843. {
  1844. state.RThumbstick.x = rightControllerState.rAxis2.x;
  1845. state.RThumbstick.y = rightControllerState.rAxis2.y;
  1846. }
  1847. if (openVRControllerDetails[1].controllerType == OpenVRController.OculusTouch)
  1848. state.RHandTrigger = rightControllerState.rAxis2.x;
  1849. else if (openVRControllerDetails[1].controllerType == OpenVRController.ViveController || openVRControllerDetails[1].controllerType == OpenVRController.WindowsMRController)
  1850. state.RHandTrigger = ((rightControllerState.ulButtonPressed & ((ulong)OpenVRButton.Grip)) == ((ulong)OpenVRButton.Grip)) ? 1 : 0;
  1851. }
  1852. return state;
  1853. }
  1854. public virtual void SetControllerVibration(float frequency, float amplitude)
  1855. {
  1856. OVRPlugin.SetControllerVibration((uint)controllerType, frequency, amplitude);
  1857. }
  1858. public virtual void RecenterController()
  1859. {
  1860. OVRPlugin.RecenterTrackingOrigin(OVRPlugin.RecenterFlags.Controllers);
  1861. }
  1862. public virtual bool WasRecentered()
  1863. {
  1864. return false;
  1865. }
  1866. public virtual byte GetRecenterCount()
  1867. {
  1868. return 0;
  1869. }
  1870. public virtual byte GetBatteryPercentRemaining()
  1871. {
  1872. return 0;
  1873. }
  1874. public abstract void ConfigureButtonMap();
  1875. public abstract void ConfigureTouchMap();
  1876. public abstract void ConfigureNearTouchMap();
  1877. public abstract void ConfigureAxis1DMap();
  1878. public abstract void ConfigureAxis2DMap();
  1879. public RawButton ResolveToRawMask(Button virtualMask)
  1880. {
  1881. return buttonMap.ToRawMask(virtualMask);
  1882. }
  1883. public RawTouch ResolveToRawMask(Touch virtualMask)
  1884. {
  1885. return touchMap.ToRawMask(virtualMask);
  1886. }
  1887. public RawNearTouch ResolveToRawMask(NearTouch virtualMask)
  1888. {
  1889. return nearTouchMap.ToRawMask(virtualMask);
  1890. }
  1891. public RawAxis1D ResolveToRawMask(Axis1D virtualMask)
  1892. {
  1893. return axis1DMap.ToRawMask(virtualMask);
  1894. }
  1895. public RawAxis2D ResolveToRawMask(Axis2D virtualMask)
  1896. {
  1897. return axis2DMap.ToRawMask(virtualMask);
  1898. }
  1899. }
  1900. private class OVRControllerTouch : OVRControllerBase
  1901. {
  1902. public OVRControllerTouch()
  1903. {
  1904. controllerType = Controller.Touch;
  1905. }
  1906. public override void ConfigureButtonMap()
  1907. {
  1908. buttonMap.None = RawButton.None;
  1909. buttonMap.One = RawButton.A;
  1910. buttonMap.Two = RawButton.B;
  1911. buttonMap.Three = RawButton.X;
  1912. buttonMap.Four = RawButton.Y;
  1913. buttonMap.Start = RawButton.Start;
  1914. buttonMap.Back = RawButton.None;
  1915. buttonMap.PrimaryShoulder = RawButton.None;
  1916. buttonMap.PrimaryIndexTrigger = RawButton.LIndexTrigger;
  1917. buttonMap.PrimaryHandTrigger = RawButton.LHandTrigger;
  1918. buttonMap.PrimaryThumbstick = RawButton.LThumbstick;
  1919. buttonMap.PrimaryThumbstickUp = RawButton.LThumbstickUp;
  1920. buttonMap.PrimaryThumbstickDown = RawButton.LThumbstickDown;
  1921. buttonMap.PrimaryThumbstickLeft = RawButton.LThumbstickLeft;
  1922. buttonMap.PrimaryThumbstickRight = RawButton.LThumbstickRight;
  1923. buttonMap.PrimaryTouchpad = RawButton.None;
  1924. buttonMap.SecondaryShoulder = RawButton.None;
  1925. buttonMap.SecondaryIndexTrigger = RawButton.RIndexTrigger;
  1926. buttonMap.SecondaryHandTrigger = RawButton.RHandTrigger;
  1927. buttonMap.SecondaryThumbstick = RawButton.RThumbstick;
  1928. buttonMap.SecondaryThumbstickUp = RawButton.RThumbstickUp;
  1929. buttonMap.SecondaryThumbstickDown = RawButton.RThumbstickDown;
  1930. buttonMap.SecondaryThumbstickLeft = RawButton.RThumbstickLeft;
  1931. buttonMap.SecondaryThumbstickRight = RawButton.RThumbstickRight;
  1932. buttonMap.SecondaryTouchpad = RawButton.None;
  1933. buttonMap.DpadUp = RawButton.None;
  1934. buttonMap.DpadDown = RawButton.None;
  1935. buttonMap.DpadLeft = RawButton.None;
  1936. buttonMap.DpadRight = RawButton.None;
  1937. buttonMap.Up = RawButton.LThumbstickUp;
  1938. buttonMap.Down = RawButton.LThumbstickDown;
  1939. buttonMap.Left = RawButton.LThumbstickLeft;
  1940. buttonMap.Right = RawButton.LThumbstickRight;
  1941. }
  1942. public override void ConfigureTouchMap()
  1943. {
  1944. touchMap.None = RawTouch.None;
  1945. touchMap.One = RawTouch.A;
  1946. touchMap.Two = RawTouch.B;
  1947. touchMap.Three = RawTouch.X;
  1948. touchMap.Four = RawTouch.Y;
  1949. touchMap.PrimaryIndexTrigger = RawTouch.LIndexTrigger;
  1950. touchMap.PrimaryThumbstick = RawTouch.LThumbstick;
  1951. touchMap.PrimaryThumbRest = RawTouch.LThumbRest;
  1952. touchMap.PrimaryTouchpad = RawTouch.None;
  1953. touchMap.SecondaryIndexTrigger = RawTouch.RIndexTrigger;
  1954. touchMap.SecondaryThumbstick = RawTouch.RThumbstick;
  1955. touchMap.SecondaryThumbRest = RawTouch.RThumbRest;
  1956. touchMap.SecondaryTouchpad = RawTouch.None;
  1957. }
  1958. public override void ConfigureNearTouchMap()
  1959. {
  1960. nearTouchMap.None = RawNearTouch.None;
  1961. nearTouchMap.PrimaryIndexTrigger = RawNearTouch.LIndexTrigger;
  1962. nearTouchMap.PrimaryThumbButtons = RawNearTouch.LThumbButtons;
  1963. nearTouchMap.SecondaryIndexTrigger = RawNearTouch.RIndexTrigger;
  1964. nearTouchMap.SecondaryThumbButtons = RawNearTouch.RThumbButtons;
  1965. }
  1966. public override void ConfigureAxis1DMap()
  1967. {
  1968. axis1DMap.None = RawAxis1D.None;
  1969. axis1DMap.PrimaryIndexTrigger = RawAxis1D.LIndexTrigger;
  1970. axis1DMap.PrimaryHandTrigger = RawAxis1D.LHandTrigger;
  1971. axis1DMap.SecondaryIndexTrigger = RawAxis1D.RIndexTrigger;
  1972. axis1DMap.SecondaryHandTrigger = RawAxis1D.RHandTrigger;
  1973. }
  1974. public override void ConfigureAxis2DMap()
  1975. {
  1976. axis2DMap.None = RawAxis2D.None;
  1977. axis2DMap.PrimaryThumbstick = RawAxis2D.LThumbstick;
  1978. axis2DMap.PrimaryTouchpad = RawAxis2D.None;
  1979. axis2DMap.SecondaryThumbstick = RawAxis2D.RThumbstick;
  1980. axis2DMap.SecondaryTouchpad = RawAxis2D.None;
  1981. }
  1982. public override bool WasRecentered()
  1983. {
  1984. return ((currentState.LRecenterCount + currentState.RRecenterCount) != (previousState.LRecenterCount + previousState.RRecenterCount));
  1985. }
  1986. public override byte GetRecenterCount()
  1987. {
  1988. return (byte)(currentState.LRecenterCount + currentState.RRecenterCount);
  1989. }
  1990. public override byte GetBatteryPercentRemaining()
  1991. {
  1992. byte leftBattery = currentState.LBatteryPercentRemaining;
  1993. byte rightBattery = currentState.RBatteryPercentRemaining;
  1994. byte minBattery = (leftBattery <= rightBattery) ? leftBattery : rightBattery;
  1995. return minBattery;
  1996. }
  1997. }
  1998. private class OVRControllerLTouch : OVRControllerBase
  1999. {
  2000. public OVRControllerLTouch()
  2001. {
  2002. controllerType = Controller.LTouch;
  2003. }
  2004. public override void ConfigureButtonMap()
  2005. {
  2006. buttonMap.None = RawButton.None;
  2007. buttonMap.One = RawButton.X;
  2008. buttonMap.Two = RawButton.Y;
  2009. buttonMap.Three = RawButton.None;
  2010. buttonMap.Four = RawButton.None;
  2011. buttonMap.Start = RawButton.Start;
  2012. buttonMap.Back = RawButton.None;
  2013. buttonMap.PrimaryShoulder = RawButton.None;
  2014. buttonMap.PrimaryIndexTrigger = RawButton.LIndexTrigger;
  2015. buttonMap.PrimaryHandTrigger = RawButton.LHandTrigger;
  2016. buttonMap.PrimaryThumbstick = RawButton.LThumbstick;
  2017. buttonMap.PrimaryThumbstickUp = RawButton.LThumbstickUp;
  2018. buttonMap.PrimaryThumbstickDown = RawButton.LThumbstickDown;
  2019. buttonMap.PrimaryThumbstickLeft = RawButton.LThumbstickLeft;
  2020. buttonMap.PrimaryThumbstickRight = RawButton.LThumbstickRight;
  2021. buttonMap.PrimaryTouchpad = RawButton.None;
  2022. buttonMap.SecondaryShoulder = RawButton.None;
  2023. buttonMap.SecondaryIndexTrigger = RawButton.None;
  2024. buttonMap.SecondaryHandTrigger = RawButton.None;
  2025. buttonMap.SecondaryThumbstick = RawButton.None;
  2026. buttonMap.SecondaryThumbstickUp = RawButton.None;
  2027. buttonMap.SecondaryThumbstickDown = RawButton.None;
  2028. buttonMap.SecondaryThumbstickLeft = RawButton.None;
  2029. buttonMap.SecondaryThumbstickRight = RawButton.None;
  2030. buttonMap.SecondaryTouchpad = RawButton.None;
  2031. buttonMap.DpadUp = RawButton.None;
  2032. buttonMap.DpadDown = RawButton.None;
  2033. buttonMap.DpadLeft = RawButton.None;
  2034. buttonMap.DpadRight = RawButton.None;
  2035. buttonMap.Up = RawButton.LThumbstickUp;
  2036. buttonMap.Down = RawButton.LThumbstickDown;
  2037. buttonMap.Left = RawButton.LThumbstickLeft;
  2038. buttonMap.Right = RawButton.LThumbstickRight;
  2039. }
  2040. public override void ConfigureTouchMap()
  2041. {
  2042. touchMap.None = RawTouch.None;
  2043. touchMap.One = RawTouch.X;
  2044. touchMap.Two = RawTouch.Y;
  2045. touchMap.Three = RawTouch.None;
  2046. touchMap.Four = RawTouch.None;
  2047. touchMap.PrimaryIndexTrigger = RawTouch.LIndexTrigger;
  2048. touchMap.PrimaryThumbstick = RawTouch.LThumbstick;
  2049. touchMap.PrimaryThumbRest = RawTouch.LThumbRest;
  2050. touchMap.PrimaryTouchpad = RawTouch.None;
  2051. touchMap.SecondaryIndexTrigger = RawTouch.None;
  2052. touchMap.SecondaryThumbstick = RawTouch.None;
  2053. touchMap.SecondaryThumbRest = RawTouch.None;
  2054. touchMap.SecondaryTouchpad = RawTouch.None;
  2055. }
  2056. public override void ConfigureNearTouchMap()
  2057. {
  2058. nearTouchMap.None = RawNearTouch.None;
  2059. nearTouchMap.PrimaryIndexTrigger = RawNearTouch.LIndexTrigger;
  2060. nearTouchMap.PrimaryThumbButtons = RawNearTouch.LThumbButtons;
  2061. nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None;
  2062. nearTouchMap.SecondaryThumbButtons = RawNearTouch.None;
  2063. }
  2064. public override void ConfigureAxis1DMap()
  2065. {
  2066. axis1DMap.None = RawAxis1D.None;
  2067. axis1DMap.PrimaryIndexTrigger = RawAxis1D.LIndexTrigger;
  2068. axis1DMap.PrimaryHandTrigger = RawAxis1D.LHandTrigger;
  2069. axis1DMap.SecondaryIndexTrigger = RawAxis1D.None;
  2070. axis1DMap.SecondaryHandTrigger = RawAxis1D.None;
  2071. }
  2072. public override void ConfigureAxis2DMap()
  2073. {
  2074. axis2DMap.None = RawAxis2D.None;
  2075. axis2DMap.PrimaryThumbstick = RawAxis2D.LThumbstick;
  2076. axis2DMap.PrimaryTouchpad = RawAxis2D.None;
  2077. axis2DMap.SecondaryThumbstick = RawAxis2D.None;
  2078. axis2DMap.SecondaryTouchpad = RawAxis2D.None;
  2079. }
  2080. public override bool WasRecentered()
  2081. {
  2082. return (currentState.LRecenterCount != previousState.LRecenterCount);
  2083. }
  2084. public override byte GetRecenterCount()
  2085. {
  2086. return currentState.LRecenterCount;
  2087. }
  2088. public override byte GetBatteryPercentRemaining()
  2089. {
  2090. return currentState.LBatteryPercentRemaining;
  2091. }
  2092. }
  2093. private class OVRControllerRTouch : OVRControllerBase
  2094. {
  2095. public OVRControllerRTouch()
  2096. {
  2097. controllerType = Controller.RTouch;
  2098. }
  2099. public override void ConfigureButtonMap()
  2100. {
  2101. buttonMap.None = RawButton.None;
  2102. buttonMap.One = RawButton.A;
  2103. buttonMap.Two = RawButton.B;
  2104. buttonMap.Three = RawButton.None;
  2105. buttonMap.Four = RawButton.None;
  2106. buttonMap.Start = RawButton.None;
  2107. buttonMap.Back = RawButton.None;
  2108. buttonMap.PrimaryShoulder = RawButton.None;
  2109. buttonMap.PrimaryIndexTrigger = RawButton.RIndexTrigger;
  2110. buttonMap.PrimaryHandTrigger = RawButton.RHandTrigger;
  2111. buttonMap.PrimaryThumbstick = RawButton.RThumbstick;
  2112. buttonMap.PrimaryThumbstickUp = RawButton.RThumbstickUp;
  2113. buttonMap.PrimaryThumbstickDown = RawButton.RThumbstickDown;
  2114. buttonMap.PrimaryThumbstickLeft = RawButton.RThumbstickLeft;
  2115. buttonMap.PrimaryThumbstickRight = RawButton.RThumbstickRight;
  2116. buttonMap.PrimaryTouchpad = RawButton.None;
  2117. buttonMap.SecondaryShoulder = RawButton.None;
  2118. buttonMap.SecondaryIndexTrigger = RawButton.None;
  2119. buttonMap.SecondaryHandTrigger = RawButton.None;
  2120. buttonMap.SecondaryThumbstick = RawButton.None;
  2121. buttonMap.SecondaryThumbstickUp = RawButton.None;
  2122. buttonMap.SecondaryThumbstickDown = RawButton.None;
  2123. buttonMap.SecondaryThumbstickLeft = RawButton.None;
  2124. buttonMap.SecondaryThumbstickRight = RawButton.None;
  2125. buttonMap.SecondaryTouchpad = RawButton.None;
  2126. buttonMap.DpadUp = RawButton.None;
  2127. buttonMap.DpadDown = RawButton.None;
  2128. buttonMap.DpadLeft = RawButton.None;
  2129. buttonMap.DpadRight = RawButton.None;
  2130. buttonMap.Up = RawButton.RThumbstickUp;
  2131. buttonMap.Down = RawButton.RThumbstickDown;
  2132. buttonMap.Left = RawButton.RThumbstickLeft;
  2133. buttonMap.Right = RawButton.RThumbstickRight;
  2134. }
  2135. public override void ConfigureTouchMap()
  2136. {
  2137. touchMap.None = RawTouch.None;
  2138. touchMap.One = RawTouch.A;
  2139. touchMap.Two = RawTouch.B;
  2140. touchMap.Three = RawTouch.None;
  2141. touchMap.Four = RawTouch.None;
  2142. touchMap.PrimaryIndexTrigger = RawTouch.RIndexTrigger;
  2143. touchMap.PrimaryThumbstick = RawTouch.RThumbstick;
  2144. touchMap.PrimaryThumbRest = RawTouch.RThumbRest;
  2145. touchMap.PrimaryTouchpad = RawTouch.None;
  2146. touchMap.SecondaryIndexTrigger = RawTouch.None;
  2147. touchMap.SecondaryThumbstick = RawTouch.None;
  2148. touchMap.SecondaryThumbRest = RawTouch.None;
  2149. touchMap.SecondaryTouchpad = RawTouch.None;
  2150. }
  2151. public override void ConfigureNearTouchMap()
  2152. {
  2153. nearTouchMap.None = RawNearTouch.None;
  2154. nearTouchMap.PrimaryIndexTrigger = RawNearTouch.RIndexTrigger;
  2155. nearTouchMap.PrimaryThumbButtons = RawNearTouch.RThumbButtons;
  2156. nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None;
  2157. nearTouchMap.SecondaryThumbButtons = RawNearTouch.None;
  2158. }
  2159. public override void ConfigureAxis1DMap()
  2160. {
  2161. axis1DMap.None = RawAxis1D.None;
  2162. axis1DMap.PrimaryIndexTrigger = RawAxis1D.RIndexTrigger;
  2163. axis1DMap.PrimaryHandTrigger = RawAxis1D.RHandTrigger;
  2164. axis1DMap.SecondaryIndexTrigger = RawAxis1D.None;
  2165. axis1DMap.SecondaryHandTrigger = RawAxis1D.None;
  2166. }
  2167. public override void ConfigureAxis2DMap()
  2168. {
  2169. axis2DMap.None = RawAxis2D.None;
  2170. axis2DMap.PrimaryThumbstick = RawAxis2D.RThumbstick;
  2171. axis2DMap.PrimaryTouchpad = RawAxis2D.None;
  2172. axis2DMap.SecondaryThumbstick = RawAxis2D.None;
  2173. axis2DMap.SecondaryTouchpad = RawAxis2D.None;
  2174. }
  2175. public override bool WasRecentered()
  2176. {
  2177. return (currentState.RRecenterCount != previousState.RRecenterCount);
  2178. }
  2179. public override byte GetRecenterCount()
  2180. {
  2181. return currentState.RRecenterCount;
  2182. }
  2183. public override byte GetBatteryPercentRemaining()
  2184. {
  2185. return currentState.RBatteryPercentRemaining;
  2186. }
  2187. }
  2188. private class OVRControllerHands : OVRControllerBase
  2189. {
  2190. public OVRControllerHands()
  2191. {
  2192. controllerType = Controller.Hands;
  2193. }
  2194. public override void ConfigureButtonMap()
  2195. {
  2196. buttonMap.None = RawButton.None;
  2197. buttonMap.One = RawButton.A;
  2198. buttonMap.Two = RawButton.None;
  2199. buttonMap.Three = RawButton.X;
  2200. buttonMap.Four = RawButton.None;
  2201. buttonMap.Start = RawButton.None;
  2202. buttonMap.Back = RawButton.None;
  2203. buttonMap.PrimaryShoulder = RawButton.None;
  2204. buttonMap.PrimaryIndexTrigger = RawButton.None;
  2205. buttonMap.PrimaryHandTrigger = RawButton.None;
  2206. buttonMap.PrimaryThumbstick = RawButton.None;
  2207. buttonMap.PrimaryThumbstickUp = RawButton.None;
  2208. buttonMap.PrimaryThumbstickDown = RawButton.None;
  2209. buttonMap.PrimaryThumbstickLeft = RawButton.None;
  2210. buttonMap.PrimaryThumbstickRight = RawButton.None;
  2211. buttonMap.PrimaryTouchpad = RawButton.None;
  2212. buttonMap.SecondaryShoulder = RawButton.None;
  2213. buttonMap.SecondaryIndexTrigger = RawButton.None;
  2214. buttonMap.SecondaryHandTrigger = RawButton.None;
  2215. buttonMap.SecondaryThumbstick = RawButton.None;
  2216. buttonMap.SecondaryThumbstickUp = RawButton.None;
  2217. buttonMap.SecondaryThumbstickDown = RawButton.None;
  2218. buttonMap.SecondaryThumbstickLeft = RawButton.None;
  2219. buttonMap.SecondaryThumbstickRight = RawButton.None;
  2220. buttonMap.SecondaryTouchpad = RawButton.None;
  2221. buttonMap.DpadUp = RawButton.None;
  2222. buttonMap.DpadDown = RawButton.None;
  2223. buttonMap.DpadLeft = RawButton.None;
  2224. buttonMap.DpadRight = RawButton.None;
  2225. buttonMap.Up = RawButton.None;
  2226. buttonMap.Down = RawButton.None;
  2227. buttonMap.Left = RawButton.None;
  2228. buttonMap.Right = RawButton.None;
  2229. }
  2230. public override void ConfigureTouchMap()
  2231. {
  2232. touchMap.None = RawTouch.None;
  2233. touchMap.One = RawTouch.None;
  2234. touchMap.Two = RawTouch.None;
  2235. touchMap.Three = RawTouch.None;
  2236. touchMap.Four = RawTouch.None;
  2237. touchMap.PrimaryIndexTrigger = RawTouch.None;
  2238. touchMap.PrimaryThumbstick = RawTouch.None;
  2239. touchMap.PrimaryThumbRest = RawTouch.None;
  2240. touchMap.PrimaryTouchpad = RawTouch.None;
  2241. touchMap.SecondaryIndexTrigger = RawTouch.None;
  2242. touchMap.SecondaryThumbstick = RawTouch.None;
  2243. touchMap.SecondaryThumbRest = RawTouch.None;
  2244. touchMap.SecondaryTouchpad = RawTouch.None;
  2245. }
  2246. public override void ConfigureNearTouchMap()
  2247. {
  2248. nearTouchMap.None = RawNearTouch.None;
  2249. nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None;
  2250. nearTouchMap.PrimaryThumbButtons = RawNearTouch.None;
  2251. nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None;
  2252. nearTouchMap.SecondaryThumbButtons = RawNearTouch.None;
  2253. }
  2254. public override void ConfigureAxis1DMap()
  2255. {
  2256. axis1DMap.None = RawAxis1D.None;
  2257. axis1DMap.PrimaryIndexTrigger = RawAxis1D.None;
  2258. axis1DMap.PrimaryHandTrigger = RawAxis1D.None;
  2259. axis1DMap.SecondaryIndexTrigger = RawAxis1D.None;
  2260. axis1DMap.SecondaryHandTrigger = RawAxis1D.None;
  2261. }
  2262. public override void ConfigureAxis2DMap()
  2263. {
  2264. axis2DMap.None = RawAxis2D.None;
  2265. axis2DMap.PrimaryThumbstick = RawAxis2D.None;
  2266. axis2DMap.PrimaryTouchpad = RawAxis2D.None;
  2267. axis2DMap.SecondaryThumbstick = RawAxis2D.None;
  2268. axis2DMap.SecondaryTouchpad = RawAxis2D.None;
  2269. }
  2270. public override bool WasRecentered()
  2271. {
  2272. return ((currentState.LRecenterCount + currentState.RRecenterCount) != (previousState.LRecenterCount + previousState.RRecenterCount));
  2273. }
  2274. public override byte GetRecenterCount()
  2275. {
  2276. return (byte)(currentState.LRecenterCount + currentState.RRecenterCount);
  2277. }
  2278. public override byte GetBatteryPercentRemaining()
  2279. {
  2280. byte leftBattery = currentState.LBatteryPercentRemaining;
  2281. byte rightBattery = currentState.RBatteryPercentRemaining;
  2282. byte minBattery = (leftBattery <= rightBattery) ? leftBattery : rightBattery;
  2283. return minBattery;
  2284. }
  2285. }
  2286. private class OVRControllerLHand : OVRControllerBase
  2287. {
  2288. public OVRControllerLHand()
  2289. {
  2290. controllerType = Controller.LHand;
  2291. }
  2292. public override void ConfigureButtonMap()
  2293. {
  2294. buttonMap.None = RawButton.None;
  2295. buttonMap.One = RawButton.X;
  2296. buttonMap.Two = RawButton.None;
  2297. buttonMap.Three = RawButton.None;
  2298. buttonMap.Four = RawButton.None;
  2299. buttonMap.Start = RawButton.None;
  2300. buttonMap.Back = RawButton.None;
  2301. buttonMap.PrimaryShoulder = RawButton.None;
  2302. buttonMap.PrimaryIndexTrigger = RawButton.None;
  2303. buttonMap.PrimaryHandTrigger = RawButton.None;
  2304. buttonMap.PrimaryThumbstick = RawButton.None;
  2305. buttonMap.PrimaryThumbstickUp = RawButton.None;
  2306. buttonMap.PrimaryThumbstickDown = RawButton.None;
  2307. buttonMap.PrimaryThumbstickLeft = RawButton.None;
  2308. buttonMap.PrimaryThumbstickRight = RawButton.None;
  2309. buttonMap.PrimaryTouchpad = RawButton.None;
  2310. buttonMap.SecondaryShoulder = RawButton.None;
  2311. buttonMap.SecondaryIndexTrigger = RawButton.None;
  2312. buttonMap.SecondaryHandTrigger = RawButton.None;
  2313. buttonMap.SecondaryThumbstick = RawButton.None;
  2314. buttonMap.SecondaryThumbstickUp = RawButton.None;
  2315. buttonMap.SecondaryThumbstickDown = RawButton.None;
  2316. buttonMap.SecondaryThumbstickLeft = RawButton.None;
  2317. buttonMap.SecondaryThumbstickRight = RawButton.None;
  2318. buttonMap.SecondaryTouchpad = RawButton.None;
  2319. buttonMap.DpadUp = RawButton.None;
  2320. buttonMap.DpadDown = RawButton.None;
  2321. buttonMap.DpadLeft = RawButton.None;
  2322. buttonMap.DpadRight = RawButton.None;
  2323. buttonMap.Up = RawButton.None;
  2324. buttonMap.Down = RawButton.None;
  2325. buttonMap.Left = RawButton.None;
  2326. buttonMap.Right = RawButton.None;
  2327. }
  2328. public override void ConfigureTouchMap()
  2329. {
  2330. touchMap.None = RawTouch.None;
  2331. touchMap.One = RawTouch.None;
  2332. touchMap.Two = RawTouch.None;
  2333. touchMap.Three = RawTouch.None;
  2334. touchMap.Four = RawTouch.None;
  2335. touchMap.PrimaryIndexTrigger = RawTouch.None;
  2336. touchMap.PrimaryThumbstick = RawTouch.None;
  2337. touchMap.PrimaryThumbRest = RawTouch.None;
  2338. touchMap.PrimaryTouchpad = RawTouch.None;
  2339. touchMap.SecondaryIndexTrigger = RawTouch.None;
  2340. touchMap.SecondaryThumbstick = RawTouch.None;
  2341. touchMap.SecondaryThumbRest = RawTouch.None;
  2342. touchMap.SecondaryTouchpad = RawTouch.None;
  2343. }
  2344. public override void ConfigureNearTouchMap()
  2345. {
  2346. nearTouchMap.None = RawNearTouch.None;
  2347. nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None;
  2348. nearTouchMap.PrimaryThumbButtons = RawNearTouch.None;
  2349. nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None;
  2350. nearTouchMap.SecondaryThumbButtons = RawNearTouch.None;
  2351. }
  2352. public override void ConfigureAxis1DMap()
  2353. {
  2354. axis1DMap.None = RawAxis1D.None;
  2355. axis1DMap.PrimaryIndexTrigger = RawAxis1D.None;
  2356. axis1DMap.PrimaryHandTrigger = RawAxis1D.None;
  2357. axis1DMap.SecondaryIndexTrigger = RawAxis1D.None;
  2358. axis1DMap.SecondaryHandTrigger = RawAxis1D.None;
  2359. }
  2360. public override void ConfigureAxis2DMap()
  2361. {
  2362. axis2DMap.None = RawAxis2D.None;
  2363. axis2DMap.PrimaryThumbstick = RawAxis2D.None;
  2364. axis2DMap.PrimaryTouchpad = RawAxis2D.None;
  2365. axis2DMap.SecondaryThumbstick = RawAxis2D.None;
  2366. axis2DMap.SecondaryTouchpad = RawAxis2D.None;
  2367. }
  2368. public override bool WasRecentered()
  2369. {
  2370. return (currentState.LRecenterCount != previousState.LRecenterCount);
  2371. }
  2372. public override byte GetRecenterCount()
  2373. {
  2374. return currentState.LRecenterCount;
  2375. }
  2376. public override byte GetBatteryPercentRemaining()
  2377. {
  2378. return currentState.LBatteryPercentRemaining;
  2379. }
  2380. }
  2381. private class OVRControllerRHand : OVRControllerBase
  2382. {
  2383. public OVRControllerRHand()
  2384. {
  2385. controllerType = Controller.RHand;
  2386. }
  2387. public override void ConfigureButtonMap()
  2388. {
  2389. buttonMap.None = RawButton.None;
  2390. buttonMap.One = RawButton.A;
  2391. buttonMap.Two = RawButton.None;
  2392. buttonMap.Three = RawButton.None;
  2393. buttonMap.Four = RawButton.None;
  2394. buttonMap.Start = RawButton.None;
  2395. buttonMap.Back = RawButton.None;
  2396. buttonMap.PrimaryShoulder = RawButton.None;
  2397. buttonMap.PrimaryIndexTrigger = RawButton.None;
  2398. buttonMap.PrimaryHandTrigger = RawButton.None;
  2399. buttonMap.PrimaryThumbstick = RawButton.None;
  2400. buttonMap.PrimaryThumbstickUp = RawButton.None;
  2401. buttonMap.PrimaryThumbstickDown = RawButton.None;
  2402. buttonMap.PrimaryThumbstickLeft = RawButton.None;
  2403. buttonMap.PrimaryThumbstickRight = RawButton.None;
  2404. buttonMap.PrimaryTouchpad = RawButton.None;
  2405. buttonMap.SecondaryShoulder = RawButton.None;
  2406. buttonMap.SecondaryIndexTrigger = RawButton.None;
  2407. buttonMap.SecondaryHandTrigger = RawButton.None;
  2408. buttonMap.SecondaryThumbstick = RawButton.None;
  2409. buttonMap.SecondaryThumbstickUp = RawButton.None;
  2410. buttonMap.SecondaryThumbstickDown = RawButton.None;
  2411. buttonMap.SecondaryThumbstickLeft = RawButton.None;
  2412. buttonMap.SecondaryThumbstickRight = RawButton.None;
  2413. buttonMap.SecondaryTouchpad = RawButton.None;
  2414. buttonMap.DpadUp = RawButton.None;
  2415. buttonMap.DpadDown = RawButton.None;
  2416. buttonMap.DpadLeft = RawButton.None;
  2417. buttonMap.DpadRight = RawButton.None;
  2418. buttonMap.Up = RawButton.None;
  2419. buttonMap.Down = RawButton.None;
  2420. buttonMap.Left = RawButton.None;
  2421. buttonMap.Right = RawButton.None;
  2422. }
  2423. public override void ConfigureTouchMap()
  2424. {
  2425. touchMap.None = RawTouch.None;
  2426. touchMap.One = RawTouch.None;
  2427. touchMap.Two = RawTouch.None;
  2428. touchMap.Three = RawTouch.None;
  2429. touchMap.Four = RawTouch.None;
  2430. touchMap.PrimaryIndexTrigger = RawTouch.None;
  2431. touchMap.PrimaryThumbstick = RawTouch.None;
  2432. touchMap.PrimaryThumbRest = RawTouch.None;
  2433. touchMap.PrimaryTouchpad = RawTouch.None;
  2434. touchMap.SecondaryIndexTrigger = RawTouch.None;
  2435. touchMap.SecondaryThumbstick = RawTouch.None;
  2436. touchMap.SecondaryThumbRest = RawTouch.None;
  2437. touchMap.SecondaryTouchpad = RawTouch.None;
  2438. }
  2439. public override void ConfigureNearTouchMap()
  2440. {
  2441. nearTouchMap.None = RawNearTouch.None;
  2442. nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None;
  2443. nearTouchMap.PrimaryThumbButtons = RawNearTouch.None;
  2444. nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None;
  2445. nearTouchMap.SecondaryThumbButtons = RawNearTouch.None;
  2446. }
  2447. public override void ConfigureAxis1DMap()
  2448. {
  2449. axis1DMap.None = RawAxis1D.None;
  2450. axis1DMap.PrimaryIndexTrigger = RawAxis1D.None;
  2451. axis1DMap.PrimaryHandTrigger = RawAxis1D.None;
  2452. axis1DMap.SecondaryIndexTrigger = RawAxis1D.None;
  2453. axis1DMap.SecondaryHandTrigger = RawAxis1D.None;
  2454. }
  2455. public override void ConfigureAxis2DMap()
  2456. {
  2457. axis2DMap.None = RawAxis2D.None;
  2458. axis2DMap.PrimaryThumbstick = RawAxis2D.None;
  2459. axis2DMap.PrimaryTouchpad = RawAxis2D.None;
  2460. axis2DMap.SecondaryThumbstick = RawAxis2D.None;
  2461. axis2DMap.SecondaryTouchpad = RawAxis2D.None;
  2462. }
  2463. public override bool WasRecentered()
  2464. {
  2465. return (currentState.RRecenterCount != previousState.RRecenterCount);
  2466. }
  2467. public override byte GetRecenterCount()
  2468. {
  2469. return currentState.RRecenterCount;
  2470. }
  2471. public override byte GetBatteryPercentRemaining()
  2472. {
  2473. return currentState.RBatteryPercentRemaining;
  2474. }
  2475. }
  2476. private class OVRControllerRemote : OVRControllerBase
  2477. {
  2478. public OVRControllerRemote()
  2479. {
  2480. controllerType = Controller.Remote;
  2481. }
  2482. public override void ConfigureButtonMap()
  2483. {
  2484. buttonMap.None = RawButton.None;
  2485. buttonMap.One = RawButton.Start;
  2486. buttonMap.Two = RawButton.Back;
  2487. buttonMap.Three = RawButton.None;
  2488. buttonMap.Four = RawButton.None;
  2489. buttonMap.Start = RawButton.Start;
  2490. buttonMap.Back = RawButton.Back;
  2491. buttonMap.PrimaryShoulder = RawButton.None;
  2492. buttonMap.PrimaryIndexTrigger = RawButton.None;
  2493. buttonMap.PrimaryHandTrigger = RawButton.None;
  2494. buttonMap.PrimaryThumbstick = RawButton.None;
  2495. buttonMap.PrimaryThumbstickUp = RawButton.None;
  2496. buttonMap.PrimaryThumbstickDown = RawButton.None;
  2497. buttonMap.PrimaryThumbstickLeft = RawButton.None;
  2498. buttonMap.PrimaryThumbstickRight = RawButton.None;
  2499. buttonMap.PrimaryTouchpad = RawButton.None;
  2500. buttonMap.SecondaryShoulder = RawButton.None;
  2501. buttonMap.SecondaryIndexTrigger = RawButton.None;
  2502. buttonMap.SecondaryHandTrigger = RawButton.None;
  2503. buttonMap.SecondaryThumbstick = RawButton.None;
  2504. buttonMap.SecondaryThumbstickUp = RawButton.None;
  2505. buttonMap.SecondaryThumbstickDown = RawButton.None;
  2506. buttonMap.SecondaryThumbstickLeft = RawButton.None;
  2507. buttonMap.SecondaryThumbstickRight = RawButton.None;
  2508. buttonMap.SecondaryTouchpad = RawButton.None;
  2509. buttonMap.DpadUp = RawButton.DpadUp;
  2510. buttonMap.DpadDown = RawButton.DpadDown;
  2511. buttonMap.DpadLeft = RawButton.DpadLeft;
  2512. buttonMap.DpadRight = RawButton.DpadRight;
  2513. buttonMap.Up = RawButton.DpadUp;
  2514. buttonMap.Down = RawButton.DpadDown;
  2515. buttonMap.Left = RawButton.DpadLeft;
  2516. buttonMap.Right = RawButton.DpadRight;
  2517. }
  2518. public override void ConfigureTouchMap()
  2519. {
  2520. touchMap.None = RawTouch.None;
  2521. touchMap.One = RawTouch.None;
  2522. touchMap.Two = RawTouch.None;
  2523. touchMap.Three = RawTouch.None;
  2524. touchMap.Four = RawTouch.None;
  2525. touchMap.PrimaryIndexTrigger = RawTouch.None;
  2526. touchMap.PrimaryThumbstick = RawTouch.None;
  2527. touchMap.PrimaryThumbRest = RawTouch.None;
  2528. touchMap.PrimaryTouchpad = RawTouch.None;
  2529. touchMap.SecondaryIndexTrigger = RawTouch.None;
  2530. touchMap.SecondaryThumbstick = RawTouch.None;
  2531. touchMap.SecondaryThumbRest = RawTouch.None;
  2532. touchMap.SecondaryTouchpad = RawTouch.None;
  2533. }
  2534. public override void ConfigureNearTouchMap()
  2535. {
  2536. nearTouchMap.None = RawNearTouch.None;
  2537. nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None;
  2538. nearTouchMap.PrimaryThumbButtons = RawNearTouch.None;
  2539. nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None;
  2540. nearTouchMap.SecondaryThumbButtons = RawNearTouch.None;
  2541. }
  2542. public override void ConfigureAxis1DMap()
  2543. {
  2544. axis1DMap.None = RawAxis1D.None;
  2545. axis1DMap.PrimaryIndexTrigger = RawAxis1D.None;
  2546. axis1DMap.PrimaryHandTrigger = RawAxis1D.None;
  2547. axis1DMap.SecondaryIndexTrigger = RawAxis1D.None;
  2548. axis1DMap.SecondaryHandTrigger = RawAxis1D.None;
  2549. }
  2550. public override void ConfigureAxis2DMap()
  2551. {
  2552. axis2DMap.None = RawAxis2D.None;
  2553. axis2DMap.PrimaryThumbstick = RawAxis2D.None;
  2554. axis2DMap.PrimaryTouchpad = RawAxis2D.None;
  2555. axis2DMap.SecondaryThumbstick = RawAxis2D.None;
  2556. axis2DMap.SecondaryTouchpad = RawAxis2D.None;
  2557. }
  2558. }
  2559. private class OVRControllerGamepadPC : OVRControllerBase
  2560. {
  2561. public OVRControllerGamepadPC()
  2562. {
  2563. controllerType = Controller.Gamepad;
  2564. }
  2565. public override void ConfigureButtonMap()
  2566. {
  2567. buttonMap.None = RawButton.None;
  2568. buttonMap.One = RawButton.A;
  2569. buttonMap.Two = RawButton.B;
  2570. buttonMap.Three = RawButton.X;
  2571. buttonMap.Four = RawButton.Y;
  2572. buttonMap.Start = RawButton.Start;
  2573. buttonMap.Back = RawButton.Back;
  2574. buttonMap.PrimaryShoulder = RawButton.LShoulder;
  2575. buttonMap.PrimaryIndexTrigger = RawButton.LIndexTrigger;
  2576. buttonMap.PrimaryHandTrigger = RawButton.None;
  2577. buttonMap.PrimaryThumbstick = RawButton.LThumbstick;
  2578. buttonMap.PrimaryThumbstickUp = RawButton.LThumbstickUp;
  2579. buttonMap.PrimaryThumbstickDown = RawButton.LThumbstickDown;
  2580. buttonMap.PrimaryThumbstickLeft = RawButton.LThumbstickLeft;
  2581. buttonMap.PrimaryThumbstickRight = RawButton.LThumbstickRight;
  2582. buttonMap.PrimaryTouchpad = RawButton.None;
  2583. buttonMap.SecondaryShoulder = RawButton.RShoulder;
  2584. buttonMap.SecondaryIndexTrigger = RawButton.RIndexTrigger;
  2585. buttonMap.SecondaryHandTrigger = RawButton.None;
  2586. buttonMap.SecondaryThumbstick = RawButton.RThumbstick;
  2587. buttonMap.SecondaryThumbstickUp = RawButton.RThumbstickUp;
  2588. buttonMap.SecondaryThumbstickDown = RawButton.RThumbstickDown;
  2589. buttonMap.SecondaryThumbstickLeft = RawButton.RThumbstickLeft;
  2590. buttonMap.SecondaryThumbstickRight = RawButton.RThumbstickRight;
  2591. buttonMap.SecondaryTouchpad = RawButton.None;
  2592. buttonMap.DpadUp = RawButton.DpadUp;
  2593. buttonMap.DpadDown = RawButton.DpadDown;
  2594. buttonMap.DpadLeft = RawButton.DpadLeft;
  2595. buttonMap.DpadRight = RawButton.DpadRight;
  2596. buttonMap.Up = RawButton.LThumbstickUp;
  2597. buttonMap.Down = RawButton.LThumbstickDown;
  2598. buttonMap.Left = RawButton.LThumbstickLeft;
  2599. buttonMap.Right = RawButton.LThumbstickRight;
  2600. }
  2601. public override void ConfigureTouchMap()
  2602. {
  2603. touchMap.None = RawTouch.None;
  2604. touchMap.One = RawTouch.None;
  2605. touchMap.Two = RawTouch.None;
  2606. touchMap.Three = RawTouch.None;
  2607. touchMap.Four = RawTouch.None;
  2608. touchMap.PrimaryIndexTrigger = RawTouch.None;
  2609. touchMap.PrimaryThumbstick = RawTouch.None;
  2610. touchMap.PrimaryThumbRest = RawTouch.None;
  2611. touchMap.PrimaryTouchpad = RawTouch.None;
  2612. touchMap.SecondaryIndexTrigger = RawTouch.None;
  2613. touchMap.SecondaryThumbstick = RawTouch.None;
  2614. touchMap.SecondaryThumbRest = RawTouch.None;
  2615. touchMap.SecondaryTouchpad = RawTouch.None;
  2616. }
  2617. public override void ConfigureNearTouchMap()
  2618. {
  2619. nearTouchMap.None = RawNearTouch.None;
  2620. nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None;
  2621. nearTouchMap.PrimaryThumbButtons = RawNearTouch.None;
  2622. nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None;
  2623. nearTouchMap.SecondaryThumbButtons = RawNearTouch.None;
  2624. }
  2625. public override void ConfigureAxis1DMap()
  2626. {
  2627. axis1DMap.None = RawAxis1D.None;
  2628. axis1DMap.PrimaryIndexTrigger = RawAxis1D.LIndexTrigger;
  2629. axis1DMap.PrimaryHandTrigger = RawAxis1D.None;
  2630. axis1DMap.SecondaryIndexTrigger = RawAxis1D.RIndexTrigger;
  2631. axis1DMap.SecondaryHandTrigger = RawAxis1D.None;
  2632. }
  2633. public override void ConfigureAxis2DMap()
  2634. {
  2635. axis2DMap.None = RawAxis2D.None;
  2636. axis2DMap.PrimaryThumbstick = RawAxis2D.LThumbstick;
  2637. axis2DMap.PrimaryTouchpad = RawAxis2D.None;
  2638. axis2DMap.SecondaryThumbstick = RawAxis2D.RThumbstick;
  2639. axis2DMap.SecondaryTouchpad = RawAxis2D.None;
  2640. }
  2641. }
  2642. private class OVRControllerGamepadMac : OVRControllerBase
  2643. {
  2644. /// <summary> An axis on the gamepad. </summary>
  2645. private enum AxisGPC
  2646. {
  2647. None = -1,
  2648. LeftXAxis = 0,
  2649. LeftYAxis,
  2650. RightXAxis,
  2651. RightYAxis,
  2652. LeftTrigger,
  2653. RightTrigger,
  2654. DPad_X_Axis,
  2655. DPad_Y_Axis,
  2656. Max,
  2657. };
  2658. /// <summary> A button on the gamepad. </summary>
  2659. public enum ButtonGPC
  2660. {
  2661. None = -1,
  2662. A = 0,
  2663. B,
  2664. X,
  2665. Y,
  2666. Up,
  2667. Down,
  2668. Left,
  2669. Right,
  2670. Start,
  2671. Back,
  2672. LStick,
  2673. RStick,
  2674. LeftShoulder,
  2675. RightShoulder,
  2676. Max
  2677. };
  2678. private bool initialized = false;
  2679. public OVRControllerGamepadMac()
  2680. {
  2681. controllerType = Controller.Gamepad;
  2682. initialized = OVR_GamepadController_Initialize();
  2683. }
  2684. ~OVRControllerGamepadMac()
  2685. {
  2686. if (!initialized)
  2687. return;
  2688. OVR_GamepadController_Destroy();
  2689. }
  2690. public override Controller Update()
  2691. {
  2692. if (!initialized)
  2693. {
  2694. return Controller.None;
  2695. }
  2696. OVRPlugin.ControllerState4 state = new OVRPlugin.ControllerState4();
  2697. bool result = OVR_GamepadController_Update();
  2698. if (result)
  2699. state.ConnectedControllers = (uint)Controller.Gamepad;
  2700. if (OVR_GamepadController_GetButton((int)ButtonGPC.A))
  2701. state.Buttons |= (uint)RawButton.A;
  2702. if (OVR_GamepadController_GetButton((int)ButtonGPC.B))
  2703. state.Buttons |= (uint)RawButton.B;
  2704. if (OVR_GamepadController_GetButton((int)ButtonGPC.X))
  2705. state.Buttons |= (uint)RawButton.X;
  2706. if (OVR_GamepadController_GetButton((int)ButtonGPC.Y))
  2707. state.Buttons |= (uint)RawButton.Y;
  2708. if (OVR_GamepadController_GetButton((int)ButtonGPC.Up))
  2709. state.Buttons |= (uint)RawButton.DpadUp;
  2710. if (OVR_GamepadController_GetButton((int)ButtonGPC.Down))
  2711. state.Buttons |= (uint)RawButton.DpadDown;
  2712. if (OVR_GamepadController_GetButton((int)ButtonGPC.Left))
  2713. state.Buttons |= (uint)RawButton.DpadLeft;
  2714. if (OVR_GamepadController_GetButton((int)ButtonGPC.Right))
  2715. state.Buttons |= (uint)RawButton.DpadRight;
  2716. if (OVR_GamepadController_GetButton((int)ButtonGPC.Start))
  2717. state.Buttons |= (uint)RawButton.Start;
  2718. if (OVR_GamepadController_GetButton((int)ButtonGPC.Back))
  2719. state.Buttons |= (uint)RawButton.Back;
  2720. if (OVR_GamepadController_GetButton((int)ButtonGPC.LStick))
  2721. state.Buttons |= (uint)RawButton.LThumbstick;
  2722. if (OVR_GamepadController_GetButton((int)ButtonGPC.RStick))
  2723. state.Buttons |= (uint)RawButton.RThumbstick;
  2724. if (OVR_GamepadController_GetButton((int)ButtonGPC.LeftShoulder))
  2725. state.Buttons |= (uint)RawButton.LShoulder;
  2726. if (OVR_GamepadController_GetButton((int)ButtonGPC.RightShoulder))
  2727. state.Buttons |= (uint)RawButton.RShoulder;
  2728. state.LThumbstick.x = OVR_GamepadController_GetAxis((int)AxisGPC.LeftXAxis);
  2729. state.LThumbstick.y = OVR_GamepadController_GetAxis((int)AxisGPC.LeftYAxis);
  2730. state.RThumbstick.x = OVR_GamepadController_GetAxis((int)AxisGPC.RightXAxis);
  2731. state.RThumbstick.y = OVR_GamepadController_GetAxis((int)AxisGPC.RightYAxis);
  2732. state.LIndexTrigger = OVR_GamepadController_GetAxis((int)AxisGPC.LeftTrigger);
  2733. state.RIndexTrigger = OVR_GamepadController_GetAxis((int)AxisGPC.RightTrigger);
  2734. if (state.LIndexTrigger >= AXIS_AS_BUTTON_THRESHOLD)
  2735. state.Buttons |= (uint)RawButton.LIndexTrigger;
  2736. if (state.LHandTrigger >= AXIS_AS_BUTTON_THRESHOLD)
  2737. state.Buttons |= (uint)RawButton.LHandTrigger;
  2738. if (state.LThumbstick.y >= AXIS_AS_BUTTON_THRESHOLD)
  2739. state.Buttons |= (uint)RawButton.LThumbstickUp;
  2740. if (state.LThumbstick.y <= -AXIS_AS_BUTTON_THRESHOLD)
  2741. state.Buttons |= (uint)RawButton.LThumbstickDown;
  2742. if (state.LThumbstick.x <= -AXIS_AS_BUTTON_THRESHOLD)
  2743. state.Buttons |= (uint)RawButton.LThumbstickLeft;
  2744. if (state.LThumbstick.x >= AXIS_AS_BUTTON_THRESHOLD)
  2745. state.Buttons |= (uint)RawButton.LThumbstickRight;
  2746. if (state.RIndexTrigger >= AXIS_AS_BUTTON_THRESHOLD)
  2747. state.Buttons |= (uint)RawButton.RIndexTrigger;
  2748. if (state.RHandTrigger >= AXIS_AS_BUTTON_THRESHOLD)
  2749. state.Buttons |= (uint)RawButton.RHandTrigger;
  2750. if (state.RThumbstick.y >= AXIS_AS_BUTTON_THRESHOLD)
  2751. state.Buttons |= (uint)RawButton.RThumbstickUp;
  2752. if (state.RThumbstick.y <= -AXIS_AS_BUTTON_THRESHOLD)
  2753. state.Buttons |= (uint)RawButton.RThumbstickDown;
  2754. if (state.RThumbstick.x <= -AXIS_AS_BUTTON_THRESHOLD)
  2755. state.Buttons |= (uint)RawButton.RThumbstickLeft;
  2756. if (state.RThumbstick.x >= AXIS_AS_BUTTON_THRESHOLD)
  2757. state.Buttons |= (uint)RawButton.RThumbstickRight;
  2758. previousState = currentState;
  2759. currentState = state;
  2760. return ((Controller)currentState.ConnectedControllers & controllerType);
  2761. }
  2762. public override void ConfigureButtonMap()
  2763. {
  2764. buttonMap.None = RawButton.None;
  2765. buttonMap.One = RawButton.A;
  2766. buttonMap.Two = RawButton.B;
  2767. buttonMap.Three = RawButton.X;
  2768. buttonMap.Four = RawButton.Y;
  2769. buttonMap.Start = RawButton.Start;
  2770. buttonMap.Back = RawButton.Back;
  2771. buttonMap.PrimaryShoulder = RawButton.LShoulder;
  2772. buttonMap.PrimaryIndexTrigger = RawButton.LIndexTrigger;
  2773. buttonMap.PrimaryHandTrigger = RawButton.None;
  2774. buttonMap.PrimaryThumbstick = RawButton.LThumbstick;
  2775. buttonMap.PrimaryThumbstickUp = RawButton.LThumbstickUp;
  2776. buttonMap.PrimaryThumbstickDown = RawButton.LThumbstickDown;
  2777. buttonMap.PrimaryThumbstickLeft = RawButton.LThumbstickLeft;
  2778. buttonMap.PrimaryThumbstickRight = RawButton.LThumbstickRight;
  2779. buttonMap.PrimaryTouchpad = RawButton.None;
  2780. buttonMap.SecondaryShoulder = RawButton.RShoulder;
  2781. buttonMap.SecondaryIndexTrigger = RawButton.RIndexTrigger;
  2782. buttonMap.SecondaryHandTrigger = RawButton.None;
  2783. buttonMap.SecondaryThumbstick = RawButton.RThumbstick;
  2784. buttonMap.SecondaryThumbstickUp = RawButton.RThumbstickUp;
  2785. buttonMap.SecondaryThumbstickDown = RawButton.RThumbstickDown;
  2786. buttonMap.SecondaryThumbstickLeft = RawButton.RThumbstickLeft;
  2787. buttonMap.SecondaryThumbstickRight = RawButton.RThumbstickRight;
  2788. buttonMap.SecondaryTouchpad = RawButton.None;
  2789. buttonMap.DpadUp = RawButton.DpadUp;
  2790. buttonMap.DpadDown = RawButton.DpadDown;
  2791. buttonMap.DpadLeft = RawButton.DpadLeft;
  2792. buttonMap.DpadRight = RawButton.DpadRight;
  2793. buttonMap.Up = RawButton.LThumbstickUp;
  2794. buttonMap.Down = RawButton.LThumbstickDown;
  2795. buttonMap.Left = RawButton.LThumbstickLeft;
  2796. buttonMap.Right = RawButton.LThumbstickRight;
  2797. }
  2798. public override void ConfigureTouchMap()
  2799. {
  2800. touchMap.None = RawTouch.None;
  2801. touchMap.One = RawTouch.None;
  2802. touchMap.Two = RawTouch.None;
  2803. touchMap.Three = RawTouch.None;
  2804. touchMap.Four = RawTouch.None;
  2805. touchMap.PrimaryIndexTrigger = RawTouch.None;
  2806. touchMap.PrimaryThumbstick = RawTouch.None;
  2807. touchMap.PrimaryThumbRest = RawTouch.None;
  2808. touchMap.PrimaryTouchpad = RawTouch.None;
  2809. touchMap.SecondaryIndexTrigger = RawTouch.None;
  2810. touchMap.SecondaryThumbstick = RawTouch.None;
  2811. touchMap.SecondaryThumbRest = RawTouch.None;
  2812. touchMap.SecondaryTouchpad = RawTouch.None;
  2813. }
  2814. public override void ConfigureNearTouchMap()
  2815. {
  2816. nearTouchMap.None = RawNearTouch.None;
  2817. nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None;
  2818. nearTouchMap.PrimaryThumbButtons = RawNearTouch.None;
  2819. nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None;
  2820. nearTouchMap.SecondaryThumbButtons = RawNearTouch.None;
  2821. }
  2822. public override void ConfigureAxis1DMap()
  2823. {
  2824. axis1DMap.None = RawAxis1D.None;
  2825. axis1DMap.PrimaryIndexTrigger = RawAxis1D.LIndexTrigger;
  2826. axis1DMap.PrimaryHandTrigger = RawAxis1D.None;
  2827. axis1DMap.SecondaryIndexTrigger = RawAxis1D.RIndexTrigger;
  2828. axis1DMap.SecondaryHandTrigger = RawAxis1D.None;
  2829. }
  2830. public override void ConfigureAxis2DMap()
  2831. {
  2832. axis2DMap.None = RawAxis2D.None;
  2833. axis2DMap.PrimaryThumbstick = RawAxis2D.LThumbstick;
  2834. axis2DMap.PrimaryTouchpad = RawAxis2D.None;
  2835. axis2DMap.SecondaryThumbstick = RawAxis2D.RThumbstick;
  2836. axis2DMap.SecondaryTouchpad = RawAxis2D.None;
  2837. }
  2838. public override void SetControllerVibration(float frequency, float amplitude)
  2839. {
  2840. int gpcNode = 0;
  2841. float gpcFrequency = frequency * 200.0f; //Map frequency from 0-1 CAPI range to 0-200 GPC range
  2842. float gpcStrength = amplitude;
  2843. OVR_GamepadController_SetVibration(gpcNode, gpcStrength, gpcFrequency);
  2844. }
  2845. private const string DllName = "OVRGamepad";
  2846. [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  2847. private static extern bool OVR_GamepadController_Initialize();
  2848. [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  2849. private static extern bool OVR_GamepadController_Destroy();
  2850. [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  2851. private static extern bool OVR_GamepadController_Update();
  2852. [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  2853. private static extern float OVR_GamepadController_GetAxis(int axis);
  2854. [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  2855. private static extern bool OVR_GamepadController_GetButton(int button);
  2856. [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)]
  2857. private static extern bool OVR_GamepadController_SetVibration(int node, float strength, float frequency);
  2858. }
  2859. private class OVRControllerGamepadAndroid : OVRControllerBase
  2860. {
  2861. public OVRControllerGamepadAndroid()
  2862. {
  2863. controllerType = Controller.Gamepad;
  2864. }
  2865. public override void ConfigureButtonMap()
  2866. {
  2867. buttonMap.None = RawButton.None;
  2868. buttonMap.One = RawButton.A;
  2869. buttonMap.Two = RawButton.B;
  2870. buttonMap.Three = RawButton.X;
  2871. buttonMap.Four = RawButton.Y;
  2872. buttonMap.Start = RawButton.Start;
  2873. buttonMap.Back = RawButton.Back;
  2874. buttonMap.PrimaryShoulder = RawButton.LShoulder;
  2875. buttonMap.PrimaryIndexTrigger = RawButton.LIndexTrigger;
  2876. buttonMap.PrimaryHandTrigger = RawButton.None;
  2877. buttonMap.PrimaryThumbstick = RawButton.LThumbstick;
  2878. buttonMap.PrimaryThumbstickUp = RawButton.LThumbstickUp;
  2879. buttonMap.PrimaryThumbstickDown = RawButton.LThumbstickDown;
  2880. buttonMap.PrimaryThumbstickLeft = RawButton.LThumbstickLeft;
  2881. buttonMap.PrimaryThumbstickRight = RawButton.LThumbstickRight;
  2882. buttonMap.PrimaryTouchpad = RawButton.None;
  2883. buttonMap.SecondaryShoulder = RawButton.RShoulder;
  2884. buttonMap.SecondaryIndexTrigger = RawButton.RIndexTrigger;
  2885. buttonMap.SecondaryHandTrigger = RawButton.None;
  2886. buttonMap.SecondaryThumbstick = RawButton.RThumbstick;
  2887. buttonMap.SecondaryThumbstickUp = RawButton.RThumbstickUp;
  2888. buttonMap.SecondaryThumbstickDown = RawButton.RThumbstickDown;
  2889. buttonMap.SecondaryThumbstickLeft = RawButton.RThumbstickLeft;
  2890. buttonMap.SecondaryThumbstickRight = RawButton.RThumbstickRight;
  2891. buttonMap.SecondaryTouchpad = RawButton.None;
  2892. buttonMap.DpadUp = RawButton.DpadUp;
  2893. buttonMap.DpadDown = RawButton.DpadDown;
  2894. buttonMap.DpadLeft = RawButton.DpadLeft;
  2895. buttonMap.DpadRight = RawButton.DpadRight;
  2896. buttonMap.Up = RawButton.LThumbstickUp;
  2897. buttonMap.Down = RawButton.LThumbstickDown;
  2898. buttonMap.Left = RawButton.LThumbstickLeft;
  2899. buttonMap.Right = RawButton.LThumbstickRight;
  2900. }
  2901. public override void ConfigureTouchMap()
  2902. {
  2903. touchMap.None = RawTouch.None;
  2904. touchMap.One = RawTouch.None;
  2905. touchMap.Two = RawTouch.None;
  2906. touchMap.Three = RawTouch.None;
  2907. touchMap.Four = RawTouch.None;
  2908. touchMap.PrimaryIndexTrigger = RawTouch.None;
  2909. touchMap.PrimaryThumbstick = RawTouch.None;
  2910. touchMap.PrimaryThumbRest = RawTouch.None;
  2911. touchMap.PrimaryTouchpad = RawTouch.None;
  2912. touchMap.SecondaryIndexTrigger = RawTouch.None;
  2913. touchMap.SecondaryThumbstick = RawTouch.None;
  2914. touchMap.SecondaryThumbRest = RawTouch.None;
  2915. touchMap.SecondaryTouchpad = RawTouch.None;
  2916. }
  2917. public override void ConfigureNearTouchMap()
  2918. {
  2919. nearTouchMap.None = RawNearTouch.None;
  2920. nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None;
  2921. nearTouchMap.PrimaryThumbButtons = RawNearTouch.None;
  2922. nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None;
  2923. nearTouchMap.SecondaryThumbButtons = RawNearTouch.None;
  2924. }
  2925. public override void ConfigureAxis1DMap()
  2926. {
  2927. axis1DMap.None = RawAxis1D.None;
  2928. axis1DMap.PrimaryIndexTrigger = RawAxis1D.LIndexTrigger;
  2929. axis1DMap.PrimaryHandTrigger = RawAxis1D.None;
  2930. axis1DMap.SecondaryIndexTrigger = RawAxis1D.RIndexTrigger;
  2931. axis1DMap.SecondaryHandTrigger = RawAxis1D.None;
  2932. }
  2933. public override void ConfigureAxis2DMap()
  2934. {
  2935. axis2DMap.None = RawAxis2D.None;
  2936. axis2DMap.PrimaryThumbstick = RawAxis2D.LThumbstick;
  2937. axis2DMap.PrimaryTouchpad = RawAxis2D.None;
  2938. axis2DMap.SecondaryThumbstick = RawAxis2D.RThumbstick;
  2939. axis2DMap.SecondaryTouchpad = RawAxis2D.None;
  2940. }
  2941. }
  2942. private class OVRControllerTouchpad : OVRControllerBase
  2943. {
  2944. private OVRPlugin.Vector2f moveAmount;
  2945. private float maxTapMagnitude = 0.1f;
  2946. private float minMoveMagnitude = 0.15f;
  2947. public OVRControllerTouchpad()
  2948. {
  2949. controllerType = Controller.Touchpad;
  2950. }
  2951. public override Controller Update()
  2952. {
  2953. Controller res = base.Update();
  2954. if (GetDown(RawTouch.LTouchpad, OVRInput.Controller.Touchpad))
  2955. {
  2956. moveAmount = currentState.LTouchpad;
  2957. }
  2958. if (GetUp(RawTouch.LTouchpad, OVRInput.Controller.Touchpad))
  2959. {
  2960. moveAmount.x = previousState.LTouchpad.x - moveAmount.x;
  2961. moveAmount.y = previousState.LTouchpad.y - moveAmount.y;
  2962. Vector2 move = new Vector2(moveAmount.x, moveAmount.y);
  2963. float moveMag = move.magnitude;
  2964. if (moveMag < maxTapMagnitude)
  2965. {
  2966. // Emit Touchpad Tap
  2967. currentState.Buttons |= (uint)RawButton.Start;
  2968. currentState.Buttons |= (uint)RawButton.LTouchpad;
  2969. }
  2970. else if (moveMag >= minMoveMagnitude)
  2971. {
  2972. move.Normalize();
  2973. // Left/Right
  2974. if (Mathf.Abs(move.x) > Mathf.Abs(move.y))
  2975. {
  2976. if (move.x < 0.0f)
  2977. {
  2978. currentState.Buttons |= (uint)RawButton.DpadLeft;
  2979. }
  2980. else
  2981. {
  2982. currentState.Buttons |= (uint)RawButton.DpadRight;
  2983. }
  2984. }
  2985. // Up/Down
  2986. else
  2987. {
  2988. if (move.y < 0.0f)
  2989. {
  2990. currentState.Buttons |= (uint)RawButton.DpadDown;
  2991. }
  2992. else
  2993. {
  2994. currentState.Buttons |= (uint)RawButton.DpadUp;
  2995. }
  2996. }
  2997. }
  2998. }
  2999. return res;
  3000. }
  3001. public override void ConfigureButtonMap()
  3002. {
  3003. buttonMap.None = RawButton.None;
  3004. buttonMap.One = RawButton.LTouchpad;
  3005. buttonMap.Two = RawButton.Back;
  3006. buttonMap.Three = RawButton.None;
  3007. buttonMap.Four = RawButton.None;
  3008. buttonMap.Start = RawButton.Start;
  3009. buttonMap.Back = RawButton.Back;
  3010. buttonMap.PrimaryShoulder = RawButton.None;
  3011. buttonMap.PrimaryIndexTrigger = RawButton.None;
  3012. buttonMap.PrimaryHandTrigger = RawButton.None;
  3013. buttonMap.PrimaryThumbstick = RawButton.None;
  3014. buttonMap.PrimaryThumbstickUp = RawButton.None;
  3015. buttonMap.PrimaryThumbstickDown = RawButton.None;
  3016. buttonMap.PrimaryThumbstickLeft = RawButton.None;
  3017. buttonMap.PrimaryThumbstickRight = RawButton.None;
  3018. buttonMap.PrimaryTouchpad = RawButton.LTouchpad;
  3019. buttonMap.SecondaryShoulder = RawButton.None;
  3020. buttonMap.SecondaryIndexTrigger = RawButton.None;
  3021. buttonMap.SecondaryHandTrigger = RawButton.None;
  3022. buttonMap.SecondaryThumbstick = RawButton.None;
  3023. buttonMap.SecondaryThumbstickUp = RawButton.None;
  3024. buttonMap.SecondaryThumbstickDown = RawButton.None;
  3025. buttonMap.SecondaryThumbstickLeft = RawButton.None;
  3026. buttonMap.SecondaryThumbstickRight = RawButton.None;
  3027. buttonMap.SecondaryTouchpad = RawButton.None;
  3028. buttonMap.DpadUp = RawButton.DpadUp;
  3029. buttonMap.DpadDown = RawButton.DpadDown;
  3030. buttonMap.DpadLeft = RawButton.DpadLeft;
  3031. buttonMap.DpadRight = RawButton.DpadRight;
  3032. buttonMap.Up = RawButton.DpadUp;
  3033. buttonMap.Down = RawButton.DpadDown;
  3034. buttonMap.Left = RawButton.DpadLeft;
  3035. buttonMap.Right = RawButton.DpadRight;
  3036. }
  3037. public override void ConfigureTouchMap()
  3038. {
  3039. touchMap.None = RawTouch.None;
  3040. touchMap.One = RawTouch.LTouchpad;
  3041. touchMap.Two = RawTouch.None;
  3042. touchMap.Three = RawTouch.None;
  3043. touchMap.Four = RawTouch.None;
  3044. touchMap.PrimaryIndexTrigger = RawTouch.None;
  3045. touchMap.PrimaryThumbstick = RawTouch.None;
  3046. touchMap.PrimaryThumbRest = RawTouch.None;
  3047. touchMap.PrimaryTouchpad = RawTouch.LTouchpad;
  3048. touchMap.SecondaryIndexTrigger = RawTouch.None;
  3049. touchMap.SecondaryThumbstick = RawTouch.None;
  3050. touchMap.SecondaryThumbRest = RawTouch.None;
  3051. touchMap.SecondaryTouchpad = RawTouch.None;
  3052. }
  3053. public override void ConfigureNearTouchMap()
  3054. {
  3055. nearTouchMap.None = RawNearTouch.None;
  3056. nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None;
  3057. nearTouchMap.PrimaryThumbButtons = RawNearTouch.None;
  3058. nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None;
  3059. nearTouchMap.SecondaryThumbButtons = RawNearTouch.None;
  3060. }
  3061. public override void ConfigureAxis1DMap()
  3062. {
  3063. axis1DMap.None = RawAxis1D.None;
  3064. axis1DMap.PrimaryIndexTrigger = RawAxis1D.None;
  3065. axis1DMap.PrimaryHandTrigger = RawAxis1D.None;
  3066. axis1DMap.SecondaryIndexTrigger = RawAxis1D.None;
  3067. axis1DMap.SecondaryHandTrigger = RawAxis1D.None;
  3068. }
  3069. public override void ConfigureAxis2DMap()
  3070. {
  3071. axis2DMap.None = RawAxis2D.None;
  3072. axis2DMap.PrimaryThumbstick = RawAxis2D.None;
  3073. axis2DMap.PrimaryTouchpad = RawAxis2D.LTouchpad;
  3074. axis2DMap.SecondaryThumbstick = RawAxis2D.None;
  3075. axis2DMap.SecondaryTouchpad = RawAxis2D.None;
  3076. }
  3077. }
  3078. private class OVRControllerLTrackedRemote : OVRControllerBase
  3079. {
  3080. private bool emitSwipe;
  3081. private OVRPlugin.Vector2f moveAmount;
  3082. private float minMoveMagnitude = 0.3f;
  3083. public OVRControllerLTrackedRemote()
  3084. {
  3085. controllerType = Controller.LTrackedRemote;
  3086. }
  3087. public override void ConfigureButtonMap()
  3088. {
  3089. buttonMap.None = RawButton.None;
  3090. buttonMap.One = RawButton.LTouchpad;
  3091. buttonMap.Two = RawButton.Back;
  3092. buttonMap.Three = RawButton.None;
  3093. buttonMap.Four = RawButton.None;
  3094. buttonMap.Start = RawButton.Start;
  3095. buttonMap.Back = RawButton.Back;
  3096. buttonMap.PrimaryShoulder = RawButton.None;
  3097. buttonMap.PrimaryIndexTrigger = RawButton.LIndexTrigger;
  3098. buttonMap.PrimaryHandTrigger = RawButton.LHandTrigger;
  3099. buttonMap.PrimaryThumbstick = RawButton.None;
  3100. buttonMap.PrimaryThumbstickUp = RawButton.None;
  3101. buttonMap.PrimaryThumbstickDown = RawButton.None;
  3102. buttonMap.PrimaryThumbstickLeft = RawButton.None;
  3103. buttonMap.PrimaryThumbstickRight = RawButton.None;
  3104. buttonMap.PrimaryTouchpad = RawButton.LTouchpad;
  3105. buttonMap.SecondaryShoulder = RawButton.None;
  3106. buttonMap.SecondaryIndexTrigger = RawButton.None;
  3107. buttonMap.SecondaryHandTrigger = RawButton.None;
  3108. buttonMap.SecondaryThumbstick = RawButton.None;
  3109. buttonMap.SecondaryThumbstickUp = RawButton.None;
  3110. buttonMap.SecondaryThumbstickDown = RawButton.None;
  3111. buttonMap.SecondaryThumbstickLeft = RawButton.None;
  3112. buttonMap.SecondaryThumbstickRight = RawButton.None;
  3113. buttonMap.SecondaryTouchpad = RawButton.None;
  3114. buttonMap.DpadUp = RawButton.DpadUp;
  3115. buttonMap.DpadDown = RawButton.DpadDown;
  3116. buttonMap.DpadLeft = RawButton.DpadLeft;
  3117. buttonMap.DpadRight = RawButton.DpadRight;
  3118. buttonMap.Up = RawButton.DpadUp;
  3119. buttonMap.Down = RawButton.DpadDown;
  3120. buttonMap.Left = RawButton.DpadLeft;
  3121. buttonMap.Right = RawButton.DpadRight;
  3122. }
  3123. public override void ConfigureTouchMap()
  3124. {
  3125. touchMap.None = RawTouch.None;
  3126. touchMap.One = RawTouch.LTouchpad;
  3127. touchMap.Two = RawTouch.None;
  3128. touchMap.Three = RawTouch.None;
  3129. touchMap.Four = RawTouch.None;
  3130. touchMap.PrimaryIndexTrigger = RawTouch.LIndexTrigger;
  3131. touchMap.PrimaryThumbstick = RawTouch.None;
  3132. touchMap.PrimaryThumbRest = RawTouch.None;
  3133. touchMap.PrimaryTouchpad = RawTouch.LTouchpad;
  3134. touchMap.SecondaryIndexTrigger = RawTouch.None;
  3135. touchMap.SecondaryThumbstick = RawTouch.None;
  3136. touchMap.SecondaryThumbRest = RawTouch.None;
  3137. touchMap.SecondaryTouchpad = RawTouch.None;
  3138. }
  3139. public override void ConfigureNearTouchMap()
  3140. {
  3141. nearTouchMap.None = RawNearTouch.None;
  3142. nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None;
  3143. nearTouchMap.PrimaryThumbButtons = RawNearTouch.None;
  3144. nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None;
  3145. nearTouchMap.SecondaryThumbButtons = RawNearTouch.None;
  3146. }
  3147. public override void ConfigureAxis1DMap()
  3148. {
  3149. axis1DMap.None = RawAxis1D.None;
  3150. axis1DMap.PrimaryIndexTrigger = RawAxis1D.LIndexTrigger;
  3151. axis1DMap.PrimaryHandTrigger = RawAxis1D.LHandTrigger;
  3152. axis1DMap.SecondaryIndexTrigger = RawAxis1D.None;
  3153. axis1DMap.SecondaryHandTrigger = RawAxis1D.None;
  3154. }
  3155. public override void ConfigureAxis2DMap()
  3156. {
  3157. axis2DMap.None = RawAxis2D.None;
  3158. axis2DMap.PrimaryThumbstick = RawAxis2D.None;
  3159. axis2DMap.PrimaryTouchpad = RawAxis2D.LTouchpad;
  3160. axis2DMap.SecondaryThumbstick = RawAxis2D.None;
  3161. axis2DMap.SecondaryTouchpad = RawAxis2D.None;
  3162. }
  3163. public override Controller Update()
  3164. {
  3165. Controller res = base.Update();
  3166. if (GetDown(RawTouch.LTouchpad, OVRInput.Controller.LTrackedRemote))
  3167. {
  3168. emitSwipe = true;
  3169. moveAmount = currentState.LTouchpad;
  3170. }
  3171. if (GetDown(RawButton.LTouchpad, OVRInput.Controller.LTrackedRemote))
  3172. {
  3173. emitSwipe = false;
  3174. }
  3175. if (GetUp(RawTouch.LTouchpad, OVRInput.Controller.LTrackedRemote) && emitSwipe)
  3176. {
  3177. emitSwipe = false;
  3178. moveAmount.x = previousState.LTouchpad.x - moveAmount.x;
  3179. moveAmount.y = previousState.LTouchpad.y - moveAmount.y;
  3180. Vector2 move = new Vector2(moveAmount.x, moveAmount.y);
  3181. if (move.magnitude >= minMoveMagnitude)
  3182. {
  3183. move.Normalize();
  3184. // Left/Right
  3185. if (Mathf.Abs(move.x) > Mathf.Abs(move.y))
  3186. {
  3187. if (move.x < 0.0f)
  3188. {
  3189. currentState.Buttons |= (uint)RawButton.DpadLeft;
  3190. }
  3191. else
  3192. {
  3193. currentState.Buttons |= (uint)RawButton.DpadRight;
  3194. }
  3195. }
  3196. // Up/Down
  3197. else
  3198. {
  3199. if (move.y < 0.0f)
  3200. {
  3201. currentState.Buttons |= (uint)RawButton.DpadDown;
  3202. }
  3203. else
  3204. {
  3205. currentState.Buttons |= (uint)RawButton.DpadUp;
  3206. }
  3207. }
  3208. }
  3209. }
  3210. return res;
  3211. }
  3212. public override bool WasRecentered()
  3213. {
  3214. return (currentState.LRecenterCount != previousState.LRecenterCount);
  3215. }
  3216. public override byte GetRecenterCount()
  3217. {
  3218. return currentState.LRecenterCount;
  3219. }
  3220. public override byte GetBatteryPercentRemaining()
  3221. {
  3222. return currentState.LBatteryPercentRemaining;
  3223. }
  3224. }
  3225. private class OVRControllerRTrackedRemote : OVRControllerBase
  3226. {
  3227. private bool emitSwipe;
  3228. private OVRPlugin.Vector2f moveAmount;
  3229. private float minMoveMagnitude = 0.3f;
  3230. public OVRControllerRTrackedRemote()
  3231. {
  3232. controllerType = Controller.RTrackedRemote;
  3233. }
  3234. public override void ConfigureButtonMap()
  3235. {
  3236. buttonMap.None = RawButton.None;
  3237. buttonMap.One = RawButton.RTouchpad;
  3238. buttonMap.Two = RawButton.Back;
  3239. buttonMap.Three = RawButton.None;
  3240. buttonMap.Four = RawButton.None;
  3241. buttonMap.Start = RawButton.Start;
  3242. buttonMap.Back = RawButton.Back;
  3243. buttonMap.PrimaryShoulder = RawButton.None;
  3244. buttonMap.PrimaryIndexTrigger = RawButton.RIndexTrigger;
  3245. buttonMap.PrimaryHandTrigger = RawButton.RHandTrigger;
  3246. buttonMap.PrimaryThumbstick = RawButton.None;
  3247. buttonMap.PrimaryThumbstickUp = RawButton.None;
  3248. buttonMap.PrimaryThumbstickDown = RawButton.None;
  3249. buttonMap.PrimaryThumbstickLeft = RawButton.None;
  3250. buttonMap.PrimaryThumbstickRight = RawButton.None;
  3251. buttonMap.PrimaryTouchpad = RawButton.RTouchpad;
  3252. buttonMap.SecondaryShoulder = RawButton.None;
  3253. buttonMap.SecondaryIndexTrigger = RawButton.None;
  3254. buttonMap.SecondaryHandTrigger = RawButton.None;
  3255. buttonMap.SecondaryThumbstick = RawButton.None;
  3256. buttonMap.SecondaryThumbstickUp = RawButton.None;
  3257. buttonMap.SecondaryThumbstickDown = RawButton.None;
  3258. buttonMap.SecondaryThumbstickLeft = RawButton.None;
  3259. buttonMap.SecondaryThumbstickRight = RawButton.None;
  3260. buttonMap.SecondaryTouchpad = RawButton.None;
  3261. buttonMap.DpadUp = RawButton.DpadUp;
  3262. buttonMap.DpadDown = RawButton.DpadDown;
  3263. buttonMap.DpadLeft = RawButton.DpadLeft;
  3264. buttonMap.DpadRight = RawButton.DpadRight;
  3265. buttonMap.Up = RawButton.DpadUp;
  3266. buttonMap.Down = RawButton.DpadDown;
  3267. buttonMap.Left = RawButton.DpadLeft;
  3268. buttonMap.Right = RawButton.DpadRight;
  3269. }
  3270. public override void ConfigureTouchMap()
  3271. {
  3272. touchMap.None = RawTouch.None;
  3273. touchMap.One = RawTouch.RTouchpad;
  3274. touchMap.Two = RawTouch.None;
  3275. touchMap.Three = RawTouch.None;
  3276. touchMap.Four = RawTouch.None;
  3277. touchMap.PrimaryIndexTrigger = RawTouch.RIndexTrigger;
  3278. touchMap.PrimaryThumbstick = RawTouch.None;
  3279. touchMap.PrimaryThumbRest = RawTouch.None;
  3280. touchMap.PrimaryTouchpad = RawTouch.RTouchpad;
  3281. touchMap.SecondaryIndexTrigger = RawTouch.None;
  3282. touchMap.SecondaryThumbstick = RawTouch.None;
  3283. touchMap.SecondaryThumbRest = RawTouch.None;
  3284. touchMap.SecondaryTouchpad = RawTouch.None;
  3285. }
  3286. public override void ConfigureNearTouchMap()
  3287. {
  3288. nearTouchMap.None = RawNearTouch.None;
  3289. nearTouchMap.PrimaryIndexTrigger = RawNearTouch.None;
  3290. nearTouchMap.PrimaryThumbButtons = RawNearTouch.None;
  3291. nearTouchMap.SecondaryIndexTrigger = RawNearTouch.None;
  3292. nearTouchMap.SecondaryThumbButtons = RawNearTouch.None;
  3293. }
  3294. public override void ConfigureAxis1DMap()
  3295. {
  3296. axis1DMap.None = RawAxis1D.None;
  3297. axis1DMap.PrimaryIndexTrigger = RawAxis1D.RIndexTrigger;
  3298. axis1DMap.PrimaryHandTrigger = RawAxis1D.RHandTrigger;
  3299. axis1DMap.SecondaryIndexTrigger = RawAxis1D.None;
  3300. axis1DMap.SecondaryHandTrigger = RawAxis1D.None;
  3301. }
  3302. public override void ConfigureAxis2DMap()
  3303. {
  3304. axis2DMap.None = RawAxis2D.None;
  3305. axis2DMap.PrimaryThumbstick = RawAxis2D.None;
  3306. axis2DMap.PrimaryTouchpad = RawAxis2D.RTouchpad;
  3307. axis2DMap.SecondaryThumbstick = RawAxis2D.None;
  3308. axis2DMap.SecondaryTouchpad = RawAxis2D.None;
  3309. }
  3310. public override Controller Update()
  3311. {
  3312. Controller res = base.Update();
  3313. if (GetDown(RawTouch.RTouchpad, OVRInput.Controller.RTrackedRemote))
  3314. {
  3315. emitSwipe = true;
  3316. moveAmount = currentState.RTouchpad;
  3317. }
  3318. if (GetDown(RawButton.RTouchpad, OVRInput.Controller.RTrackedRemote))
  3319. {
  3320. emitSwipe = false;
  3321. }
  3322. if (GetUp(RawTouch.RTouchpad, OVRInput.Controller.RTrackedRemote) && emitSwipe)
  3323. {
  3324. emitSwipe = false;
  3325. moveAmount.x = previousState.RTouchpad.x - moveAmount.x;
  3326. moveAmount.y = previousState.RTouchpad.y - moveAmount.y;
  3327. Vector2 move = new Vector2(moveAmount.x, moveAmount.y);
  3328. if (move.magnitude >= minMoveMagnitude)
  3329. {
  3330. move.Normalize();
  3331. // Left/Right
  3332. if (Mathf.Abs(move.x) > Mathf.Abs(move.y))
  3333. {
  3334. if (move.x < 0.0f)
  3335. {
  3336. currentState.Buttons |= (uint)RawButton.DpadLeft;
  3337. }
  3338. else
  3339. {
  3340. currentState.Buttons |= (uint)RawButton.DpadRight;
  3341. }
  3342. }
  3343. // Up/Down
  3344. else
  3345. {
  3346. if (move.y < 0.0f)
  3347. {
  3348. currentState.Buttons |= (uint)RawButton.DpadDown;
  3349. }
  3350. else
  3351. {
  3352. currentState.Buttons |= (uint)RawButton.DpadUp;
  3353. }
  3354. }
  3355. }
  3356. }
  3357. return res;
  3358. }
  3359. public override bool WasRecentered()
  3360. {
  3361. return (currentState.RRecenterCount != previousState.RRecenterCount);
  3362. }
  3363. public override byte GetRecenterCount()
  3364. {
  3365. return currentState.RRecenterCount;
  3366. }
  3367. public override byte GetBatteryPercentRemaining()
  3368. {
  3369. return currentState.RBatteryPercentRemaining;
  3370. }
  3371. }
  3372. }