summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorewewukek <ewewukek@gmail.com>2024-01-11 15:58:46 +0300
committerFedja Beader <fedja@protonmail.ch>2024-05-15 00:11:38 +0200
commit4c6d99ef4e393f900c4acf521a376f6ea0e012f6 (patch)
tree9c86f9b3d13480362780b57a08b8f245b9546745 /src
parent45ce5db3a8b84e441ebeb10c54b2a2b7821e5c29 (diff)
downloadplus-4c6d99ef4e393f900c4acf521a376f6ea0e012f6.tar.gz
plus-4c6d99ef4e393f900c4acf521a376f6ea0e012f6.tar.bz2
plus-4c6d99ef4e393f900c4acf521a376f6ea0e012f6.tar.xz
plus-4c6d99ef4e393f900c4acf521a376f6ea0e012f6.zip
Heuristic to detect controller triggers
Diffstat (limited to 'src')
-rw-r--r--src/input/joystick.cpp29
-rw-r--r--src/input/joystick.h1
2 files changed, 27 insertions, 3 deletions
diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp
index 126d5018a..1072d3892 100644
--- a/src/input/joystick.cpp
+++ b/src/input/joystick.cpp
@@ -61,7 +61,10 @@ Joystick::Joystick(const int no) :
mKeyTimeMap()
{
for (int i = 0; i < MAX_AXES; i++)
+ {
+ mIsTrigger[i] = false;
mAxesPositions[i] = 0;
+ }
for (int i = 0; i < MAX_BUTTONS; i++)
mActiveButtons[i] = false;
}
@@ -211,6 +214,14 @@ bool Joystick::open()
mUseHatForMovement = config.getBoolValue("useHatForMovement");
mUseInactive = config.getBoolValue("useInactiveJoystick");
+ for (int i = 0; i < mAxesNumber; i++)
+ {
+ // heuristic to detect controller triggers.
+ // their resting position is at SDL_JOYSTICK_AXIS_MIN = -32768
+ if (SDL_JoystickGetAxis(mJoystick, i) == SDL_JOYSTICK_AXIS_MIN)
+ mIsTrigger[i] = true;
+ }
+
return true;
}
@@ -253,7 +264,14 @@ void Joystick::logic()
settings.inputFocused != KeyboardFocus::Unfocused)
{
for (int i = 0; i < mAxesNumber; i++)
- mAxesPositions[i] = SDL_JoystickGetAxis(mJoystick, i);
+ {
+ int position = SDL_JoystickGetAxis(mJoystick, i);
+ // transform range [SDL_JOYSTICK_AXIS_MIN, SDL_JOYSTICK_AXIS_MAX]
+ // to [0, SDL_JOYSTICK_AXIS_MAX]
+ if (mIsTrigger[i])
+ position = (position - SDL_JOYSTICK_AXIS_MIN) / 2;
+ mAxesPositions[i] = position;
+ }
// X-Axis
int position = mAxesPositions[0];
@@ -434,10 +452,15 @@ int Joystick::getButtonFromEvent(const SDL_Event &event) const
const int axis = event.jaxis.axis;
if (axis < RESERVED_AXES)
return -1;
- if (event.jaxis.value < mTolerance * SDL_JOYSTICK_AXIS_MIN
+ int position = event.jaxis.value;
+ // transform range [SDL_JOYSTICK_AXIS_MIN, SDL_JOYSTICK_AXIS_MAX]
+ // to [0, SDL_JOYSTICK_AXIS_MAX]
+ if (mIsTrigger[axis])
+ position = (position - SDL_JOYSTICK_AXIS_MIN) / 2;
+ if (position < 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
+ if (position > mTolerance * SDL_JOYSTICK_AXIS_MAX
&& mAxesPositions[axis] < mTolerance * SDL_JOYSTICK_AXIS_MAX)
return KEY_POSITIVE_AXIS_FIRST + axis;
}
diff --git a/src/input/joystick.h b/src/input/joystick.h
index bd5ca533d..49c4e553a 100644
--- a/src/input/joystick.h
+++ b/src/input/joystick.h
@@ -179,6 +179,7 @@ class Joystick final
unsigned char mHatPosition;
int mAxesPositions[MAX_AXES];
+ bool mIsTrigger[MAX_AXES];
bool mActiveButtons[MAX_BUTTONS];
SDL_Joystick *mJoystick;