forked from MixedRealityToolkit/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMRTKBaseInteractable.cs
More file actions
377 lines (295 loc) · 13.3 KB
/
Copy pathMRTKBaseInteractable.cs
File metadata and controls
377 lines (295 loc) · 13.3 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright (c) Mixed Reality Toolkit Contributors
// Licensed under the BSD 3-Clause
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.XR.Interaction.Toolkit;
namespace MixedReality.Toolkit
{
/// <summary>
/// Extended version of XRBaseInteractable to enable multi-hand interactions.
/// </summary>
[AddComponentMenu("MRTK/Core/MRTK Base Interactable")]
public class MRTKBaseInteractable : XRBaseInteractable
{
#region Gaze
private readonly List<IGazeInteractor> hoveringGazeInteractors = new List<IGazeInteractor>();
/// <summary>
/// (Read Only) The list of <see cref="IGazeInteractor"/> components currently gazing this object.
/// </summary>
public List<IGazeInteractor> HoveringGazeInteractors => hoveringGazeInteractors;
#endregion Gaze
#region GazePinch
private readonly List<IGazePinchInteractor> hoveringGazePinchInteractors = new List<IGazePinchInteractor>();
/// <summary>
/// (Read Only) The list of <see cref="IGazePinchInteractor"/> components currently hovering this object.
/// </summary>
public List<IGazePinchInteractor> HoveringGazePinchInteractors => hoveringGazePinchInteractors;
private readonly List<IGazePinchInteractor> selectingGazePinchInteractors = new List<IGazePinchInteractor>();
/// <summary>
/// (Read Only) The list of <see cref="IGazePinchInteractor"/> components currently selecting this object.
/// </summary>
public List<IGazePinchInteractor> SelectingGazePinchInteractors => selectingGazePinchInteractors;
#endregion GazePinch
#region Poke
private readonly List<IPokeInteractor> hoveringPokeInteractors = new List<IPokeInteractor>();
/// <summary>
/// (Read Only) The list of <see cref="IPokeInteractor"/> components currently hovering this object.
/// </summary>
public List<IPokeInteractor> HoveringPokeInteractors => hoveringPokeInteractors;
#endregion Poke
#region Grab
private readonly List<IGrabInteractor> hoveringGrabInteractors = new List<IGrabInteractor>();
/// <summary>
/// (Read Only) The list of <see cref="IGrabInteractor"/> components currently hovering this object.
/// </summary>]
public List<IGrabInteractor> HoveringGrabInteractors => hoveringGrabInteractors;
private readonly List<IGrabInteractor> selectingGrabInteractors = new List<IGrabInteractor>();
/// <summary>
/// (Read Only) The list of <see cref="IGrabInteractor"/> components currently selecting this object.
/// </summary>
public List<IGrabInteractor> SelectingGrabInteractors => selectingGrabInteractors;
#endregion Grab
#region Ray
private readonly List<IRayInteractor> hoveringRayInteractors = new List<IRayInteractor>();
/// <summary>
/// (Read Only) The list of <see cref="IRayInteractor"/> components currently hovering this object.
/// </summary>]
public List<IRayInteractor> HoveringRayInteractors => hoveringRayInteractors;
#endregion Ray
#region TimedFlags
[SerializeField]
[Tooltip("Is this object selected by a gaze-pinch interactor?")]
private TimedFlag isGazePinchSelected = new TimedFlag();
/// <summary>
/// Is this object selected by a gaze-pinch interactor?
/// </summary>
public TimedFlag IsGazePinchSelected => isGazePinchSelected;
[SerializeField]
[Tooltip("Is this object selected by a non-gaze ray interactor?")]
private TimedFlag isRaySelected = new TimedFlag();
/// <summary>
/// Is this object selected by a non-gaze ray interactor?
/// </summary>
public TimedFlag IsRaySelected => isRaySelected;
[SerializeField]
[Tooltip("Is this object selected by a poke interactor?")]
private TimedFlag isPokeSelected = new TimedFlag();
/// <summary>
/// Is this object selected by a poke interactor?
/// </summary>
public TimedFlag IsPokeSelected => isPokeSelected;
[SerializeField]
[Tooltip("Is this object selected by a grab interactor?")]
private TimedFlag isGrabSelected = new TimedFlag();
/// <summary>
/// Is this object selected by a grab interactor?
/// </summary>
public TimedFlag IsGrabSelected => isGrabSelected;
[SerializeField]
[Tooltip("Is this object hovered by any gaze interactor?")]
private TimedFlag isGazeHovered = new TimedFlag();
/// <summary>
/// Is this object hovered by any gaze interactor?
/// </summary>
public TimedFlag IsGazeHovered => isGazeHovered;
[SerializeField]
[Tooltip("Is this object hovered by a gaze-pinch interactor?")]
private TimedFlag isGazePinchHovered = new TimedFlag();
/// <summary>
/// Is this object hovered by a gaze-pinch interactor?
/// </summary>
public TimedFlag IsGazePinchHovered => isGazePinchHovered;
[SerializeField]
[Tooltip("Is this object hovered by a non-gaze ray interactor?")]
private TimedFlag isRayHovered = new TimedFlag();
/// <summary>
/// Is this object hovered by a non-gaze ray interactor?
/// </summary>
public TimedFlag IsRayHovered => isRayHovered;
[SerializeField]
[Tooltip("Is this object hovered by a grab interactor?")]
private TimedFlag isGrabHovered = new TimedFlag();
/// <summary>
/// Is this object hovered by a grab interactor?
/// </summary>
public TimedFlag IsGrabHovered => isGrabHovered;
[SerializeField]
[Tooltip("Is this object hovered by a near touch/poke interactor?")]
[FormerlySerializedAs("isTouchHovered")]
private TimedFlag isPokeHovered = new TimedFlag();
/// <summary>
/// Is this object hovered by a near touch/poke interactor?
/// </summary>
public TimedFlag IsPokeHovered => isPokeHovered;
/// <summary>
/// Is this object hovered by any interactor other than passive targeting interactors?
/// </summary>
public TimedFlag IsActiveHovered => isActiveHovered;
[SerializeField]
[Tooltip("Is this object hovered by any interactor other than only passive targeting interactors?")]
private TimedFlag isActiveHovered = new TimedFlag();
#endregion
#region Interactor Filtering
// private field to ensure serialization
// todo: can we rework/get rid of this? in the NEAR FUTURE??
[SerializeField]
[Tooltip("Subtractively specifies the set of interactors allowed to select this interactable")]
[Implements(typeof(IXRInteractor), TypeGrouping.ByNamespaceFlat, AllowAbstract = true)]
private List<SystemInterfaceType> disabledInteractorTypes = new List<SystemInterfaceType>();
/// <summary>
/// Adds the specified type to the set of interactors which cannot select this interactable
/// </summary>
public void DisableInteractorType(SystemInterfaceType type)
{
if (!disabledInteractorTypes.Contains(type))
{
disabledInteractorTypes.Add(type);
}
}
/// <summary>
/// Removes the specified type from the set of interactors which cannot select this interactable
/// </summary>
public void EnableInteractorType(SystemInterfaceType type)
{
disabledInteractorTypes.Remove(type);
}
/// <summary>
/// Is the given type of interactor permitted to interact with this interactable?
/// </summary>
public bool IsInteractorTypeValid(IXRInteractor interactor)
{
// Cache queried interactor type to extract from hot loop.
Type interactorType = interactor.GetType();
foreach (SystemInterfaceType disabledType in disabledInteractorTypes)
{
if (disabledType.Type.IsAssignableFrom(interactorType))
{
return false;
}
}
return true;
}
#endregion Interactor Filtering
// Refcount-style trackers to avoid LINQs to query
// selection types.
private int raySelectCount = 0;
private int gazePinchSelectCount = 0;
private int grabSelectCount = 0;
private int pokeSelectCount = 0;
private int activeHoverCount = 0;
private void UpdateHoverFlags()
{
IsGazeHovered.Active = hoveringGazeInteractors.Count > 0;
IsGazePinchHovered.Active = hoveringGazePinchInteractors.Count > 0;
IsRayHovered.Active = hoveringRayInteractors.Count > 0;
IsGrabHovered.Active = hoveringGrabInteractors.Count > 0;
IsPokeHovered.Active = hoveringPokeInteractors.Count > 0;
IsActiveHovered.Active = activeHoverCount > 0;
}
private void UpdateSelectFlags(bool increment, IXRInteractor interactor)
{
if (interactor is IRayInteractor) { raySelectCount += increment ? 1 : -1; }
if (interactor is IGazePinchInteractor) { gazePinchSelectCount += increment ? 1 : -1; }
if (interactor is IGrabInteractor) { grabSelectCount += increment ? 1 : -1; }
if (interactor is IPokeInteractor) { pokeSelectCount += increment ? 1 : -1; }
isRaySelected.Active = raySelectCount > 0;
isGazePinchSelected.Active = gazePinchSelectCount > 0;
isGrabSelected.Active = grabSelectCount > 0;
isPokeSelected.Active = pokeSelectCount > 0;
}
#region XRI methods
/// <inheritdoc />
public override bool IsSelectableBy(IXRSelectInteractor interactor)
{
return base.IsSelectableBy(interactor) && IsInteractorTypeValid(interactor);
}
/// <inheritdoc />
public override bool IsHoverableBy(IXRHoverInteractor interactor)
{
return base.IsHoverableBy(interactor) && IsInteractorTypeValid(interactor);
}
/// <inheritdoc />
protected override void OnSelectEntering(SelectEnterEventArgs args)
{
base.OnSelectEntering(args);
UpdateSelectFlags(true, args.interactorObject);
if (args.interactorObject is IGazePinchInteractor gazePinchInteractor)
{
selectingGazePinchInteractors.Add(gazePinchInteractor);
}
}
/// <inheritdoc />
protected override void OnSelectExiting(SelectExitEventArgs args)
{
base.OnSelectExiting(args);
UpdateSelectFlags(false, args.interactorObject);
if (args.interactorObject is IGazePinchInteractor gazePinchInteractor)
{
selectingGazePinchInteractors.Remove(gazePinchInteractor);
}
}
/// <inheritdoc />
protected override void OnHoverEntering(HoverEnterEventArgs args)
{
base.OnHoverEntering(args);
if (args.interactorObject is IGazeInteractor gazeInteractor)
{
hoveringGazeInteractors.Add(gazeInteractor);
}
else // TODO: possibly move to IPassiveInteractor instead of hardcoding on gaze
{
activeHoverCount++;
}
if (args.interactorObject is IGazePinchInteractor gazePinchInteractor)
{
hoveringGazePinchInteractors.Add(gazePinchInteractor);
}
if (args.interactorObject is IGrabInteractor grabInteractor)
{
hoveringGrabInteractors.Add(grabInteractor);
}
if (args.interactorObject is IPokeInteractor pokeInteractor)
{
hoveringPokeInteractors.Add(pokeInteractor);
}
if (args.interactorObject is IRayInteractor rayInteractor)
{
hoveringRayInteractors.Add(rayInteractor);
}
UpdateHoverFlags();
}
/// <inheritdoc />
protected override void OnHoverExiting(HoverExitEventArgs args)
{
base.OnHoverExiting(args);
if (args.interactorObject is IGazeInteractor gazeInteractor)
{
hoveringGazeInteractors.Remove(gazeInteractor);
}
else // TODO: possibly move to IPassiveInteractor instead of hardcoding on gaze
{
if (activeHoverCount > 0) { activeHoverCount--; }
}
if (args.interactorObject is IGazePinchInteractor gazePinchInteractor)
{
hoveringGazePinchInteractors.Remove(gazePinchInteractor);
}
if (args.interactorObject is IGrabInteractor grabInteractor)
{
hoveringGrabInteractors.Remove(grabInteractor);
}
if (args.interactorObject is IPokeInteractor pokeInteractor)
{
hoveringPokeInteractors.Remove(pokeInteractor);
}
if (args.interactorObject is IRayInteractor rayInteractor)
{
hoveringRayInteractors.Remove(rayInteractor);
}
UpdateHoverFlags();
}
#endregion
}
}