AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
GamePadInputSource.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 
7 namespace HoloToolkit.Unity.InputModule
8 {
10  {
11  protected const string XboxController = "Xbox Controller";
12  protected const string XboxOneForWindows = "Xbox One For Windows";
13  protected const string XboxBluetoothGamePad = "Xbox Bluetooth Gamepad";
14  protected const string XboxWirelessController = "Xbox Wireless Controller";
15  protected const string MotionControllerLeft = "Spatial Controller - Left";
16  protected const string MotionControllerRight = "Spatial Controller - Right";
17 
18  protected uint SourceId;
19 
20  [SerializeField]
21  [Tooltip("Time in seconds to determine if an Input Device has been connected or disconnected")]
22  protected float DeviceRefreshInterval = 3.0f;
23  protected int LastDeviceUpdateCount;
24  protected string[] LastDeviceList;
25 
26  protected StandaloneInputModule InputModule;
27  protected const string DefaultHorizontalAxis = "Horizontal";
28  protected const string DefaultVerticalAxis = "Vertical";
29  protected const string DefaultSubmitButton = "Submit";
30  protected const string DefaultCancelButton = "Cancel";
31  protected const bool DefaultForceActiveState = false;
32 
33  protected string PreviousHorizontalAxis;
34  protected string PreviousVerticalAxis;
35  protected string PreviousSubmitButton;
36  protected string PreviousCancelButton;
37  protected bool PreviousForceActiveState;
38 
39  private float deviceRefreshTimer;
40 
41  #region Unity methods
42 
43  protected virtual void Awake()
44  {
45  InputModule = FindObjectOfType<StandaloneInputModule>();
46 
47  if (InputModule == null)
48  {
49  Debug.LogError("Missing the Standalone Input Module for GamePad Input Source!\n" +
50  "Ensure you have an Event System in your scene.");
51  }
52  }
53 
54  protected virtual void Update()
55  {
56  deviceRefreshTimer += Time.unscaledDeltaTime;
57 
58  if (deviceRefreshTimer >= DeviceRefreshInterval)
59  {
60  deviceRefreshTimer = 0.0f;
61  RefreshDevices();
62  }
63  }
64 
65  #endregion // Unity methods
66 
67  protected virtual void RefreshDevices()
68  {
69  var joystickNames = Input.GetJoystickNames();
70 
71  if (joystickNames.Length <= 0) { return; }
72 
73  for (var i = 0; i < joystickNames.Length; i++)
74  {
75  Debug.LogWarningFormat("Joystick \"{0}\" has not been setup with the input manager. Create a new class that inherits from \"GamePadInputSource\" and implement it.", joystickNames[i]);
76  }
77  }
78 
79  #region Base Input Source Methods
80 
81  public override bool TryGetSourceKind(uint sourceId, out InteractionSourceInfo sourceKind)
82  {
83  sourceKind = InteractionSourceInfo.Controller;
84  return true;
85  }
86 
87  public override bool TryGetPointerPosition(uint sourceId, out Vector3 position)
88  {
89  position = Vector3.zero;
90  return false;
91  }
92 
93  public override bool TryGetPointerRotation(uint sourceId, out Quaternion rotation)
94  {
95  rotation = Quaternion.identity;
96  return false;
97  }
98 
99  public override bool TryGetPointingRay(uint sourceId, out Ray pointingRay)
100  {
101  pointingRay = default(Ray);
102  return false;
103  }
104 
105  public override bool TryGetGripPosition(uint sourceId, out Vector3 position)
106  {
107  position = Vector3.zero;
108  return false;
109  }
110 
111  public override bool TryGetGripRotation(uint sourceId, out Quaternion rotation)
112  {
113  rotation = Quaternion.identity;
114  return false;
115  }
116 
117  public override SupportedInputInfo GetSupportedInputInfo(uint sourceId)
118  {
119  return SupportedInputInfo.None;
120  }
121 
122  public override bool TryGetThumbstick(uint sourceId, out bool isPressed, out Vector2 position)
123  {
124  isPressed = false;
125  position = Vector2.zero;
126  return false;
127  }
128 
129  public override bool TryGetTouchpad(uint sourceId, out bool isPressed, out bool isTouched, out Vector2 position)
130  {
131  isPressed = false;
132  isTouched = false;
133  position = Vector2.zero;
134  return false;
135  }
136 
137  public override bool TryGetSelect(uint sourceId, out bool isPressed, out double pressedAmount)
138  {
139  isPressed = false;
140  pressedAmount = 0.0;
141  return false;
142  }
143 
144  public override bool TryGetGrasp(uint sourceId, out bool isPressed)
145  {
146  isPressed = false;
147  return false;
148  }
149 
150  public override bool TryGetMenu(uint sourceId, out bool isPressed)
151  {
152  isPressed = false;
153  return false;
154  }
155 
156  #endregion // Base Input Source Methods
157  }
158 }
override bool TryGetTouchpad(uint sourceId, out bool isPressed, out bool isTouched, out Vector2 position)
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 TryGetPointerPosition(uint sourceId, out Vector3 position)
Returns the position of the input source, if available. Not all input sources support positional info...
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 TryGetSelect(uint sourceId, out bool isPressed, out double pressedAmount)
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 TryGetGrasp(uint sourceId, out bool isPressed)
Base class for an input source.
override bool TryGetThumbstick(uint sourceId, out bool isPressed, out Vector2 position)
override SupportedInputInfo GetSupportedInputInfo(uint sourceId)
Returns the input info that the input source can provide.