Assignment for RMIT Mixed Reality in 2020
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.

260 lines
8.9 KiB

  1. namespace VRTK
  2. {
  3. using UnityEngine;
  4. using System.Collections;
  5. public struct VRTKTrackedControllerEventArgs
  6. {
  7. public uint currentIndex;
  8. public uint previousIndex;
  9. }
  10. public delegate void VRTKTrackedControllerEventHandler(object sender, VRTKTrackedControllerEventArgs e);
  11. public class VRTK_TrackedController : MonoBehaviour
  12. {
  13. public uint index = uint.MaxValue;
  14. public event VRTKTrackedControllerEventHandler ControllerEnabled;
  15. public event VRTKTrackedControllerEventHandler ControllerDisabled;
  16. public event VRTKTrackedControllerEventHandler ControllerIndexChanged;
  17. public event VRTKTrackedControllerEventHandler ControllerModelAvailable;
  18. protected GameObject aliasController;
  19. protected SDK_BaseController.ControllerType controllerType = SDK_BaseController.ControllerType.Undefined;
  20. protected bool controllerModelWaitSubscribed = false;
  21. protected Coroutine emitControllerEnabledRoutine = null;
  22. protected Coroutine emitControllerModelAvailableRoutine = null;
  23. protected VRTK_ControllerReference controllerReference
  24. {
  25. get
  26. {
  27. return VRTK_ControllerReference.GetControllerReference(index);
  28. }
  29. }
  30. public virtual void OnControllerEnabled(VRTKTrackedControllerEventArgs e)
  31. {
  32. if (ControllerEnabled != null)
  33. {
  34. ControllerEnabled(this, e);
  35. }
  36. }
  37. public virtual void OnControllerDisabled(VRTKTrackedControllerEventArgs e)
  38. {
  39. if (ControllerDisabled != null)
  40. {
  41. ControllerDisabled(this, e);
  42. }
  43. }
  44. public virtual void OnControllerIndexChanged(VRTKTrackedControllerEventArgs e)
  45. {
  46. if (ControllerIndexChanged != null)
  47. {
  48. ControllerIndexChanged(this, e);
  49. }
  50. }
  51. public virtual void OnControllerModelAvailable(VRTKTrackedControllerEventArgs e)
  52. {
  53. if (ControllerModelAvailable != null)
  54. {
  55. ControllerModelAvailable(this, e);
  56. }
  57. }
  58. public virtual SDK_BaseController.ControllerType GetControllerType()
  59. {
  60. return controllerType;
  61. }
  62. protected virtual VRTKTrackedControllerEventArgs SetEventPayload(uint previousIndex = uint.MaxValue)
  63. {
  64. VRTKTrackedControllerEventArgs e;
  65. e.currentIndex = index;
  66. e.previousIndex = previousIndex;
  67. return e;
  68. }
  69. protected virtual void Awake()
  70. {
  71. VRTK_SDKManager.AttemptAddBehaviourToToggleOnLoadedSetupChange(this);
  72. }
  73. protected virtual void OnEnable()
  74. {
  75. aliasController = VRTK_DeviceFinder.GetScriptAliasController(gameObject);
  76. if (aliasController == null)
  77. {
  78. aliasController = gameObject;
  79. }
  80. index = VRTK_DeviceFinder.GetControllerIndex(gameObject);
  81. SetControllerType();
  82. StartEmitControllerEnabledAtEndOfFrame();
  83. ManageControllerModelListeners(true);
  84. }
  85. protected virtual void OnDisable()
  86. {
  87. CancelCoroutines();
  88. index = uint.MaxValue;
  89. ManageControllerModelListeners(false);
  90. OnControllerDisabled(SetEventPayload());
  91. }
  92. protected virtual void OnDestroy()
  93. {
  94. VRTK_SDKManager.AttemptRemoveBehaviourToToggleOnLoadedSetupChange(this);
  95. }
  96. protected virtual void FixedUpdate()
  97. {
  98. VRTK_SDK_Bridge.ControllerProcessFixedUpdate(VRTK_ControllerReference.GetControllerReference(index));
  99. }
  100. protected virtual void Update()
  101. {
  102. uint checkIndex = VRTK_DeviceFinder.GetControllerIndex(gameObject);
  103. if (checkIndex != index)
  104. {
  105. uint previousIndex = index;
  106. index = checkIndex;
  107. //If the controller model listeners have already been registered, then make sure to unsub and sub when index changes.
  108. if (controllerModelWaitSubscribed)
  109. {
  110. ManageControllerModelListeners(false);
  111. ManageControllerModelListeners(true);
  112. }
  113. OnControllerIndexChanged(SetEventPayload(previousIndex));
  114. SetControllerType();
  115. }
  116. VRTK_SDK_Bridge.ControllerProcessUpdate(VRTK_ControllerReference.GetControllerReference(index));
  117. if (aliasController != null && gameObject.activeInHierarchy && !aliasController.activeSelf)
  118. {
  119. aliasController.SetActive(true);
  120. }
  121. }
  122. protected virtual void ManageLeftControllerListener(bool register, VRTKSDKBaseControllerEventHandler callbackMethod)
  123. {
  124. if (register)
  125. {
  126. VRTK_SDK_Bridge.GetControllerSDK().LeftControllerModelReady += callbackMethod;
  127. }
  128. else
  129. {
  130. VRTK_SDK_Bridge.GetControllerSDK().LeftControllerModelReady -= callbackMethod;
  131. }
  132. }
  133. protected virtual void ManageRightControllerListener(bool register, VRTKSDKBaseControllerEventHandler callbackMethod)
  134. {
  135. if (register)
  136. {
  137. VRTK_SDK_Bridge.GetControllerSDK().RightControllerModelReady += callbackMethod;
  138. }
  139. else
  140. {
  141. VRTK_SDK_Bridge.GetControllerSDK().RightControllerModelReady -= callbackMethod;
  142. }
  143. }
  144. protected virtual void RegisterHandControllerListener(bool register, SDK_BaseController.ControllerHand givenHand)
  145. {
  146. switch (givenHand)
  147. {
  148. case SDK_BaseController.ControllerHand.Left:
  149. ManageLeftControllerListener(register, ControllerModelReady);
  150. break;
  151. case SDK_BaseController.ControllerHand.Right:
  152. ManageRightControllerListener(register, ControllerModelReady);
  153. break;
  154. }
  155. controllerModelWaitSubscribed = register;
  156. }
  157. protected virtual void ManageControllerModelListener(bool register, SDK_BaseController.ControllerHand givenHand)
  158. {
  159. if (VRTK_SDK_Bridge.WaitForControllerModel(givenHand))
  160. {
  161. RegisterHandControllerListener(register, givenHand);
  162. }
  163. else
  164. {
  165. if (register)
  166. {
  167. StartEmitControllerModelReadyAtEndOfFrame();
  168. }
  169. else if (controllerModelWaitSubscribed)
  170. {
  171. RegisterHandControllerListener(register, givenHand);
  172. }
  173. }
  174. }
  175. protected virtual void ManageControllerModelListeners(bool register)
  176. {
  177. ManageControllerModelListener(register, VRTK_DeviceFinder.GetControllerHand(gameObject));
  178. }
  179. protected virtual void SetControllerType()
  180. {
  181. controllerType = (controllerReference != null ? VRTK_DeviceFinder.GetCurrentControllerType(controllerReference) : SDK_BaseController.ControllerType.Undefined);
  182. }
  183. protected virtual void StartEmitControllerEnabledAtEndOfFrame()
  184. {
  185. if (gameObject.activeInHierarchy)
  186. {
  187. emitControllerEnabledRoutine = StartCoroutine(EmitControllerEnabledAtEndOfFrame());
  188. }
  189. }
  190. protected virtual IEnumerator EmitControllerEnabledAtEndOfFrame()
  191. {
  192. yield return new WaitForEndOfFrame();
  193. OnControllerEnabled(SetEventPayload());
  194. }
  195. protected virtual void ControllerModelReady(object sender, VRTKSDKBaseControllerEventArgs e)
  196. {
  197. SetControllerType();
  198. if (e.controllerReference == null || controllerReference == e.controllerReference)
  199. {
  200. StartEmitControllerModelReadyAtEndOfFrame();
  201. }
  202. }
  203. protected virtual void StartEmitControllerModelReadyAtEndOfFrame()
  204. {
  205. if (gameObject != null && gameObject.activeInHierarchy)
  206. {
  207. emitControllerModelAvailableRoutine = StartCoroutine(EmitControllerModelReadyAtEndOfFrame());
  208. }
  209. }
  210. protected virtual IEnumerator EmitControllerModelReadyAtEndOfFrame()
  211. {
  212. yield return new WaitForEndOfFrame();
  213. OnControllerModelAvailable(SetEventPayload());
  214. }
  215. protected virtual void CancelCoroutines()
  216. {
  217. if (emitControllerModelAvailableRoutine != null)
  218. {
  219. StopCoroutine(emitControllerModelAvailableRoutine);
  220. }
  221. if (emitControllerEnabledRoutine != null)
  222. {
  223. StopCoroutine(emitControllerEnabledRoutine);
  224. }
  225. }
  226. }
  227. }