From 78f73fbc427d449a9fbfb35461ff22eac309486b Mon Sep 17 00:00:00 2001 From: ewewukek Date: Wed, 10 Jan 2024 23:11:14 +0300 Subject: Add a slider to joystick settings to set axis tolerance --- src/gui/widgets/tabs/setup_joystick.cpp | 36 ++++++++++++++++++++++++++++----- src/gui/widgets/tabs/setup_joystick.h | 3 +++ 2 files changed, 34 insertions(+), 5 deletions(-) (limited to 'src/gui/widgets/tabs') diff --git a/src/gui/widgets/tabs/setup_joystick.cpp b/src/gui/widgets/tabs/setup_joystick.cpp index ab623b901..16c248c48 100644 --- a/src/gui/widgets/tabs/setup_joystick.cpp +++ b/src/gui/widgets/tabs/setup_joystick.cpp @@ -35,6 +35,7 @@ #include "gui/widgets/dropdown.h" #include "gui/widgets/label.h" #include "gui/widgets/layouthelper.h" +#include "gui/widgets/slider.h" #include "utils/delete2.h" #include "utils/gettext.h" @@ -58,6 +59,8 @@ Setup_Joystick::Setup_Joystick(const Widget2 *const widget) : mNamesModel(new NamesModel), mNamesDropDown(new DropDown(this, mNamesModel, false, Modal_false, nullptr, std::string())), + mToleranceLabel(new Label(this)), + mToleranceSlider(new Slider(this, 1000, 32767, 1000)), mUseInactiveCheckBox(new CheckBox(this, // TRANSLATORS: joystick settings tab checkbox _("Use joystick if client window inactive"), @@ -75,8 +78,17 @@ Setup_Joystick::Setup_Joystick(const Widget2 *const widget) : mJoystickEnabled->addActionListener(this); mCalibrateButton->setEnabled(mOriginalJoystickEnabled); + int tolerance = config.getIntValue("joystickTolerance"); + mToleranceSlider->setValue(tolerance); + // TRANSLATORS: joystick settings tab label + mToleranceLabel->setCaption(_("Axis tolerance: ") + strprintf("%d", tolerance)); + mToleranceLabel->setWidth(150); + mToleranceLabel->setHeight(20); + mNamesDropDown->setActionEventId("name"); mNamesDropDown->addActionListener(this); + mToleranceSlider->setActionEventId("toleranceslider"); + mToleranceSlider->addActionListener(this); if (joystick != nullptr) { @@ -96,12 +108,16 @@ Setup_Joystick::Setup_Joystick(const Widget2 *const widget) : place(0, 0, mJoystickEnabled, 1, 1); place(0, 1, mNamesDropDown, 1, 1); - place(0, 2, mUseInactiveCheckBox, 1, 1); - place(0, 3, mDetectButton, 1, 1); - place(0, 4, mCalibrateLabel, 1, 1); - place(0, 5, mCalibrateButton, 1, 1); - setDimension(Rect(0, 0, 365, 75)); + 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, 5, mCalibrateLabel, 1, 1); + place(0, 6, mCalibrateButton, 1, 1); + + setDimension(Rect(0, 0, 365, 95)); } Setup_Joystick::~Setup_Joystick() @@ -121,6 +137,12 @@ void Setup_Joystick::action(const ActionEvent &event) if (joystick != nullptr) joystick->setNumber(mNamesDropDown->getSelected()); } + else if (source == mToleranceSlider) + { + int tolerance = mToleranceSlider->getValue(); + // TRANSLATORS: joystick settings tab label + mToleranceLabel->setCaption(_("Axis tolerance: ") + strprintf("%d", tolerance)); + } else if (source == mDetectButton) { Joystick::detect(); @@ -188,4 +210,8 @@ void Setup_Joystick::apply() config.setValue("useInactiveJoystick", mUseInactiveCheckBox->isSelected()); joystick->setUseInactive(mUseInactiveCheckBox->isSelected()); + + int tolerance = mToleranceSlider->getValue(); + config.setValue("joystickTolerance", tolerance); + joystick->setTolerance(tolerance); } diff --git a/src/gui/widgets/tabs/setup_joystick.h b/src/gui/widgets/tabs/setup_joystick.h index 174cd82b2..8e347d0f6 100644 --- a/src/gui/widgets/tabs/setup_joystick.h +++ b/src/gui/widgets/tabs/setup_joystick.h @@ -31,6 +31,7 @@ class CheckBox; class DropDown; class Label; class NamesModel; +class Slider; class Setup_Joystick final : public SetupTab { @@ -56,6 +57,8 @@ class Setup_Joystick final : public SetupTab CheckBox *mJoystickEnabled A_NONNULLPOINTER; NamesModel *mNamesModel A_NONNULLPOINTER; DropDown *mNamesDropDown A_NONNULLPOINTER; + Label *mToleranceLabel A_NONNULLPOINTER; + Slider *mToleranceSlider A_NONNULLPOINTER; CheckBox *mUseInactiveCheckBox A_NONNULLPOINTER; bool mOriginalJoystickEnabled A_NONNULLPOINTER; }; -- cgit v1.2.3-70-g09d2 From e0f535979ad9315efbe4a16ab1ad345d9e9a4347 Mon Sep 17 00:00:00 2001 From: ewewukek Date: Wed, 10 Jan 2024 23:17:43 +0300 Subject: Remove joystick calibration logic --- src/gui/widgets/tabs/setup_joystick.cpp | 36 +----------------------- src/gui/widgets/tabs/setup_joystick.h | 2 -- src/input/joystick.cpp | 49 ++------------------------------- src/input/joystick.h | 11 -------- 4 files changed, 3 insertions(+), 95 deletions(-) (limited to 'src/gui/widgets/tabs') diff --git a/src/gui/widgets/tabs/setup_joystick.cpp b/src/gui/widgets/tabs/setup_joystick.cpp index 16c248c48..3542f39a5 100644 --- a/src/gui/widgets/tabs/setup_joystick.cpp +++ b/src/gui/widgets/tabs/setup_joystick.cpp @@ -44,12 +44,6 @@ Setup_Joystick::Setup_Joystick(const Widget2 *const widget) : SetupTab(widget), - mCalibrateLabel(new Label(this, - // TRANSLATORS: joystick settings tab label - _("Press the button to start calibration"))), - // TRANSLATORS: joystick settings tab button - mCalibrateButton(new Button(this, _("Calibrate"), "calibrate", - BUTTON_SKIN, this)), // TRANSLATORS: joystick settings tab button mDetectButton(new Button(this, _("Detect joysticks"), "detect", BUTTON_SKIN, this)), @@ -76,7 +70,6 @@ Setup_Joystick::Setup_Joystick(const Widget2 *const widget) : mJoystickEnabled->setSelected(mOriginalJoystickEnabled); mJoystickEnabled->setActionEventId("joystick"); mJoystickEnabled->addActionListener(this); - mCalibrateButton->setEnabled(mOriginalJoystickEnabled); int tolerance = config.getIntValue("joystickTolerance"); mToleranceSlider->setValue(tolerance); @@ -114,10 +107,8 @@ Setup_Joystick::Setup_Joystick(const Widget2 *const widget) : place(0, 3, mUseInactiveCheckBox, 1, 1); place(0, 4, mDetectButton, 1, 1); - place(0, 5, mCalibrateLabel, 1, 1); - place(0, 6, mCalibrateButton, 1, 1); - setDimension(Rect(0, 0, 365, 95)); + setDimension(Rect(0, 0, 365, 60)); } Setup_Joystick::~Setup_Joystick() @@ -152,36 +143,11 @@ void Setup_Joystick::action(const ActionEvent &event) else mNamesDropDown->setSelected(0); } - else - { - if (joystick == nullptr) - return; - - if (joystick->isCalibrating()) - { - // TRANSLATORS: joystick settings tab button - mCalibrateButton->setCaption(_("Calibrate")); - mCalibrateLabel->setCaption - // TRANSLATORS: joystick settings tab label - (_("Press the button to start calibration")); - joystick->finishCalibration(); - } - else - { - // TRANSLATORS: joystick settings tab button - mCalibrateButton->setCaption(_("Stop")); - mCalibrateLabel->setCaption( - // TRANSLATORS: joystick settings tab label - _("Rotate the stick and don't press buttons")); - joystick->startCalibration(); - } - } } void Setup_Joystick::setTempEnabled(const bool sel) { Joystick::setEnabled(sel); - mCalibrateButton->setEnabled(sel); if (joystick != nullptr) { if (sel) diff --git a/src/gui/widgets/tabs/setup_joystick.h b/src/gui/widgets/tabs/setup_joystick.h index 8e347d0f6..fce59a02c 100644 --- a/src/gui/widgets/tabs/setup_joystick.h +++ b/src/gui/widgets/tabs/setup_joystick.h @@ -51,8 +51,6 @@ class Setup_Joystick final : public SetupTab void setTempEnabled(const bool sel); private: - Label *mCalibrateLabel A_NONNULLPOINTER; - Button *mCalibrateButton A_NONNULLPOINTER; Button *mDetectButton A_NONNULLPOINTER; CheckBox *mJoystickEnabled A_NONNULLPOINTER; NamesModel *mNamesModel A_NONNULLPOINTER; diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp index 48b4488b0..4bcb070d3 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -49,9 +49,7 @@ Joystick::Joystick(const int no) : mDirection(0), mJoystick(nullptr), mTolerance(0), - mCalibrating(false), mNumber(no >= joystickCount ? joystickCount : no), - mCalibrated(false), mButtonsNumber(MAX_BUTTONS), mUseInactive(false), mHaveHats(false), @@ -197,12 +195,8 @@ bool Joystick::open() mButtonsNumber = MAX_BUTTONS; #ifdef __SWITCH__ - config.setValue("joystick" + toString(mNumber) + "calibrated", true); config.setValue("joystickTolerance", 10000); #endif - mCalibrated = config.getValueBool("joystick" - + toString(mNumber) + "calibrated", false); - mTolerance = config.getIntValue("joystickTolerance"); mUseInactive = config.getBoolValue("useInactiveJoystick"); @@ -236,15 +230,7 @@ void Joystick::setNumber(const int n) void Joystick::logic() { BLOCK_START("Joystick::logic") - // When calibrating, don't bother the outside with our state - if (mCalibrating) - { - doCalibration(); - BLOCK_END("Joystick::logic") - return; - } - - if (!mEnabled || !mCalibrated) + if (!mEnabled) { BLOCK_END("Joystick::logic") return; @@ -313,37 +299,6 @@ void Joystick::logic() BLOCK_END("Joystick::logic") } -void Joystick::startCalibration() -{ - mTolerance = 0; - mCalibrating = true; -} - -void Joystick::doCalibration() -{ - // X-Axis - int position = SDL_JoystickGetAxis(mJoystick, 0); - if (position > mTolerance) - mTolerance = position; - else if (position < -mTolerance) - mTolerance = -position; - - // Y-Axis - position = SDL_JoystickGetAxis(mJoystick, 1); - if (position > mTolerance) - mTolerance = position; - else if (position < -mTolerance) - mTolerance = -position; -} - -void Joystick::finishCalibration() -{ - mCalibrated = true; - mCalibrating = false; - config.setValue("joystick" + toString(mNumber) + "calibrated", true); - config.setValue("joystickTolerance", mTolerance); -} - bool Joystick::buttonPressed(const unsigned char no) const { return (mEnabled && no < MAX_BUTTONS) ? mActiveButtons[no] : false; @@ -414,7 +369,7 @@ bool Joystick::isActionActive(const InputActionT index) const bool Joystick::validate() const { - if (mCalibrating || !mEnabled || !mCalibrated) + if (!mEnabled) return false; return mUseInactive || diff --git a/src/input/joystick.h b/src/input/joystick.h index 198b6fe9e..d90420395 100644 --- a/src/input/joystick.h +++ b/src/input/joystick.h @@ -98,13 +98,6 @@ class Joystick final */ void logic(); - void startCalibration(); - - void finishCalibration(); - - bool isCalibrating() const noexcept2 A_WARN_UNUSED - { return mCalibrating; } - bool buttonPressed(const unsigned char no) const A_WARN_UNUSED; bool isUp() const noexcept2 A_WARN_UNUSED @@ -152,9 +145,7 @@ class Joystick final SDL_Joystick *mJoystick; int mTolerance; - bool mCalibrating; int mNumber; - bool mCalibrated; int mButtonsNumber; bool mUseInactive; bool mHaveHats; @@ -171,8 +162,6 @@ class Joystick final static bool mEnabled; static bool mInitialized; static int joystickCount; - - void doCalibration(); }; extern Joystick *joystick; -- cgit v1.2.3-70-g09d2 From 84a11a02aad86d9388175c7fd0971accdb586473 Mon Sep 17 00:00:00 2001 From: ewewukek Date: Wed, 10 Jan 2024 23:26:42 +0300 Subject: Switch joystick axis tolerance type to float --- src/defaults.cpp | 2 +- src/gui/widgets/tabs/setup_joystick.cpp | 14 ++++++++------ src/input/joystick.cpp | 12 ++++++------ src/input/joystick.h | 13 +++++++++++-- 4 files changed, 26 insertions(+), 15 deletions(-) (limited to 'src/gui/widgets/tabs') diff --git a/src/defaults.cpp b/src/defaults.cpp index 30c5f3caf..462a278c3 100644 --- a/src/defaults.cpp +++ b/src/defaults.cpp @@ -194,7 +194,7 @@ void setConfigDefaults(Configuration &cfg) #else AddDEF("joystickEnabled", false); #endif - AddDEF("joystickTolerance", 10000); + AddDEF("joystickTolerance", 0.1F); 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 3542f39a5..c7ece8c7a 100644 --- a/src/gui/widgets/tabs/setup_joystick.cpp +++ b/src/gui/widgets/tabs/setup_joystick.cpp @@ -54,7 +54,7 @@ Setup_Joystick::Setup_Joystick(const Widget2 *const widget) : mNamesDropDown(new DropDown(this, mNamesModel, false, Modal_false, nullptr, std::string())), mToleranceLabel(new Label(this)), - mToleranceSlider(new Slider(this, 1000, 32767, 1000)), + mToleranceSlider(new Slider(this, 0.01, 1, 0.01)), mUseInactiveCheckBox(new CheckBox(this, // TRANSLATORS: joystick settings tab checkbox _("Use joystick if client window inactive"), @@ -71,10 +71,11 @@ Setup_Joystick::Setup_Joystick(const Widget2 *const widget) : mJoystickEnabled->setActionEventId("joystick"); mJoystickEnabled->addActionListener(this); - int tolerance = config.getIntValue("joystickTolerance"); + float tolerance = config.getFloatValue("joystickTolerance"); mToleranceSlider->setValue(tolerance); // TRANSLATORS: joystick settings tab label - mToleranceLabel->setCaption(_("Axis tolerance: ") + strprintf("%d", tolerance)); + mToleranceLabel->setCaption(_("Axis tolerance: ") + + strprintf("%.2f", tolerance)); mToleranceLabel->setWidth(150); mToleranceLabel->setHeight(20); @@ -130,9 +131,10 @@ void Setup_Joystick::action(const ActionEvent &event) } else if (source == mToleranceSlider) { - int tolerance = mToleranceSlider->getValue(); + float tolerance = mToleranceSlider->getValue(); // TRANSLATORS: joystick settings tab label - mToleranceLabel->setCaption(_("Axis tolerance: ") + strprintf("%d", tolerance)); + mToleranceLabel->setCaption(_("Axis tolerance: ") + + strprintf("%.2f", tolerance)); } else if (source == mDetectButton) { @@ -177,7 +179,7 @@ void Setup_Joystick::apply() config.setValue("useInactiveJoystick", mUseInactiveCheckBox->isSelected()); joystick->setUseInactive(mUseInactiveCheckBox->isSelected()); - int tolerance = mToleranceSlider->getValue(); + float tolerance = mToleranceSlider->getValue(); config.setValue("joystickTolerance", tolerance); joystick->setTolerance(tolerance); } diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp index 4bcb070d3..0ab6102c5 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -195,9 +195,9 @@ bool Joystick::open() mButtonsNumber = MAX_BUTTONS; #ifdef __SWITCH__ - config.setValue("joystickTolerance", 10000); + config.setValue("joystickTolerance", 0.1F); #endif - mTolerance = config.getIntValue("joystickTolerance"); + mTolerance = config.getFloatValue("joystickTolerance"); mUseInactive = config.getBoolValue("useInactiveJoystick"); return true; @@ -243,16 +243,16 @@ void Joystick::logic() { // X-Axis int position = SDL_JoystickGetAxis(mJoystick, 0); - if (position >= mTolerance) + if (position >= mTolerance * SDL_JOYSTICK_AXIS_MAX) mDirection |= RIGHT; - else if (position <= -mTolerance) + else if (position <= mTolerance * SDL_JOYSTICK_AXIS_MIN) mDirection |= LEFT; // Y-Axis position = SDL_JoystickGetAxis(mJoystick, 1); - if (position <= -mTolerance) + if (position <= mTolerance * SDL_JOYSTICK_AXIS_MIN) mDirection |= UP; - else if (position >= mTolerance) + else if (position >= mTolerance * SDL_JOYSTICK_AXIS_MAX) mDirection |= DOWN; #ifdef DEBUG_JOYSTICK diff --git a/src/input/joystick.h b/src/input/joystick.h index d90420395..32fdddf2e 100644 --- a/src/input/joystick.h +++ b/src/input/joystick.h @@ -31,6 +31,15 @@ PRAGMA48(GCC diagnostic ignored "-Wshadow") #include PRAGMA48(GCC diagnostic pop) +// defined first in SDL 2.0.6 +// not available on older distros +#ifndef SDL_JOYSTICK_AXIS_MIN +#define SDL_JOYSTICK_AXIS_MIN -32768 +#endif +#ifndef SDL_JOYSTICK_AXIS_MAX +#define SDL_JOYSTICK_AXIS_MAX 32767 +#endif + class Joystick final { public: @@ -115,7 +124,7 @@ class Joystick final int getNumber() const noexcept2 A_WARN_UNUSED { return mNumber; } - void setTolerance(const int tolerance) + void setTolerance(const float tolerance) { mTolerance = tolerance; } void setUseInactive(const bool b) @@ -144,7 +153,7 @@ class Joystick final SDL_Joystick *mJoystick; - int mTolerance; + float mTolerance; int mNumber; int mButtonsNumber; bool mUseInactive; -- cgit v1.2.3-70-g09d2 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(-) (limited to 'src/gui/widgets/tabs') 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 From ce7efa33a54fdf37416631057dc13799dc2f847f Mon Sep 17 00:00:00 2001 From: Fedja Beader Date: Wed, 15 May 2024 20:49:43 +0200 Subject: Rename axis tolerance to joystick dead zone + reformat as per discussion --- src/gui/widgets/tabs/setup_joystick.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/gui/widgets/tabs') diff --git a/src/gui/widgets/tabs/setup_joystick.cpp b/src/gui/widgets/tabs/setup_joystick.cpp index 860f7a251..297f5bafc 100644 --- a/src/gui/widgets/tabs/setup_joystick.cpp +++ b/src/gui/widgets/tabs/setup_joystick.cpp @@ -76,11 +76,11 @@ Setup_Joystick::Setup_Joystick(const Widget2 *const widget) : mJoystickEnabled->setActionEventId("joystick"); mJoystickEnabled->addActionListener(this); - float tolerance = config.getFloatValue("joystickTolerance"); + const float tolerance = config.getFloatValue("joystickTolerance"); mToleranceSlider->setValue(tolerance); // TRANSLATORS: joystick settings tab label - mToleranceLabel->setCaption(_("Axis tolerance: ") - + strprintf("%.2f", tolerance)); + mToleranceLabel->setCaption(_("joystick dead zone: ") + + strprintf("%.2f", tolerance)); mToleranceLabel->setWidth(150); mToleranceLabel->setHeight(20); @@ -137,10 +137,10 @@ void Setup_Joystick::action(const ActionEvent &event) } else if (source == mToleranceSlider) { - float tolerance = mToleranceSlider->getValue(); + const float tolerance = mToleranceSlider->getValue(); // TRANSLATORS: joystick settings tab label - mToleranceLabel->setCaption(_("Axis tolerance: ") - + strprintf("%.2f", tolerance)); + mToleranceLabel->setCaption(_("joystick dead zone: ") + + strprintf("%.2f", tolerance)); } else if (source == mDetectButton) { -- cgit v1.2.3-70-g09d2