2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
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.

846 lines
38 KiB

5 years ago
  1. //========= Copyright 2016-2020, HTC Corporation. All rights reserved. ===========
  2. using HTC.UnityPlugin.Utility;
  3. using HTC.UnityPlugin.VRModuleManagement;
  4. using System;
  5. using UnityEngine;
  6. namespace HTC.UnityPlugin.Vive
  7. {
  8. /// <summary>
  9. /// To provide static APIs to retrieve controller's button status
  10. /// </summary>
  11. [DisallowMultipleComponent]
  12. public partial class ViveInput : SingletonBehaviour<ViveInput>
  13. {
  14. #region origin
  15. /// <summary>
  16. /// Returns true while the button on the controller identified by role is held down
  17. /// </summary>
  18. public static bool GetPress(HandRole role, ControllerButton button)
  19. {
  20. return GetPressEx(role, button);
  21. }
  22. /// <summary>
  23. /// Returns true during the frame the user pressed down the button on the controller identified by role
  24. /// </summary>
  25. public static bool GetPressDown(HandRole role, ControllerButton button)
  26. {
  27. return GetPressDownEx(role, button);
  28. }
  29. /// <summary>
  30. /// Returns true during the frame the user releases the button on the controller identified by role
  31. /// </summary>
  32. public static bool GetPressUp(HandRole role, ControllerButton button)
  33. {
  34. return GetPressUpEx(role, button);
  35. }
  36. /// <summary>
  37. /// Returns time of the last frame that user pressed down the button on the controller identified by role
  38. /// </summary>
  39. public static float LastPressDownTime(HandRole role, ControllerButton button)
  40. {
  41. return LastPressDownTimeEx(role, button);
  42. }
  43. /// <summary>
  44. /// Return amount of clicks in a row for the button on the controller identified by role
  45. /// Set ViveInput.clickInterval to configure click interval
  46. /// </summary>
  47. public static int ClickCount(HandRole role, ControllerButton button)
  48. {
  49. return ClickCountEx(role, button);
  50. }
  51. public static float GetAxis(HandRole role, ControllerAxis axis, bool usePrevState = false)
  52. {
  53. return GetAxisEx(role, axis, usePrevState);
  54. }
  55. /// <summary>
  56. /// Returns raw analog value of the trigger button on the controller identified by role
  57. /// </summary>
  58. public static float GetTriggerValue(HandRole role, bool usePrevState = false)
  59. {
  60. return GetTriggerValueEx(role, usePrevState);
  61. }
  62. /// <summary>
  63. /// Returns raw analog value of the touch pad on the controller identified by role
  64. /// </summary>
  65. public static Vector2 GetPadAxis(HandRole role, bool usePrevState = false)
  66. {
  67. return GetPadAxisEx(role, usePrevState);
  68. }
  69. /// <summary>
  70. /// Returns raw analog value of the touch pad on the controller identified by role if pressed,
  71. /// otherwise, returns Vector2.zero
  72. /// </summary>
  73. public static Vector2 GetPadPressAxis(HandRole role)
  74. {
  75. return GetPadPressAxisEx(role);
  76. }
  77. /// <summary>
  78. /// Returns raw analog value of the touch pad on the controller identified by role if touched,
  79. /// otherwise, returns Vector2.zero
  80. /// </summary>
  81. public static Vector2 GetPadTouchAxis(HandRole role)
  82. {
  83. return GetPadTouchAxisEx(role);
  84. }
  85. public static Vector2 GetPadPressVector(HandRole role)
  86. {
  87. return GetPadPressVectorEx(role);
  88. }
  89. public static Vector2 GetPadTouchVector(HandRole role)
  90. {
  91. return GetPadTouchVectorEx(role);
  92. }
  93. public static Vector2 GetPadPressDelta(HandRole role)
  94. {
  95. return GetPadPressDeltaEx(role);
  96. }
  97. public static Vector2 GetPadTouchDelta(HandRole role)
  98. {
  99. return GetPadTouchDeltaEx(role);
  100. }
  101. public static Vector2 GetScrollDelta(HandRole role, ScrollType scrollType, Vector2 scale, ControllerAxis xAxis = ControllerAxis.PadX, ControllerAxis yAxis = ControllerAxis.PadY)
  102. {
  103. return GetScrollDeltaEx(role, scrollType, scale, xAxis, yAxis);
  104. }
  105. /// <summary>
  106. /// Add press handler for the button on the controller identified by role
  107. /// </summary>
  108. public static void AddPress(HandRole role, ControllerButton button, Action callback)
  109. {
  110. AddListenerEx(role, button, ButtonEventType.Press, callback);
  111. }
  112. /// <summary>
  113. /// Add press down handler for the button on the controller identified by role
  114. /// </summary>
  115. public static void AddPressDown(HandRole role, ControllerButton button, Action callback)
  116. {
  117. AddListenerEx(role, button, ButtonEventType.Down, callback);
  118. }
  119. /// <summary>
  120. /// Add press up handler for the button on the controller identified by role
  121. /// </summary>
  122. public static void AddPressUp(HandRole role, ControllerButton button, Action callback)
  123. {
  124. AddListenerEx(role, button, ButtonEventType.Up, callback);
  125. }
  126. /// <summary>
  127. /// Add click handler for the button on the controller identified by role
  128. /// Use ViveInput.ClickCount to get click count
  129. /// </summary>
  130. public static void AddClick(HandRole role, ControllerButton button, Action callback)
  131. {
  132. AddListenerEx(role, button, ButtonEventType.Click, callback);
  133. }
  134. /// <summary>
  135. /// Remove press handler for the button on the controller identified by role
  136. /// </summary>
  137. public static void RemovePress(HandRole role, ControllerButton button, Action callback)
  138. {
  139. RemoveListenerEx(role, button, ButtonEventType.Press, callback);
  140. }
  141. /// <summary>
  142. /// Remove press down handler for the button on the controller identified by role
  143. /// </summary>
  144. public static void RemovePressDown(HandRole role, ControllerButton button, Action callback)
  145. {
  146. RemoveListenerEx(role, button, ButtonEventType.Down, callback);
  147. }
  148. /// <summary>
  149. /// Remove press up handler for the button on the controller identified by role
  150. /// </summary>
  151. public static void RemovePressUp(HandRole role, ControllerButton button, Action callback)
  152. {
  153. RemoveListenerEx(role, button, ButtonEventType.Up, callback);
  154. }
  155. /// <summary>
  156. /// Remove click handler for the button on the controller identified by role
  157. /// </summary>
  158. public static void RemoveClick(HandRole role, ControllerButton button, Action callback)
  159. {
  160. RemoveListenerEx(role, button, ButtonEventType.Click, callback);
  161. }
  162. /// <summary>
  163. /// Trigger vibration of the controller identified by role
  164. /// </summary>
  165. public static void TriggerHapticPulse(HandRole role, ushort durationMicroSec = 500)
  166. {
  167. TriggerHapticPulseEx(role, durationMicroSec);
  168. }
  169. /// <summary>
  170. /// Trigger vibration of the controller identified by role
  171. /// </summary>
  172. public static void TriggerHapticVibration(HandRole role, float durationSeconds = 0.01f, float frequency = 85f, float amplitude = 0.125f, float startSecondsFromNow = 0f)
  173. {
  174. TriggerHapticVibrationEx(role, durationSeconds, frequency, amplitude, startSecondsFromNow);
  175. }
  176. #endregion origin
  177. #region general role property
  178. /// <summary>
  179. /// Returns true while the button on the controller identified by role is held down
  180. /// </summary>
  181. public static bool GetPress(ViveRoleProperty role, ControllerButton button)
  182. {
  183. return GetPressEx(role.roleType, role.roleValue, button);
  184. }
  185. /// <summary>
  186. /// Returns true during the frame the user pressed down the button on the controller identified by role
  187. /// </summary>
  188. public static bool GetPressDown(ViveRoleProperty role, ControllerButton button)
  189. {
  190. return GetPressDownEx(role.roleType, role.roleValue, button);
  191. }
  192. /// <summary>
  193. /// Returns true during the frame the user releases the button on the controller identified by role
  194. /// </summary>
  195. public static bool GetPressUp(ViveRoleProperty role, ControllerButton button)
  196. {
  197. return GetPressUpEx(role.roleType, role.roleValue, button);
  198. }
  199. /// <summary>
  200. /// Returns time of the last frame that user pressed down the button on the controller identified by role
  201. /// </summary>
  202. public static float LastPressDownTime(ViveRoleProperty role, ControllerButton button)
  203. {
  204. return LastPressDownTimeEx(role.roleType, role.roleValue, button);
  205. }
  206. /// <summary>
  207. /// Return amount of clicks in a row for the button on the controller identified by role
  208. /// Set ViveInput.clickInterval to configure click interval
  209. /// </summary>
  210. public static int ClickCount(ViveRoleProperty role, ControllerButton button)
  211. {
  212. return ClickCountEx(role.roleType, role.roleValue, button);
  213. }
  214. public static float GetAxis(ViveRoleProperty role, ControllerAxis axis, bool usePrevState = false)
  215. {
  216. return GetAxisEx(role.roleType, role.roleValue, axis, usePrevState);
  217. }
  218. /// <summary>
  219. /// Returns raw analog value of the trigger button on the controller identified by role
  220. /// </summary>
  221. public static float GetTriggerValue(ViveRoleProperty role, bool usePrevState = false)
  222. {
  223. return GetTriggerValueEx(role.roleType, role.roleValue, usePrevState);
  224. }
  225. /// <summary>
  226. /// Returns raw analog value of the touch pad on the controller identified by role
  227. /// </summary>
  228. public static Vector2 GetPadAxis(ViveRoleProperty role, bool usePrevState = false)
  229. {
  230. return GetPadAxisEx(role.roleType, role.roleValue, usePrevState);
  231. }
  232. /// <summary>
  233. /// Returns raw analog value of the touch pad on the controller identified by role if pressed,
  234. /// otherwise, returns Vector2.zero
  235. /// </summary>
  236. public static Vector2 GetPadPressAxis(ViveRoleProperty role)
  237. {
  238. return GetPadPressAxisEx(role.roleType, role.roleValue);
  239. }
  240. /// <summary>
  241. /// Returns raw analog value of the touch pad on the controller identified by role if touched,
  242. /// otherwise, returns Vector2.zero
  243. /// </summary>
  244. public static Vector2 GetPadTouchAxis(ViveRoleProperty role)
  245. {
  246. return GetPadTouchAxisEx(role.roleType, role.roleValue);
  247. }
  248. public static Vector2 GetPadPressVector(ViveRoleProperty role)
  249. {
  250. return GetPadPressVectorEx(role.roleType, role.roleValue);
  251. }
  252. public static Vector2 GetPadTouchVector(ViveRoleProperty role)
  253. {
  254. return GetPadTouchVectorEx(role.roleType, role.roleValue);
  255. }
  256. public static Vector2 GetPadPressDelta(ViveRoleProperty role)
  257. {
  258. return GetPadPressDeltaEx(role.roleType, role.roleValue);
  259. }
  260. public static Vector2 GetPadTouchDelta(ViveRoleProperty role)
  261. {
  262. return GetPadTouchDeltaEx(role.roleType, role.roleValue);
  263. }
  264. public static Vector2 GetScrollDelta(ViveRoleProperty role, ScrollType scrollType, Vector2 scale, ControllerAxis xAxis = ControllerAxis.PadX, ControllerAxis yAxis = ControllerAxis.PadY)
  265. {
  266. return GetScrollDeltaEx(role.roleType, role.roleValue, scrollType, scale, xAxis, yAxis);
  267. }
  268. public static void AddListener(ViveRoleProperty role, ControllerButton button, ButtonEventType eventType, Action callback)
  269. {
  270. AddListenerEx(role.roleType, role.roleValue, button, eventType, callback);
  271. }
  272. public static void RemoveListener(ViveRoleProperty role, ControllerButton button, ButtonEventType eventType, Action callback)
  273. {
  274. RemoveListenerEx(role.roleType, role.roleValue, button, eventType, callback);
  275. }
  276. public static void AddListener(ViveRoleProperty role, ControllerButton button, ButtonEventType eventType, RoleValueEventListener callback)
  277. {
  278. AddListenerEx(role.roleType, role.roleValue, button, eventType, callback);
  279. }
  280. public static void RemoveListener(ViveRoleProperty role, ControllerButton button, ButtonEventType eventType, RoleValueEventListener callback)
  281. {
  282. RemoveListenerEx(role.roleType, role.roleValue, button, eventType, callback);
  283. }
  284. /// <summary>
  285. /// Trigger vibration of the controller identified by role
  286. /// </summary>
  287. public static void TriggerHapticPulse(ViveRoleProperty role, ushort durationMicroSec = 500)
  288. {
  289. TriggerHapticPulseEx(role.roleType, role.roleValue, durationMicroSec);
  290. }
  291. /// <summary>
  292. /// Trigger vibration of the controller identified by role
  293. /// </summary>
  294. public static void TriggerHapticVibration(ViveRoleProperty role, float durationSeconds = 0.01f, float frequency = 85f, float amplitude = 0.125f, float startSecondsFromNow = 0f)
  295. {
  296. TriggerHapticVibrationEx(role.roleType, role.roleValue, durationSeconds, frequency, amplitude, startSecondsFromNow);
  297. }
  298. #endregion
  299. #region extend generic role
  300. /// <typeparam name="TRole">
  301. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  302. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  303. /// </typeparam>
  304. /// <param name="role">
  305. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  306. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  307. /// </param>
  308. public static bool GetPressEx<TRole>(TRole role, ControllerButton button)
  309. {
  310. return GetState(role).GetPress(button);
  311. }
  312. /// <typeparam name="TRole">
  313. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  314. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  315. /// </typeparam>
  316. /// <param name="role">
  317. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  318. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  319. /// </param>
  320. public static bool GetPressDownEx<TRole>(TRole role, ControllerButton button)
  321. {
  322. return GetState(role).GetPressDown(button);
  323. }
  324. /// <typeparam name="TRole">
  325. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  326. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  327. /// </typeparam>
  328. /// <param name="role">
  329. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  330. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  331. /// </param>
  332. public static bool GetPressUpEx<TRole>(TRole role, ControllerButton button)
  333. {
  334. return GetState(role).GetPressUp(button);
  335. }
  336. /// <typeparam name="TRole">
  337. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  338. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  339. /// </typeparam>
  340. /// <param name="role">
  341. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  342. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  343. /// </param>
  344. public static float LastPressDownTimeEx<TRole>(TRole role, ControllerButton button)
  345. {
  346. return GetState(role).LastPressDownTime(button);
  347. }
  348. /// <typeparam name="TRole">
  349. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  350. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  351. /// </typeparam>
  352. /// <param name="role">
  353. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  354. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  355. /// </param>
  356. public static int ClickCountEx<TRole>(TRole role, ControllerButton button)
  357. {
  358. return GetState(role).ClickCount(button);
  359. }
  360. public static float GetAxisEx<TRole>(TRole role, ControllerAxis axis, bool usePrevState = false)
  361. {
  362. return GetState(role).GetAxis(axis, usePrevState);
  363. }
  364. /// <typeparam name="TRole">
  365. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  366. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  367. /// </typeparam>
  368. /// <param name="role">
  369. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  370. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  371. /// </param>
  372. public static float GetTriggerValueEx<TRole>(TRole role, bool usePrevState = false)
  373. {
  374. return GetState(role).GetAxis(ControllerAxis.Trigger, usePrevState);
  375. }
  376. /// <typeparam name="TRole">
  377. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  378. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  379. /// </typeparam>
  380. /// <param name="role">
  381. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  382. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  383. /// </param>
  384. public static Vector2 GetPadAxisEx<TRole>(TRole role, bool usePrevState = false)
  385. {
  386. return GetState(role).GetPadAxis(usePrevState);
  387. }
  388. /// <typeparam name="TRole">
  389. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  390. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  391. /// </typeparam>
  392. /// <param name="role">
  393. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  394. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  395. /// </param>
  396. public static Vector2 GetPadPressAxisEx<TRole>(TRole role)
  397. {
  398. var handState = GetState(role);
  399. return handState.GetPress(ControllerButton.Pad) ? handState.GetPadAxis() : Vector2.zero;
  400. }
  401. /// <typeparam name="TRole">
  402. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  403. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  404. /// </typeparam>
  405. /// <param name="role">
  406. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  407. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  408. /// </param>
  409. public static Vector2 GetPadTouchAxisEx<TRole>(TRole role)
  410. {
  411. var handState = GetState(role);
  412. return handState.GetPress(ControllerButton.PadTouch) ? handState.GetPadAxis() : Vector2.zero;
  413. }
  414. /// <typeparam name="TRole">
  415. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  416. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  417. /// </typeparam>
  418. /// <param name="role">
  419. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  420. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  421. /// </param>
  422. public static Vector2 GetPadPressVectorEx<TRole>(TRole role)
  423. {
  424. return GetState(role).GetPadPressVector();
  425. }
  426. /// <typeparam name="TRole">
  427. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  428. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  429. /// </typeparam>
  430. /// <param name="role">
  431. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  432. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  433. /// </param>
  434. public static Vector2 GetPadTouchVectorEx<TRole>(TRole role)
  435. {
  436. return GetState(role).GetPadTouchVector();
  437. }
  438. /// <typeparam name="TRole">
  439. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  440. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  441. /// </typeparam>
  442. /// <param name="role">
  443. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  444. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  445. /// </param>
  446. public static Vector2 GetPadPressDeltaEx<TRole>(TRole role)
  447. {
  448. var handState = GetState(role);
  449. if (handState.GetPress(ControllerButton.Pad) && !handState.GetPressDown(ControllerButton.Pad))
  450. {
  451. return handState.GetPadAxis() - handState.GetPadAxis(true);
  452. }
  453. return Vector2.zero;
  454. }
  455. /// <typeparam name="TRole">
  456. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  457. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  458. /// </typeparam>
  459. /// <param name="role">
  460. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  461. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  462. /// </param>
  463. public static Vector2 GetPadTouchDeltaEx<TRole>(TRole role)
  464. {
  465. var handState = GetState(role);
  466. if (handState.GetPress(ControllerButton.PadTouch) && !handState.GetPressDown(ControllerButton.PadTouch))
  467. {
  468. return handState.GetPadAxis() - handState.GetPadAxis(true);
  469. }
  470. return Vector2.zero;
  471. }
  472. /// <typeparam name="TRole">
  473. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  474. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  475. /// </typeparam>
  476. /// <param name="role">
  477. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  478. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  479. /// </param>
  480. public static Vector2 GetScrollDeltaEx<TRole>(TRole role, ScrollType scrollType, Vector2 scale, ControllerAxis xAxis = ControllerAxis.PadX, ControllerAxis yAxis = ControllerAxis.PadY)
  481. {
  482. return GetState(role).GetScrollDelta(scrollType, scale, xAxis, yAxis);
  483. }
  484. /// <typeparam name="TRole">
  485. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  486. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  487. /// </typeparam>
  488. /// <param name="role">
  489. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  490. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  491. /// </param>
  492. public static void AddListenerEx<TRole>(TRole role, ControllerButton button, ButtonEventType eventType, Action callback)
  493. {
  494. GetState(role).AddListener(button, callback, eventType);
  495. }
  496. /// <typeparam name="TRole">
  497. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  498. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  499. /// </typeparam>
  500. /// <param name="role">
  501. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  502. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  503. /// </param>
  504. public static void RemoveListenerEx<TRole>(TRole role, ControllerButton button, ButtonEventType eventType, Action callback)
  505. {
  506. GetState(role).RemoveListener(button, callback, eventType);
  507. }
  508. /// <typeparam name="TRole">
  509. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  510. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  511. /// </typeparam>
  512. /// <param name="role">
  513. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  514. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  515. /// </param>
  516. public static void AddListenerEx<TRole>(TRole role, ControllerButton button, ButtonEventType eventType, RoleValueEventListener callback)
  517. {
  518. GetState(role).AddListener(button, callback, eventType);
  519. }
  520. /// <typeparam name="TRole">
  521. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  522. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  523. /// </typeparam>
  524. /// <param name="role">
  525. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  526. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  527. /// </param>
  528. public static void RemoveListenerEx<TRole>(TRole role, ControllerButton button, ButtonEventType eventType, RoleValueEventListener callback)
  529. {
  530. GetState(role).RemoveListener(button, callback, eventType);
  531. }
  532. /// <typeparam name="TRole">
  533. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  534. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  535. /// </typeparam>
  536. /// <param name="role">
  537. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  538. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  539. /// </param>
  540. public static void AddListenerEx<TRole>(TRole role, ControllerButton button, ButtonEventType eventType, RoleEventListener<TRole> callback)
  541. {
  542. GetState(role).AddListener(button, callback, eventType);
  543. }
  544. /// <typeparam name="TRole">
  545. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  546. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  547. /// </typeparam>
  548. /// <param name="role">
  549. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  550. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  551. /// </param>
  552. public static void RemoveListenerEx<TRole>(TRole role, ControllerButton button, ButtonEventType eventType, RoleEventListener<TRole> callback)
  553. {
  554. GetState(role).RemoveListener(button, callback, eventType);
  555. }
  556. /// <typeparam name="TRole">
  557. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  558. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  559. /// </typeparam>
  560. /// <param name="role">
  561. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  562. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  563. /// </param>
  564. public static void TriggerHapticPulseEx<TRole>(TRole role, ushort durationMicroSec = 500)
  565. {
  566. VRModule.TriggerViveControllerHaptic(ViveRole.GetDeviceIndexEx(role), durationMicroSec);
  567. }
  568. /// <typeparam name="TRole">
  569. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  570. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  571. /// </typeparam>
  572. /// <param name="role">
  573. /// TRole can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  574. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  575. /// </param>
  576. public static void TriggerHapticVibrationEx<TRole>(TRole role, float durationSeconds = 0.01f, float frequency = 85f, float amplitude = 0.125f, float startSecondsFromNow = 0f)
  577. {
  578. VRModule.TriggerHapticVibration(ViveRole.GetDeviceIndexEx(role), durationSeconds, frequency, amplitude, startSecondsFromNow);
  579. }
  580. #endregion extend generic
  581. #region extend property role type & value
  582. /// <param name="roleType">
  583. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  584. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  585. /// </param>
  586. public static bool GetPressEx(Type roleType, int roleValue, ControllerButton button)
  587. {
  588. return GetState(roleType, roleValue).GetPress(button);
  589. }
  590. /// <param name="roleType">
  591. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  592. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  593. /// </param>
  594. public static bool GetPressDownEx(Type roleType, int roleValue, ControllerButton button)
  595. {
  596. return GetState(roleType, roleValue).GetPressDown(button);
  597. }
  598. /// <param name="roleType">
  599. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  600. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  601. /// </param>
  602. public static bool GetPressUpEx(Type roleType, int roleValue, ControllerButton button)
  603. {
  604. return GetState(roleType, roleValue).GetPressUp(button);
  605. }
  606. /// <param name="roleType">
  607. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  608. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  609. /// </param>
  610. public static float LastPressDownTimeEx(Type roleType, int roleValue, ControllerButton button)
  611. {
  612. return GetState(roleType, roleValue).LastPressDownTime(button);
  613. }
  614. /// <param name="roleType">
  615. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  616. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  617. /// </param>
  618. public static int ClickCountEx(Type roleType, int roleValue, ControllerButton button)
  619. {
  620. return GetState(roleType, roleValue).ClickCount(button);
  621. }
  622. public static float GetAxisEx(Type roleType, int roleValue, ControllerAxis axis, bool usePrevState = false)
  623. {
  624. return GetState(roleType, roleValue).GetAxis(axis, usePrevState);
  625. }
  626. /// <param name="roleType">
  627. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  628. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  629. /// </param>
  630. public static float GetTriggerValueEx(Type roleType, int roleValue, bool usePrevState = false)
  631. {
  632. return GetState(roleType, roleValue).GetAxis(ControllerAxis.Trigger, usePrevState);
  633. }
  634. /// <param name="roleType">
  635. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  636. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  637. /// </param>
  638. public static Vector2 GetPadAxisEx(Type roleType, int roleValue, bool usePrevState = false)
  639. {
  640. return GetState(roleType, roleValue).GetPadAxis(usePrevState);
  641. }
  642. /// <param name="roleType">
  643. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  644. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  645. /// </param>
  646. public static Vector2 GetPadPressAxisEx(Type roleType, int roleValue)
  647. {
  648. var handState = GetState(roleType, roleValue);
  649. return handState.GetPress(ControllerButton.Pad) ? handState.GetPadAxis() : Vector2.zero;
  650. }
  651. /// <param name="roleType">
  652. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  653. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  654. /// </param>
  655. public static Vector2 GetPadTouchAxisEx(Type roleType, int roleValue)
  656. {
  657. var handState = GetState(roleType, roleValue);
  658. return handState.GetPress(ControllerButton.PadTouch) ? handState.GetPadAxis() : Vector2.zero;
  659. }
  660. /// <param name="roleType">
  661. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  662. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  663. /// </param>
  664. public static Vector2 GetPadPressVectorEx(Type roleType, int roleValue)
  665. {
  666. return GetState(roleType, roleValue).GetPadPressVector();
  667. }
  668. /// <param name="roleType">
  669. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  670. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  671. /// </param>
  672. public static Vector2 GetPadTouchVectorEx(Type roleType, int roleValue)
  673. {
  674. return GetState(roleType, roleValue).GetPadTouchVector();
  675. }
  676. /// <param name="roleType">
  677. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  678. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  679. /// </param>
  680. public static Vector2 GetPadPressDeltaEx(Type roleType, int roleValue)
  681. {
  682. var handState = GetState(roleType, roleValue);
  683. if (handState.GetPress(ControllerButton.Pad) && !handState.GetPressDown(ControllerButton.Pad))
  684. {
  685. return handState.GetPadAxis() - handState.GetPadAxis(true);
  686. }
  687. return Vector2.zero;
  688. }
  689. /// <param name="roleType">
  690. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  691. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  692. /// </param>
  693. public static Vector2 GetPadTouchDeltaEx(Type roleType, int roleValue)
  694. {
  695. var handState = GetState(roleType, roleValue);
  696. if (handState.GetPress(ControllerButton.PadTouch) && !handState.GetPressDown(ControllerButton.PadTouch))
  697. {
  698. return handState.GetPadAxis() - handState.GetPadAxis(true);
  699. }
  700. return Vector2.zero;
  701. }
  702. public static Vector2 GetScrollDeltaEx(Type roleType, int roleValue, ScrollType scrollType, Vector2 scale, ControllerAxis xAxis = ControllerAxis.PadX, ControllerAxis yAxis = ControllerAxis.PadY)
  703. {
  704. return GetState(roleType, roleValue).GetScrollDelta(scrollType, scale, xAxis, yAxis);
  705. }
  706. /// <param name="roleType">
  707. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  708. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  709. /// </param>
  710. public static void AddListenerEx(Type roleType, int roleValue, ControllerButton button, ButtonEventType eventType, Action callback)
  711. {
  712. GetState(roleType, roleValue).AddListener(button, callback, eventType);
  713. }
  714. /// <param name="roleType">
  715. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  716. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  717. /// </param>
  718. public static void RemoveListenerEx(Type roleType, int roleValue, ControllerButton button, ButtonEventType eventType, Action callback)
  719. {
  720. GetState(roleType, roleValue).RemoveListener(button, callback, eventType);
  721. }
  722. /// <param name="roleType">
  723. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  724. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  725. /// </param>
  726. public static void AddListenerEx(Type roleType, int roleValue, ControllerButton button, ButtonEventType eventType, RoleValueEventListener callback)
  727. {
  728. GetState(roleType, roleValue).AddListener(button, callback, eventType);
  729. }
  730. /// <param name="roleType">
  731. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  732. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  733. /// </param>
  734. public static void RemoveListenerEx(Type roleType, int roleValue, ControllerButton button, ButtonEventType eventType, RoleValueEventListener callback)
  735. {
  736. GetState(roleType, roleValue).RemoveListener(button, callback, eventType);
  737. }
  738. /// <param name="roleType">
  739. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  740. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  741. /// </param>
  742. public static void TriggerHapticPulseEx(Type roleType, int roleValue, ushort durationMicroSec = 500)
  743. {
  744. VRModule.TriggerViveControllerHaptic(ViveRole.GetDeviceIndexEx(roleType, roleValue), durationMicroSec);
  745. }
  746. /// <param name="roleType">
  747. /// Can be DeviceRole, TrackerRole or any other enum type that have ViveRoleEnumAttribute.
  748. /// Use ViveRole.ValidateViveRoleEnum() to validate role type
  749. /// </param>
  750. public static void TriggerHapticVibrationEx(Type roleType, int roleValue, float durationSeconds = 0.01f, float frequency = 85f, float amplitude = 0.125f, float startSecondsFromNow = 0f)
  751. {
  752. VRModule.TriggerHapticVibration(ViveRole.GetDeviceIndexEx(roleType, roleValue), durationSeconds, frequency, amplitude, startSecondsFromNow);
  753. }
  754. #endregion extend general
  755. }
  756. }