summaryrefslogtreecommitdiff
path: root/src/input
diff options
context:
space:
mode:
authorewewukek <ewewukek@gmail.com>2024-01-11 15:09:17 +0300
committerFedja Beader <fedja@protonmail.ch>2024-05-15 00:11:38 +0200
commit45ce5db3a8b84e441ebeb10c54b2a2b7821e5c29 (patch)
tree194c20902eb756c2e5e2bbdf81152ce5049cadcf /src/input
parent842b7fd4db44d85308afbb401023429621d9c586 (diff)
downloadmv-45ce5db3a8b84e441ebeb10c54b2a2b7821e5c29.tar.gz
mv-45ce5db3a8b84e441ebeb10c54b2a2b7821e5c29.tar.bz2
mv-45ce5db3a8b84e441ebeb10c54b2a2b7821e5c29.tar.xz
mv-45ce5db3a8b84e441ebeb10c54b2a2b7821e5c29.zip
Refactor joystick input code
Diffstat (limited to 'src/input')
-rw-r--r--src/input/joystick.cpp126
-rw-r--r--src/input/joystick.h6
2 files changed, 74 insertions, 58 deletions
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;