From 1dc559144c8809e2d9ab91a5f90a2047fc0595bc Mon Sep 17 00:00:00 2001 From: ewewukek Date: Thu, 11 Jan 2024 13:50:03 +0300 Subject: Gui support for joystick buttons --- src/gui/sdlinput.cpp | 14 ++++++++++++++ src/input/inputmanager.cpp | 12 ++++++++++-- src/input/joystick.cpp | 10 ++++++++++ src/input/joystick.h | 2 ++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/gui/sdlinput.cpp b/src/gui/sdlinput.cpp index 9b8b1ec6a..f9ff0f618 100644 --- a/src/gui/sdlinput.cpp +++ b/src/gui/sdlinput.cpp @@ -171,6 +171,20 @@ void SDLInput::pushInput(const SDL_Event &event) break; } + case SDL_JOYBUTTONDOWN: + { + const InputActionT actionId = inputManager.getActionByKey(event); + if (actionId > InputAction::NO_VALUE) + { + keyInput.setActionId(actionId); + keyInput.setType(KeyEventType::PRESSED); + mKeyInputQueue.push(keyInput); + keyInput.setType(KeyEventType::RELEASED); + mKeyInputQueue.push(keyInput); + } + break; + } + #ifdef USE_SDL2 case SDL_TEXTINPUT: keyInput.setType(KeyEventType::PRESSED); diff --git a/src/input/inputmanager.cpp b/src/input/inputmanager.cpp index 0a113bd76..0bffbe231 100644 --- a/src/input/inputmanager.cpp +++ b/src/input/inputmanager.cpp @@ -738,7 +738,7 @@ bool InputManager::handleEvent(const SDL_Event &restrict event) restrict2 if (gui != nullptr) { const bool res = gui->handleInput(); - if (res && event.type == SDL_KEYDOWN) + if (res && (event.type == SDL_KEYDOWN || event.type == SDL_JOYBUTTONDOWN)) { BLOCK_END("InputManager::handleEvent") return true; @@ -1103,7 +1103,6 @@ InputActionT InputManager::getKeyIndex(const int value, InputActionT InputManager::getActionByKey(const SDL_Event &restrict event) const restrict2 { - // for now support only keyboard events if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) { const InputActionT idx = keyboard.getActionId(event); @@ -1113,6 +1112,15 @@ InputActionT InputManager::getActionByKey(const SDL_Event &restrict event) return idx; } } + if (joystick != nullptr && event.type == SDL_JOYBUTTONDOWN) + { + const InputActionT idx = joystick->getActionId(event); + if (CAST_S32(idx) >= 0 && + checkKey(&inputActionData[CAST_SIZE(idx)])) + { + return idx; + } + } return InputAction::NO_VALUE; } diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp index 01414b4dc..3a4a47fb1 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -357,6 +357,16 @@ KeysVector *Joystick::getActionVectorByKey(const int i) return nullptr; } +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]; + return InputAction::NO_VALUE; +} + int Joystick::getButtonFromEvent(const SDL_Event &event) const { if (event.type == SDL_JOYBUTTONDOWN) diff --git a/src/input/joystick.h b/src/input/joystick.h index 805efa6ad..2a43d1a14 100644 --- a/src/input/joystick.h +++ b/src/input/joystick.h @@ -151,6 +151,8 @@ class Joystick final KeysVector *getActionVectorByKey(const int i) A_WARN_UNUSED; + InputActionT getActionId(const SDL_Event &event) A_WARN_UNUSED; + int getButtonFromEvent(const SDL_Event &event) const A_WARN_UNUSED; bool isActionActive(const InputActionT index) const A_WARN_UNUSED; -- cgit v1.2.3-60-g2f50