SteamVR_Events.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Simple event system for SteamVR.
  4. //
  5. // Example usage:
  6. //
  7. // void OnDeviceConnected(int i, bool connected) { ... }
  8. // SteamVR_Events.DeviceConnected.Listen(OnDeviceConnected); // Usually in OnEnable
  9. // SteamVR_Events.DeviceConnected.Remove(OnDeviceConnected); // Usually in OnDisable
  10. //
  11. // Alternatively, if Listening/Removing often these can be cached as follows:
  12. //
  13. // SteamVR_Event.Action deviceConnectedAction;
  14. // void OnAwake() { deviceConnectedAction = SteamVR_Event.DeviceConnectedAction(OnDeviceConnected); }
  15. // void OnEnable() { deviceConnectedAction.enabled = true; }
  16. // void OnDisable() { deviceConnectedAction.enabled = false; }
  17. //
  18. //=============================================================================
  19. using UnityEngine;
  20. using UnityEngine.Events;
  21. using Valve.VR;
  22. public static class SteamVR_Events
  23. {
  24. public abstract class Action
  25. {
  26. public abstract void Enable(bool enabled);
  27. public bool enabled { set { Enable(value); } }
  28. }
  29. [System.Serializable]
  30. public class ActionNoArgs : Action
  31. {
  32. public ActionNoArgs(Event _event, UnityAction action)
  33. {
  34. this._event = _event;
  35. this.action = action;
  36. }
  37. public override void Enable(bool enabled)
  38. {
  39. if (enabled)
  40. _event.Listen(action);
  41. else
  42. _event.Remove(action);
  43. }
  44. Event _event;
  45. UnityAction action;
  46. }
  47. [System.Serializable]
  48. public class Action<T> : Action
  49. {
  50. public Action(Event<T> _event, UnityAction<T> action)
  51. {
  52. this._event = _event;
  53. this.action = action;
  54. }
  55. public override void Enable(bool enabled)
  56. {
  57. if (enabled)
  58. _event.Listen(action);
  59. else
  60. _event.Remove(action);
  61. }
  62. Event<T> _event;
  63. UnityAction<T> action;
  64. }
  65. [System.Serializable]
  66. public class Action<T0, T1> : Action
  67. {
  68. public Action(Event<T0, T1> _event, UnityAction<T0, T1> action)
  69. {
  70. this._event = _event;
  71. this.action = action;
  72. }
  73. public override void Enable(bool enabled)
  74. {
  75. if (enabled)
  76. _event.Listen(action);
  77. else
  78. _event.Remove(action);
  79. }
  80. Event<T0, T1> _event;
  81. UnityAction<T0, T1> action;
  82. }
  83. [System.Serializable]
  84. public class Action<T0, T1, T2> : Action
  85. {
  86. public Action(Event<T0, T1, T2> _event, UnityAction<T0, T1, T2> action)
  87. {
  88. this._event = _event;
  89. this.action = action;
  90. }
  91. public override void Enable(bool enabled)
  92. {
  93. if (enabled)
  94. _event.Listen(action);
  95. else
  96. _event.Remove(action);
  97. }
  98. Event<T0, T1, T2> _event;
  99. UnityAction<T0, T1, T2> action;
  100. }
  101. public class Event : UnityEvent
  102. {
  103. public void Listen(UnityAction action) { this.AddListener(action); }
  104. public void Remove(UnityAction action) { this.RemoveListener(action); }
  105. public void Send() { this.Invoke(); }
  106. }
  107. public class Event<T> : UnityEvent<T>
  108. {
  109. public void Listen(UnityAction<T> action) { this.AddListener(action); }
  110. public void Remove(UnityAction<T> action) { this.RemoveListener(action); }
  111. public void Send(T arg0) { this.Invoke(arg0); }
  112. }
  113. public class Event<T0, T1> : UnityEvent<T0, T1>
  114. {
  115. public void Listen(UnityAction<T0, T1> action) { this.AddListener(action); }
  116. public void Remove(UnityAction<T0, T1> action) { this.RemoveListener(action); }
  117. public void Send(T0 arg0, T1 arg1) { this.Invoke(arg0, arg1); }
  118. }
  119. public class Event<T0, T1, T2> : UnityEvent<T0, T1, T2>
  120. {
  121. public void Listen(UnityAction<T0, T1, T2> action) { this.AddListener(action); }
  122. public void Remove(UnityAction<T0, T1, T2> action) { this.RemoveListener(action); }
  123. public void Send(T0 arg0, T1 arg1, T2 arg2) { this.Invoke(arg0, arg1, arg2); }
  124. }
  125. public static Event<bool> Calibrating = new Event<bool>();
  126. public static Action CalibratingAction(UnityAction<bool> action) { return new Action<bool>(Calibrating, action); }
  127. public static Event<int, bool> DeviceConnected = new Event<int, bool>();
  128. public static Action DeviceConnectedAction(UnityAction<int, bool> action) { return new Action<int, bool>(DeviceConnected, action); }
  129. public static Event<Color, float, bool> Fade = new Event<Color, float, bool>();
  130. public static Action FadeAction(UnityAction<Color, float, bool> action) { return new Action<Color, float, bool>(Fade, action); }
  131. public static Event FadeReady = new Event();
  132. public static Action FadeReadyAction(UnityAction action) { return new ActionNoArgs(FadeReady, action); }
  133. public static Event<bool> HideRenderModels = new Event<bool>();
  134. public static Action HideRenderModelsAction(UnityAction<bool> action) { return new Action<bool>(HideRenderModels, action); }
  135. public static Event<bool> Initializing = new Event<bool>();
  136. public static Action InitializingAction(UnityAction<bool> action) { return new Action<bool>(Initializing, action); }
  137. public static Event<bool> InputFocus = new Event<bool>();
  138. public static Action InputFocusAction(UnityAction<bool> action) { return new Action<bool>(InputFocus, action); }
  139. public static Event<bool> Loading = new Event<bool>();
  140. public static Action LoadingAction(UnityAction<bool> action) { return new Action<bool>(Loading, action); }
  141. public static Event<float> LoadingFadeIn = new Event<float>();
  142. public static Action LoadingFadeInAction(UnityAction<float> action) { return new Action<float>(LoadingFadeIn, action); }
  143. public static Event<float> LoadingFadeOut = new Event<float>();
  144. public static Action LoadingFadeOutAction(UnityAction<float> action) { return new Action<float>(LoadingFadeOut, action); }
  145. public static Event<TrackedDevicePose_t[]> NewPoses = new Event<TrackedDevicePose_t[]>();
  146. public static Action NewPosesAction(UnityAction<TrackedDevicePose_t[]> action) { return new Action<TrackedDevicePose_t[]>(NewPoses, action); }
  147. public static Event NewPosesApplied = new Event();
  148. public static Action NewPosesAppliedAction(UnityAction action) { return new ActionNoArgs(NewPosesApplied, action); }
  149. public static Event<bool> OutOfRange = new Event<bool>();
  150. public static Action OutOfRangeAction(UnityAction<bool> action) { return new Action<bool>(OutOfRange, action); }
  151. public static Event<SteamVR_RenderModel, bool> RenderModelLoaded = new Event<SteamVR_RenderModel, bool>();
  152. public static Action RenderModelLoadedAction(UnityAction<SteamVR_RenderModel, bool> action) { return new Action<SteamVR_RenderModel, bool>(RenderModelLoaded, action); }
  153. static System.Collections.Generic.Dictionary<EVREventType, Event<VREvent_t>> systemEvents = new System.Collections.Generic.Dictionary<EVREventType, Event<VREvent_t>>();
  154. public static Event<VREvent_t> System(EVREventType eventType)
  155. {
  156. Event<VREvent_t> e;
  157. if (!systemEvents.TryGetValue(eventType, out e))
  158. {
  159. e = new Event<VREvent_t>();
  160. systemEvents.Add(eventType, e);
  161. }
  162. return e;
  163. }
  164. public static Action SystemAction(EVREventType eventType, UnityAction<VREvent_t> action)
  165. {
  166. return new Action<VREvent_t>(System(eventType), action);
  167. }
  168. }