SteamVR_TrackedController.cs 6.7 KB

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