SteamVR_TrackedController.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System;
  2. using System.Diagnostics;
  3. using UnityEngine;
  4. using Valve.VR;
  5. public class SteamVR_TrackedController : MonoBehaviour
  6. {
  7. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  8. public event ClickedEventHandler MenuButtonClicked;
  9. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  10. public event ClickedEventHandler MenuButtonUnclicked;
  11. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  12. public event ClickedEventHandler TriggerClicked;
  13. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  14. public event ClickedEventHandler TriggerUnclicked;
  15. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  16. public event ClickedEventHandler SteamClicked;
  17. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  18. public event ClickedEventHandler PadClicked;
  19. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  20. public event ClickedEventHandler PadUnclicked;
  21. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  22. public event ClickedEventHandler PadTouched;
  23. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  24. public event ClickedEventHandler PadUntouched;
  25. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  26. public event ClickedEventHandler Gripped;
  27. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  28. public event ClickedEventHandler Ungripped;
  29. private void Start()
  30. {
  31. if (base.GetComponent<SteamVR_TrackedObject>() == null)
  32. {
  33. base.gameObject.AddComponent<SteamVR_TrackedObject>();
  34. }
  35. if (this.controllerIndex != 0u)
  36. {
  37. base.GetComponent<SteamVR_TrackedObject>().index = (SteamVR_TrackedObject.EIndex)this.controllerIndex;
  38. if (base.GetComponent<SteamVR_RenderModel>() != null)
  39. {
  40. base.GetComponent<SteamVR_RenderModel>().index = (SteamVR_TrackedObject.EIndex)this.controllerIndex;
  41. }
  42. }
  43. else
  44. {
  45. this.controllerIndex = (uint)base.GetComponent<SteamVR_TrackedObject>().index;
  46. }
  47. }
  48. public void SetDeviceIndex(int index)
  49. {
  50. this.controllerIndex = (uint)index;
  51. }
  52. public virtual void OnTriggerClicked(ClickedEventArgs e)
  53. {
  54. if (this.TriggerClicked != null)
  55. {
  56. this.TriggerClicked(this, e);
  57. }
  58. }
  59. public virtual void OnTriggerUnclicked(ClickedEventArgs e)
  60. {
  61. if (this.TriggerUnclicked != null)
  62. {
  63. this.TriggerUnclicked(this, e);
  64. }
  65. }
  66. public virtual void OnMenuClicked(ClickedEventArgs e)
  67. {
  68. if (this.MenuButtonClicked != null)
  69. {
  70. this.MenuButtonClicked(this, e);
  71. }
  72. }
  73. public virtual void OnMenuUnclicked(ClickedEventArgs e)
  74. {
  75. if (this.MenuButtonUnclicked != null)
  76. {
  77. this.MenuButtonUnclicked(this, e);
  78. }
  79. }
  80. public virtual void OnSteamClicked(ClickedEventArgs e)
  81. {
  82. if (this.SteamClicked != null)
  83. {
  84. this.SteamClicked(this, e);
  85. }
  86. }
  87. public virtual void OnPadClicked(ClickedEventArgs e)
  88. {
  89. if (this.PadClicked != null)
  90. {
  91. this.PadClicked(this, e);
  92. }
  93. }
  94. public virtual void OnPadUnclicked(ClickedEventArgs e)
  95. {
  96. if (this.PadUnclicked != null)
  97. {
  98. this.PadUnclicked(this, e);
  99. }
  100. }
  101. public virtual void OnPadTouched(ClickedEventArgs e)
  102. {
  103. if (this.PadTouched != null)
  104. {
  105. this.PadTouched(this, e);
  106. }
  107. }
  108. public virtual void OnPadUntouched(ClickedEventArgs e)
  109. {
  110. if (this.PadUntouched != null)
  111. {
  112. this.PadUntouched(this, e);
  113. }
  114. }
  115. public virtual void OnGripped(ClickedEventArgs e)
  116. {
  117. if (this.Gripped != null)
  118. {
  119. this.Gripped(this, e);
  120. }
  121. }
  122. public virtual void OnUngripped(ClickedEventArgs e)
  123. {
  124. if (this.Ungripped != null)
  125. {
  126. this.Ungripped(this, e);
  127. }
  128. }
  129. private void Update()
  130. {
  131. CVRSystem system = OpenVR.System;
  132. if (system != null && system.GetControllerState(this.controllerIndex, ref this.controllerState))
  133. {
  134. ulong num = this.controllerState.ulButtonPressed & 8589934592UL;
  135. if (num > 0UL && !this.triggerPressed)
  136. {
  137. this.triggerPressed = true;
  138. ClickedEventArgs e;
  139. e.controllerIndex = this.controllerIndex;
  140. e.flags = (uint)this.controllerState.ulButtonPressed;
  141. e.padX = this.controllerState.rAxis0.x;
  142. e.padY = this.controllerState.rAxis0.y;
  143. this.OnTriggerClicked(e);
  144. }
  145. else if (num == 0UL && this.triggerPressed)
  146. {
  147. this.triggerPressed = false;
  148. ClickedEventArgs e2;
  149. e2.controllerIndex = this.controllerIndex;
  150. e2.flags = (uint)this.controllerState.ulButtonPressed;
  151. e2.padX = this.controllerState.rAxis0.x;
  152. e2.padY = this.controllerState.rAxis0.y;
  153. this.OnTriggerUnclicked(e2);
  154. }
  155. ulong num2 = this.controllerState.ulButtonPressed & 4UL;
  156. if (num2 > 0UL && !this.gripped)
  157. {
  158. this.gripped = true;
  159. ClickedEventArgs e3;
  160. e3.controllerIndex = this.controllerIndex;
  161. e3.flags = (uint)this.controllerState.ulButtonPressed;
  162. e3.padX = this.controllerState.rAxis0.x;
  163. e3.padY = this.controllerState.rAxis0.y;
  164. this.OnGripped(e3);
  165. }
  166. else if (num2 == 0UL && this.gripped)
  167. {
  168. this.gripped = false;
  169. ClickedEventArgs e4;
  170. e4.controllerIndex = this.controllerIndex;
  171. e4.flags = (uint)this.controllerState.ulButtonPressed;
  172. e4.padX = this.controllerState.rAxis0.x;
  173. e4.padY = this.controllerState.rAxis0.y;
  174. this.OnUngripped(e4);
  175. }
  176. ulong num3 = this.controllerState.ulButtonPressed & 4294967296UL;
  177. if (num3 > 0UL && !this.padPressed)
  178. {
  179. this.padPressed = true;
  180. ClickedEventArgs e5;
  181. e5.controllerIndex = this.controllerIndex;
  182. e5.flags = (uint)this.controllerState.ulButtonPressed;
  183. e5.padX = this.controllerState.rAxis0.x;
  184. e5.padY = this.controllerState.rAxis0.y;
  185. this.OnPadClicked(e5);
  186. }
  187. else if (num3 == 0UL && this.padPressed)
  188. {
  189. this.padPressed = false;
  190. ClickedEventArgs e6;
  191. e6.controllerIndex = this.controllerIndex;
  192. e6.flags = (uint)this.controllerState.ulButtonPressed;
  193. e6.padX = this.controllerState.rAxis0.x;
  194. e6.padY = this.controllerState.rAxis0.y;
  195. this.OnPadUnclicked(e6);
  196. }
  197. ulong num4 = this.controllerState.ulButtonPressed & 2UL;
  198. if (num4 > 0UL && !this.menuPressed)
  199. {
  200. this.menuPressed = true;
  201. ClickedEventArgs e7;
  202. e7.controllerIndex = this.controllerIndex;
  203. e7.flags = (uint)this.controllerState.ulButtonPressed;
  204. e7.padX = this.controllerState.rAxis0.x;
  205. e7.padY = this.controllerState.rAxis0.y;
  206. this.OnMenuClicked(e7);
  207. }
  208. else if (num4 == 0UL && this.menuPressed)
  209. {
  210. this.menuPressed = false;
  211. ClickedEventArgs e8;
  212. e8.controllerIndex = this.controllerIndex;
  213. e8.flags = (uint)this.controllerState.ulButtonPressed;
  214. e8.padX = this.controllerState.rAxis0.x;
  215. e8.padY = this.controllerState.rAxis0.y;
  216. this.OnMenuUnclicked(e8);
  217. }
  218. num3 = (this.controllerState.ulButtonTouched & 4294967296UL);
  219. if (num3 > 0UL && !this.padTouched)
  220. {
  221. this.padTouched = true;
  222. ClickedEventArgs e9;
  223. e9.controllerIndex = this.controllerIndex;
  224. e9.flags = (uint)this.controllerState.ulButtonPressed;
  225. e9.padX = this.controllerState.rAxis0.x;
  226. e9.padY = this.controllerState.rAxis0.y;
  227. this.OnPadTouched(e9);
  228. }
  229. else if (num3 == 0UL && this.padTouched)
  230. {
  231. this.padTouched = false;
  232. ClickedEventArgs e10;
  233. e10.controllerIndex = this.controllerIndex;
  234. e10.flags = (uint)this.controllerState.ulButtonPressed;
  235. e10.padX = this.controllerState.rAxis0.x;
  236. e10.padY = this.controllerState.rAxis0.y;
  237. this.OnPadUntouched(e10);
  238. }
  239. }
  240. }
  241. public uint controllerIndex;
  242. public VRControllerState_t controllerState;
  243. public bool triggerPressed;
  244. public bool steamPressed;
  245. public bool menuPressed;
  246. public bool padPressed;
  247. public bool padTouched;
  248. public bool gripped;
  249. }