GSXRController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2017 Qualcomm Technologies, Inc.
  3. // All Rights Reserved. Qualcomm Technologies Proprietary and Confidential.
  4. //-----------------------------------------------------------------------------
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. /// <summary>
  9. /// GSXRController
  10. /// </summary>
  11. public class GSXRController : MonoBehaviour, GSXRManager.SlamEventListener {
  12. string controllerParams = "";
  13. //! \brief Events to use in slamControllerSendEvent
  14. public enum slamControllerMessageType
  15. {
  16. kControllerMessageRecenter = 0,
  17. kControllerMessageVibration = 1
  18. };
  19. //! \brief Query Values
  20. public enum slamControllerQueryType
  21. {
  22. kControllerBatteryRemaining,
  23. kControllerControllerCaps
  24. };
  25. //! Controller Connection state
  26. public enum slamControllerConnectionState {
  27. kNotInitialized = 0,
  28. kDisconnected = 1,
  29. kConnected = 2,
  30. kConnecting = 3,
  31. kError = 4
  32. };
  33. /// <summary>
  34. /// Start this instance.
  35. /// </summary>
  36. //---------------------------------------------------------------------------------------------
  37. void Start ()
  38. {
  39. //Register for SlamEvents
  40. GSXRManager.Instance.AddEventListener (this);
  41. }
  42. /// <summary>
  43. /// Raises the slam event event.
  44. /// </summary>
  45. /// <param name="ev">Ev.</param>
  46. //---------------------------------------------------------------------------------------------
  47. public void OnSlamEvent(GSXRManager.SlamEvent ev)
  48. {
  49. switch (ev.eventType) {
  50. case GSXRManager.slamEventType.kEventVrModeStarted:
  51. handle = GSXRManager.Instance.ControllerStartTracking (controllerParams);
  52. space = GetCapability.caps != 0 ? 1 : 0; // Has Position so needs to be transformed from HMD to World space
  53. break;
  54. }
  55. }
  56. /// <summary>
  57. /// Raises the application pause event.
  58. /// </summary>
  59. /// <param name="isPaused">If set to <c>true</c> is paused.</param>
  60. //---------------------------------------------------------------------------------------------
  61. void OnApplicationPause(bool isPaused)
  62. {
  63. return;
  64. if (isPaused) {
  65. GSXRManager.Instance.ControllerStopTracking (handle);
  66. }
  67. }
  68. /// <summary>
  69. /// Get the current controller state
  70. /// </summary>
  71. /// <returns>The state.</returns>
  72. //---------------------------------------------------------------------------------------------
  73. public GSXRControllerState State
  74. {
  75. get {
  76. return currentState;
  77. }
  78. }
  79. /// <summary>
  80. /// Gets the state of the connection.
  81. /// </summary>
  82. /// <value>The state of the connection.</value>
  83. //---------------------------------------------------------------------------------------------
  84. public slamControllerConnectionState ConnectionState {
  85. get {
  86. return (slamControllerConnectionState)currentState.connectionState;
  87. }
  88. }
  89. /// <summary>
  90. /// Sends the message.
  91. /// </summary>
  92. /// <param name="what">What.</param>
  93. /// <param name="arg1">Arg1.</param>
  94. /// <param name="arg2">Arg2.</param>
  95. //---------------------------------------------------------------------------------------------
  96. public void SendMessage(slamControllerMessageType what, int arg1, int arg2)
  97. {
  98. GSXRManager.Instance.ControllerSendMessage(handle, what, arg1, arg2);
  99. }
  100. /// <summary>
  101. /// Recenter this instance.
  102. /// </summary>
  103. //---------------------------------------------------------------------------------------------
  104. public void Recenter( )
  105. {
  106. GSXRManager.Instance.ControllerSendMessage(handle,
  107. slamControllerMessageType.kControllerMessageRecenter,
  108. 0,
  109. 0);
  110. }
  111. /// <summary>
  112. /// Send message to vibrate
  113. /// </summary>
  114. //---------------------------------------------------------------------------------------------
  115. public void Vibrate(int arg1, int arg2)
  116. {
  117. GSXRManager.Instance.ControllerSendMessage (handle,
  118. slamControllerMessageType.kControllerMessageVibration,
  119. arg1,
  120. arg2);
  121. }
  122. /// <summary>
  123. /// Gets the current state of the button.
  124. /// </summary>
  125. /// <returns><c>true</c>, if button is down, <c>false</c> otherwise.</returns>
  126. /// <param name="buttonId">Button identifier.</param>
  127. //---------------------------------------------------------------------------------------------
  128. public bool GetButton(slamControllerButton buttonId)
  129. {
  130. int mask = (int)buttonId;
  131. return ((currentState.buttonState & mask) != 0);
  132. }
  133. /// <summary>
  134. /// Gets the button up.
  135. /// </summary>
  136. /// <returns><c>true</c>, if button is up this frame, <c>false</c> otherwise.</returns>
  137. /// <param name="buttonId">Button identifier.</param>
  138. //---------------------------------------------------------------------------------------------
  139. public bool GetButtonUp(slamControllerButton buttonId)
  140. {
  141. int mask = (int)(buttonId);
  142. return ((previousButtonState & mask) != 0) && ((currentState.buttonState & mask) == 0);
  143. }
  144. /// <summary>
  145. /// Gets the button down.
  146. /// </summary>
  147. /// <returns><c>true</c>, if button is down this frame, <c>false</c> otherwise.</returns>
  148. /// <param name="buttonId">Button identifier.</param>
  149. //---------------------------------------------------------------------------------------------
  150. public bool GetButtonDown(slamControllerButton buttonId)
  151. {
  152. int mask = (int)buttonId;
  153. return ((previousButtonState & mask) == 0) && ((currentState.buttonState & mask) != 0);
  154. }
  155. /// <summary>
  156. /// Get the current orientation.
  157. /// </summary>
  158. /// <value>The orientation.</value>
  159. //---------------------------------------------------------------------------------------------
  160. public Quaternion Orientation
  161. {
  162. get {
  163. return currentState.rotation;
  164. }
  165. }
  166. /// <summary>
  167. /// Get the current position.
  168. /// </summary>
  169. /// <value>The position.</value>
  170. //---------------------------------------------------------------------------------------------
  171. public Vector3 Position {
  172. get {
  173. return currentState.position;
  174. }
  175. }
  176. /// <summary>
  177. /// the timestamp.
  178. /// </summary>
  179. /// <value>The timestamp.</value>
  180. //---------------------------------------------------------------------------------------------
  181. public long Timestamp {
  182. get {
  183. return currentState.timestamp;
  184. }
  185. }
  186. /// <summary>
  187. /// Gets the analog.
  188. /// </summary>
  189. /// <returns>The analog.</returns>
  190. /// <param name="id">Identifier.</param>
  191. //---------------------------------------------------------------------------------------------
  192. public Vector2 GetAxis2D(slamControllerAxis2D axi2d)
  193. {
  194. return currentState.analog2D != null ? currentState.analog2D [(int)axi2d] : Vector2.zero;
  195. }
  196. /// <summary>
  197. /// Gets the analog.
  198. /// </summary>
  199. /// <returns>The analog.</returns>
  200. /// <param name="id">Identifier.</param>
  201. //---------------------------------------------------------------------------------------------
  202. public float GetAxis1D(slamControllerAxis1D axis1d)
  203. {
  204. return currentState.analog1D != null ? currentState.analog1D [(int)axis1d] : 0f;
  205. }
  206. /// <summary>
  207. /// Determines whether this instance is touching the specified id.
  208. /// </summary>
  209. /// <returns><c>true</c> if this instance is touching the specified id; otherwise, <c>false</c>.</returns>
  210. /// <param name="id">Identifier.</param>
  211. //---------------------------------------------------------------------------------------------
  212. public bool GetTouch(slamControllerTouch touch)
  213. {
  214. int mask = (int)touch;
  215. return ((currentState.isTouching & mask) != 0);
  216. }
  217. /// <summary>
  218. /// Gets the touch down.
  219. /// </summary>
  220. /// <returns><c>true</c>, if touch down was gotten, <c>false</c> otherwise.</returns>
  221. /// <param name="id">Identifier.</param>
  222. //---------------------------------------------------------------------------------------------
  223. public bool GetTouchDown(slamControllerTouch touch)
  224. {
  225. int mask = (int)touch;
  226. return ((previousTouchState & mask) == 0) && ((currentState.isTouching & mask) != 0);
  227. }
  228. /// <summary>
  229. /// Gets the touch up.
  230. /// </summary>
  231. /// <returns><c>true</c>, if touch up was gotten, <c>false</c> otherwise.</returns>
  232. /// <param name="id">Identifier.</param>
  233. //---------------------------------------------------------------------------------------------
  234. public bool GetTouchUp(slamControllerTouch touch)
  235. {
  236. int mask = (int)touch;
  237. return ((previousTouchState & mask) != 0) && ((currentState.isTouching & mask) == 0);
  238. }
  239. /// <summary>
  240. /// Gets the battery.
  241. /// </summary>
  242. /// <value>The battery.</value>
  243. //---------------------------------------------------------------------------------------------
  244. public int BatteryLevel {
  245. get {
  246. int batteryLevel = -1;
  247. object obj = (GSXRManager.Instance.ControllerQuery(handle, slamControllerQueryType.kControllerBatteryRemaining));
  248. if (obj != null) {
  249. batteryLevel = (int)(obj);
  250. }
  251. return batteryLevel;
  252. }
  253. }
  254. /// <summary>
  255. public SlamControllerCaps GetCapability
  256. {
  257. get
  258. {
  259. SlamControllerCaps Cap = new SlamControllerCaps();
  260. object obj = (GSXRManager.Instance.ControllerQuery(handle, slamControllerQueryType.kControllerControllerCaps));
  261. if (obj != null)
  262. {
  263. Cap = (SlamControllerCaps)(obj);
  264. }
  265. return Cap;
  266. }
  267. }
  268. /// Raises the enable event.
  269. /// </summary>
  270. //---------------------------------------------------------------------------------------------
  271. public void OnEnable()
  272. {
  273. frameDelimiter = StartCoroutine (OnFrameEnd());
  274. }
  275. /// <summary>
  276. /// Raises the disable event.
  277. /// </summary>
  278. //---------------------------------------------------------------------------------------------
  279. public void OnDisable()
  280. {
  281. StopCoroutine (frameDelimiter);
  282. }
  283. /// <summary>
  284. /// Raises the per frame event.
  285. /// </summary>
  286. //---------------------------------------------------------------------------------------------
  287. IEnumerator OnFrameEnd()
  288. {
  289. while (true) {
  290. yield return waitForEndOfFrame;
  291. previousButtonState = currentState.buttonState;
  292. previousTouchState = currentState.isTouching;
  293. currentState = GSXRManager.Instance.ControllerGetState (handle, space);
  294. }
  295. }
  296. /**
  297. *
  298. */
  299. public object Query(slamControllerQueryType what)
  300. {
  301. return GSXRManager.Instance.ControllerQuery (handle, what);
  302. }
  303. /**
  304. * Handle for the Controller.
  305. */
  306. private int handle = -1;
  307. /**
  308. * Handle for the Controller.
  309. */
  310. private int space = 0;
  311. /**
  312. * The Current State. Updated each frame.
  313. */
  314. private GSXRControllerState currentState;
  315. /**
  316. * Previous Button State.
  317. */
  318. private int previousButtonState = 0;
  319. /**
  320. * Previous Touch State.
  321. */
  322. private int previousTouchState = 0;
  323. /**
  324. * Coroutine for WaitForEndOfFrame
  325. */
  326. private Coroutine frameDelimiter = null;
  327. /**
  328. *
  329. */
  330. private WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();
  331. //! Controller Touch button enumerations
  332. public enum slamControllerTouch {
  333. None = 0x00000000,
  334. One = 0x00000001,
  335. Two = 0x00000002,
  336. Three = 0x00000004,
  337. Four = 0x00000008,
  338. PrimaryThumbstick = 0x00000010,
  339. SecondaryThumstick = 0x00000020,
  340. Any = ~None
  341. };
  342. //! Controller Trigger enumerations
  343. public enum slamControllerAxis1D {
  344. PrimaryIndexTrigger = 0x00000000,
  345. SecondaryIndexTrigger = 0x00000001,
  346. PrimaryHandTrigger = 0x00000002,
  347. SecondaryHandTrigger = 0x00000003
  348. };
  349. //! Controller Joystick enumerations
  350. public enum slamControllerAxis2D {
  351. PrimaryThumbstick = 0x00000000,
  352. SecondaryThumbstick = 0x00000001
  353. };
  354. //! Controller Button enumerations
  355. public enum slamControllerButton {
  356. None = 0x00000000,
  357. One = 0x00000001,
  358. Two = 0x00000002,
  359. Three = 0x00000004,
  360. Four = 0x00000008,
  361. DpadUp = 0x00000010,
  362. DpadDown = 0x00000020,
  363. DpadLeft = 0x00000040,
  364. DpadRight = 0x00000080,
  365. Start = 0x00000100,
  366. Back = 0x00000200,
  367. PrimaryShoulder = 0x00001000,
  368. PrimaryIndexTrigger = 0x00002000,
  369. PrimaryHandTrigger = 0x00004000,
  370. PrimaryThumbstick = 0x00008000,
  371. PrimaryThumbstickUp = 0x00010000,
  372. PrimaryThumbstickDown = 0x00020000,
  373. PrimaryThumbstickLeft = 0x00040000,
  374. PrimaryThumbstickRight = 0x00080000,
  375. SecondaryShoulder = 0x00100000,
  376. SecondaryIndexTrigger = 0x00200000,
  377. SecondaryHandTrigger = 0x00400000,
  378. SecondaryThumbstick = 0x00800000,
  379. SecondaryThumbstickUp = 0x01000000,
  380. SecondaryThumbstickDown = 0x02000000,
  381. SecondaryThumbstickLeft = 0x04000000,
  382. SecondaryThumbstickRight = 0x08000000,
  383. Up = 0x10000000,
  384. Down = 0x20000000,
  385. Left = 0x40000000,
  386. Right = unchecked((int)0x80000000),
  387. Any = ~None
  388. };
  389. }