ViveControllerButtons.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using Valve.VR;
  5. public class ViveControllerButtons : AVRControllerButtons
  6. {
  7. public ViveControllerButtons()
  8. {
  9. ulong[] array = new ulong[5];
  10. array[1] = 4294967296UL;
  11. this.m_aryTouch2RealVive = array;
  12. base..ctor();
  13. }
  14. protected override void Awake()
  15. {
  16. base.Awake();
  17. this.m_eType = AVRControllerButtons.TYPE.VIVE;
  18. this.m_trackedObject = base.GetComponent<SteamVR_TrackedObject>();
  19. }
  20. private void Start()
  21. {
  22. this.m_device = SteamVR_Controller.Input((int)this.m_trackedObject.index);
  23. this.m_trTrackedDeviceParent = base.transform.parent.Find("Tracked Devices");
  24. this.m_TrackObjs = this.m_trTrackedDeviceParent.GetComponentsInChildren<SteamVR_TrackedObject>(true);
  25. }
  26. public override bool ShowHand
  27. {
  28. get
  29. {
  30. foreach (SteamVR_TrackedObject steamVR_TrackedObject in this.m_TrackObjs)
  31. {
  32. if (steamVR_TrackedObject.index == this.m_trackedObject.index)
  33. {
  34. return steamVR_TrackedObject.gameObject.transform.localScale.sqrMagnitude < 0.0100000007f;
  35. }
  36. }
  37. return false;
  38. }
  39. set
  40. {
  41. foreach (SteamVR_TrackedObject steamVR_TrackedObject in this.m_TrackObjs)
  42. {
  43. if (steamVR_TrackedObject.index == this.m_trackedObject.index)
  44. {
  45. if (value)
  46. {
  47. steamVR_TrackedObject.gameObject.transform.localScale = new Vector3(0f, 0f, 0f);
  48. }
  49. else
  50. {
  51. steamVR_TrackedObject.gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. public override void Haptic(byte f_byFoce, float f_fTime)
  58. {
  59. if (this.m_device != null)
  60. {
  61. if (this.m_coVib != null)
  62. {
  63. base.StopCoroutine(this.m_coVib);
  64. }
  65. this.m_coVib = base.StartCoroutine(this.CoVib(f_byFoce, f_fTime));
  66. }
  67. }
  68. private IEnumerator CoVib(byte f_byFoce, float f_fTime)
  69. {
  70. for (float fTime = 0f; fTime < f_fTime; fTime += Time.deltaTime)
  71. {
  72. yield return null;
  73. if (this.m_device != null)
  74. {
  75. this.m_device.TriggerHapticPulse((ushort)(1000f * ((float)f_byFoce / 255f)), EVRButtonId.k_EButton_Axis0);
  76. }
  77. }
  78. this.m_coVib = null;
  79. yield break;
  80. }
  81. public override bool GetPressDown(AVRControllerButtons.BTN f_eBtn)
  82. {
  83. if (this.m_device == null)
  84. {
  85. return false;
  86. }
  87. if (f_eBtn != AVRControllerButtons.BTN.VIRTUAL_L_CLICK && f_eBtn != AVRControllerButtons.BTN.VIRTUAL_R_CLICK)
  88. {
  89. return this.m_device.GetPressDown(this.m_aryBtn2RealVive[(int)f_eBtn]);
  90. }
  91. if (!this.m_device.GetPressDown(4294967296UL))
  92. {
  93. return false;
  94. }
  95. if (this.m_device.GetAxis(EVRButtonId.k_EButton_Axis0).x < 0.7f)
  96. {
  97. this.m_bPressLeft = true;
  98. return f_eBtn == AVRControllerButtons.BTN.VIRTUAL_L_CLICK;
  99. }
  100. this.m_bPressLeft = false;
  101. return f_eBtn == AVRControllerButtons.BTN.VIRTUAL_R_CLICK;
  102. }
  103. public override bool GetPress(AVRControllerButtons.BTN f_eBtn)
  104. {
  105. if (this.m_device == null)
  106. {
  107. return false;
  108. }
  109. if (f_eBtn == AVRControllerButtons.BTN.VIRTUAL_L_CLICK || f_eBtn == AVRControllerButtons.BTN.VIRTUAL_R_CLICK)
  110. {
  111. return this.m_device.GetPress(4294967296UL) && ((!this.m_bPressLeft) ? (f_eBtn == AVRControllerButtons.BTN.VIRTUAL_R_CLICK) : (f_eBtn == AVRControllerButtons.BTN.VIRTUAL_L_CLICK));
  112. }
  113. return this.m_device.GetPress(this.m_aryBtn2RealVive[(int)f_eBtn]);
  114. }
  115. public override bool GetPressUp(AVRControllerButtons.BTN f_eBtn)
  116. {
  117. if (this.m_device == null)
  118. {
  119. return false;
  120. }
  121. if (f_eBtn == AVRControllerButtons.BTN.VIRTUAL_L_CLICK || f_eBtn == AVRControllerButtons.BTN.VIRTUAL_R_CLICK)
  122. {
  123. return this.m_device.GetPressUp(4294967296UL) && ((!this.m_bPressLeft) ? (f_eBtn == AVRControllerButtons.BTN.VIRTUAL_R_CLICK) : (f_eBtn == AVRControllerButtons.BTN.VIRTUAL_L_CLICK));
  124. }
  125. return this.m_device.GetPressUp(this.m_aryBtn2RealVive[(int)f_eBtn]);
  126. }
  127. public override bool GetTouchDown(AVRControllerButtons.TOUCH f_eTouch)
  128. {
  129. return this.m_device != null && this.m_device.GetTouchDown(this.m_aryTouch2RealVive[(int)f_eTouch]);
  130. }
  131. public override bool GetTouch(AVRControllerButtons.TOUCH f_eTouch)
  132. {
  133. return this.m_device != null && this.m_device.GetTouch(this.m_aryTouch2RealVive[(int)f_eTouch]);
  134. }
  135. public override bool GetTouchUp(AVRControllerButtons.TOUCH f_eTouch)
  136. {
  137. return this.m_device != null && this.m_device.GetTouchUp(this.m_aryTouch2RealVive[(int)f_eTouch]);
  138. }
  139. public override Vector2 GetAxis()
  140. {
  141. if (this.m_device == null)
  142. {
  143. return Vector2.zero;
  144. }
  145. return this.m_device.GetAxis(EVRButtonId.k_EButton_Axis0);
  146. }
  147. public override float GetTriggerRate()
  148. {
  149. if (this.m_device == null)
  150. {
  151. return 0f;
  152. }
  153. return this.m_device.GetAxis(EVRButtonId.k_EButton_Axis1).x;
  154. }
  155. private void Update()
  156. {
  157. this.m_device = SteamVR_Controller.Input((int)this.m_trackedObject.index);
  158. if (this.m_device != null && this.m_device.GetPressDown(4294967296UL))
  159. {
  160. if (this.m_device.GetAxis(EVRButtonId.k_EButton_Axis0).x < 0.7f)
  161. {
  162. this.m_bPressLeft = true;
  163. }
  164. else
  165. {
  166. this.m_bPressLeft = false;
  167. }
  168. }
  169. }
  170. private ulong[] m_aryBtn2RealVive = new ulong[]
  171. {
  172. 2UL,
  173. 4294967296UL,
  174. 4294967296UL,
  175. 8589934592UL,
  176. 2UL,
  177. 8589934592UL,
  178. 4294967296UL,
  179. 4UL
  180. };
  181. private ulong[] m_aryTouch2RealVive;
  182. private SteamVR_TrackedObject m_trackedObject;
  183. private Transform m_trTrackedDeviceParent;
  184. private SteamVR_TrackedObject[] m_TrackObjs;
  185. private SteamVR_Controller.Device m_device;
  186. private bool m_bPressLeft;
  187. private Coroutine m_coVib;
  188. }