AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
GamepadInput.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 
7 #if UNITY_WSA
8 #if UNITY_2017_2_OR_NEWER
9 using UnityEngine.XR.WSA.Input;
10 #else
11 using UnityEngine.VR.WSA.Input;
12 #endif
13 #endif
14 
15 namespace HoloToolkit.Unity.InputModule
16 {
23  [Obsolete("Please use XboxControllerHandlerBase")]
25  {
26  [Tooltip("Game pad button to press for air tap.")]
27  public string GamePadButtonA = "XBOX_A";
28 
29  [Tooltip("Change this value to give a different source id to your controller.")]
30  public uint GamePadId = 50000;
31 
32  [Tooltip("Elapsed time for hold started gesture in seconds.")]
33  public float HoldStartedInterval = 2.0f;
34  [Tooltip("Elapsed time for hold completed gesture in seconds.")]
35  public float HoldCompletedInterval = 3.0f;
36 
37  [Tooltip("Name of the joystick axis that navigates around X.")]
38  public string NavigateAroundXAxisName = "CONTROLLER_LEFT_STICK_HORIZONTAL";
39 
40  [Tooltip("Name of the joystick axis that navigates around Y.")]
41  public string NavigateAroundYAxisName = "CONTROLLER_LEFT_STICK_VERTICAL";
42 
43  bool isAPressed = false;
44  bool holdStarted = false;
45  bool raiseOnce = false;
46  bool navigationStarted = false;
47  bool navigationCompleted = false;
48 
49  private InputManager inputManager;
50 
51  private enum GestureState
52  {
53  APressed,
54  NavigationStarted,
55  NavigationCompleted,
56  HoldStarted,
57  HoldCompleted,
58  HoldCanceled
59  }
60 
61  private GestureState currentGestureState;
62 
63  protected virtual void Start()
64  {
66  {
67  inputManager = InputManager.Instance;
68  }
69 
70  if (inputManager == null)
71  {
72  Debug.LogError("Ensure your scene has the InputManager prefab.");
73  Destroy(this);
74  }
75  }
76 
77  private void Update()
78  {
79 #if UNITY_WSA
80  if (InteractionManager.numSourceStates > 0)
81  {
82  return;
83  }
84 #endif
85 
86  HandleGamepadAPressed();
87  }
88 
89  private void HandleGamepadAPressed()
90  {
91  // TODO: Should this handle Submit from Edit > ProjectSettings > Input ?
92  if (Input.GetButtonDown(GamePadButtonA))
93  {
94  inputManager.RaiseSourceDown(this, GamePadId, InteractionSourcePressInfo.Select);
95  isAPressed = true;
96  navigationCompleted = false;
97  currentGestureState = GestureState.APressed;
98  }
99 
100  if (isAPressed)
101  {
102  HandleNavigation();
103 
104  if (!holdStarted && !raiseOnce && !navigationStarted)
105  {
106  // Raise hold started when user has held A down for certain interval.
107  Invoke("HandleHoldStarted", HoldStartedInterval);
108  }
109 
110  // Check if we get a subsequent release on A.
111  HandleGamepadAReleased();
112  }
113  }
114 
115  private void HandleNavigation()
116  {
117  if (navigationCompleted)
118  {
119  return;
120  }
121 
122  float displacementAlongX = 0.0f;
123  float displacementAlongY = 0.0f;
124 
125  try
126  {
127  displacementAlongX = Input.GetAxis(NavigateAroundXAxisName);
128  displacementAlongY = Input.GetAxis(NavigateAroundYAxisName);
129  }
130  catch (Exception)
131  {
132  Debug.LogWarningFormat("Ensure you have Edit > ProjectSettings > Input > Axes set with values: {0} and {1}",
133  NavigateAroundXAxisName, NavigateAroundYAxisName);
134  navigationCompleted = true; // just so we don't keep logging the same message.
135  return;
136  }
137 
138  if (displacementAlongX != 0.0f || displacementAlongY != 0.0f || navigationStarted)
139  {
140  Vector3 normalizedOffset = new Vector3(displacementAlongX, displacementAlongY, 0.0f);
141 
142  if (!navigationStarted)
143  {
144  currentGestureState = GestureState.NavigationStarted;
145  navigationStarted = true;
146  // Raise navigation started event.
147  inputManager.RaiseNavigationStarted(this, GamePadId);
148  }
149 
150  // Raise navigation updated event.
151  inputManager.RaiseNavigationUpdated(this, GamePadId, normalizedOffset);
152  }
153  }
154 
155  private void HandleGamepadAReleased()
156  {
157  if (Input.GetButtonUp(GamePadButtonA))
158  {
159  inputManager.RaiseSourceUp(this, GamePadId, InteractionSourcePressInfo.Select);
160 
161  switch (currentGestureState)
162  {
163  case GestureState.NavigationStarted:
164  navigationCompleted = true;
165  CancelInvoke("HandleHoldStarted");
166  CancelInvoke("HandleHoldCompleted");
167  inputManager.RaiseNavigationCompleted(this, GamePadId, Vector3.zero);
168  Reset();
169  break;
170 
171  case GestureState.HoldStarted:
172  CancelInvoke("HandleHoldCompleted");
173  inputManager.RaiseHoldCanceled(this, GamePadId);
174  Reset();
175  break;
176 
177  case GestureState.HoldCompleted:
178  inputManager.RaiseHoldCompleted(this, GamePadId);
179  Reset();
180  break;
181 
182  default:
183  CancelInvoke("HandleHoldStarted");
184  CancelInvoke("HandleHoldCompleted");
185  inputManager.RaiseInputClicked(this, GamePadId, InteractionSourcePressInfo.Select, 1);
186  Reset();
187  break;
188  }
189  }
190  }
191 
192  private void Reset()
193  {
194  isAPressed = false;
195  holdStarted = false;
196  raiseOnce = false;
197  navigationStarted = false;
198  }
199 
200  private void HandleHoldStarted()
201  {
202  if (raiseOnce || currentGestureState == GestureState.HoldStarted || currentGestureState == GestureState.NavigationStarted)
203  {
204  return;
205  }
206 
207  holdStarted = true;
208 
209  currentGestureState = GestureState.HoldStarted;
210  inputManager.RaiseHoldStarted(this, GamePadId);
211  raiseOnce = true;
212 
213  Invoke("HandleHoldCompleted", HoldCompletedInterval);
214  }
215 
216  private void HandleHoldCompleted()
217  {
218  currentGestureState = GestureState.HoldCompleted;
219  }
220 
221  #region BaseInputSource Events
222  public override SupportedInputInfo GetSupportedInputInfo(uint sourceId)
223  {
224  // Since the game pad is not a 3dof or 6dof controller.
225  return SupportedInputInfo.None;
226  }
227 
228  public override bool TryGetPointerPosition(uint sourceId, out Vector3 position)
229  {
230  position = Vector3.zero;
231  return false;
232  }
233 
234  public override bool TryGetPointerRotation(uint sourceId, out Quaternion rotation)
235  {
236  rotation = Quaternion.identity;
237  return false;
238  }
239 
240  public override bool TryGetPointingRay(uint sourceId, out Ray pointingRay)
241  {
242  pointingRay = new Ray(Vector3.zero, Vector3.zero);
243  return false;
244  }
245 
246  public override bool TryGetGripPosition(uint sourceId, out Vector3 position)
247  {
248  position = Vector3.zero;
249  return false;
250  }
251 
252  public override bool TryGetGripRotation(uint sourceId, out Quaternion rotation)
253  {
254  rotation = Quaternion.identity;
255  return false;
256  }
257 
258  public override bool TryGetSourceKind(uint sourceId, out InteractionSourceInfo sourceKind)
259  {
260  sourceKind = InteractionSourceInfo.Controller;
261  return true;
262  }
263 
264  public override bool TryGetThumbstick(uint sourceId, out bool isPressed, out Vector2 position)
265  {
266  isPressed = false;
267  position = Vector2.zero;
268  return false;
269  }
270 
271  public override bool TryGetTouchpad(uint sourceId, out bool isPressed, out bool isTouched, out Vector2 position)
272  {
273  isPressed = false;
274  isTouched = false;
275  position = Vector2.zero;
276  return false;
277  }
278 
279  public override bool TryGetSelect(uint sourceId, out bool isPressed, out double pressedAmount)
280  {
281  isPressed = false;
282  pressedAmount = 0.0;
283  return false;
284  }
285 
286  public override bool TryGetGrasp(uint sourceId, out bool isPressed)
287  {
288  isPressed = false;
289  return false;
290  }
291 
292  public override bool TryGetMenu(uint sourceId, out bool isPressed)
293  {
294  isPressed = false;
295  return false;
296  }
297  #endregion BaseInputSource Events
298  }
299 }
override bool TryGetMenu(uint sourceId, out bool isPressed)
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 bool TryGetPointingRay(uint sourceId, out Ray pointingRay)
Returns the pointing ray of the input source, if available. Not all input sources support pointing in...
override bool TryGetSourceKind(uint sourceId, out InteractionSourceInfo sourceKind)
override bool TryGetThumbstick(uint sourceId, out bool isPressed, out Vector2 position)
void RaiseNavigationUpdated(IInputSource source, uint sourceId, Vector3 normalizedOffset, object[] tags=null)
override bool TryGetGrasp(uint sourceId, out bool isPressed)
override bool TryGetGripPosition(uint sourceId, out Vector3 position)
Returns the position of the input source, if available. Not all input sources support positional info...
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.
void RaiseHoldStarted(IInputSource source, uint sourceId, object[] tags=null)
override SupportedInputInfo GetSupportedInputInfo(uint sourceId)
Returns the input info that the input source can provide.
void RaiseHoldCompleted(IInputSource source, uint sourceId, object[] tags=null)
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
void RaiseSourceUp(IInputSource source, uint sourceId, InteractionSourcePressInfo pressType, object[] tags=null)
void RaiseNavigationCompleted(IInputSource source, uint sourceId, Vector3 normalizedOffset, object[] tags=null)
void RaiseHoldCanceled(IInputSource source, uint sourceId, object[] tags=null)
Base class for an input source.
void RaiseSourceDown(IInputSource source, uint sourceId, InteractionSourcePressInfo pressType, object[] tags=null)
override bool TryGetTouchpad(uint sourceId, out bool isPressed, out bool isTouched, out Vector2 position)
void RaiseNavigationStarted(IInputSource source, uint sourceId, object[] tags=null)
GamepadInput class maps Xbox GamePad buttons to the GestureRecognizer. Gamepad button A press and rel...
Definition: GamepadInput.cs:24
void RaiseInputClicked(IInputSource source, uint sourceId, InteractionSourcePressInfo pressType, int tapCount, object[] tags=null)
static bool IsInitialized
Returns whether the instance has been initialized or not.
Definition: Singleton.cs:58
override bool TryGetPointerPosition(uint sourceId, out Vector3 position)
Returns the position of the input source, if available. Not all input sources support positional info...