SteamVR_Events.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. namespace Valve.VR
  23. {
  24. public static class SteamVR_Events
  25. {
  26. public abstract class Action
  27. {
  28. public abstract void Enable(bool enabled);
  29. public bool enabled { set { Enable(value); } }
  30. }
  31. [System.Serializable]
  32. public class ActionNoArgs : Action
  33. {
  34. public ActionNoArgs(Event _event, UnityAction action)
  35. {
  36. this._event = _event;
  37. this.action = action;
  38. }
  39. public override void Enable(bool enabled)
  40. {
  41. if (enabled)
  42. _event.Listen(action);
  43. else
  44. _event.Remove(action);
  45. }
  46. Event _event;
  47. UnityAction action;
  48. }
  49. [System.Serializable]
  50. public class Action<T> : Action
  51. {
  52. public Action(Event<T> _event, UnityAction<T> action)
  53. {
  54. this._event = _event;
  55. this.action = action;
  56. }
  57. public override void Enable(bool enabled)
  58. {
  59. if (enabled)
  60. _event.Listen(action);
  61. else
  62. _event.Remove(action);
  63. }
  64. Event<T> _event;
  65. UnityAction<T> action;
  66. }
  67. [System.Serializable]
  68. public class Action<T0, T1> : Action
  69. {
  70. public Action(Event<T0, T1> _event, UnityAction<T0, T1> action)
  71. {
  72. this._event = _event;
  73. this.action = action;
  74. }
  75. public override void Enable(bool enabled)
  76. {
  77. if (enabled)
  78. _event.Listen(action);
  79. else
  80. _event.Remove(action);
  81. }
  82. Event<T0, T1> _event;
  83. UnityAction<T0, T1> action;
  84. }
  85. [System.Serializable]
  86. public class Action<T0, T1, T2> : Action
  87. {
  88. public Action(Event<T0, T1, T2> _event, UnityAction<T0, T1, T2> action)
  89. {
  90. this._event = _event;
  91. this.action = action;
  92. }
  93. public override void Enable(bool enabled)
  94. {
  95. if (enabled)
  96. _event.Listen(action);
  97. else
  98. _event.Remove(action);
  99. }
  100. Event<T0, T1, T2> _event;
  101. UnityAction<T0, T1, T2> action;
  102. }
  103. public class Event : UnityEvent
  104. {
  105. public void Listen(UnityAction action) { this.AddListener(action); }
  106. public void Remove(UnityAction action) { this.RemoveListener(action); }
  107. public void Send() { this.Invoke(); }
  108. }
  109. public class Event<T> : UnityEvent<T>
  110. {
  111. public void Listen(UnityAction<T> action) { this.AddListener(action); }
  112. public void Remove(UnityAction<T> action) { this.RemoveListener(action); }
  113. public void Send(T arg0) { this.Invoke(arg0); }
  114. }
  115. public class Event<T0, T1> : UnityEvent<T0, T1>
  116. {
  117. public void Listen(UnityAction<T0, T1> action) { this.AddListener(action); }
  118. public void Remove(UnityAction<T0, T1> action) { this.RemoveListener(action); }
  119. public void Send(T0 arg0, T1 arg1) { this.Invoke(arg0, arg1); }
  120. }
  121. public class Event<T0, T1, T2> : UnityEvent<T0, T1, T2>
  122. {
  123. public void Listen(UnityAction<T0, T1, T2> action) { this.AddListener(action); }
  124. public void Remove(UnityAction<T0, T1, T2> action) { this.RemoveListener(action); }
  125. public void Send(T0 arg0, T1 arg1, T2 arg2) { this.Invoke(arg0, arg1, arg2); }
  126. }
  127. public static Event<bool> Calibrating = new Event<bool>();
  128. public static Action CalibratingAction(UnityAction<bool> action) { return new Action<bool>(Calibrating, action); }
  129. public static Event<int, bool> DeviceConnected = new Event<int, bool>();
  130. public static Action DeviceConnectedAction(UnityAction<int, bool> action) { return new Action<int, bool>(DeviceConnected, action); }
  131. public static Event<Color, float, bool> Fade = new Event<Color, float, bool>();
  132. public static Action FadeAction(UnityAction<Color, float, bool> action) { return new Action<Color, float, bool>(Fade, action); }
  133. public static Event FadeReady = new Event();
  134. public static Action FadeReadyAction(UnityAction action) { return new ActionNoArgs(FadeReady, action); }
  135. public static Event<bool> HideRenderModels = new Event<bool>();
  136. public static Action HideRenderModelsAction(UnityAction<bool> action) { return new Action<bool>(HideRenderModels, action); }
  137. public static Event<bool> Initializing = new Event<bool>();
  138. public static Action InitializingAction(UnityAction<bool> action) { return new Action<bool>(Initializing, action); }
  139. public static Event<bool> InputFocus = new Event<bool>();
  140. public static Action InputFocusAction(UnityAction<bool> action) { return new Action<bool>(InputFocus, action); }
  141. public static Event<bool> Loading = new Event<bool>();
  142. public static Action LoadingAction(UnityAction<bool> action) { return new Action<bool>(Loading, action); }
  143. public static Event<float> LoadingFadeIn = new Event<float>();
  144. public static Action LoadingFadeInAction(UnityAction<float> action) { return new Action<float>(LoadingFadeIn, action); }
  145. public static Event<float> LoadingFadeOut = new Event<float>();
  146. public static Action LoadingFadeOutAction(UnityAction<float> action) { return new Action<float>(LoadingFadeOut, action); }
  147. public static Event<TrackedDevicePose_t[]> NewPoses = new Event<TrackedDevicePose_t[]>();
  148. public static Action NewPosesAction(UnityAction<TrackedDevicePose_t[]> action) { return new Action<TrackedDevicePose_t[]>(NewPoses, action); }
  149. public static Event NewPosesApplied = new Event();
  150. public static Action NewPosesAppliedAction(UnityAction action) { return new ActionNoArgs(NewPosesApplied, action); }
  151. public static Event<bool> Initialized = new Event<bool>();
  152. public static Action InitializedAction(UnityAction<bool> action) { return new Action<bool>(Initialized, action); }
  153. public static Event<bool> OutOfRange = new Event<bool>();
  154. public static Action OutOfRangeAction(UnityAction<bool> action) { return new Action<bool>(OutOfRange, action); }
  155. public static Event<SteamVR_RenderModel, bool> RenderModelLoaded = new Event<SteamVR_RenderModel, bool>();
  156. public static Action RenderModelLoadedAction(UnityAction<SteamVR_RenderModel, bool> action) { return new Action<SteamVR_RenderModel, bool>(RenderModelLoaded, action); }
  157. static System.Collections.Generic.Dictionary<EVREventType, Event<VREvent_t>> systemEvents = new System.Collections.Generic.Dictionary<EVREventType, Event<VREvent_t>>();
  158. public static Event<VREvent_t> System(EVREventType eventType)
  159. {
  160. Event<VREvent_t> e;
  161. if (!systemEvents.TryGetValue(eventType, out e))
  162. {
  163. e = new Event<VREvent_t>();
  164. systemEvents.Add(eventType, e);
  165. }
  166. return e;
  167. }
  168. public static Action SystemAction(EVREventType eventType, UnityAction<VREvent_t> action)
  169. {
  170. return new Action<VREvent_t>(System(eventType), action);
  171. }
  172. }
  173. }