From 046ac8fceda89092f01325abbc8d2b5b8602719c Mon Sep 17 00:00:00 2001 From: ewewukek Date: Thu, 11 Jan 2024 13:03:53 +0300 Subject: Add a checkbox for d-pad to switch between movement and button behavior --- src/defaults.cpp | 1 + src/gui/widgets/tabs/setup_joystick.cpp | 22 +++++++++++++---- src/gui/widgets/tabs/setup_joystick.h | 1 + src/input/joystick.cpp | 44 ++++++++++++++++++++------------- src/input/joystick.h | 4 +++ 5 files changed, 50 insertions(+), 22 deletions(-) diff --git a/src/defaults.cpp b/src/defaults.cpp index 462a278c3..2c990c42f 100644 --- a/src/defaults.cpp +++ b/src/defaults.cpp @@ -195,6 +195,7 @@ void setConfigDefaults(Configuration &cfg) AddDEF("joystickEnabled", false); #endif AddDEF("joystickTolerance", 0.1F); + AddDEF("useHatForMovement", true); AddDEF("logNpcInGui", true); AddDEF("download-music", true); AddDEF("guialpha", 0.8F); diff --git a/src/gui/widgets/tabs/setup_joystick.cpp b/src/gui/widgets/tabs/setup_joystick.cpp index c7ece8c7a..860f7a251 100644 --- a/src/gui/widgets/tabs/setup_joystick.cpp +++ b/src/gui/widgets/tabs/setup_joystick.cpp @@ -55,6 +55,11 @@ Setup_Joystick::Setup_Joystick(const Widget2 *const widget) : false, Modal_false, nullptr, std::string())), mToleranceLabel(new Label(this)), mToleranceSlider(new Slider(this, 0.01, 1, 0.01)), + mUseHatForMovementCheckBox(new CheckBox(this, + // TRANSLATORS: joystick settings tab checkbox + _("Use joystick hat (d-pad) for movement"), + config.getBoolValue("useHatForMovement"), + nullptr, std::string())), mUseInactiveCheckBox(new CheckBox(this, // TRANSLATORS: joystick settings tab checkbox _("Use joystick if client window inactive"), @@ -106,10 +111,11 @@ Setup_Joystick::Setup_Joystick(const Widget2 *const widget) : place(0, 2, mToleranceSlider, 1, 1); place(1, 2, mToleranceLabel, 1, 1).setPadding(3); - place(0, 3, mUseInactiveCheckBox, 1, 1); - place(0, 4, mDetectButton, 1, 1); + place(0, 3, mUseHatForMovementCheckBox, 1, 1); + place(0, 4, mUseInactiveCheckBox, 1, 1); + place(0, 5, mDetectButton, 1, 1); - setDimension(Rect(0, 0, 365, 60)); + setDimension(Rect(0, 0, 365, 75)); } Setup_Joystick::~Setup_Joystick() @@ -176,8 +182,14 @@ void Setup_Joystick::apply() config.setValue("joystickEnabled", Joystick::isEnabled()); - config.setValue("useInactiveJoystick", mUseInactiveCheckBox->isSelected()); - joystick->setUseInactive(mUseInactiveCheckBox->isSelected()); + config.setValue("useHatForMovement", + mUseHatForMovementCheckBox->isSelected()); + joystick->setUseHatForMovement( + mUseHatForMovementCheckBox->isSelected()); + config.setValue("useInactiveJoystick", + mUseInactiveCheckBox->isSelected()); + joystick->setUseInactive( + mUseInactiveCheckBox->isSelected()); float tolerance = mToleranceSlider->getValue(); config.setValue("joystickTolerance", tolerance); diff --git a/src/gui/widgets/tabs/setup_joystick.h b/src/gui/widgets/tabs/setup_joystick.h index fce59a02c..2c138daf5 100644 --- a/src/gui/widgets/tabs/setup_joystick.h +++ b/src/gui/widgets/tabs/setup_joystick.h @@ -57,6 +57,7 @@ class Setup_Joystick final : public SetupTab DropDown *mNamesDropDown A_NONNULLPOINTER; Label *mToleranceLabel A_NONNULLPOINTER; Slider *mToleranceSlider A_NONNULLPOINTER; + CheckBox *mUseHatForMovementCheckBox A_NONNULLPOINTER; CheckBox *mUseInactiveCheckBox A_NONNULLPOINTER; bool mOriginalJoystickEnabled A_NONNULLPOINTER; }; diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp index 59087c2c7..01414b4dc 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -52,6 +52,7 @@ Joystick::Joystick(const int no) : mTolerance(0), mNumber(no >= joystickCount ? joystickCount : no), mButtonsNumber(MAX_BUTTONS), + mUseHatForMovement(true), mUseInactive(false), mHaveHats(false), mKeyToAction(), @@ -199,6 +200,7 @@ bool Joystick::open() config.setValue("joystickTolerance", 0.1F); #endif mTolerance = config.getFloatValue("joystickTolerance"); + mUseHatForMovement = config.getBoolValue("useHatForMovement"); mUseInactive = config.getBoolValue("useInactiveJoystick"); return true; @@ -278,6 +280,8 @@ void Joystick::logic() mHatPosition |= UP; else if ((hat & SDL_HAT_DOWN) != 0) mHatPosition |= DOWN; + if ((mDirection == 0U) && mUseHatForMovement) + mDirection = mHatPosition; } // Buttons @@ -310,14 +314,17 @@ bool Joystick::buttonPressed(const int no) const return false; if (no < MAX_BUTTONS) return mActiveButtons[no]; - if (no == KEY_UP) - return (mHatPosition & UP) != 0; - if (no == KEY_DOWN) - return (mHatPosition & DOWN) != 0; - if (no == KEY_LEFT) - return (mHatPosition & LEFT) != 0; - if (no == KEY_RIGHT) - return (mHatPosition & RIGHT) != 0; + if (!mUseHatForMovement) + { + if (no == KEY_UP) + return (mHatPosition & UP) != 0; + if (no == KEY_DOWN) + return (mHatPosition & DOWN) != 0; + if (no == KEY_LEFT) + return (mHatPosition & LEFT) != 0; + if (no == KEY_RIGHT) + return (mHatPosition & RIGHT) != 0; + } return false; } @@ -358,7 +365,7 @@ int Joystick::getButtonFromEvent(const SDL_Event &event) const return -1; return event.jbutton.button; } - if (event.type == SDL_JOYHATMOTION) + if (!mUseHatForMovement && event.type == SDL_JOYHATMOTION) { // reading only hat 0 if (event.jhat.which != mNumber || event.jhat.hat != 0) @@ -415,14 +422,17 @@ void Joystick::handleRepeat(const int time) if (mActiveButtons[key]) repeat = true; } - 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 (!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 (repeat) { int &keyTime = (*it).second; diff --git a/src/input/joystick.h b/src/input/joystick.h index bddf520a2..805efa6ad 100644 --- a/src/input/joystick.h +++ b/src/input/joystick.h @@ -139,6 +139,9 @@ class Joystick final void setTolerance(const float tolerance) { mTolerance = tolerance; } + void setUseHatForMovement(const bool b) + { mUseHatForMovement = b; } + void setUseInactive(const bool b) { mUseInactive = b; } @@ -169,6 +172,7 @@ class Joystick final float mTolerance; int mNumber; int mButtonsNumber; + bool mUseHatForMovement; bool mUseInactive; bool mHaveHats; -- cgit v1.2.3-70-g09d2