AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
InputSourcePointer.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 System;
5 using UnityEngine;
6 using UnityEngine.EventSystems;
7 
8 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
9 using UnityEngine.XR.WSA.Input;
10 #endif
11 
12 namespace HoloToolkit.Unity.InputModule
13 {
19  {
20  public IInputSource InputSource { get; set; }
21 
22  public uint InputSourceId { get; set; }
23 
24  public BaseRayStabilizer RayStabilizer { get; set; }
25 
26  public bool OwnAllInput { get; set; }
27 
28  [Obsolete("Will be removed in a later version. Use Rays instead.")]
29  public Ray Ray { get { return Rays[0]; } }
30 
31  public RayStep[] Rays
32  {
33  get
34  {
35  return rays;
36  }
37  }
38 
39  public PointerResult Result { get; set; }
40 
41  public float? ExtentOverride { get; set; }
42 
43  public LayerMask[] PrioritizedLayerMasksOverride { get; set; }
44 
45  public bool InteractionEnabled
46  {
47  get
48  {
49  return true;
50  }
51  }
52 
53  public bool FocusLocked { get; set; }
54 
55  public PointerLine PointerRay { get; set; }
56 
57  private RayStep[] rays = new RayStep[1] { new RayStep(Vector3.zero, Vector3.forward) };
58 
59  private bool selectPressed = false;
60 
61  [Obsolete("Will be removed in a later version. Use OnPreRaycast / OnPostRaycast instead.")]
62  public void UpdatePointer()
63  {
64  }
65 
66  public virtual void OnPreRaycast()
67  {
68  if (InputSource == null)
69  {
70  rays[0] = default(RayStep);
71  }
72  else
73  {
74  Debug.Assert(InputSource.SupportsInputInfo(InputSourceId, SupportedInputInfo.Pointing), string.Format("{0} with id {1} does not support pointing!", InputSource, InputSourceId));
75 
76 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
77  // For visualization with controllers, we don't want to use the event-based data the InputManager has.
78  // Instead, we query the source states manually here.
79  InteractionSourceState[] currentReading = InteractionManager.GetCurrentReading();
80  for (int i = 0; i < currentReading.Length; i++)
81  {
82  InteractionSourceState sourceState = currentReading[i];
83 
84  if (sourceState.source.id != InputSourceId)
85  {
86  continue;
87  }
88 
89  selectPressed = sourceState.selectPressed;
90 
91  Vector3 position;
92  Vector3 forward;
93 
94  if (!sourceState.sourcePose.TryGetPosition(out position, InteractionSourceNode.Pointer))
95  {
96  return;
97  }
98 
99  if (!sourceState.sourcePose.TryGetForward(out forward, InteractionSourceNode.Pointer))
100  {
101  return;
102  }
103 
104  if (CameraCache.Main.transform.parent != null)
105  {
106  position = CameraCache.Main.transform.parent.TransformPoint(position);
107  forward = CameraCache.Main.transform.parent.TransformDirection(forward);
108  }
109 
110  rays[0].CopyRay(new Ray(position, forward), FocusManager.Instance.GetPointingExtent(this));
111  }
112 #else
113  Ray pointingRay;
114  if (InputSource.TryGetPointingRay(InputSourceId, out pointingRay))
115  {
116  rays[0].CopyRay(pointingRay, FocusManager.Instance.GetPointingExtent(this));
117  }
118 #endif
119  }
120 
121  if (RayStabilizer != null)
122  {
123  RayStabilizer.UpdateStability(rays[0].Origin, rays[0].Direction);
124  rays[0].CopyRay(RayStabilizer.StableRay, FocusManager.Instance.GetPointingExtent(this));
125  }
126  }
127 
128  public virtual void OnPostRaycast()
129  {
130  if (PointerRay != null)
131  {
132  PointerRay.UpdateRenderedLine(rays, Result, selectPressed, FocusManager.Instance.GetPointingExtent(this));
133  }
134  }
135 
136  public bool OwnsInput(BaseEventData eventData)
137  {
138  return (OwnAllInput || InputIsFromSource(eventData));
139  }
140 
141  public bool InputIsFromSource(BaseEventData eventData)
142  {
143  var inputData = (eventData as IInputSourceInfoProvider);
144 
145  return (inputData != null)
146  && (inputData.InputSource == InputSource)
147  && (inputData.SourceId == InputSourceId);
148  }
149  }
150 }
void CopyRay(Ray ray, float rayLength)
Definition: RayStep.cs:38
SupportedInputInfo
Flags used to indicate which input information is supported by an input source.
A base class for a stabilizer that takes an input position and rotation, and performs operations on t...
Definition: Origin.cs:6
IInputSourceInfoProvider gives you the input source like hands or motion controller. It will also report the source id for that source.
The purpose of this class is to provide a cached reference to the main camera. Calling Camera...
Definition: CameraCache.cs:12
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
static Camera Main
Returns a cached reference to the main camera and uses Camera.main if it hasn&#39;t been cached yet...
Definition: CameraCache.cs:20
Interface for an input source. An input source can be anything that a user can use to interact with a...
Definition: IInputSource.cs:12
Focus manager is the bridge that handles different types of pointing sources like gaze cursor or poin...
Definition: FocusManager.cs:16
Implement this interface to register your pointer as a pointing source. This could be gaze based or m...
Class implementing IPointingSource to demonstrate how to create a pointing source. This is consumed by SimpleSinglePointerSelector.