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.

113 lines
3.5 KiB

  1. namespace VRTK
  2. {
  3. using UnityEngine;
  4. public abstract class VRTK_SDKControllerReady : MonoBehaviour
  5. {
  6. protected SDK_BaseController previousControllerSDK;
  7. protected virtual void OnEnable()
  8. {
  9. VRTK_SDKManager.SubscribeLoadedSetupChanged(LoadedSetupChanged);
  10. CheckControllersReady();
  11. }
  12. protected virtual void OnDisable()
  13. {
  14. if (VRTK_SDKManager.UnsubscribeLoadedSetupChanged(LoadedSetupChanged))
  15. {
  16. UnregisterPreviousLeftController();
  17. UnregisterPreviousRightController();
  18. }
  19. }
  20. protected virtual void LoadedSetupChanged(VRTK_SDKManager sender, VRTK_SDKManager.LoadedSetupChangeEventArgs e)
  21. {
  22. CheckControllersReady();
  23. previousControllerSDK = VRTK_SDK_Bridge.GetControllerSDK();
  24. }
  25. protected virtual void CheckControllersReady()
  26. {
  27. RegisterLeftControllerReady();
  28. RegisterRightControllerReady();
  29. VRTK_ControllerReference leftRef = VRTK_DeviceFinder.GetControllerReferenceLeftHand();
  30. VRTK_ControllerReference rightRef = VRTK_DeviceFinder.GetControllerReferenceRightHand();
  31. if (VRTK_ControllerReference.IsValid(leftRef))
  32. {
  33. ControllerReady(leftRef);
  34. }
  35. if (VRTK_ControllerReference.IsValid(rightRef))
  36. {
  37. ControllerReady(rightRef);
  38. }
  39. }
  40. protected virtual void UnregisterPreviousLeftController()
  41. {
  42. try
  43. {
  44. previousControllerSDK.LeftControllerReady -= LeftControllerReady;
  45. }
  46. catch (System.Exception)
  47. {
  48. }
  49. }
  50. protected virtual void UnregisterPreviousRightController()
  51. {
  52. try
  53. {
  54. previousControllerSDK.RightControllerReady -= RightControllerReady;
  55. }
  56. catch (System.Exception)
  57. {
  58. }
  59. }
  60. protected virtual void RegisterLeftControllerReady()
  61. {
  62. UnregisterPreviousLeftController();
  63. try
  64. {
  65. VRTK_SDK_Bridge.GetControllerSDK().LeftControllerReady -= LeftControllerReady;
  66. VRTK_SDK_Bridge.GetControllerSDK().LeftControllerReady += LeftControllerReady;
  67. }
  68. catch (System.Exception)
  69. {
  70. VRTK_SDK_Bridge.GetControllerSDK().LeftControllerReady += LeftControllerReady;
  71. }
  72. }
  73. protected virtual void RegisterRightControllerReady()
  74. {
  75. UnregisterPreviousRightController();
  76. try
  77. {
  78. VRTK_SDK_Bridge.GetControllerSDK().RightControllerReady -= RightControllerReady;
  79. VRTK_SDK_Bridge.GetControllerSDK().RightControllerReady += RightControllerReady;
  80. }
  81. catch (System.Exception)
  82. {
  83. VRTK_SDK_Bridge.GetControllerSDK().RightControllerReady += RightControllerReady;
  84. }
  85. }
  86. protected virtual void RightControllerReady(object sender, VRTKSDKBaseControllerEventArgs e)
  87. {
  88. ControllerReady(e.controllerReference);
  89. }
  90. protected virtual void LeftControllerReady(object sender, VRTKSDKBaseControllerEventArgs e)
  91. {
  92. ControllerReady(e.controllerReference);
  93. }
  94. protected virtual void ControllerReady(VRTK_ControllerReference controllerReference)
  95. {
  96. }
  97. }
  98. }