From ea3d0737a18498d21c3def8c3773f57e5edec4ce Mon Sep 17 00:00:00 2001 From: Maximilian Mayer Date: Fri, 26 Jul 2024 15:03:57 +0100 Subject: [PATCH] [rcore] fix gamepad axis movement and its automation event recording This commit fixes 2 issues: - Automation events aren't recorded for negative axis movements on gamepads (e.g. stick going left/up) - 'GetGamepadAxisMovement' drift check isn't working correctly for triggers. Axis values between [-0.1, 0.1] are clamped to 0.0 Behaviour change: - 'GetGamepadAxisMovement' returns default value for each axis, even if gamepad isn't attached. --- src/rcore.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/rcore.c b/src/rcore.c index af310cfad..ebf04bae3 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -2949,13 +2949,22 @@ int GetGamepadAxisCount(int gamepad) return CORE.Input.Gamepad.axisCount[gamepad]; } +static float GetGamepadAxisMovementDefault(int axis) +{ + return (axis == GAMEPAD_AXIS_LEFT_TRIGGER || axis == GAMEPAD_AXIS_RIGHT_TRIGGER) ? -1.0f : 0.0f; +} + // Get axis movement vector for a gamepad float GetGamepadAxisMovement(int gamepad, int axis) { - float value = 0; + float value = GetGamepadAxisMovementDefault(axis); - if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (axis < MAX_GAMEPAD_AXIS) && - (fabsf(CORE.Input.Gamepad.axisState[gamepad][axis]) > 0.1f)) value = CORE.Input.Gamepad.axisState[gamepad][axis]; // 0.1f = GAMEPAD_AXIS_MINIMUM_DRIFT/DELTA + if ((gamepad < MAX_GAMEPADS) && CORE.Input.Gamepad.ready[gamepad] && (axis < MAX_GAMEPAD_AXIS)) { + float movement = value < 0.0f ? CORE.Input.Gamepad.axisState[gamepad][axis] : fabs(CORE.Input.Gamepad.axisState[gamepad][axis]); + + // 0.1f = GAMEPAD_AXIS_MINIMUM_DRIFT/DELTA + if (movement > value + 0.1f) value = CORE.Input.Gamepad.axisState[gamepad][axis]; + } return value; } @@ -3599,7 +3608,7 @@ static void RecordAutomationEvent(void) for (int axis = 0; axis < MAX_GAMEPAD_AXIS; axis++) { // Event type: INPUT_GAMEPAD_AXIS_MOTION - if (CORE.Input.Gamepad.axisState[gamepad][axis] > 0.1f) + if (GetGamepadAxisMovement(gamepad, axis) != GetGamepadAxisMovementDefault(axis)) { currentEventList->events[currentEventList->count].frame = CORE.Time.frameCounter; currentEventList->events[currentEventList->count].type = INPUT_GAMEPAD_AXIS_MOTION;