IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
SteamVR_Events.cs
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 
20 using UnityEngine;
21 using UnityEngine.Events;
22 using 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 
32  [System.Serializable]
33  public class ActionNoArgs : Action
34  {
35  public ActionNoArgs(Event _event, UnityAction action)
36  {
37  this._event = _event;
38  this.action = action;
39  }
40 
41  public override void Enable(bool enabled)
42  {
43  if (enabled)
44  _event.Listen(action);
45  else
46  _event.Remove(action);
47  }
48 
49  Event _event;
50  UnityAction action;
51  }
52 
53  [System.Serializable]
54  public class Action<T> : Action
55  {
56  public Action(Event<T> _event, UnityAction<T> action)
57  {
58  this._event = _event;
59  this.action = action;
60  }
61 
62  public override void Enable(bool enabled)
63  {
64  if (enabled)
65  _event.Listen(action);
66  else
67  _event.Remove(action);
68  }
69 
70  Event<T> _event;
71  UnityAction<T> action;
72  }
73 
74  [System.Serializable]
75  public class Action<T0, T1> : Action
76  {
77  public Action(Event<T0, T1> _event, UnityAction<T0, T1> action)
78  {
79  this._event = _event;
80  this.action = action;
81  }
82 
83  public override void Enable(bool enabled)
84  {
85  if (enabled)
86  _event.Listen(action);
87  else
88  _event.Remove(action);
89  }
90 
91  Event<T0, T1> _event;
92  UnityAction<T0, T1> action;
93  }
94 
95  [System.Serializable]
96  public class Action<T0, T1, T2> : Action
97  {
98  public Action(Event<T0, T1, T2> _event, UnityAction<T0, T1, T2> action)
99  {
100  this._event = _event;
101  this.action = action;
102  }
103 
104  public override void Enable(bool enabled)
105  {
106  if (enabled)
107  _event.Listen(action);
108  else
109  _event.Remove(action);
110  }
111 
112  Event<T0, T1, T2> _event;
113  UnityAction<T0, T1, T2> action;
114  }
115 
116  public class Event : UnityEvent
117  {
118  public void Listen(UnityAction action) { this.AddListener(action); }
119  public void Remove(UnityAction action) { this.RemoveListener(action); }
120  public void Send() { this.Invoke(); }
121  }
122 
123  public class Event<T> : UnityEvent<T>
124  {
125  public void Listen(UnityAction<T> action) { this.AddListener(action); }
126  public void Remove(UnityAction<T> action) { this.RemoveListener(action); }
127  public void Send(T arg0) { this.Invoke(arg0); }
128  }
129 
130  public class Event<T0, T1> : UnityEvent<T0, T1>
131  {
132  public void Listen(UnityAction<T0, T1> action) { this.AddListener(action); }
133  public void Remove(UnityAction<T0, T1> action) { this.RemoveListener(action); }
134  public void Send(T0 arg0, T1 arg1) { this.Invoke(arg0, arg1); }
135  }
136 
137  public class Event<T0, T1, T2> : UnityEvent<T0, T1, T2>
138  {
139  public void Listen(UnityAction<T0, T1, T2> action) { this.AddListener(action); }
140  public void Remove(UnityAction<T0, T1, T2> action) { this.RemoveListener(action); }
141  public void Send(T0 arg0, T1 arg1, T2 arg2) { this.Invoke(arg0, arg1, arg2); }
142  }
143 
144  public static Event<bool> Calibrating = new Event<bool>();
145  public static Action CalibratingAction(UnityAction<bool> action) { return new Action<bool>(Calibrating, action); }
146 
147  public static Event<int, bool> DeviceConnected = new Event<int, bool>();
148  public static Action DeviceConnectedAction(UnityAction<int, bool> action) { return new Action<int, bool>(DeviceConnected, action); }
149 
150  public static Event<Color, float, bool> Fade = new Event<Color, float, bool>();
151  public static Action FadeAction(UnityAction<Color, float, bool> action) { return new Action<Color, float, bool>(Fade, action); }
152 
153  public static Event FadeReady = new Event();
154  public static Action FadeReadyAction(UnityAction action) { return new ActionNoArgs(FadeReady, action); }
155 
156  public static Event<bool> HideRenderModels = new Event<bool>();
157  public static Action HideRenderModelsAction(UnityAction<bool> action) { return new Action<bool>(HideRenderModels, action); }
158 
159  public static Event<bool> Initializing = new Event<bool>();
160  public static Action InitializingAction(UnityAction<bool> action) { return new Action<bool>(Initializing, action); }
161 
162  public static Event<bool> InputFocus = new Event<bool>();
163  public static Action InputFocusAction(UnityAction<bool> action) { return new Action<bool>(InputFocus, action); }
164 
165  public static Event<bool> Loading = new Event<bool>();
166  public static Action LoadingAction(UnityAction<bool> action) { return new Action<bool>(Loading, action); }
167 
168  public static Event<float> LoadingFadeIn = new Event<float>();
169  public static Action LoadingFadeInAction(UnityAction<float> action) { return new Action<float>(LoadingFadeIn, action); }
170 
171  public static Event<float> LoadingFadeOut = new Event<float>();
172  public static Action LoadingFadeOutAction(UnityAction<float> action) { return new Action<float>(LoadingFadeOut, action); }
173 
174  public static Event<TrackedDevicePose_t[]> NewPoses = new Event<TrackedDevicePose_t[]>();
175  public static Action NewPosesAction(UnityAction<TrackedDevicePose_t[]> action) { return new Action<TrackedDevicePose_t[]>(NewPoses, action); }
176 
177  public static Event NewPosesApplied = new Event();
178  public static Action NewPosesAppliedAction(UnityAction action) { return new ActionNoArgs(NewPosesApplied, action); }
179 
180  public static Event<bool> OutOfRange = new Event<bool>();
181  public static Action OutOfRangeAction(UnityAction<bool> action) { return new Action<bool>(OutOfRange, action); }
182 
183  public static Event<SteamVR_RenderModel, bool> RenderModelLoaded = new Event<SteamVR_RenderModel, bool>();
184  public static Action RenderModelLoadedAction(UnityAction<SteamVR_RenderModel, bool> action) { return new Action<SteamVR_RenderModel, bool>(RenderModelLoaded, action); }
185 
186  static System.Collections.Generic.Dictionary<EVREventType, Event<VREvent_t>> systemEvents = new System.Collections.Generic.Dictionary<EVREventType, Event<VREvent_t>>();
187  public static Event<VREvent_t> System(EVREventType eventType)
188  {
189  Event<VREvent_t> e;
190  if (!systemEvents.TryGetValue(eventType, out e))
191  {
192  e = new Event<VREvent_t>();
193  systemEvents.Add(eventType, e);
194  }
195  return e;
196  }
197 
198  public static Action SystemAction(EVREventType eventType, UnityAction<VREvent_t> action)
199  {
200  return new Action<VREvent_t>(System(eventType), action);
201  }
202 }
203