-
Notifications
You must be signed in to change notification settings - Fork 103
/
InteractorBehaviorControls.cs
310 lines (273 loc) · 8.86 KB
/
InteractorBehaviorControls.cs
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright (c) Mixed Reality Toolkit Contributors
// Licensed under the BSD 3-Clause
using MixedReality.Toolkit.Input;
using System;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
namespace MixedReality.Toolkit.Examples
{
/// <summary>
/// Example class to demonstrate how to turn various interactors on and off.
/// </summary>
/// <remarks>
/// Hook up buttons to the public functions to turn interactors on and off.
/// </remarks>
[AddComponentMenu("MRTK/Examples/InteractorBehaviorControls")]
public class InteractorBehaviorControls : MonoBehaviour
{
[SerializeField]
private InteractionModeManager interactionModeManager;
[SerializeField]
private XRInteractionManager interactionManager;
[SerializeField]
private XRBaseInteractor[] handRaysInteractors;
[SerializeField]
private XRBaseInteractor[] controllerRayInteractors;
[SerializeField]
private XRBaseInteractor[] grabInteractors;
[SerializeField]
private XRBaseInteractor[] pokeInteractors;
[SerializeField]
private XRBaseInteractor[] gazePinchInteractors;
[SerializeField]
private XRBaseInteractor gazeInteractor;
/// <summary>
/// event triggered when hand rays are toggled on or off
/// </summary>
public event Action<bool> onHandRayToggled;
/// <summary>
/// event triggered when controller rays are toggled on or off
/// </summary>
public event Action<bool> onControllerRayToggled;
/// <summary>
/// event triggered when grab interactors are toggled on/off
/// </summary>
public event Action<bool> onGrabToggled;
/// <summary>
/// event triggered when poke interactors are toggled on/off
/// </summary>
public event Action<bool> onPokeToggled;
/// <summary>
/// event triggered when gaze interactors are toggled on/off
/// </summary>
public event Action<bool> onGazeToggled;
/// <summary>
/// Enable all interactors
/// </summary>
public void EnableAll()
{
SetControllerRayActive(true);
SetHandGrabActive(true);
SetHandPokeActive(true);
SetHandRayActive(true);
SetGazeActive(true);
SetGazePinchActive(true);
}
/// <summary>
/// Enable everything, and disable all gaze interactions.
/// </summary>
public void OnlyHands()
{
EnableAll();
SetGazeActive(false);
SetGazePinchActive(false);
}
/// <summary>
/// Enable everything, and disable all hand interactions.
/// </summary>
public void OnlyGaze()
{
EnableAll();
SetControllerRayActive(false);
SetHandGrabActive(false);
SetHandPokeActive(false);
SetHandRayActive(false);
}
/// <summary>
/// Enable everything, and disable controller ray interactors.
/// </summary>
public void DisablControllerRays()
{
EnableAll();
SetControllerRayActive(false);
}
/// <summary>
/// Enable everything, and disable hand grab interactors.
/// </summary>
public void DisableHandGrabs()
{
EnableAll();
SetHandGrabActive(false);
}
/// <summary>
/// Enable everything, and disable hand poke interactors.
/// </summary>
public void DisableHandPokes()
{
EnableAll();
SetHandPokeActive(false);
}
/// <summary>
/// Enable everything, and disable hand poke interactors.
/// </summary>
public void DisableHandRays()
{
EnableAll();
SetHandRayActive(false);
}
/// <summary>
/// Enable everything, and disable gaze interactors.
/// </summary>
public void DisableGaze()
{
EnableAll();
SetGazeActive(false);
}
/// <summary>
/// Enable everything, and disable gaze pinch interactors.
/// </summary>
public void DisableGazePinch()
{
EnableAll();
SetGazePinchActive(false);
}
/// <summary>
/// Enable or disable the specified gaze interactors.
/// </summary>
public void SetGazeActive(bool isActive)
{
if (ToggleInteractor(gazeInteractor, isActive))
{
onGazeToggled?.Invoke(isActive);
}
}
/// <summary>
/// Enable or disable the specified gaze pinch interactors.
/// </summary>
public void SetGazePinchActive(bool isActive)
{
if (ToggleInteractors(gazePinchInteractors, isActive))
{
onGazeToggled?.Invoke(isActive);
}
}
/// <summary>
/// Enable or disable the specified poke interactors.
/// </summary>
public void SetHandPokeActive(bool isActive)
{
if (ToggleInteractors(pokeInteractors, isActive))
{
onPokeToggled?.Invoke(isActive);
}
}
/// <summary>
/// Enable or disable the specified hand grab interactors.
/// </summary>
public void SetHandGrabActive(bool isActive)
{
if (ToggleInteractors(grabInteractors, isActive))
{
onGrabToggled?.Invoke(isActive);
}
}
/// <summary>
/// Enable or disable the specified controller ray interactors.
/// </summary>
public void SetControllerRayActive(bool isActive)
{
if (ToggleInteractors(controllerRayInteractors, isActive))
{
onControllerRayToggled?.Invoke(isActive);
}
}
/// <summary>
/// Enable or disable the specified hand ray interactors.
/// </summary>
public void SetHandRayActive(bool isActive)
{
if (ToggleInteractors(handRaysInteractors, isActive))
{
onHandRayToggled?.Invoke(isActive);
}
}
/// <summary>
/// Toggle interactors, and return true if something changed.
/// </summary>
private bool ToggleInteractors(XRBaseInteractor[] interactors, bool isActive)
{
if (isActive)
{
return ActivateInteractors(interactors);
}
else
{
return DeactivateInteractors(interactors);
}
}
/// <summary>
/// Toggle interactor, and return true if something changed.
/// </summary>
private bool ToggleInteractor(XRBaseInteractor interactor, bool isActive)
{
if (isActive)
{
return ActivateInteractor(interactor);
}
else
{
return DeactivateInteractor(interactor);
}
}
/// <summary>
/// Activate interactors, and return true if something changed.
/// </summary>
private bool ActivateInteractors(XRBaseInteractor[] interactors)
{
bool change = false;
for (int i = 0; i < interactors.Length; i++)
{
change |= ActivateInteractor(interactors[i]);
}
return change;
}
/// <summary>
/// Activate interactor, and return true if something changed.
/// </summary>
private bool ActivateInteractor(XRBaseInteractor interactor)
{
if (interactor.gameObject.activeSelf)
{
return false;
}
interactor.gameObject.SetActive(true);
interactionModeManager.RegisterInteractor(interactor);
return true;
}
/// <summary>
/// Deactivate interactors, and return true if something changed.
/// </summary>
private bool DeactivateInteractors(XRBaseInteractor[] interactors)
{
bool change = false;
for (int i = 0; i < interactors.Length; i++)
{
change |= DeactivateInteractor(interactors[i]);
}
return change;
}
/// <summary>
/// Deactivate interactor, and return true if something changed.
/// </summary>
private bool DeactivateInteractor(XRBaseInteractor interactor)
{
if (!interactor.gameObject.activeSelf)
{
return false;
}
interactionModeManager.UnregisterInteractor(interactor);
interactor.gameObject.SetActive(false);
return true;
}
}
}