diff options
author | ewewukek <ewewukek@gmail.com> | 2024-01-11 15:58:46 +0300 |
---|---|---|
committer | Fedja Beader <fedja@protonmail.ch> | 2024-05-15 00:11:38 +0200 |
commit | 4c6d99ef4e393f900c4acf521a376f6ea0e012f6 (patch) | |
tree | 9c86f9b3d13480362780b57a08b8f245b9546745 /src/input/joystick.cpp | |
parent | 45ce5db3a8b84e441ebeb10c54b2a2b7821e5c29 (diff) | |
download | plus-4c6d99ef4e393f900c4acf521a376f6ea0e012f6.tar.gz plus-4c6d99ef4e393f900c4acf521a376f6ea0e012f6.tar.bz2 plus-4c6d99ef4e393f900c4acf521a376f6ea0e012f6.tar.xz plus-4c6d99ef4e393f900c4acf521a376f6ea0e012f6.zip |
Heuristic to detect controller triggers
Diffstat (limited to 'src/input/joystick.cpp')
-rw-r--r-- | src/input/joystick.cpp | 29 |
1 files changed, 26 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; } |