ControllerState.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System;
  12. using System.Collections.Generic;
  13. using UnityEngine;
  14. /// <summary> Values that represent controller types. </summary>
  15. public enum ControllerType
  16. {
  17. /// <summary> An enum constant representing the controller type editor option. </summary>
  18. CONTROLLER_TYPE_EDITOR = 1001,
  19. /// <summary> An enum constant representing the controller type unknown option. </summary>
  20. CONTROLLER_TYPE_UNKNOWN = -1,
  21. /// <summary> An enum constant representing the controller type nreallight option. </summary>
  22. CONTROLLER_TYPE_NREALLIGHT = 0,
  23. /// <summary> An enum constant representing the controller type phone option. </summary>
  24. CONTROLLER_TYPE_PHONE = 1,
  25. /// <summary> An enum constant representing the controller type hand option. </summary>
  26. CONTROLLER_TYPE_HAND = 2
  27. }
  28. /// <summary> Values that represent controller available features. </summary>
  29. public enum ControllerAvailableFeature
  30. {
  31. /// <summary> The position is avaliable. </summary>
  32. CONTROLLER_AVAILABLE_FEATURE_POSITION = (1 << 0),
  33. /// <summary> The rotation is avaliable. </summary>
  34. CONTROLLER_AVAILABLE_FEATURE_ROTATION = (1 << 1),
  35. /// <summary>
  36. /// An enum constant representing the controller available feature gyro option. </summary>
  37. CONTROLLER_AVAILABLE_FEATURE_GYRO = (1 << 2),
  38. /// <summary>
  39. /// An enum constant representing the controller available feature accel option. </summary>
  40. CONTROLLER_AVAILABLE_FEATURE_ACCEL = (1 << 3),
  41. /// <summary>
  42. /// An enum constant representing the controller available feature Magnitude option. </summary>
  43. CONTROLLER_AVAILABLE_FEATURE_MAG = (1 << 4),
  44. /// <summary>
  45. /// An enum constant representing the controller available feature battery option. </summary>
  46. CONTROLLER_AVAILABLE_FEATURE_BATTERY = (1 << 5),
  47. /// <summary>
  48. /// An enum constant representing the controller available feature charging option. </summary>
  49. CONTROLLER_AVAILABLE_FEATURE_CHARGING = (1 << 6),
  50. /// <summary>
  51. /// An enum constant representing the controller available feature recenter option. </summary>
  52. CONTROLLER_AVAILABLE_FEATURE_RECENTER = (1 << 7),
  53. /// <summary>
  54. /// An enum constant representing the controller available feature haptic vibrate option. </summary>
  55. CONTROLLER_AVAILABLE_FEATURE_HAPTIC_VIBRATE = (1 << 8)
  56. }
  57. /// <summary> Values that represent controller buttons. </summary>
  58. public enum ControllerButton
  59. {
  60. /// <summary> An enum constant representing the first option. </summary>
  61. BEGIN = 1 << 0,
  62. /// <summary> An enum constant representing the trigger option. </summary>
  63. TRIGGER = 1 << 0,
  64. /// <summary> An enum constant representing the Application option. </summary>
  65. APP = 1 << 1,
  66. /// <summary> An enum constant representing the home option. </summary>
  67. HOME = 1 << 2,
  68. /// <summary> An enum constant representing the grip option. </summary>
  69. GRIP = 1 << 3,
  70. /// <summary> An enum constant representing the touchpad button option. </summary>
  71. TOUCHPAD_BUTTON = 1 << 4,
  72. /// <summary> An enum constant representing the last option. </summary>
  73. END = 1 << 4,
  74. }
  75. /// <summary> Values that represent controller connection states. </summary>
  76. public enum ControllerConnectionState
  77. {
  78. /// <summary>
  79. /// An enum constant representing the controller connection state error option. </summary>
  80. CONTROLLER_CONNECTION_STATE_ERROR = -1,
  81. /// <summary>
  82. /// An enum constant representing the controller connection state not initialized option. </summary>
  83. CONTROLLER_CONNECTION_STATE_NOT_INITIALIZED = 0,
  84. /// <summary>
  85. /// An enum constant representing the controller connection state disconnected option. </summary>
  86. CONTROLLER_CONNECTION_STATE_DISCONNECTED = 1,
  87. /// <summary>
  88. /// An enum constant representing the controller connection state connecting option. </summary>
  89. CONTROLLER_CONNECTION_STATE_CONNECTING = 2,
  90. /// <summary>
  91. /// An enum constant representing the controller connection state connected option. </summary>
  92. CONTROLLER_CONNECTION_STATE_CONNECTED = 3
  93. }
  94. internal enum HandednessType
  95. {
  96. LEFT_HANDEDNESS = 1,
  97. RIGHT_HANDEDNESS,
  98. }
  99. /// <summary> Values that represent button event types. </summary>
  100. internal enum ButtonEventType
  101. {
  102. /// <summary> An enum constant representing the down option. </summary>
  103. Down = 0,
  104. /// <summary> An enum constant representing the pressing option. </summary>
  105. Pressing,
  106. /// <summary> An enum constant representing the up option. </summary>
  107. Up,
  108. /// <summary> An enum constant representing the click option. </summary>
  109. Click,
  110. }
  111. /// <summary> A controller state. </summary>
  112. public class ControllerState
  113. {
  114. /// <summary> Type of the controller. </summary>
  115. internal ControllerType controllerType;
  116. /// <summary> State of the connection. </summary>
  117. internal ControllerConnectionState connectionState;
  118. /// <summary> The rotation. </summary>
  119. internal Quaternion rotation;
  120. /// <summary> The position. </summary>
  121. internal Vector3 position;
  122. /// <summary> The gyro. </summary>
  123. internal Vector3 gyro;
  124. /// <summary> The accel. </summary>
  125. internal Vector3 accel;
  126. /// <summary> The magnitude. </summary>
  127. internal Vector3 mag;
  128. /// <summary> The touch position. </summary>
  129. internal Vector2 touchPos;
  130. /// <summary> The delta touch. </summary>
  131. internal Vector2 deltaTouch;
  132. /// <summary> True if is touching, false if not. </summary>
  133. internal bool isTouching;
  134. /// <summary> True if recentered. </summary>
  135. internal bool recentered;
  136. /// <summary> State of the buttons. </summary>
  137. internal ControllerButton buttonsState;
  138. /// <summary> The buttons down. </summary>
  139. internal ControllerButton buttonsDown;
  140. /// <summary> The buttons up. </summary>
  141. internal ControllerButton buttonsUp;
  142. /// <summary> True if is charging, false if not. </summary>
  143. internal bool isCharging;
  144. /// <summary> The battery level. </summary>
  145. internal int batteryLevel;
  146. /// <summary> The available feature. </summary>
  147. internal ControllerAvailableFeature availableFeature;
  148. /// <summary> The last touch position. </summary>
  149. private Vector2 m_LastTouchPos;
  150. /// <summary> Dictionary of last down times. </summary>
  151. private Dictionary<ControllerButton, float> m_LastDownTimeDict = new Dictionary<ControllerButton, float>();
  152. /// <summary> Array of listeners. </summary>
  153. private Dictionary<ControllerButton, Action>[] m_ListenersArr = new Dictionary<ControllerButton, Action>[Enum.GetValues(typeof(ButtonEventType)).Length];
  154. /// <summary> Default constructor. </summary>
  155. public ControllerState()
  156. {
  157. Reset();
  158. }
  159. /// <summary> Gets a value indicating whether this object is 6dof. </summary>
  160. /// <value> True if this object is 6dof, false if not. </value>
  161. public bool Is6dof
  162. {
  163. get
  164. {
  165. return IsFeatureAvailable(ControllerAvailableFeature.CONTROLLER_AVAILABLE_FEATURE_POSITION) && IsFeatureAvailable(ControllerAvailableFeature.CONTROLLER_AVAILABLE_FEATURE_ROTATION);
  166. }
  167. }
  168. /// <summary> Queries if a feature is available. </summary>
  169. /// <param name="feature"> The feature.</param>
  170. /// <returns> True if the feature is available, false if not. </returns>
  171. public bool IsFeatureAvailable(ControllerAvailableFeature feature)
  172. {
  173. return (availableFeature & feature) != 0;
  174. }
  175. /// <summary> Gets a button. </summary>
  176. /// <param name="button"> The button.</param>
  177. /// <returns> True if it succeeds, false if it fails. </returns>
  178. public bool GetButton(ControllerButton button)
  179. {
  180. return (buttonsState & button) != 0;
  181. }
  182. /// <summary> Gets button down. </summary>
  183. /// <param name="button"> The button.</param>
  184. /// <returns> True if it succeeds, false if it fails. </returns>
  185. public bool GetButtonDown(ControllerButton button)
  186. {
  187. return (buttonsDown & button) != 0;
  188. }
  189. /// <summary> Gets button up. </summary>
  190. /// <param name="button"> The button.</param>
  191. /// <returns> True if it succeeds, false if it fails. </returns>
  192. public bool GetButtonUp(ControllerButton button)
  193. {
  194. return (buttonsUp & button) != 0;
  195. }
  196. /// <summary> Resets this object. </summary>
  197. public void Reset()
  198. {
  199. controllerType = ControllerType.CONTROLLER_TYPE_UNKNOWN;
  200. connectionState = ControllerConnectionState.CONTROLLER_CONNECTION_STATE_NOT_INITIALIZED;
  201. rotation = Quaternion.identity;
  202. position = Vector3.zero;
  203. gyro = Vector3.zero;
  204. accel = Vector3.zero;
  205. mag = Vector3.zero;
  206. touchPos = Vector2.zero;
  207. isTouching = false;
  208. recentered = false;
  209. buttonsState = 0;
  210. buttonsDown = 0;
  211. buttonsUp = 0;
  212. isCharging = false;
  213. batteryLevel = 0;
  214. availableFeature = 0;
  215. }
  216. /// <summary> Adds a button listener. </summary>
  217. /// <param name="buttonEventType"> Type of the button event.</param>
  218. /// <param name="button"> The button.</param>
  219. /// <param name="action"> The action.</param>
  220. internal void AddButtonListener(ButtonEventType buttonEventType, ControllerButton button, Action action)
  221. {
  222. int buttonEventID = (int)buttonEventType;
  223. if (m_ListenersArr[buttonEventID] == null)
  224. {
  225. m_ListenersArr[buttonEventID] = new Dictionary<ControllerButton, Action>();
  226. }
  227. if (m_ListenersArr[buttonEventID].ContainsKey(button))
  228. {
  229. m_ListenersArr[buttonEventID][button] += action;
  230. }
  231. else
  232. {
  233. m_ListenersArr[buttonEventID].Add(button, action);
  234. }
  235. }
  236. /// <summary> Removes the button listener. </summary>
  237. /// <param name="buttonEventType"> Type of the button event.</param>
  238. /// <param name="button"> The button.</param>
  239. /// <param name="action"> The action.</param>
  240. internal void RemoveButtonListener(ButtonEventType buttonEventType, ControllerButton button, Action action)
  241. {
  242. int buttonEventID = (int)buttonEventType;
  243. if (m_ListenersArr[buttonEventID] == null)
  244. {
  245. m_ListenersArr[buttonEventID] = new Dictionary<ControllerButton, Action>();
  246. }
  247. if (m_ListenersArr[buttonEventID].ContainsKey(button) && m_ListenersArr[buttonEventID][button] != null)
  248. {
  249. m_ListenersArr[buttonEventID][button] -= action;
  250. if (m_ListenersArr[buttonEventID][button] == null)
  251. {
  252. m_ListenersArr[buttonEventID].Remove(button);
  253. }
  254. }
  255. }
  256. /// <summary> Updates the delta touch. </summary>
  257. internal void UpdateDeltaTouch()
  258. {
  259. if (m_LastTouchPos.Equals(Vector2.zero) || touchPos.Equals(Vector2.zero))
  260. {
  261. deltaTouch = Vector2.zero;
  262. }
  263. else
  264. {
  265. deltaTouch = touchPos - m_LastTouchPos;
  266. }
  267. m_LastTouchPos = touchPos;
  268. }
  269. /// <summary> Check button events. </summary>
  270. internal void CheckButtonEvents()
  271. {
  272. UpdateDeltaTouch();
  273. for (int i = (int)ControllerButton.BEGIN; i <= (int)ControllerButton.END; i <<= 1)
  274. {
  275. var button = (ControllerButton)i;
  276. if (GetButtonDown(button))
  277. {
  278. if (m_LastDownTimeDict.ContainsKey(button))
  279. {
  280. m_LastDownTimeDict[button] = Time.unscaledTime;
  281. }
  282. else
  283. {
  284. m_LastDownTimeDict.Add(button, Time.unscaledTime);
  285. }
  286. TryInvokeListener(ButtonEventType.Down, button);
  287. }
  288. if (GetButton(button))
  289. {
  290. TryInvokeListener(ButtonEventType.Pressing, button);
  291. }
  292. else if (GetButtonUp(button))
  293. {
  294. TryInvokeListener(ButtonEventType.Up, button);
  295. float lastDownTime;
  296. if (m_LastDownTimeDict.TryGetValue(button, out lastDownTime) && Time.unscaledTime - lastDownTime < NRInput.ClickInterval)
  297. {
  298. TryInvokeListener(ButtonEventType.Click, button);
  299. }
  300. }
  301. }
  302. }
  303. /// <summary> Try invoke listener. </summary>
  304. /// <param name="buttonEventType"> Type of the button event.</param>
  305. /// <param name="button"> The button.</param>
  306. private void TryInvokeListener(ButtonEventType buttonEventType, ControllerButton button)
  307. {
  308. int buttonEventID = (int)buttonEventType;
  309. if (m_ListenersArr[buttonEventID] == null)
  310. return;
  311. if (m_ListenersArr[buttonEventID].ContainsKey(button) && m_ListenersArr[buttonEventID][button] != null)
  312. {
  313. m_ListenersArr[buttonEventID][button].Invoke();
  314. }
  315. }
  316. }
  317. }