From 45ce5db3a8b84e441ebeb10c54b2a2b7821e5c29 Mon Sep 17 00:00:00 2001 From: ewewukek Date: Thu, 11 Jan 2024 15:09:17 +0300 Subject: Refactor joystick input code --- src/input/joystick.cpp | 126 ++++++++++++++++++++++++++----------------------- src/input/joystick.h | 6 +++ 2 files changed, 74 insertions(+), 58 deletions(-) (limited to 'src/input') diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp index 21de2ad3d..126d5018a 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -323,35 +323,28 @@ bool Joystick::buttonPressed(const int no) const { if (!mEnabled) return false; - if (no < 0) - return false; - if (no < MAX_BUTTONS) - return mActiveButtons[no]; - if (!mUseHatForMovement) + const int button = getButton(no); + if (button >= 0) { - if (no == KEY_UP) + if (button < mButtonsNumber) + return mActiveButtons[button]; + if (button == KEY_UP) return (mHatPosition & UP) != 0; - if (no == KEY_DOWN) + if (button == KEY_DOWN) return (mHatPosition & DOWN) != 0; - if (no == KEY_LEFT) + if (button == KEY_LEFT) return (mHatPosition & LEFT) != 0; - if (no == KEY_RIGHT) + if (button == 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; - } + const int naxis = getNegativeAxis(no); + if (naxis >= 0) + return mAxesPositions[naxis] < mTolerance * SDL_JOYSTICK_AXIS_MIN; + + const int paxis = getPositiveAxis(no); + if (paxis >= 0) + return mAxesPositions[paxis] > mTolerance * SDL_JOYSTICK_AXIS_MAX; + return false; } @@ -381,11 +374,18 @@ KeysVector *Joystick::getActionVector(const SDL_Event &event) KeysVector *Joystick::getActionVectorByKey(const int i) { - if (i < 0 || (i >= mButtonsNumber && i < MAX_BUTTONS) || i >= KEY_END) + if (getButton(i) < 0 + && getNegativeAxis(i) < 0 + && getPositiveAxis(i) < 0) + { return nullptr; + } // logger->log("button triggerAction: %d", i); - if (mKeyToAction.find(i) != mKeyToAction.end()) - return &mKeyToAction[i]; + + KeyToActionMapIter it = mKeyToAction.find(i); + if (it != mKeyToAction.end()) + return &it->second; + return nullptr; } @@ -394,8 +394,11 @@ InputActionT Joystick::getActionId(const SDL_Event &event) const int i = getButtonFromEvent(event); if (i < 0) return InputAction::NO_VALUE; - if (mKeyToId.find(i) != mKeyToId.end()) - return mKeyToId[i]; + + KeyToIdMapIter it = mKeyToId.find(i); + if (it != mKeyToId.end()) + return it->second; + return InputAction::NO_VALUE; } @@ -441,6 +444,42 @@ int Joystick::getButtonFromEvent(const SDL_Event &event) const return -1; } +int Joystick::getButton(const int key) const +{ + if (key < 0 + || (mButtonsNumber <= key && key < MAX_BUTTONS) + || (mUseHatForMovement && key >= MAX_BUTTONS) + || key >= KEY_NEGATIVE_AXIS_FIRST) + { + return -1; + } + return key; +} + +int Joystick::getNegativeAxis(const int key) const +{ + if (key < KEY_NEGATIVE_AXIS_FIRST || key >= KEY_POSITIVE_AXIS_FIRST) + return -1; + + const int axis = key - KEY_NEGATIVE_AXIS_FIRST; + if (axis < RESERVED_AXES || axis >= mAxesNumber) + return -1; + + return axis; +} + +int Joystick::getPositiveAxis(const int key) const +{ + if (key < KEY_POSITIVE_AXIS_FIRST || key >= KEY_END) + return -1; + + const int axis = key - KEY_POSITIVE_AXIS_FIRST; + if (axis < RESERVED_AXES || axis >= mAxesNumber) + return -1; + + return axis; +} + bool Joystick::isActionActive(const InputActionT index) const { if (!validate()) @@ -471,37 +510,8 @@ void Joystick::handleRepeat(const int time) BLOCK_START("Joystick::handleRepeat") FOR_EACH (KeyTimeMapIter, it, mKeyTimeMap) { - bool repeat(false); const int key = (*it).first; - if (key >= 0 && key < mButtonsNumber) - { - if (mActiveButtons[key]) - repeat = true; - } - if (!mUseHatForMovement) - { - if (key == KEY_UP && (mHatPosition & UP) != 0) - repeat = true; - if (key == KEY_DOWN && (mHatPosition & DOWN) != 0) - repeat = true; - if (key == KEY_LEFT && (mHatPosition & LEFT) != 0) - repeat = true; - 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) + if (buttonPressed(key)) { int &keyTime = (*it).second; if (time > keyTime && abs(time - keyTime) diff --git a/src/input/joystick.h b/src/input/joystick.h index 2a7e0f638..bd5ca533d 100644 --- a/src/input/joystick.h +++ b/src/input/joystick.h @@ -203,6 +203,12 @@ class Joystick final static bool mEnabled; static bool mInitialized; static int joystickCount; + + int getButton(const int key) const A_WARN_UNUSED; + + int getNegativeAxis(const int key) const A_WARN_UNUSED; + + int getPositiveAxis(const int key) const A_WARN_UNUSED; }; extern Joystick *joystick; -- cgit v1.2.3-70-g09d2