From 4b049200df42e6078d8ff2ae589f09c49ab9647d Mon Sep 17 00:00:00 2001 From: ewewukek Date: Thu, 11 Jan 2024 15:58:46 +0300 Subject: Heuristic to detect controller triggers --- src/input/joystick.cpp | 29 ++++++++++++++++++++++++++--- src/input/joystick.h | 1 + 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp index 126d5018a..1072d3892 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -61,7 +61,10 @@ Joystick::Joystick(const int no) : mKeyTimeMap() { for (int i = 0; i < MAX_AXES; i++) + { + mIsTrigger[i] = false; mAxesPositions[i] = 0; + } for (int i = 0; i < MAX_BUTTONS; i++) mActiveButtons[i] = false; } @@ -211,6 +214,14 @@ bool Joystick::open() mUseHatForMovement = config.getBoolValue("useHatForMovement"); mUseInactive = config.getBoolValue("useInactiveJoystick"); + for (int i = 0; i < mAxesNumber; i++) + { + // heuristic to detect controller triggers. + // their resting position is at SDL_JOYSTICK_AXIS_MIN = -32768 + if (SDL_JoystickGetAxis(mJoystick, i) == SDL_JOYSTICK_AXIS_MIN) + mIsTrigger[i] = true; + } + return true; } @@ -253,7 +264,14 @@ void Joystick::logic() settings.inputFocused != KeyboardFocus::Unfocused) { for (int i = 0; i < mAxesNumber; i++) - mAxesPositions[i] = SDL_JoystickGetAxis(mJoystick, i); + { + int position = SDL_JoystickGetAxis(mJoystick, i); + // transform range [SDL_JOYSTICK_AXIS_MIN, SDL_JOYSTICK_AXIS_MAX] + // to [0, SDL_JOYSTICK_AXIS_MAX] + if (mIsTrigger[i]) + position = (position - SDL_JOYSTICK_AXIS_MIN) / 2; + mAxesPositions[i] = position; + } // X-Axis int position = mAxesPositions[0]; @@ -434,10 +452,15 @@ int Joystick::getButtonFromEvent(const SDL_Event &event) const const int axis = event.jaxis.axis; if (axis < RESERVED_AXES) return -1; - if (event.jaxis.value < mTolerance * SDL_JOYSTICK_AXIS_MIN + int position = event.jaxis.value; + // transform range [SDL_JOYSTICK_AXIS_MIN, SDL_JOYSTICK_AXIS_MAX] + // to [0, SDL_JOYSTICK_AXIS_MAX] + if (mIsTrigger[axis]) + position = (position - SDL_JOYSTICK_AXIS_MIN) / 2; + if (position < mTolerance * SDL_JOYSTICK_AXIS_MIN && mAxesPositions[axis] > mTolerance * SDL_JOYSTICK_AXIS_MIN) return KEY_NEGATIVE_AXIS_FIRST + axis; - if (event.jaxis.value > mTolerance * SDL_JOYSTICK_AXIS_MAX + if (position > mTolerance * SDL_JOYSTICK_AXIS_MAX && mAxesPositions[axis] < mTolerance * SDL_JOYSTICK_AXIS_MAX) return KEY_POSITIVE_AXIS_FIRST + axis; } diff --git a/src/input/joystick.h b/src/input/joystick.h index bd5ca533d..49c4e553a 100644 --- a/src/input/joystick.h +++ b/src/input/joystick.h @@ -179,6 +179,7 @@ class Joystick final unsigned char mHatPosition; int mAxesPositions[MAX_AXES]; + bool mIsTrigger[MAX_AXES]; bool mActiveButtons[MAX_BUTTONS]; SDL_Joystick *mJoystick; -- cgit v1.2.3-70-g09d2