AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
TouchscreenInputSource.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 System.Collections.Generic;
6 
7 namespace HoloToolkit.Unity.InputModule
8 {
17  {
18  const float kContactEpsilon = 2.0f/60.0f;
19 
20  [SerializeField]
21  [Tooltip("Time in seconds to determine if the contact registers as a tap or a hold")]
22  protected float MaxTapContactTime = 0.5f;
23 
24  private List<PersistentTouch> ActiveTouches = new List<PersistentTouch>(0);
25 
26  private class PersistentTouch
27  {
28  public Touch touchData;
29  public Ray screenpointRay;
30  public float lifetime;
31  public PersistentTouch(Touch touch, Ray ray)
32  {
33  touchData = touch;
34  this.screenpointRay = ray;
35  lifetime = 0.0f;
36  }
37  }
38 
39  #region Unity methods
40 
41  protected virtual void Start()
42  {
43  // Disable the inputsource if not supported by the device
44  if (!Input.touchSupported)
45  {
46  this.enabled = false;
47  }
48  }
49 
50  protected virtual void Update()
51  {
52  foreach (Touch touch in Input.touches)
53  {
54  // Construct a ray from the current touch coordinates
55  Ray ray = CameraCache.Main.ScreenPointToRay(touch.position);
56 
57  switch (touch.phase)
58  {
59  case TouchPhase.Began:
60  case TouchPhase.Moved:
61  case TouchPhase.Stationary:
62  UpdateTouch(touch, ray);
63  break;
64 
65  case TouchPhase.Ended:
66  case TouchPhase.Canceled:
67  RemoveTouch(touch);
68  break;
69  }
70  }
71  }
72 
73  #endregion // Unity methods
74 
75  #region Event generation logic
76 
77  public bool UpdateTouch(Touch touch, Ray ray)
78  {
79  PersistentTouch knownTouch = GetPersistentTouch(touch.fingerId);
80  if (knownTouch != null)
81  {
82  knownTouch.lifetime += Time.deltaTime;
83 
84  return true;
85  }
86  else
87  {
88  ActiveTouches.Add(new PersistentTouch(touch, ray));
89  OnHoldStartedEvent(touch.fingerId);
90  return false;
91  }
92  }
93 
94  public void RemoveTouch(Touch touch)
95  {
96  PersistentTouch knownTouch = GetPersistentTouch(touch.fingerId);
97  if (knownTouch != null)
98  {
99  if (touch.phase == TouchPhase.Ended)
100  {
101  if (knownTouch.lifetime < kContactEpsilon)
102  {
103  OnHoldCanceledEvent(touch.fingerId);
104  }
105  else if (knownTouch.lifetime < MaxTapContactTime)
106  {
107  OnHoldCanceledEvent(touch.fingerId);
108  OnTappedEvent(touch.fingerId, touch.tapCount);
109  }
110  else
111  {
112  OnHoldCompletedEvent(touch.fingerId);
113  }
114  }
115  else
116  {
117  OnHoldCanceledEvent(touch.fingerId);
118  }
119  ActiveTouches.Remove(knownTouch);
120  }
121  }
122 
123  #endregion // Event generation logic
124 
125  private PersistentTouch GetPersistentTouch(int id)
126  {
127  for (int i = 0; i < ActiveTouches.Count; ++i)
128  {
129  if (ActiveTouches[i].touchData.fingerId == id)
130  {
131  return ActiveTouches[i];
132  }
133  }
134  return null;
135  }
136 
137  private Touch? GetTouch(int id)
138  {
139  PersistentTouch knownTouch = GetPersistentTouch(id);
140  if (knownTouch != null)
141  {
142  return knownTouch.touchData;
143  }
144  else
145  {
146  return null;
147  }
148  }
149 
150  protected void OnTappedEvent(int id, int tapCount)
151  {
152  InputManager.Instance.RaiseInputClicked(this, (uint)id, InteractionSourcePressInfo.Select, tapCount);
153  }
154 
155  protected void OnHoldStartedEvent(int id)
156  {
157  InputManager.Instance.RaiseHoldStarted(this, (uint)id);
158  }
159 
160  protected void OnHoldCanceledEvent(int id)
161  {
162  InputManager.Instance.RaiseHoldCanceled(this, (uint)id);
163  }
164 
165  protected void OnHoldCompletedEvent(int id)
166  {
167  InputManager.Instance.RaiseHoldCompleted(this, (uint)id);
168  }
169 
170 
171  #region Base Input Source Methods
172 
173  public override bool TryGetSourceKind(uint sourceId, out InteractionSourceInfo sourceKind)
174  {
175  sourceKind = InteractionSourceInfo.Hand;
176  return true;
177  }
178 
179  public override bool TryGetPointerPosition(uint sourceId, out Vector3 position)
180  {
181  Touch? knownTouch = GetTouch((int)sourceId);
182  position = (knownTouch.HasValue) ? (Vector3)knownTouch.Value.position : Vector3.zero;
183  return knownTouch.HasValue;
184  }
185 
186  public override bool TryGetPointerRotation(uint sourceId, out Quaternion rotation)
187  {
188  rotation = Quaternion.identity;
189  return false;
190  }
191 
192  public override bool TryGetPointingRay(uint sourceId, out Ray pointingRay)
193  {
194  PersistentTouch knownTouch = GetPersistentTouch((int)sourceId);
195  if (knownTouch != null)
196  {
197  pointingRay = knownTouch.screenpointRay;
198  return true;
199  }
200  pointingRay = default(Ray);
201  return false;
202  }
203 
204  public override bool TryGetGripPosition(uint sourceId, out Vector3 position)
205  {
206  position = Vector3.zero;
207  return false;
208  }
209 
210  public override bool TryGetGripRotation(uint sourceId, out Quaternion rotation)
211  {
212  rotation = Quaternion.identity;
213  return false;
214  }
215 
216  public override SupportedInputInfo GetSupportedInputInfo(uint sourceId)
217  {
218  return SupportedInputInfo.PointerPosition | SupportedInputInfo.Pointing;
219  }
220 
221  public override bool TryGetThumbstick(uint sourceId, out bool isPressed, out Vector2 position)
222  {
223  isPressed = false;
224  position = Vector2.zero;
225  return false;
226  }
227 
228  public override bool TryGetTouchpad(uint sourceId, out bool isPressed, out bool isTouched, out Vector2 position)
229  {
230  isPressed = false;
231  isTouched = false;
232  position = Vector2.zero;
233  return false;
234  }
235 
236  public override bool TryGetSelect(uint sourceId, out bool isPressed, out double pressedAmount)
237  {
238  isPressed = false;
239  pressedAmount = 0.0;
240  return false;
241  }
242 
243  public override bool TryGetGrasp(uint sourceId, out bool isPressed)
244  {
245  isPressed = false;
246  return false;
247  }
248 
249  public override bool TryGetMenu(uint sourceId, out bool isPressed)
250  {
251  isPressed = false;
252  return false;
253  }
254 
255  #endregion // Base Input Source Methods
256 
257  }
258 }
override bool TryGetPointerPosition(uint sourceId, out Vector3 position)
Returns the position of the input source, if available. Not all input sources support positional info...
override bool TryGetPointingRay(uint sourceId, out Ray pointingRay)
Returns the pointing ray of the input source, if available. Not all input sources support pointing in...
Input Manager is responsible for managing input sources and dispatching relevant events to the approp...
Definition: InputManager.cs:19
override bool TryGetGripRotation(uint sourceId, out Quaternion rotation)
Returns the rotation of the input source, if available. Not all input sources support rotation inform...
override SupportedInputInfo GetSupportedInputInfo(uint sourceId)
Returns the input info that the input source can provide.
override bool TryGetPointerRotation(uint sourceId, out Quaternion rotation)
Returns the rotation of the input source, if available. Not all input sources support rotation inform...
override bool TryGetSelect(uint sourceId, out bool isPressed, out double pressedAmount)
SupportedInputInfo
Flags used to indicate which input information is supported by an input source.
override bool TryGetMenu(uint sourceId, out bool isPressed)
override bool TryGetSourceKind(uint sourceId, out InteractionSourceInfo sourceKind)
override bool TryGetThumbstick(uint sourceId, out bool isPressed, out Vector2 position)
override bool TryGetGrasp(uint sourceId, out bool isPressed)
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
override bool TryGetGripPosition(uint sourceId, out Vector3 position)
Returns the position of the input source, if available. Not all input sources support positional info...
Base class for an input source.
override bool TryGetTouchpad(uint sourceId, out bool isPressed, out bool isTouched, out Vector2 position)
Input source supporting basic touchscreen input: