summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorewewukek <ewewukek@gmail.com>2024-01-11 13:50:03 +0300
committerFedja Beader <fedja@protonmail.ch>2024-05-15 00:11:38 +0200
commit1dc559144c8809e2d9ab91a5f90a2047fc0595bc (patch)
tree47fbac5776f2277df4a625cc5a8547c99d28589f
parent046ac8fceda89092f01325abbc8d2b5b8602719c (diff)
downloadmv-1dc559144c8809e2d9ab91a5f90a2047fc0595bc.tar.gz
mv-1dc559144c8809e2d9ab91a5f90a2047fc0595bc.tar.bz2
mv-1dc559144c8809e2d9ab91a5f90a2047fc0595bc.tar.xz
mv-1dc559144c8809e2d9ab91a5f90a2047fc0595bc.zip
Gui support for joystick buttons
-rw-r--r--src/gui/sdlinput.cpp14
-rw-r--r--src/input/inputmanager.cpp12
-rw-r--r--src/input/joystick.cpp10
-rw-r--r--src/input/joystick.h2
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;