Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit bdecd73

Browse files
authored
Merge pull request #498 from cortex-command-community/digital-axis-fix
Fix digital axis simulation triggering on all analog events
2 parents 2ed6d9b + 293e786 commit bdecd73

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

Managers/UInputMan.cpp

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,14 @@ namespace RTE {
191191
case InputDevice::DEVICE_KEYB_ONLY:
192192
if (ElementHeld(player, InputElements::INPUT_L_UP)) {
193193
allInput.m_Y += -1.0F;
194-
} else if (ElementHeld(player, InputElements::INPUT_L_DOWN)) {
194+
}
195+
if (ElementHeld(player, InputElements::INPUT_L_DOWN)) {
195196
allInput.m_Y += 1.0F;
196-
} else if (ElementHeld(player, InputElements::INPUT_L_LEFT)) {
197+
}
198+
if (ElementHeld(player, InputElements::INPUT_L_LEFT)) {
197199
allInput.m_X += -1.0F;
198-
} else if (ElementHeld(player, InputElements::INPUT_L_RIGHT)) {
200+
}
201+
if (ElementHeld(player, InputElements::INPUT_L_RIGHT)) {
199202
allInput.m_X += 1.0F;
200203
}
201204
break;
@@ -800,6 +803,7 @@ namespace RTE {
800803
m_NetworkAccumulatedRawMouseMovement[Players::PlayerOne] += m_RawMouseMovement;
801804
}
802805
UpdateMouseInput();
806+
UpdateJoystickDigitalAxis();
803807
HandleSpecialInput();
804808
StoreInputEventsForNextUpdate();
805809

@@ -996,19 +1000,9 @@ namespace RTE {
9961000
int joystickIndex = device - s_PrevJoystickStates.begin();
9971001

9981002
int prevAxisValue = device->m_Axis[axis];
999-
int prevDigitalValue = device->m_DigitalAxis[axis];
1000-
1001-
int newDigitalState = 0;
1002-
if (value > c_AxisDigitalThreshold) {
1003-
newDigitalState = 1;
1004-
} else if (value < -c_AxisDigitalThreshold) {
1005-
newDigitalState = -1;
1006-
}
10071003

10081004
s_ChangedJoystickStates[joystickIndex].m_Axis[axis] = Sign(value - device->m_Axis[axis]);
1009-
s_ChangedJoystickStates[joystickIndex].m_DigitalAxis[axis] = Sign(newDigitalState - prevDigitalValue);
10101005
device->m_Axis[axis] = value;
1011-
device->m_DigitalAxis[axis] = newDigitalState;
10121006

10131007
Players joystickPlayer = Players::NoPlayer;
10141008
float deadZone = 0.0F;
@@ -1051,30 +1045,45 @@ namespace RTE {
10511045
if (aimValues.MagnitudeIsLessThan(deadZone)) {
10521046
if (axisLeft != SDL_CONTROLLER_AXIS_INVALID) {
10531047
s_ChangedJoystickStates[joystickIndex].m_Axis[axisLeft] = Sign(axisLeft == axis ? -prevAxisValue : -device->m_Axis[axisLeft]);
1054-
s_ChangedJoystickStates[joystickIndex].m_DigitalAxis[axisLeft] = Sign(axisLeft == axis ? -prevDigitalValue : -device->m_DigitalAxis[axisLeft]);
10551048
device->m_Axis[axisLeft] = 0;
1056-
device->m_DigitalAxis[axisLeft] = 0;
10571049
}
10581050
if (axisUp != SDL_CONTROLLER_AXIS_INVALID) {
10591051
s_ChangedJoystickStates[joystickIndex].m_Axis[axisUp] = Sign(axisUp == axis ? -prevAxisValue : -device->m_Axis[axisUp]);
1060-
s_ChangedJoystickStates[joystickIndex].m_DigitalAxis[axisUp] = Sign(axisLeft == axis ? -prevDigitalValue : -device->m_DigitalAxis[axisUp]);
10611052
device->m_Axis[axisUp] = 0;
1062-
device->m_DigitalAxis[axisUp] = 0;
10631053
}
10641054
}
10651055
} else if (deadZoneType == DeadZoneType::SQUARE && deadZone > 0.0F) {
10661056
if (std::abs(static_cast<double>(value) / c_GamepadAxisLimit) < deadZone) {
10671057
s_ChangedJoystickStates[joystickIndex].m_Axis[axis] = Sign(-prevAxisValue);
1068-
s_ChangedJoystickStates[joystickIndex].m_Axis[axis] = Sign(-prevDigitalValue);
10691058
device->m_Axis[axis] = 0;
1070-
device->m_DigitalAxis[axis] = 0;
10711059
}
10721060
}
10731061
}
10741062
}
10751063
}
10761064
}
10771065

1066+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1067+
1068+
void UInputMan::UpdateJoystickDigitalAxis() {
1069+
for (size_t i = 0; i < s_PrevJoystickStates.size(); ++i) {
1070+
for (size_t axis = 0; axis < s_PrevJoystickStates[i].m_DigitalAxis.size(); ++axis) {
1071+
int prevDigitalValue = s_PrevJoystickStates[i].m_DigitalAxis[axis];
1072+
int newDigitalValue = 0;
1073+
if (prevDigitalValue != 0 && std::abs(s_PrevJoystickStates[i].m_Axis[axis]) > c_AxisDigitalReleasedThreshold) {
1074+
newDigitalValue = prevDigitalValue;
1075+
}
1076+
if (s_PrevJoystickStates[i].m_Axis[axis] > c_AxisDigitalPressedThreshold) {
1077+
newDigitalValue = 1;
1078+
} else if (s_PrevJoystickStates[i].m_Axis[axis] < -c_AxisDigitalPressedThreshold) {
1079+
newDigitalValue = -1;
1080+
}
1081+
s_ChangedJoystickStates[i].m_DigitalAxis[axis] = Sign(newDigitalValue - prevDigitalValue);
1082+
s_PrevJoystickStates[i].m_DigitalAxis[axis] = newDigitalValue;
1083+
}
1084+
}
1085+
}
1086+
10781087
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10791088

10801089
void UInputMan::HandleGamepadHotPlug(int deviceIndex) {

Managers/UInputMan.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,8 @@ namespace RTE {
712712
bool m_TrapMousePosPerPlayer[Players::MaxPlayerCount]; //!< Whether to trap the mouse position to the middle of the screen for each player during network multiplayer.
713713

714714
static constexpr double c_GamepadAxisLimit = 32767.0; //!< Maximum axis value as defined by SDL (int16 max).
715-
static constexpr int c_AxisDigitalThreshold = 8192; //!< Digital Axis threshold value as defined by allegro.
715+
static constexpr int c_AxisDigitalPressedThreshold = 8192; //!< Digital Axis threshold value as defined by allegro.
716+
static constexpr int c_AxisDigitalReleasedThreshold = c_AxisDigitalPressedThreshold - 100; //!< Digital Axis release threshold, to debounce values.
716717

717718
#pragma region Input State Handling
718719
/// <summary>
@@ -826,6 +827,11 @@ namespace RTE {
826827
/// </summary>
827828
void UpdateJoystickAxis(std::vector<Gamepad>::iterator device, int axis, int newValue);
828829

830+
/// <summary>
831+
/// Updates simulated digital joystick axis. This is called from Update().
832+
/// </summary>
833+
void UpdateJoystickDigitalAxis();
834+
829835
/// <summary>
830836
/// Connect a joystick or gamepad device and add it to the joystick list if a slot is available (up to max player count).
831837
/// </summary>

0 commit comments

Comments
 (0)