diff options
author | ewewukek <ewewukek@gmail.com> | 2024-01-11 15:04:28 +0300 |
---|---|---|
committer | Fedja Beader <fedja@protonmail.ch> | 2024-05-15 00:11:38 +0200 |
commit | 842b7fd4db44d85308afbb401023429621d9c586 (patch) | |
tree | c7d815e9074e8bf1c074937b83808cc34e4c6258 /src/input/joystick.cpp | |
parent | 0ff6869e745b4a0174ab1ee116ba5d6ad08e4812 (diff) | |
download | plus-842b7fd4db44d85308afbb401023429621d9c586.tar.gz plus-842b7fd4db44d85308afbb401023429621d9c586.tar.bz2 plus-842b7fd4db44d85308afbb401023429621d9c586.tar.xz plus-842b7fd4db44d85308afbb401023429621d9c586.zip |
Make sticks/triggers behave like regular buttons
Diffstat (limited to 'src/input/joystick.cpp')
-rw-r--r-- | src/input/joystick.cpp | 61 |
1 files changed, 57 insertions, 4 deletions
diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp index 1e375cb0a..21de2ad3d 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -51,6 +51,7 @@ Joystick::Joystick(const int no) : mJoystick(nullptr), mTolerance(0), mNumber(no >= joystickCount ? joystickCount : no), + mAxesNumber(MAX_AXES), mButtonsNumber(MAX_BUTTONS), mUseHatForMovement(true), mUseInactive(false), @@ -59,6 +60,8 @@ Joystick::Joystick(const int no) : mKeyToId(), mKeyTimeMap() { + for (int i = 0; i < MAX_AXES; i++) + mAxesPositions[i] = 0; for (int i = 0; i < MAX_BUTTONS; i++) mActiveButtons[i] = false; } @@ -127,7 +130,9 @@ bool Joystick::open() return false; } + mAxesNumber = SDL_JoystickNumAxes(mJoystick); mButtonsNumber = SDL_JoystickNumButtons(mJoystick); + logger->log("Joystick: %i ", mNumber); #ifdef USE_SDL2 logger->log("Name: %s", SDL_JoystickName(mJoystick)); @@ -186,13 +191,16 @@ bool Joystick::open() logger->log("Name: %s", SDL_JoystickName(mNumber)); #endif // USE_SDL2 - logger->log("Axes: %i ", SDL_JoystickNumAxes(mJoystick)); + logger->log("Axes: %i ", mAxesNumber); logger->log("Balls: %i", SDL_JoystickNumBalls(mJoystick)); logger->log("Hats: %i", SDL_JoystickNumHats(mJoystick)); logger->log("Buttons: %i", mButtonsNumber); mHaveHats = (SDL_JoystickNumHats(mJoystick) > 0); + if (mAxesNumber > MAX_AXES) + mAxesNumber = mAxesNumber; + if (mButtonsNumber > MAX_BUTTONS) mButtonsNumber = MAX_BUTTONS; @@ -244,15 +252,18 @@ void Joystick::logic() if (mUseInactive || settings.inputFocused != KeyboardFocus::Unfocused) { + for (int i = 0; i < mAxesNumber; i++) + mAxesPositions[i] = SDL_JoystickGetAxis(mJoystick, i); + // X-Axis - int position = SDL_JoystickGetAxis(mJoystick, 0); + int position = mAxesPositions[0]; if (position >= mTolerance * SDL_JOYSTICK_AXIS_MAX) mDirection |= RIGHT; else if (position <= mTolerance * SDL_JOYSTICK_AXIS_MIN) mDirection |= LEFT; // Y-Axis - position = SDL_JoystickGetAxis(mJoystick, 1); + position = mAxesPositions[1]; if (position <= mTolerance * SDL_JOYSTICK_AXIS_MIN) mDirection |= UP; else if (position >= mTolerance * SDL_JOYSTICK_AXIS_MAX) @@ -300,6 +311,8 @@ void Joystick::logic() else { mHatPosition = 0; + for (int i = 0; i < mAxesNumber; i++) + mAxesPositions[i] = 0; for (int i = 0; i < mButtonsNumber; i++) mActiveButtons[i] = false; } @@ -325,6 +338,20 @@ bool Joystick::buttonPressed(const int no) const if (no == KEY_RIGHT) return (mHatPosition & RIGHT) != 0; } + if (KEY_NEGATIVE_AXIS_FIRST <= no && no < KEY_POSITIVE_AXIS_FIRST) + { + const int axis = no - KEY_NEGATIVE_AXIS_FIRST; + if (axis < RESERVED_AXES || axis >= mAxesNumber) + return false; + return mAxesPositions[axis] < mTolerance * SDL_JOYSTICK_AXIS_MIN; + } + if (KEY_POSITIVE_AXIS_FIRST <= no && no < KEY_END) + { + const int axis = no - KEY_POSITIVE_AXIS_FIRST; + if (axis < RESERVED_AXES || axis >= mAxesNumber) + return false; + return mAxesPositions[axis] > mTolerance * SDL_JOYSTICK_AXIS_MAX; + } return false; } @@ -354,7 +381,7 @@ KeysVector *Joystick::getActionVector(const SDL_Event &event) KeysVector *Joystick::getActionVectorByKey(const int i) { - if (i < 0 || (i >= mButtonsNumber && i < MAX_BUTTONS) || i > KEY_LAST) + if (i < 0 || (i >= mButtonsNumber && i < MAX_BUTTONS) || i >= KEY_END) return nullptr; // logger->log("button triggerAction: %d", i); if (mKeyToAction.find(i) != mKeyToAction.end()) @@ -397,6 +424,20 @@ int Joystick::getButtonFromEvent(const SDL_Event &event) const if ((mHatPosition & RIGHT) == 0 && (value & SDL_HAT_RIGHT) != 0) return KEY_RIGHT; } + if (event.type == SDL_JOYAXISMOTION) + { + if (event.jaxis.which != mNumber) + return -1; + const int axis = event.jaxis.axis; + if (axis < RESERVED_AXES) + return -1; + if (event.jaxis.value < 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 + && mAxesPositions[axis] < mTolerance * SDL_JOYSTICK_AXIS_MAX) + return KEY_POSITIVE_AXIS_FIRST + axis; + } return -1; } @@ -448,6 +489,18 @@ void Joystick::handleRepeat(const int time) if (key == KEY_RIGHT && (mHatPosition & RIGHT) != 0) repeat = true; } + if (KEY_NEGATIVE_AXIS_FIRST <= key && key < KEY_POSITIVE_AXIS_FIRST) + { + const int axis = key - KEY_NEGATIVE_AXIS_FIRST; + if (axis >= RESERVED_AXES && mAxesPositions[axis] < mTolerance * SDL_JOYSTICK_AXIS_MIN) + repeat = true; + } + if (KEY_POSITIVE_AXIS_FIRST <= key && key < KEY_END) + { + const int axis = key - KEY_POSITIVE_AXIS_FIRST; + if (axis >= RESERVED_AXES && mAxesPositions[axis] > mTolerance * SDL_JOYSTICK_AXIS_MAX) + repeat = true; + } if (repeat) { int &keyTime = (*it).second; |