forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteractionReceiver.cs
370 lines (322 loc) · 12.7 KB
/
InteractionReceiver.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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections.Generic;
using HoloToolkit.Unity.InputModule;
namespace HoloToolkit.Unity.Receivers
{
/// <summary>
/// An interaction receiver is simply a component that attached to a list of interactable objects and does something
/// based on events from those interactable objects. This is the base abstract class to extend from.
/// </summary>
public abstract class InteractionReceiver : MonoBehaviour, IInputHandler, IHoldHandler, IInputClickHandler, IManipulationHandler
{
#region Public Members
/// <summary>
/// List of linked interactable objects to receive events for
/// </summary>
[Tooltip("Target interactable Object to receive events for")]
public List<GameObject> interactables = new List<GameObject>();
/// <summary>
/// List of linked targets that the receiver affects
/// </summary>
[Tooltip("Targets for the receiver to ")]
public List<GameObject> Targets = new List<GameObject>();
/// <summary>
/// Flag for locking focus while selected
/// </summary>
public bool LockFocus
{
get
{
return lockFocus;
}
set
{
lockFocus = value;
CheckLockFocus(_selectingFocuser);
}
}
#endregion
#region Private and Protected Members
[Tooltip("If true, this object will remain the prime focus while select is held")]
[SerializeField]
private bool lockFocus = false;
/// <summary>
/// Protected focuser for the current selecting focuser
/// </summary>
protected IPointingSource _selectingFocuser;
#endregion
/// <summary>
/// On start subscribe to all interaction events on elements in the interactables list.
/// </summary>
public virtual void OnEnable()
{
InputManager.Instance.AddGlobalListener(gameObject);
FocusManager.Instance.PointerSpecificFocusChanged += OnPointerSpecificFocusChanged;
}
/// <summary>
/// On disable remove all linked interactables from the delegate functions
/// </summary>
public virtual void OnDisable()
{
if (InputManager.IsInitialized)
{
InputManager.Instance.RemoveGlobalListener(gameObject);
}
if (FocusManager.IsInitialized)
{
FocusManager.Instance.PointerSpecificFocusChanged -= OnPointerSpecificFocusChanged;
}
}
/// <summary>
/// Register an interactable with this receiver.
/// </summary>
/// <param name="interactable">takes a GameObject as the interactable to register.</param>
public virtual void Registerinteractable(GameObject interactable)
{
if (interactable == null || interactables.Contains(interactable))
{
return;
}
interactables.Add(interactable);
}
#if UNITY_EDITOR
/// <summary>
/// When selected draw lines to all linked interactables
/// </summary>
protected virtual void OnDrawGizmosSelected()
{
if (this.interactables.Count > 0)
{
GameObject[] bioList = this.interactables.ToArray();
for (int i = 0; i < bioList.Length; i++)
{
if (bioList[i] != null)
{
Gizmos.color = Color.green;
Gizmos.DrawLine(this.transform.position, bioList[i].transform.position);
}
}
}
if (this.Targets.Count > 0)
{
GameObject[] targetList = this.Targets.ToArray();
for (int i = 0; i < targetList.Length; i++)
{
if (targetList[i] != null)
{
Gizmos.color = Color.red;
Gizmos.DrawLine(this.transform.position, targetList[i].transform.position);
}
}
}
}
#endif
/// <summary>
/// Function to remove an interactable from the linked list.
/// </summary>
/// <param name="interactable"></param>
public virtual void Removeinteractable(GameObject interactable)
{
if (interactable != null && interactables.Contains(interactable))
{
interactables.Remove(interactable);
}
}
/// <summary>
/// Clear the interactables list and unregister them
/// </summary>
public virtual void Clearinteractables()
{
GameObject[] _intList = interactables.ToArray();
for (int i = 0; i < _intList.Length; i++)
{
this.Removeinteractable(_intList[i]);
}
}
/// <summary>
/// Is the game object interactable in our list of interactables
/// </summary>
/// <param name="interactable"></param>
/// <returns></returns>
protected bool Isinteractable(GameObject interactable)
{
return (interactables != null && interactables.Contains(interactable));
}
private void CheckLockFocus(IPointingSource focuser)
{
// If our previous selecting focuser isn't the same
if (_selectingFocuser != null && _selectingFocuser != focuser)
{
// If our focus is currently locked, unlock it before moving on
if (LockFocus)
{
_selectingFocuser.FocusLocked = false;
}
}
// Set to the new focuser
_selectingFocuser = focuser;
if (_selectingFocuser != null)
{
_selectingFocuser.FocusLocked = LockFocus;
}
}
private void LockFocuser(IPointingSource focuser)
{
if (focuser != null)
{
ReleaseFocuser();
_selectingFocuser = focuser;
_selectingFocuser.FocusLocked = true;
}
}
private void ReleaseFocuser()
{
if (_selectingFocuser != null)
{
_selectingFocuser.FocusLocked = false;
_selectingFocuser = null;
}
}
/// <summary>
/// Handle the pointer specific changes to fire focus enter and exit events
/// </summary>
/// <param name="pointer">The pointer associated with this focus change.</param>
/// <param name="oldFocusedObject">Object that was previously being focused.</param>
/// <param name="newFocusedObject">New object being focused.</param>
private void OnPointerSpecificFocusChanged(IPointingSource pointer, GameObject oldFocusedObject, GameObject newFocusedObject)
{
PointerSpecificEventData eventData = new PointerSpecificEventData(EventSystem.current);
eventData.Initialize(pointer);
if (newFocusedObject != null && Isinteractable(newFocusedObject))
{
FocusEnter(newFocusedObject, eventData);
}
if (oldFocusedObject != null && Isinteractable(oldFocusedObject))
{
FocusExit(oldFocusedObject, eventData);
}
CheckLockFocus(pointer);
}
#region Global Listener Callbacks
public void OnInputDown(InputEventData eventData)
{
if (Isinteractable(eventData.selectedObject))
{
InputDown(eventData.selectedObject, eventData);
}
}
public void OnInputUp(InputEventData eventData)
{
if (Isinteractable(eventData.selectedObject))
{
InputUp(eventData.selectedObject, eventData);
}
}
public void OnInputClicked(InputClickedEventData eventData)
{
if (Isinteractable(eventData.selectedObject))
{
InputClicked(eventData.selectedObject, eventData);
}
}
public void OnHoldStarted(HoldEventData eventData)
{
if (Isinteractable(eventData.selectedObject))
{
HoldStarted(eventData.selectedObject, eventData);
}
}
public void OnHoldCompleted(HoldEventData eventData)
{
if (Isinteractable(eventData.selectedObject))
{
HoldCompleted(eventData.selectedObject, eventData);
}
}
public void OnHoldCanceled(HoldEventData eventData)
{
if (Isinteractable(eventData.selectedObject))
{
HoldCanceled(eventData.selectedObject, eventData);
}
}
public void OnManipulationStarted(ManipulationEventData eventData)
{
if (Isinteractable(eventData.selectedObject))
{
ManipulationStarted(eventData.selectedObject, eventData);
}
}
public void OnManipulationUpdated(ManipulationEventData eventData)
{
if (Isinteractable(eventData.selectedObject))
{
ManipulationUpdated(eventData.selectedObject, eventData);
}
}
public void OnManipulationCompleted(ManipulationEventData eventData)
{
if (Isinteractable(eventData.selectedObject))
{
ManipulationCompleted(eventData.selectedObject, eventData);
}
}
public void OnManipulationCanceled(ManipulationEventData eventData)
{
if (Isinteractable(eventData.selectedObject))
{
ManipulationCanceled(eventData.selectedObject, eventData);
}
}
public void OnNavigationStarted(NavigationEventData eventData)
{
if (Isinteractable(eventData.selectedObject))
{
NavigationStarted(eventData.selectedObject, eventData);
}
}
public void OnNavigationUpdated(NavigationEventData eventData)
{
if (Isinteractable(eventData.selectedObject))
{
NavigationUpdated(eventData.selectedObject, eventData);
}
}
public void OnNavigationCompleted(NavigationEventData eventData)
{
if (Isinteractable(eventData.selectedObject))
{
NavigationCompleted(eventData.selectedObject, eventData);
}
}
public void OnNavigationCanceled(NavigationEventData eventData)
{
if (Isinteractable(eventData.selectedObject))
{
NavigationCanceled(eventData.selectedObject, eventData);
}
}
#endregion
#region Protected Virtual Callback Functions
protected virtual void FocusEnter(GameObject obj, PointerSpecificEventData eventData) { }
protected virtual void FocusExit(GameObject obj, PointerSpecificEventData eventData) { }
protected virtual void InputDown(GameObject obj, InputEventData eventData) { }
protected virtual void InputUp(GameObject obj, InputEventData eventData) { }
protected virtual void InputClicked(GameObject obj, InputClickedEventData eventData) { }
protected virtual void HoldStarted(GameObject obj, HoldEventData eventData) { }
protected virtual void HoldCompleted(GameObject obj, HoldEventData eventData) { }
protected virtual void HoldCanceled(GameObject obj, HoldEventData eventData) { }
protected virtual void ManipulationStarted(GameObject obj, ManipulationEventData eventData) { }
protected virtual void ManipulationUpdated(GameObject obj, ManipulationEventData eventData) { }
protected virtual void ManipulationCompleted(GameObject obj, ManipulationEventData eventData) { }
protected virtual void ManipulationCanceled(GameObject obj, ManipulationEventData eventData) { }
protected virtual void NavigationStarted(GameObject obj, NavigationEventData eventData) { }
protected virtual void NavigationUpdated(GameObject obj, NavigationEventData eventData) { }
protected virtual void NavigationCompleted(GameObject obj, NavigationEventData eventData) { }
protected virtual void NavigationCanceled(GameObject obj, NavigationEventData eventData) { }
#endregion
}
}