Skip to content

Commit

Permalink
Merge pull request #1654 from thestonefox/fix/input-overrides
Browse files Browse the repository at this point in the history
fix(Input): ensure modifiers are not null and wait for controller ready
  • Loading branch information
thestonefox authored Dec 12, 2017
2 parents 3497214 + 87af970 commit d7f5346
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
9 changes: 7 additions & 2 deletions Assets/VRTK/Source/Scripts/Utilities/SDK/VRTK_SDKManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ public VRTK_SDKSetup loadedSetup
private Coroutine checkLeftControllerReadyRoutine = null;
private Coroutine checkRightControllerReadyRoutine = null;
private float checkControllerReadyDelay = 1f;
private int checkControllerValidTimer = 50;
#if UNITY_EDITOR
private BuildTargetGroup[] targetGroupsToExclude;
#endif
Expand Down Expand Up @@ -793,12 +794,14 @@ private void CheckControllersReady()
private IEnumerator CheckLeftControllerReady()
{
WaitForSeconds delayInstruction = new WaitForSeconds(checkControllerReadyDelay);
int maxCheckTime = checkControllerValidTimer;
while (true)
{
if (loadedSetup != null && loadedSetup.actualLeftController != null && loadedSetup.actualLeftController.activeInHierarchy)
if (loadedSetup != null && loadedSetup.actualLeftController != null && loadedSetup.actualLeftController.activeInHierarchy && (loadedSetup.controllerSDK.GetCurrentControllerType() != SDK_BaseController.ControllerType.Undefined || maxCheckTime < 0))
{
break;
}
maxCheckTime--;
yield return delayInstruction;
}
loadedSetup.controllerSDK.OnControllerReady(SDK_BaseController.ControllerHand.Left);
Expand All @@ -807,12 +810,14 @@ private IEnumerator CheckLeftControllerReady()
private IEnumerator CheckRightControllerReady()
{
WaitForSeconds delayInstruction = new WaitForSeconds(checkControllerReadyDelay);
int maxCheckTime = checkControllerValidTimer;
while (true)
{
if (loadedSetup != null && loadedSetup.actualRightController != null && loadedSetup.actualRightController.activeInHierarchy)
if (loadedSetup != null && loadedSetup.actualRightController != null && loadedSetup.actualRightController.activeInHierarchy && (loadedSetup.controllerSDK.GetCurrentControllerType() != SDK_BaseController.ControllerType.Undefined || maxCheckTime < 0))
{
break;
}
maxCheckTime--;
yield return delayInstruction;
}
loadedSetup.controllerSDK.OnControllerReady(SDK_BaseController.ControllerHand.Right);
Expand Down
42 changes: 23 additions & 19 deletions Assets/VRTK/Source/Scripts/Utilities/VRTK_SDKInputOverride.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,12 @@ protected override void ControllerReady(VRTK_ControllerReference controllerRefer

protected virtual VRTK_SDKButtonInputOverrideType GetSelectedModifier(List<VRTK_SDKButtonInputOverrideType> overrideTypes, VRTK_ControllerReference controllerReference)
{
VRTK_SDKButtonInputOverrideType selectedModifier = null;
//attempt to find by the overall SDK set up to start with
VRTK_SDKButtonInputOverrideType selectedModifier = overrideTypes.FirstOrDefault(item => item.loadedSDKSetup == sdkManager.loadedSetup);
if (sdkManager.loadedSetup != null)
{
selectedModifier = overrideTypes.FirstOrDefault(item => item.loadedSDKSetup == sdkManager.loadedSetup);
}

//If no sdk set up is found or it is null then try and find by the SDK controller
if (selectedModifier == null)
Expand Down Expand Up @@ -220,7 +224,7 @@ protected virtual void ManageInteractGrab()
{
VRTK_ControllerReference controllerReference = GetReferenceFromEvents(interactGrabScript.controllerEvents);
VRTK_SDKButtonInputOverrideType selectedModifier = GetSelectedModifier(interactGrabOverrides, controllerReference);
if (selectedModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedModifier != null && selectedModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
interactGrabScript.enabled = false;
interactGrabScript.grabButton = selectedModifier.overrideButton;
Expand All @@ -235,7 +239,7 @@ protected virtual void ManageInteractUse()
{
VRTK_ControllerReference controllerReference = GetReferenceFromEvents(interactUseScript.controllerEvents);
VRTK_SDKButtonInputOverrideType selectedModifier = GetSelectedModifier(interactUseOverrides, controllerReference);
if (selectedModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedModifier != null && selectedModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
interactUseScript.enabled = false;
interactUseScript.useButton = selectedModifier.overrideButton;
Expand All @@ -250,15 +254,15 @@ protected virtual void ManagePointer()
{
VRTK_ControllerReference controllerReference = GetReferenceFromEvents(pointerScript.controllerEvents);
VRTK_SDKButtonInputOverrideType selectedActivationModifier = GetSelectedModifier(pointerActivationOverrides, controllerReference);
if (selectedActivationModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedActivationModifier != null && selectedActivationModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
pointerScript.enabled = false;
pointerScript.activationButton = selectedActivationModifier.overrideButton;
pointerScript.enabled = true;
}

VRTK_SDKButtonInputOverrideType selectedSelectionModifier = GetSelectedModifier(pointerSelectionOverrides, controllerReference);
if (selectedSelectionModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedSelectionModifier != null && selectedSelectionModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
pointerScript.enabled = false;
pointerScript.selectionButton = selectedSelectionModifier.overrideButton;
Expand All @@ -273,15 +277,15 @@ protected virtual void ManageUIPointer()
{
VRTK_ControllerReference controllerReference = GetReferenceFromEvents(uiPointerScript.controllerEvents);
VRTK_SDKButtonInputOverrideType selectedActivationModifier = GetSelectedModifier(uiPointerActivationOverrides, controllerReference);
if (selectedActivationModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedActivationModifier != null && selectedActivationModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
uiPointerScript.enabled = false;
uiPointerScript.activationButton = selectedActivationModifier.overrideButton;
uiPointerScript.enabled = true;
}

VRTK_SDKButtonInputOverrideType selectedSelectionModifier = GetSelectedModifier(uiPointerSelectionOverrides, controllerReference);
if (selectedSelectionModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedSelectionModifier != null && selectedSelectionModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
uiPointerScript.enabled = false;
uiPointerScript.selectionButton = selectedSelectionModifier.overrideButton;
Expand All @@ -296,7 +300,7 @@ protected virtual void ManagePointerDirectionIndicator()
{
VRTK_ControllerReference controllerReference = GetReferenceFromEvents(pointerDirectionIndicatorScript.GetControllerEvents());
VRTK_SDKVector2AxisInputOverrideType selectedCoorinateModifier = GetSelectedModifier(directionIndicatorCoordinateOverrides, controllerReference);
if (selectedCoorinateModifier.overrideAxis != VRTK_ControllerEvents.Vector2AxisAlias.Undefined)
if (selectedCoorinateModifier != null && selectedCoorinateModifier.overrideAxis != VRTK_ControllerEvents.Vector2AxisAlias.Undefined)
{
pointerDirectionIndicatorScript.enabled = false;
pointerDirectionIndicatorScript.coordinateAxis = selectedCoorinateModifier.overrideAxis;
Expand All @@ -311,23 +315,23 @@ protected virtual void ManageTouchpadControl()
{
VRTK_ControllerReference controllerReference = GetReferenceFromEvents(touchpadControlScript.controller);
VRTK_SDKVector2AxisInputOverrideType selectedCoorinateModifier = GetSelectedModifier(touchpadControlCoordinateOverrides, controllerReference);
if (selectedCoorinateModifier.overrideAxis != VRTK_ControllerEvents.Vector2AxisAlias.Undefined)
if (selectedCoorinateModifier != null && selectedCoorinateModifier.overrideAxis != VRTK_ControllerEvents.Vector2AxisAlias.Undefined)
{
touchpadControlScript.enabled = false;
touchpadControlScript.coordinateAxis = selectedCoorinateModifier.overrideAxis;
touchpadControlScript.enabled = true;
}

VRTK_SDKButtonInputOverrideType selectedActivationModifier = GetSelectedModifier(touchpadControlActivationOverrides, controllerReference);
if (selectedActivationModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedActivationModifier != null && selectedActivationModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
touchpadControlScript.enabled = false;
touchpadControlScript.primaryActivationButton = selectedActivationModifier.overrideButton;
touchpadControlScript.enabled = true;
}

VRTK_SDKButtonInputOverrideType selectedActionModifier = GetSelectedModifier(touchpadControlModifierOverrides, controllerReference);
if (selectedActionModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedActionModifier != null && selectedActionModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
touchpadControlScript.enabled = false;
touchpadControlScript.actionModifierButton = selectedActionModifier.overrideButton;
Expand All @@ -342,31 +346,31 @@ protected virtual void ManageButtonControl()
{
VRTK_ControllerReference controllerReference = GetReferenceFromEvents(buttonControlScript.controller);
VRTK_SDKButtonInputOverrideType selectedForwardModifier = GetSelectedModifier(buttonControlForwardOverrides, controllerReference);
if (selectedForwardModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedForwardModifier != null && selectedForwardModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
buttonControlScript.enabled = false;
buttonControlScript.forwardButton = selectedForwardModifier.overrideButton;
buttonControlScript.enabled = true;
}

VRTK_SDKButtonInputOverrideType selectedBackwardModifier = GetSelectedModifier(buttonControlBackwardOverrides, controllerReference);
if (selectedBackwardModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedBackwardModifier != null && selectedBackwardModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
buttonControlScript.enabled = false;
buttonControlScript.backwardButton = selectedBackwardModifier.overrideButton;
buttonControlScript.enabled = true;
}

VRTK_SDKButtonInputOverrideType selectedLeftModifier = GetSelectedModifier(buttonControlLeftOverrides, controllerReference);
if (selectedLeftModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedLeftModifier != null && selectedLeftModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
buttonControlScript.enabled = false;
buttonControlScript.leftButton = selectedLeftModifier.overrideButton;
buttonControlScript.enabled = true;
}

VRTK_SDKButtonInputOverrideType selectedRightModifier = GetSelectedModifier(buttonControlRightOverrides, controllerReference);
if (selectedRightModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedRightModifier != null && selectedRightModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
buttonControlScript.enabled = false;
buttonControlScript.rightButton = selectedRightModifier.overrideButton;
Expand All @@ -381,15 +385,15 @@ protected virtual void ManageSlingshotJump()
{
VRTK_ControllerReference controllerReference = GetRightThenLeftReference();
VRTK_SDKButtonInputOverrideType selectedActivationModifier = GetSelectedModifier(slingshotJumpActivationOverrides, controllerReference);
if (selectedActivationModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedActivationModifier != null && selectedActivationModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
slingshotJumpScript.enabled = false;
slingshotJumpScript.SetActivationButton(selectedActivationModifier.overrideButton);
slingshotJumpScript.enabled = true;
}

VRTK_SDKButtonInputOverrideType selectedCancelModifier = GetSelectedModifier(slingshotJumpCancelOverrides, controllerReference);
if (selectedCancelModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedCancelModifier != null && selectedCancelModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
slingshotJumpScript.enabled = false;
slingshotJumpScript.SetCancelButton(selectedCancelModifier.overrideButton);
Expand All @@ -404,7 +408,7 @@ protected virtual void ManageMoveInPlace()
{
VRTK_ControllerReference controllerReference = GetRightThenLeftReference();
VRTK_SDKButtonInputOverrideType selectedEngageModifier = GetSelectedModifier(moveInPlaceEngageOverrides, controllerReference);
if (selectedEngageModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedEngageModifier != null && selectedEngageModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
moveInPlaceScript.enabled = false;
moveInPlaceScript.engageButton = selectedEngageModifier.overrideButton;
Expand All @@ -419,7 +423,7 @@ protected virtual void ManageStepMultiplier()
{
VRTK_ControllerReference controllerReference = GetReferenceFromEvents(stepMultiplierScript.controllerEvents);
VRTK_SDKButtonInputOverrideType selectedModifier = GetSelectedModifier(stepMultiplierActivationOverrides, controllerReference);
if (selectedModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
if (selectedModifier != null && selectedModifier.overrideButton != VRTK_ControllerEvents.ButtonAlias.Undefined)
{
stepMultiplierScript.enabled = false;
stepMultiplierScript.activationButton = selectedModifier.overrideButton;
Expand Down

0 comments on commit d7f5346

Please sign in to comment.