13 [RequireComponent(typeof(CustomInputControl))]
18 private class ButtonStates
22 IsSelectButtonDown =
false;
23 SelectButtonStateChanged =
false;
25 IsMenuButtonDown =
false;
26 MenuButtonStateChanged =
false;
29 GraspStateChanged =
false;
31 ManipulationInProgress =
false;
32 HoldInProgress =
false;
33 CumulativeDelta = Vector3.zero;
36 public bool IsSelectButtonDown;
37 public bool SelectButtonStateChanged;
38 public float SelectDownStartTime;
40 public bool IsMenuButtonDown;
41 public bool MenuButtonStateChanged;
43 public bool IsGrasped;
44 public bool GraspStateChanged;
46 public bool ManipulationInProgress;
47 public bool HoldInProgress;
48 public Vector3 CumulativeDelta;
49 public Vector3 CumulativeGripDelta;
52 [Tooltip(
"This property now represents Pointer position (contrast with Grip position)")]
54 [Tooltip(
"This property now represents Pointer rotation (contrast with Grip rotation)")]
64 [Tooltip(
"This property now represents controller's Pointer position (contrast with controller Grip position)")]
66 [Tooltip(
"This property now represents controller's Pointer rotation (contrast with controller Grip rotation)")]
70 Vector3 NavigatorValues = Vector3.zero;
71 Vector2 railUsedCurrently = Vector2.one;
72 bool isNavigatorUsingRails =
false;
80 private ButtonStates currentButtonStates;
82 private uint controllerId;
86 private bool currentlyVisible;
87 private bool visibilityChanged;
92 private const float MaxClickDuration = 0.5f;
95 [Tooltip(
"The total amount of input source movement that needs to happen to signal intent to start a manipulation. This is a distance, but not a distance in any one direction.")]
96 private float manipulationStartMovementThreshold = 0.03f;
100 Debug.Assert(sourceId == controllerId,
"Controller data requested for a mismatched source ID.");
104 if (SupportsPosition)
109 if (SupportsRotation)
119 if (SupportsGripPosition)
124 if (SupportsGripRotation)
129 if (SupportsMenuButton)
139 return supportedInputInfo;
144 Debug.Assert(sourceId == controllerId,
"Controller data requested for a mismatched source ID.");
146 sourceKind = SourceKind;
152 Debug.Assert(sourceId == controllerId,
"Controller data requested for a mismatched source ID.");
154 if (SupportsPosition)
156 position = ControllerPosition;
160 position = Vector3.zero;
166 Debug.Assert(sourceId == controllerId,
"Controller data requested for a mismatched source ID.");
168 if (SupportsRotation)
170 rotation = ControllerRotation;
174 rotation = Quaternion.identity;
180 Debug.Assert(sourceId == controllerId,
"Controller data requested for a mismatched source ID.");
182 if (SupportsRay && (PointingRay != null))
184 pointingRay = (Ray)PointingRay;
188 pointingRay =
default(Ray);
194 Debug.Assert(sourceId == controllerId,
"Controller data requested for a mismatched source ID.");
196 if (SupportsGripPosition)
198 position = ControllerGripPosition;
202 position = Vector3.zero;
208 Debug.Assert(sourceId == controllerId,
"Controller data requested for a mismatched source ID.");
210 if (SupportsGripRotation)
212 rotation = ControllerGripRotation;
216 rotation = Quaternion.identity;
220 public override bool TryGetThumbstick(uint sourceId, out
bool isPressed, out Vector2 position)
222 Debug.Assert(sourceId == controllerId,
"Controller data requested for a mismatched source ID.");
225 position = Vector2.zero;
229 public override bool TryGetTouchpad(uint sourceId, out
bool isPressed, out
bool isTouched, out Vector2 position)
231 Debug.Assert(sourceId == controllerId,
"Controller data requested for a mismatched source ID.");
235 position = Vector2.zero;
239 public override bool TryGetSelect(uint sourceId, out
bool isPressed, out
double pressedAmount)
241 Debug.Assert(sourceId == controllerId,
"Controller data requested for a mismatched source ID.");
248 public override bool TryGetGrasp(uint sourceId, out
bool isPressed)
250 Debug.Assert(sourceId == controllerId,
"Controller data requested for a mismatched source ID.");
254 isPressed = currentButtonStates.IsGrasped;
262 public override bool TryGetMenu(uint sourceId, out
bool isPressed)
264 Debug.Assert(sourceId == controllerId,
"Controller data requested for a mismatched source ID.");
266 if (SupportsMenuButton)
268 isPressed = currentButtonStates.IsMenuButtonDown;
278 if (!Application.isEditor)
284 manualController = GetComponent<CustomInputControl>();
286 currentButtonStates =
new ButtonStates();
287 currentlyVisible =
false;
288 visibilityChanged =
false;
289 controllerId = (uint)
Random.value;
292 if (inputSource != null)
298 private void Update()
300 if (!Application.isEditor) {
return; }
302 UpdateControllerData();
303 SendControllerVisibilityEvents();
306 private void OnEnable()
308 if (!Application.isEditor) {
return; }
313 private void OnDisable()
315 if (!Application.isEditor) {
return; }
317 DisconnectController();
320 private void ConnectController()
322 if (!RaiseEventsBasedOnVisibility)
328 private void DisconnectController()
330 if (!RaiseEventsBasedOnVisibility)
339 private void UpdateControllerData()
341 bool doUpdateState = !RaiseEventsBasedOnVisibility;
345 if (!currentlyVisible)
347 visibilityChanged =
true;
350 currentlyVisible =
true;
351 doUpdateState =
true;
355 if (currentlyVisible)
357 visibilityChanged =
true;
360 currentlyVisible =
false;
375 float time = manualController.
UseUnscaledTime ? Time.unscaledTime : Time.time;
377 currentButtonStates.SelectButtonStateChanged = (currentButtonStates.IsSelectButtonDown != source.
SelectPressed);
378 currentButtonStates.IsSelectButtonDown = source.
SelectPressed;
380 if (currentButtonStates.SelectButtonStateChanged && source.
SelectPressed)
382 currentButtonStates.SelectDownStartTime = time;
383 currentButtonStates.CumulativeDelta = Vector3.zero;
386 if (SupportsPosition)
388 Vector3 controllerPosition;
391 currentButtonStates.CumulativeDelta += controllerPosition - ControllerPosition;
392 ControllerPosition = controllerPosition;
396 if (SupportsRotation)
398 Quaternion controllerRotation;
401 ControllerRotation = controllerRotation;
410 if (SupportsGripPosition)
412 Vector3 controllerGripPosition;
415 currentButtonStates.CumulativeGripDelta += controllerGripPosition - ControllerGripPosition;
416 ControllerGripPosition = controllerGripPosition;
420 if (SupportsGripRotation)
422 Quaternion controllerGripRotation;
425 ControllerGripRotation = controllerGripRotation;
429 if (SupportsMenuButton)
431 currentButtonStates.MenuButtonStateChanged = (currentButtonStates.IsMenuButtonDown != source.
MenuPressed);
432 currentButtonStates.IsMenuButtonDown = source.
MenuPressed;
437 currentButtonStates.GraspStateChanged = (currentButtonStates.IsGrasped != source.
Grasped);
438 currentButtonStates.IsGrasped = source.
Grasped;
441 SendControllerStateEvents(time);
447 private void SendControllerStateEvents(
float time)
450 if (currentButtonStates.SelectButtonStateChanged)
452 if (currentButtonStates.IsSelectButtonDown)
460 if (currentButtonStates.ManipulationInProgress)
462 InputManager.
Instance.RaiseManipulationCompleted(
this, controllerId, currentButtonStates.CumulativeDelta);
463 currentButtonStates.ManipulationInProgress =
false;
467 NavigatorValues = Vector3.zero;
468 railUsedCurrently = Vector2.one;
471 else if (currentButtonStates.HoldInProgress)
474 currentButtonStates.HoldInProgress =
false;
487 else if (currentButtonStates.IsSelectButtonDown)
489 if (!currentButtonStates.ManipulationInProgress)
492 if (currentButtonStates.CumulativeDelta.magnitude > manipulationStartMovementThreshold)
495 if (currentButtonStates.HoldInProgress)
498 currentButtonStates.HoldInProgress =
false;
502 currentButtonStates.ManipulationInProgress =
true;
506 NavigatorValues = Vector3.zero;
507 if (isNavigatorUsingRails)
509 railUsedCurrently = (currentButtonStates.CumulativeDelta.x >= manipulationStartMovementThreshold) ?
new Vector2(1, 0) :
new Vector2(0, 1);
513 else if (!currentButtonStates.HoldInProgress && (time - currentButtonStates.SelectDownStartTime >= MaxClickDuration))
516 currentButtonStates.HoldInProgress =
true;
521 InputManager.
Instance.RaiseManipulationUpdated(
this, controllerId, currentButtonStates.CumulativeDelta);
524 NavigatorValues.x = Mathf.Clamp(currentButtonStates.CumulativeDelta.x*5, -1.0f, 1.0f) * railUsedCurrently.x;
525 NavigatorValues.y = Mathf.Clamp(currentButtonStates.CumulativeDelta.y*5, -1.0f, 1.0f) * railUsedCurrently.y;
530 if (currentButtonStates.MenuButtonStateChanged)
532 if (currentButtonStates.IsMenuButtonDown)
542 if (currentButtonStates.GraspStateChanged)
544 if (currentButtonStates.IsGrasped)
558 private void SendControllerVisibilityEvents()
561 if (RaiseEventsBasedOnVisibility && visibilityChanged)
563 if (currentlyVisible)
572 visibilityChanged =
false;