SteamVR_TrackedController.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using Valve.VR;
  4. public struct ClickedEventArgs
  5. {
  6. public uint controllerIndex;
  7. public uint flags;
  8. public float padX, padY;
  9. }
  10. public delegate void ClickedEventHandler(object sender, ClickedEventArgs e);
  11. public class SteamVR_TrackedController : MonoBehaviour
  12. {
  13. public uint controllerIndex;
  14. public VRControllerState_t controllerState;
  15. public bool triggerPressed = false;
  16. public bool steamPressed = false;
  17. public bool menuPressed = false;
  18. public bool padPressed = false;
  19. public bool padTouched = false;
  20. public bool gripped = false;
  21. public event ClickedEventHandler MenuButtonClicked;
  22. public event ClickedEventHandler MenuButtonUnclicked;
  23. public event ClickedEventHandler TriggerClicked;
  24. public event ClickedEventHandler TriggerUnclicked;
  25. public event ClickedEventHandler SteamClicked;
  26. public event ClickedEventHandler PadClicked;
  27. public event ClickedEventHandler PadUnclicked;
  28. public event ClickedEventHandler PadTouched;
  29. public event ClickedEventHandler PadUntouched;
  30. public event ClickedEventHandler Gripped;
  31. public event ClickedEventHandler Ungripped;
  32. // Use this for initialization
  33. protected virtual void Start()
  34. {
  35. if (this.GetComponent<SteamVR_TrackedObject>() == null)
  36. {
  37. gameObject.AddComponent<SteamVR_TrackedObject>();
  38. }
  39. if (controllerIndex != 0)
  40. {
  41. this.GetComponent<SteamVR_TrackedObject>().index = (SteamVR_TrackedObject.EIndex)controllerIndex;
  42. if (this.GetComponent<SteamVR_RenderModel>() != null)
  43. {
  44. this.GetComponent<SteamVR_RenderModel>().index = (SteamVR_TrackedObject.EIndex)controllerIndex;
  45. }
  46. }
  47. else
  48. {
  49. controllerIndex = (uint)this.GetComponent<SteamVR_TrackedObject>().index;
  50. }
  51. }
  52. public void SetDeviceIndex(int index)
  53. {
  54. this.controllerIndex = (uint)index;
  55. }
  56. public virtual void OnTriggerClicked(ClickedEventArgs e)
  57. {
  58. if (TriggerClicked != null)
  59. TriggerClicked(this, e);
  60. }
  61. public virtual void OnTriggerUnclicked(ClickedEventArgs e)
  62. {
  63. if (TriggerUnclicked != null)
  64. TriggerUnclicked(this, e);
  65. }
  66. public virtual void OnMenuClicked(ClickedEventArgs e)
  67. {
  68. if (MenuButtonClicked != null)
  69. MenuButtonClicked(this, e);
  70. }
  71. public virtual void OnMenuUnclicked(ClickedEventArgs e)
  72. {
  73. if (MenuButtonUnclicked != null)
  74. MenuButtonUnclicked(this, e);
  75. }
  76. public virtual void OnSteamClicked(ClickedEventArgs e)
  77. {
  78. if (SteamClicked != null)
  79. SteamClicked(this, e);
  80. }
  81. public virtual void OnPadClicked(ClickedEventArgs e)
  82. {
  83. if (PadClicked != null)
  84. PadClicked(this, e);
  85. }
  86. public virtual void OnPadUnclicked(ClickedEventArgs e)
  87. {
  88. if (PadUnclicked != null)
  89. PadUnclicked(this, e);
  90. }
  91. public virtual void OnPadTouched(ClickedEventArgs e)
  92. {
  93. if (PadTouched != null)
  94. PadTouched(this, e);
  95. }
  96. public virtual void OnPadUntouched(ClickedEventArgs e)
  97. {
  98. if (PadUntouched != null)
  99. PadUntouched(this, e);
  100. }
  101. public virtual void OnGripped(ClickedEventArgs e)
  102. {
  103. if (Gripped != null)
  104. Gripped(this, e);
  105. }
  106. public virtual void OnUngripped(ClickedEventArgs e)
  107. {
  108. if (Ungripped != null)
  109. Ungripped(this, e);
  110. }
  111. // Update is called once per frame
  112. protected virtual void Update()
  113. {
  114. var system = OpenVR.System;
  115. if (system != null && system.GetControllerState(controllerIndex, ref controllerState, (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VRControllerState_t))))
  116. {
  117. ulong trigger = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_SteamVR_Trigger));
  118. if (trigger > 0L && !triggerPressed)
  119. {
  120. triggerPressed = true;
  121. ClickedEventArgs e;
  122. e.controllerIndex = controllerIndex;
  123. e.flags = (uint)controllerState.ulButtonPressed;
  124. e.padX = controllerState.rAxis0.x;
  125. e.padY = controllerState.rAxis0.y;
  126. OnTriggerClicked(e);
  127. }
  128. else if (trigger == 0L && triggerPressed)
  129. {
  130. triggerPressed = false;
  131. ClickedEventArgs e;
  132. e.controllerIndex = controllerIndex;
  133. e.flags = (uint)controllerState.ulButtonPressed;
  134. e.padX = controllerState.rAxis0.x;
  135. e.padY = controllerState.rAxis0.y;
  136. OnTriggerUnclicked(e);
  137. }
  138. ulong grip = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_Grip));
  139. if (grip > 0L && !gripped)
  140. {
  141. gripped = true;
  142. ClickedEventArgs e;
  143. e.controllerIndex = controllerIndex;
  144. e.flags = (uint)controllerState.ulButtonPressed;
  145. e.padX = controllerState.rAxis0.x;
  146. e.padY = controllerState.rAxis0.y;
  147. OnGripped(e);
  148. }
  149. else if (grip == 0L && gripped)
  150. {
  151. gripped = false;
  152. ClickedEventArgs e;
  153. e.controllerIndex = controllerIndex;
  154. e.flags = (uint)controllerState.ulButtonPressed;
  155. e.padX = controllerState.rAxis0.x;
  156. e.padY = controllerState.rAxis0.y;
  157. OnUngripped(e);
  158. }
  159. ulong pad = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_SteamVR_Touchpad));
  160. if (pad > 0L && !padPressed)
  161. {
  162. padPressed = true;
  163. ClickedEventArgs e;
  164. e.controllerIndex = controllerIndex;
  165. e.flags = (uint)controllerState.ulButtonPressed;
  166. e.padX = controllerState.rAxis0.x;
  167. e.padY = controllerState.rAxis0.y;
  168. OnPadClicked(e);
  169. }
  170. else if (pad == 0L && padPressed)
  171. {
  172. padPressed = false;
  173. ClickedEventArgs e;
  174. e.controllerIndex = controllerIndex;
  175. e.flags = (uint)controllerState.ulButtonPressed;
  176. e.padX = controllerState.rAxis0.x;
  177. e.padY = controllerState.rAxis0.y;
  178. OnPadUnclicked(e);
  179. }
  180. ulong menu = controllerState.ulButtonPressed & (1UL << ((int)EVRButtonId.k_EButton_ApplicationMenu));
  181. if (menu > 0L && !menuPressed)
  182. {
  183. menuPressed = true;
  184. ClickedEventArgs e;
  185. e.controllerIndex = controllerIndex;
  186. e.flags = (uint)controllerState.ulButtonPressed;
  187. e.padX = controllerState.rAxis0.x;
  188. e.padY = controllerState.rAxis0.y;
  189. OnMenuClicked(e);
  190. }
  191. else if (menu == 0L && menuPressed)
  192. {
  193. menuPressed = false;
  194. ClickedEventArgs e;
  195. e.controllerIndex = controllerIndex;
  196. e.flags = (uint)controllerState.ulButtonPressed;
  197. e.padX = controllerState.rAxis0.x;
  198. e.padY = controllerState.rAxis0.y;
  199. OnMenuUnclicked(e);
  200. }
  201. pad = controllerState.ulButtonTouched & (1UL << ((int)EVRButtonId.k_EButton_SteamVR_Touchpad));
  202. if (pad > 0L && !padTouched)
  203. {
  204. padTouched = true;
  205. ClickedEventArgs e;
  206. e.controllerIndex = controllerIndex;
  207. e.flags = (uint)controllerState.ulButtonPressed;
  208. e.padX = controllerState.rAxis0.x;
  209. e.padY = controllerState.rAxis0.y;
  210. OnPadTouched(e);
  211. }
  212. else if (pad == 0L && padTouched)
  213. {
  214. padTouched = false;
  215. ClickedEventArgs e;
  216. e.controllerIndex = controllerIndex;
  217. e.flags = (uint)controllerState.ulButtonPressed;
  218. e.padX = controllerState.rAxis0.x;
  219. e.padY = controllerState.rAxis0.y;
  220. OnPadUntouched(e);
  221. }
  222. }
  223. }
  224. }