AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
InteractionReceiver.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 using UnityEngine.EventSystems;
6 using System.Collections.Generic;
8 
9 namespace HoloToolkit.Unity.Receivers
10 {
16  {
17  #region Public Members
18  [Tooltip("Target interactable Object to receive events for")]
22  public List<GameObject> interactables = new List<GameObject>();
23 
27  [Tooltip("Targets for the receiver to ")]
28  public List<GameObject> Targets = new List<GameObject>();
29 
33  public bool LockFocus
34  {
35  get
36  {
37  return lockFocus;
38  }
39  set
40  {
41  lockFocus = value;
42  CheckLockFocus(_selectingFocuser);
43  }
44  }
45  #endregion
46 
47  #region Private and Protected Members
48  [Tooltip("If true, this object will remain the prime focus while select is held")]
49  [SerializeField]
50  private bool lockFocus = false;
51 
56  #endregion
57 
61  public virtual void OnEnable()
62  {
63  InputManager.Instance.AddGlobalListener(gameObject);
64  FocusManager.Instance.PointerSpecificFocusChanged += OnPointerSpecificFocusChanged;
65  }
66 
70  public virtual void OnDisable()
71  {
73  {
74  InputManager.Instance.RemoveGlobalListener(gameObject);
75  }
76 
78  {
79  FocusManager.Instance.PointerSpecificFocusChanged -= OnPointerSpecificFocusChanged;
80  }
81  }
82 
87  public virtual void Registerinteractable(GameObject interactable)
88  {
89  if (interactable == null || interactables.Contains(interactable))
90  {
91  return;
92  }
93 
94  interactables.Add(interactable);
95  }
96 
97 #if UNITY_EDITOR
98  protected virtual void OnDrawGizmosSelected()
102  {
103  if (this.interactables.Count > 0)
104  {
105  GameObject[] bioList = this.interactables.ToArray();
106 
107  for (int i = 0; i < bioList.Length; i++)
108  {
109  if (bioList[i] != null)
110  {
111  Gizmos.color = Color.green;
112  Gizmos.DrawLine(this.transform.position, bioList[i].transform.position);
113  }
114  }
115  }
116 
117  if (this.Targets.Count > 0)
118  {
119  GameObject[] targetList = this.Targets.ToArray();
120 
121  for (int i = 0; i < targetList.Length; i++)
122  {
123  if (targetList[i] != null)
124  {
125  Gizmos.color = Color.red;
126  Gizmos.DrawLine(this.transform.position, targetList[i].transform.position);
127  }
128  }
129  }
130  }
131 
132 #endif
133 
138  public virtual void Removeinteractable(GameObject interactable)
139  {
140  if (interactable != null && interactables.Contains(interactable))
141  {
142  interactables.Remove(interactable);
143  }
144  }
145 
149  public virtual void Clearinteractables()
150  {
151  GameObject[] _intList = interactables.ToArray();
152 
153  for (int i = 0; i < _intList.Length; i++)
154  {
155  this.Removeinteractable(_intList[i]);
156  }
157  }
158 
164  protected bool Isinteractable(GameObject interactable)
165  {
166  return (interactables != null && interactables.Contains(interactable));
167  }
168 
169  private void CheckLockFocus(IPointingSource focuser)
170  {
171  // If our previous selecting focuser isn't the same
172  if (_selectingFocuser != null && _selectingFocuser != focuser)
173  {
174  // If our focus is currently locked, unlock it before moving on
175  if (LockFocus)
176  {
177  _selectingFocuser.FocusLocked = false;
178  }
179  }
180 
181  // Set to the new focuser
182  _selectingFocuser = focuser;
183  if (_selectingFocuser != null)
184  {
185  _selectingFocuser.FocusLocked = LockFocus;
186  }
187  }
188 
189  private void LockFocuser(IPointingSource focuser)
190  {
191  if (focuser != null)
192  {
193  ReleaseFocuser();
194  _selectingFocuser = focuser;
195  _selectingFocuser.FocusLocked = true;
196  }
197  }
198 
199  private void ReleaseFocuser()
200  {
201  if (_selectingFocuser != null)
202  {
203  _selectingFocuser.FocusLocked = false;
204  _selectingFocuser = null;
205  }
206  }
207 
214  private void OnPointerSpecificFocusChanged(IPointingSource pointer, GameObject oldFocusedObject, GameObject newFocusedObject)
215  {
216  PointerSpecificEventData eventData = new PointerSpecificEventData(EventSystem.current);
217  eventData.Initialize(pointer);
218 
219  if (newFocusedObject != null && Isinteractable(newFocusedObject))
220  {
221  FocusEnter(newFocusedObject, eventData);
222  }
223 
224  if (oldFocusedObject != null && Isinteractable(oldFocusedObject))
225  {
226  FocusExit(oldFocusedObject, eventData);
227  }
228 
229  CheckLockFocus(pointer);
230  }
231 
232  #region Global Listener Callbacks
233  public void OnInputDown(InputEventData eventData)
234  {
235  if (Isinteractable(eventData.selectedObject))
236  {
237  InputDown(eventData.selectedObject, eventData);
238  }
239  }
240 
241  public void OnInputUp(InputEventData eventData)
242  {
243  if (Isinteractable(eventData.selectedObject))
244  {
245  InputUp(eventData.selectedObject, eventData);
246  }
247  }
248 
249  public void OnInputClicked(InputClickedEventData eventData)
250  {
251  if (Isinteractable(eventData.selectedObject))
252  {
253  InputClicked(eventData.selectedObject, eventData);
254  }
255  }
256 
257  public void OnHoldStarted(HoldEventData eventData)
258  {
259  if (Isinteractable(eventData.selectedObject))
260  {
261  HoldStarted(eventData.selectedObject, eventData);
262  }
263  }
264 
265  public void OnHoldCompleted(HoldEventData eventData)
266  {
267  if (Isinteractable(eventData.selectedObject))
268  {
269  HoldCompleted(eventData.selectedObject, eventData);
270  }
271  }
272 
273  public void OnHoldCanceled(HoldEventData eventData)
274  {
275  if (Isinteractable(eventData.selectedObject))
276  {
277  HoldCanceled(eventData.selectedObject, eventData);
278  }
279  }
280 
282  {
283  if (Isinteractable(eventData.selectedObject))
284  {
285  ManipulationStarted(eventData.selectedObject, eventData);
286  }
287  }
288 
290  {
291  if (Isinteractable(eventData.selectedObject))
292  {
293  ManipulationUpdated(eventData.selectedObject, eventData);
294  }
295  }
296 
298  {
299  if (Isinteractable(eventData.selectedObject))
300  {
301  ManipulationCompleted(eventData.selectedObject, eventData);
302  }
303  }
304 
306  {
307  if (Isinteractable(eventData.selectedObject))
308  {
309  ManipulationCanceled(eventData.selectedObject, eventData);
310  }
311  }
312 
314  {
315  if (Isinteractable(eventData.selectedObject))
316  {
317  NavigationStarted(eventData.selectedObject, eventData);
318  }
319  }
320 
322  {
323  if (Isinteractable(eventData.selectedObject))
324  {
325  NavigationUpdated(eventData.selectedObject, eventData);
326  }
327  }
328 
330  {
331  if (Isinteractable(eventData.selectedObject))
332  {
333  NavigationCompleted(eventData.selectedObject, eventData);
334  }
335  }
336 
338  {
339  if (Isinteractable(eventData.selectedObject))
340  {
341  NavigationCanceled(eventData.selectedObject, eventData);
342  }
343  }
344  #endregion
345 
346  #region Protected Virtual Callback Functions
347  protected virtual void FocusEnter(GameObject obj, PointerSpecificEventData eventData) { }
348  protected virtual void FocusExit(GameObject obj, PointerSpecificEventData eventData) { }
349 
350  protected virtual void InputDown(GameObject obj, InputEventData eventData) { }
351  protected virtual void InputUp(GameObject obj, InputEventData eventData) { }
352  protected virtual void InputClicked(GameObject obj, InputClickedEventData eventData) { }
353 
354  protected virtual void HoldStarted(GameObject obj, HoldEventData eventData) { }
355  protected virtual void HoldCompleted(GameObject obj, HoldEventData eventData) { }
356  protected virtual void HoldCanceled(GameObject obj, HoldEventData eventData) { }
357 
358  protected virtual void ManipulationStarted(GameObject obj, ManipulationEventData eventData) { }
359  protected virtual void ManipulationUpdated(GameObject obj, ManipulationEventData eventData) { }
360  protected virtual void ManipulationCompleted(GameObject obj, ManipulationEventData eventData) { }
361  protected virtual void ManipulationCanceled(GameObject obj, ManipulationEventData eventData) { }
362 
363  protected virtual void NavigationStarted(GameObject obj, NavigationEventData eventData) { }
364  protected virtual void NavigationUpdated(GameObject obj, NavigationEventData eventData) { }
365  protected virtual void NavigationCompleted(GameObject obj, NavigationEventData eventData) { }
366  protected virtual void NavigationCanceled(GameObject obj, NavigationEventData eventData) { }
367  #endregion
368 
369  }
370 }
virtual void NavigationCompleted(GameObject obj, NavigationEventData eventData)
void OnNavigationUpdated(NavigationEventData eventData)
virtual void OnDisable()
On disable remove all linked interactables from the delegate functions
virtual void OnEnable()
On start subscribe to all interaction events on elements in the interactables list.
Input Manager is responsible for managing input sources and dispatching relevant events to the approp...
Definition: InputManager.cs:19
virtual void ManipulationCanceled(GameObject obj, ManipulationEventData eventData)
virtual void InputUp(GameObject obj, InputEventData eventData)
void OnNavigationCanceled(NavigationEventData eventData)
Describes an input event that involves content manipulation.
virtual void InputDown(GameObject obj, InputEventData eventData)
virtual void HoldCompleted(GameObject obj, HoldEventData eventData)
void OnManipulationUpdated(ManipulationEventData eventData)
virtual void Removeinteractable(GameObject interactable)
Function to remove an interactable from the linked list.
virtual void NavigationUpdated(GameObject obj, NavigationEventData eventData)
virtual void ManipulationStarted(GameObject obj, ManipulationEventData eventData)
virtual void HoldCanceled(GameObject obj, HoldEventData eventData)
virtual void HoldStarted(GameObject obj, HoldEventData eventData)
Event dispatched associated with a specific pointer.
virtual void ManipulationCompleted(GameObject obj, ManipulationEventData eventData)
Interface to implement to react to simple click input.
void OnInputClicked(InputClickedEventData eventData)
virtual void Clearinteractables()
Clear the interactables list and unregister them
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
Describes an input event that involves content navigation.
Interface to implement to react to hold gestures.
Definition: IHoldHandler.cs:11
void OnManipulationCanceled(ManipulationEventData eventData)
virtual void NavigationCanceled(GameObject obj, NavigationEventData eventData)
virtual void ManipulationUpdated(GameObject obj, ManipulationEventData eventData)
IPointingSource _selectingFocuser
Protected focuser for the current selecting focuser
An interaction receiver is simply a component that attached to a list of interactable objects and doe...
void OnManipulationStarted(ManipulationEventData eventData)
virtual void Registerinteractable(GameObject interactable)
Register an interactable with this receiver.
Focus manager is the bridge that handles different types of pointing sources like gaze cursor or poin...
Definition: FocusManager.cs:16
void OnManipulationCompleted(ManipulationEventData eventData)
bool Isinteractable(GameObject interactable)
Is the game object interactable in our list of interactables
Describes an input event that involves a tap.
Interface to implement to react to simple pointer-like input.
Implement this interface to register your pointer as a pointing source. This could be gaze based or m...
void OnNavigationCompleted(NavigationEventData eventData)
virtual void NavigationStarted(GameObject obj, NavigationEventData eventData)
Event dispatched when a hold gesture is detected.
Describes an input event that has a source id and a press kind.
virtual void FocusEnter(GameObject obj, PointerSpecificEventData eventData)
virtual void InputClicked(GameObject obj, InputClickedEventData eventData)
void OnNavigationStarted(NavigationEventData eventData)
virtual void FocusExit(GameObject obj, PointerSpecificEventData eventData)
static bool IsInitialized
Returns whether the instance has been initialized or not.
Definition: Singleton.cs:58
Interface to implement to react to manipulation gestures.