Skip to content

Commit c4bf5d8

Browse files
committed
feat(Locomotion): allow alternative axis for touchpad control
The Touchpad Control script can now use an alternative coordinate axis for the control, such as touchpad two.
1 parent 649c5bd commit c4bf5d8

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

Assets/VRTK/Documentation/API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,7 @@ Provides the ability to control a GameObject's position based on the position of
22222222

22232223
### Inspector Parameters
22242224

2225+
* **Coordinate Axis:** The axis to use for the direction coordinates.
22252226
* **Primary Activation Button:** An optional button that has to be engaged to allow the touchpad control to activate.
22262227
* **Action Modifier Button:** An optional button that when engaged will activate the modifier on the touchpad control action.
22272228
* **Axis Deadzone:** A deadzone threshold on the touchpad that will ignore input if the touch position is within the specified deadzone. Between `0f` and `1f`.

Assets/VRTK/Source/Scripts/Locomotion/VRTK_TouchpadControl.cs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class VRTK_TouchpadControl : VRTK_ObjectControl
2828
{
2929
[Header("Touchpad Control Settings")]
3030

31+
[Tooltip("The axis to use for the direction coordinates.")]
32+
public SDK_BaseController.Vector2Axis coordinateAxis = SDK_BaseController.Vector2Axis.Touchpad;
3133
[Tooltip("An optional button that has to be engaged to allow the touchpad control to activate.")]
3234
public VRTK_ControllerEvents.ButtonAlias primaryActivationButton = VRTK_ControllerEvents.ButtonAlias.TouchpadTouch;
3335
[Tooltip("An optional button that when engaged will activate the modifier on the touchpad control action.")]
@@ -38,12 +40,14 @@ public class VRTK_TouchpadControl : VRTK_ObjectControl
3840
protected bool touchpadFirstChange;
3941
protected bool otherTouchpadControlEnabledState;
4042
protected bool otherTouchpadControlEnabledStateSet;
43+
protected VRTK_ControllerEvents.ButtonAlias coordniateButtonAlias;
4144

4245
protected override void OnEnable()
4346
{
4447
base.OnEnable();
4548
touchpadFirstChange = true;
4649
otherTouchpadControlEnabledStateSet = false;
50+
coordniateButtonAlias = (coordinateAxis == SDK_BaseController.Vector2Axis.Touchpad ? VRTK_ControllerEvents.ButtonAlias.TouchpadTouch : VRTK_ControllerEvents.ButtonAlias.TouchpadTwoTouch);
4751
}
4852

4953
protected override void ControlFixedUpdate()
@@ -63,7 +67,7 @@ protected override void ControlFixedUpdate()
6367
protected override VRTK_ObjectControl GetOtherControl()
6468
{
6569
GameObject foundController = (VRTK_DeviceFinder.IsControllerLeftHand(gameObject) ? VRTK_DeviceFinder.GetControllerRightHand(false) : VRTK_DeviceFinder.GetControllerLeftHand(false));
66-
if (foundController)
70+
if (foundController != null)
6771
{
6872
return foundController.GetComponentInChildren<VRTK_TouchpadControl>();
6973
}
@@ -72,17 +76,35 @@ protected override VRTK_ObjectControl GetOtherControl()
7276

7377
protected override void SetListeners(bool state)
7478
{
75-
if (controllerEvents)
79+
if (controllerEvents != null)
7680
{
7781
if (state)
7882
{
79-
controllerEvents.TouchpadAxisChanged += TouchpadAxisChanged;
80-
controllerEvents.TouchpadTouchEnd += TouchpadTouchEnd;
83+
switch (coordinateAxis)
84+
{
85+
case SDK_BaseController.Vector2Axis.Touchpad:
86+
controllerEvents.TouchpadAxisChanged += TouchpadAxisChanged;
87+
controllerEvents.TouchpadTouchEnd += TouchpadTouchEnd;
88+
break;
89+
case SDK_BaseController.Vector2Axis.TouchpadTwo:
90+
controllerEvents.TouchpadTwoAxisChanged += TouchpadAxisChanged;
91+
controllerEvents.TouchpadTwoTouchEnd += TouchpadTouchEnd;
92+
break;
93+
}
8194
}
8295
else
8396
{
84-
controllerEvents.TouchpadAxisChanged -= TouchpadAxisChanged;
85-
controllerEvents.TouchpadTouchEnd -= TouchpadTouchEnd;
97+
switch (coordinateAxis)
98+
{
99+
case SDK_BaseController.Vector2Axis.Touchpad:
100+
controllerEvents.TouchpadAxisChanged -= TouchpadAxisChanged;
101+
controllerEvents.TouchpadTouchEnd -= TouchpadTouchEnd;
102+
break;
103+
case SDK_BaseController.Vector2Axis.TouchpadTwo:
104+
controllerEvents.TouchpadTwoAxisChanged -= TouchpadAxisChanged;
105+
controllerEvents.TouchpadTwoTouchEnd -= TouchpadTouchEnd;
106+
break;
107+
}
86108
}
87109
}
88110
}
@@ -109,18 +131,19 @@ protected virtual void ModifierButtonActive()
109131

110132
protected virtual bool TouchpadTouched()
111133
{
112-
return (controllerEvents && controllerEvents.IsButtonPressed(VRTK_ControllerEvents.ButtonAlias.TouchpadTouch));
134+
return (controllerEvents && controllerEvents.IsButtonPressed(coordniateButtonAlias));
113135
}
114136

115137
protected virtual void TouchpadAxisChanged(object sender, ControllerInteractionEventArgs e)
116138
{
117-
if (touchpadFirstChange && otherObjectControl != null && disableOtherControlsOnActive && e.touchpadAxis != Vector2.zero)
139+
Vector2 actualAxis = (coordinateAxis == SDK_BaseController.Vector2Axis.Touchpad ? e.touchpadAxis : e.touchpadTwoAxis);
140+
if (touchpadFirstChange && otherObjectControl != null && disableOtherControlsOnActive && actualAxis != Vector2.zero)
118141
{
119142
otherTouchpadControlEnabledState = otherObjectControl.enabled;
120143
otherTouchpadControlEnabledStateSet = true;
121144
otherObjectControl.enabled = false;
122145
}
123-
currentAxis = (ValidPrimaryButton() ? e.touchpadAxis : Vector2.zero);
146+
currentAxis = (ValidPrimaryButton() ? actualAxis : Vector2.zero);
124147

125148
if (currentAxis != Vector2.zero)
126149
{

0 commit comments

Comments
 (0)