SvrController.cs 14 KB

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