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