Skip to content

Commit d45a36b

Browse files
committed
update tracking state according to device changes
1 parent 7fae50e commit d45a36b

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

Packages/com.unity.inputsystem/InputSystem/Plugins/XR/TrackedPoseDriver.cs

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ protected virtual void Awake()
432432
protected void OnEnable()
433433
{
434434
InputSystem.onAfterUpdate += UpdateCallback;
435+
InputSystem.onDeviceChange += OnDeviceChanged;
435436
BindActions();
436437

437438
// Read current input values when becoming enabled,
@@ -446,6 +447,7 @@ protected void OnDisable()
446447
{
447448
UnbindActions();
448449
InputSystem.onAfterUpdate -= UpdateCallback;
450+
InputSystem.onDeviceChange -= OnDeviceChanged;
449451
}
450452

451453
/// <summary>
@@ -484,7 +486,7 @@ protected void UpdateCallback()
484486
else
485487
m_CurrentRotation = transform.localRotation;
486488

487-
ReadTrackingState(hasResolvedPositionInputControl, hasResolvedRotationInputControl);
489+
ReadTrackingState();
488490

489491
m_IsFirstUpdate = false;
490492
}
@@ -495,7 +497,38 @@ protected void UpdateCallback()
495497
OnUpdate();
496498
}
497499

498-
void ReadTrackingState(bool hasResolvedPositionInputControl, bool hasResolvedRotationInputControl)
500+
void OnDeviceChanged(InputDevice inputDevice, InputDeviceChange inputDeviceChange)
501+
{
502+
ReadTrackingStateWithoutTrackingAction();
503+
}
504+
505+
/// <summary>
506+
/// React to changes of devices to stop the tracking of position / rotation or both if a device is removed, starts the tracking if
507+
/// a device is added.
508+
/// </summary>
509+
void ReadTrackingStateWithoutTrackingAction()
510+
{
511+
var trackingStateAction = m_TrackingStateInput.action;
512+
if (trackingStateAction != null && trackingStateAction.m_BindingsCount != 0)
513+
return;
514+
515+
var hasResolvedPositionInputControl = HasResolvedControl(m_PositionInput.action);
516+
var hasResolvedRotationInputControl = HasResolvedControl(m_RotationInput.action);
517+
518+
// Treat an Input Action Reference with no reference the same as
519+
// an enabled Input Action with no authored bindings, and allow driving the Transform pose.
520+
// Check if we have transform and rotation controls to drive the pose.
521+
if (hasResolvedPositionInputControl && hasResolvedRotationInputControl)
522+
m_CurrentTrackingState = TrackingStates.Position | TrackingStates.Rotation;
523+
else if (hasResolvedPositionInputControl)
524+
m_CurrentTrackingState = TrackingStates.Position;
525+
else if (hasResolvedRotationInputControl)
526+
m_CurrentTrackingState = TrackingStates.Rotation;
527+
else
528+
m_CurrentTrackingState = TrackingStates.None;
529+
}
530+
531+
void ReadTrackingState()
499532
{
500533
var trackingStateAction = m_TrackingStateInput.action;
501534
if (trackingStateAction != null && !trackingStateAction.enabled)
@@ -504,29 +537,16 @@ void ReadTrackingState(bool hasResolvedPositionInputControl, bool hasResolvedRot
504537
m_CurrentTrackingState = TrackingStates.None;
505538
return;
506539
}
507-
508-
if (trackingStateAction == null || trackingStateAction.m_BindingsCount == 0)
509-
{
510-
// Treat an Input Action Reference with no reference the same as
511-
// an enabled Input Action with no authored bindings, and allow driving the Transform pose.
512-
// Check if we have transform and rotation controls to drive the pose.
513-
if (hasResolvedPositionInputControl && hasResolvedRotationInputControl)
514-
m_CurrentTrackingState = TrackingStates.Position | TrackingStates.Rotation;
515-
else if (hasResolvedPositionInputControl)
516-
m_CurrentTrackingState = TrackingStates.Position;
517-
else if (hasResolvedRotationInputControl)
518-
m_CurrentTrackingState = TrackingStates.Rotation;
519-
else
520-
m_CurrentTrackingState = TrackingStates.None;
521-
}
522-
else if (HasResolvedControl(trackingStateAction))
540+
if (HasResolvedControl(trackingStateAction))
523541
{
524542
// Retain the current value if there is no resolved binding.
525543
// Since the field initializes to allowing position and rotation,
526544
// this allows for driving the Transform pose always when the device
527545
// doesn't support reporting the tracking state.
528546
m_CurrentTrackingState = (TrackingStates)trackingStateAction.ReadValue<int>();
547+
return;
529548
}
549+
ReadTrackingStateWithoutTrackingAction();
530550
}
531551

532552
/// <summary>

0 commit comments

Comments
 (0)