From 9e3a03079cfa530fd3885e36d3f7214ec2348818 Mon Sep 17 00:00:00 2001 From: ewewukek Date: Wed, 10 Jan 2024 22:29:22 +0300 Subject: Use single tolerance value for all 4 joystick movement directions --- src/defaults.cpp | 5 +---- src/input/joystick.cpp | 49 +++++++++++++++++-------------------------------- src/input/joystick.h | 5 +---- 3 files changed, 19 insertions(+), 40 deletions(-) diff --git a/src/defaults.cpp b/src/defaults.cpp index 4869de8ef..30c5f3caf 100644 --- a/src/defaults.cpp +++ b/src/defaults.cpp @@ -194,10 +194,7 @@ void setConfigDefaults(Configuration &cfg) #else AddDEF("joystickEnabled", false); #endif - AddDEF("upTolerance", 100); - AddDEF("downTolerance", 100); - AddDEF("leftTolerance", 100); - AddDEF("rightTolerance", 100); + AddDEF("joystickTolerance", 10000); AddDEF("logNpcInGui", true); AddDEF("download-music", true); AddDEF("guialpha", 0.8F); diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp index 52d9a7525..5396adadc 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -48,10 +48,7 @@ bool Joystick::mInitialized = false; Joystick::Joystick(const int no) : mDirection(0), mJoystick(nullptr), - mUpTolerance(0), - mDownTolerance(0), - mLeftTolerance(0), - mRightTolerance(0), + mTolerance(0), mCalibrating(false), mNumber(no >= joystickCount ? joystickCount : no), mCalibrated(false), @@ -201,18 +198,12 @@ bool Joystick::open() #ifdef __SWITCH__ config.setValue("joystick" + toString(mNumber) + "calibrated", true); - config.setValue("leftTolerance" + toString(mNumber), -10000); - config.setValue("rightTolerance" + toString(mNumber), 10000); - config.setValue("upTolerance" + toString(mNumber), -10000); - config.setValue("downTolerance" + toString(mNumber), 10000); + config.setValue("joystickTolerance" + toString(mNumber), 10000); #endif mCalibrated = config.getValueBool("joystick" + toString(mNumber) + "calibrated", false); - mUpTolerance = config.getIntValue("upTolerance" + toString(mNumber)); - mDownTolerance = config.getIntValue("downTolerance" + toString(mNumber)); - mLeftTolerance = config.getIntValue("leftTolerance" + toString(mNumber)); - mRightTolerance = config.getIntValue("rightTolerance" + toString(mNumber)); + mTolerance = config.getIntValue("joystickTolerance" + toString(mNumber)); mUseInactive = config.getBoolValue("useInactiveJoystick"); return true; @@ -266,16 +257,16 @@ void Joystick::logic() { // X-Axis int position = SDL_JoystickGetAxis(mJoystick, 0); - if (position >= mRightTolerance) + if (position >= mTolerance) mDirection |= RIGHT; - else if (position <= mLeftTolerance) + else if (position <= -mTolerance) mDirection |= LEFT; // Y-Axis position = SDL_JoystickGetAxis(mJoystick, 1); - if (position <= mUpTolerance) + if (position <= -mTolerance) mDirection |= UP; - else if (position >= mDownTolerance) + else if (position >= mTolerance) mDirection |= DOWN; #ifdef DEBUG_JOYSTICK @@ -324,10 +315,7 @@ void Joystick::logic() void Joystick::startCalibration() { - mUpTolerance = 0; - mDownTolerance = 0; - mLeftTolerance = 0; - mRightTolerance = 0; + mTolerance = 0; mCalibrating = true; } @@ -335,17 +323,17 @@ void Joystick::doCalibration() { // X-Axis int position = SDL_JoystickGetAxis(mJoystick, 0); - if (position > mRightTolerance) - mRightTolerance = position; - else if (position < mLeftTolerance) - mLeftTolerance = position; + if (position > mTolerance) + mTolerance = position; + else if (position < -mTolerance) + mTolerance = -position; // Y-Axis position = SDL_JoystickGetAxis(mJoystick, 1); - if (position > mDownTolerance) - mDownTolerance = position; - else if (position < mUpTolerance) - mUpTolerance = position; + if (position > mTolerance) + mTolerance = position; + else if (position < -mTolerance) + mTolerance = -position; } void Joystick::finishCalibration() @@ -353,10 +341,7 @@ void Joystick::finishCalibration() mCalibrated = true; mCalibrating = false; config.setValue("joystick" + toString(mNumber) + "calibrated", true); - config.setValue("leftTolerance" + toString(mNumber), mLeftTolerance); - config.setValue("rightTolerance" + toString(mNumber), mRightTolerance); - config.setValue("upTolerance" + toString(mNumber), mUpTolerance); - config.setValue("downTolerance" + toString(mNumber), mDownTolerance); + config.setValue("joystickTolerance" + toString(mNumber), mTolerance); } bool Joystick::buttonPressed(const unsigned char no) const diff --git a/src/input/joystick.h b/src/input/joystick.h index a3c129950..3587d1ba0 100644 --- a/src/input/joystick.h +++ b/src/input/joystick.h @@ -148,10 +148,7 @@ class Joystick final SDL_Joystick *mJoystick; - int mUpTolerance; - int mDownTolerance; - int mLeftTolerance; - int mRightTolerance; + int mTolerance; bool mCalibrating; int mNumber; bool mCalibrated; -- cgit v1.2.3-70-g09d2 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 +++ src/input/joystick.cpp | 6 +++--- src/input/joystick.h | 3 +++ 4 files changed, 40 insertions(+), 8 deletions(-) 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; }; diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp index 5396adadc..48b4488b0 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -198,12 +198,12 @@ bool Joystick::open() #ifdef __SWITCH__ config.setValue("joystick" + toString(mNumber) + "calibrated", true); - config.setValue("joystickTolerance" + toString(mNumber), 10000); + config.setValue("joystickTolerance", 10000); #endif mCalibrated = config.getValueBool("joystick" + toString(mNumber) + "calibrated", false); - mTolerance = config.getIntValue("joystickTolerance" + toString(mNumber)); + mTolerance = config.getIntValue("joystickTolerance"); mUseInactive = config.getBoolValue("useInactiveJoystick"); return true; @@ -341,7 +341,7 @@ void Joystick::finishCalibration() mCalibrated = true; mCalibrating = false; config.setValue("joystick" + toString(mNumber) + "calibrated", true); - config.setValue("joystickTolerance" + toString(mNumber), mTolerance); + config.setValue("joystickTolerance", mTolerance); } bool Joystick::buttonPressed(const unsigned char no) const diff --git a/src/input/joystick.h b/src/input/joystick.h index 3587d1ba0..198b6fe9e 100644 --- a/src/input/joystick.h +++ b/src/input/joystick.h @@ -122,6 +122,9 @@ class Joystick final int getNumber() const noexcept2 A_WARN_UNUSED { return mNumber; } + void setTolerance(const int tolerance) + { mTolerance = tolerance; } + void setUseInactive(const bool b) { mUseInactive = b; } -- 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(-) 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(-) 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 a9b4edd10420fb35679ee9aac3caa9eb8f35e6dd Mon Sep 17 00:00:00 2001 From: ewewukek Date: Thu, 11 Jan 2024 12:53:19 +0300 Subject: Make D-Pad behave like regular buttons --- src/input/inputmanager.cpp | 56 ++++++++++++++++++++++++++++--- src/input/joystick.cpp | 83 ++++++++++++++++++++++++++++++++-------------- src/input/joystick.h | 15 ++++++++- 3 files changed, 125 insertions(+), 29 deletions(-) diff --git a/src/input/inputmanager.cpp b/src/input/inputmanager.cpp index a379e0e86..0a113bd76 100644 --- a/src/input/inputmanager.cpp +++ b/src/input/inputmanager.cpp @@ -416,8 +416,31 @@ std::string InputManager::getKeyStringLong(const InputActionT index) const } else if (key.type == InputType::JOYSTICK) { - // TRANSLATORS: long joystick button name. must be short. - str = strprintf(_("JButton%d"), key.value + 1); + if (key.value < Joystick::MAX_BUTTONS) + { + // TRANSLATORS: joystick button name. must be short. + str = strprintf(_("JButton%d"), key.value + 1); + } + else if (key.value == Joystick::KEY_UP) + { + // TRANSLATORS: joystick up button. must be short. + str = _("JUp"); + } + else if (key.value == Joystick::KEY_DOWN) + { + // TRANSLATORS: joystick down button. must be short. + str = _("JDown"); + } + else if (key.value == Joystick::KEY_LEFT) + { + // TRANSLATORS: joystick left button. must be short. + str = _("JLeft"); + } + else if (key.value == Joystick::KEY_RIGHT) + { + // TRANSLATORS: joystick right button. must be short. + str = _("JRight"); + } } if (!str.empty()) { @@ -459,8 +482,31 @@ void InputManager::updateKeyString(const InputFunction &ki, } else if (key.type == InputType::JOYSTICK) { - // TRANSLATORS: short joystick button name. muse be very short - str = strprintf(_("JB%d"), key.value + 1); + if (key.value < Joystick::MAX_BUTTONS) + { + // TRANSLATORS: joystick button name. must be very short + str = strprintf(_("JB%d"), key.value + 1); + } + else if (key.value == Joystick::KEY_UP) + { + // TRANSLATORS: joystick up button. must be very short + str = _("JUp"); + } + else if (key.value == Joystick::KEY_DOWN) + { + // TRANSLATORS: joystick down button. must be very short + str = _("JDn"); + } + else if (key.value == Joystick::KEY_LEFT) + { + // TRANSLATORS: joystick left button. must be very short + str = _("JLt"); + } + else if (key.value == Joystick::KEY_RIGHT) + { + // TRANSLATORS: joystick right button. must be very short + str = _("JRt"); + } } if (!str.empty()) { @@ -650,6 +696,7 @@ bool InputManager::handleEvent(const SDL_Event &restrict event) restrict2 break; } case SDL_JOYBUTTONDOWN: + case SDL_JOYHATMOTION: { updateConditionMask(true); // joystick.handleActicateButton(event); @@ -724,6 +771,7 @@ bool InputManager::handleEvent(const SDL_Event &restrict event) restrict2 // break; case SDL_JOYBUTTONDOWN: + case SDL_JOYHATMOTION: if ((joystick != nullptr) && joystick->validate()) { if (triggerAction(joystick->getActionVector(event))) diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp index 0ab6102c5..59087c2c7 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -47,6 +47,7 @@ bool Joystick::mInitialized = false; Joystick::Joystick(const int no) : mDirection(0), + mHatPosition(0), mJoystick(nullptr), mTolerance(0), mNumber(no >= joystickCount ? joystickCount : no), @@ -264,18 +265,19 @@ void Joystick::logic() logger->log("axis 4 pos: %d", SDL_JoystickGetAxis(mJoystick, 4)); #endif // DEBUG_JOYSTICK - if ((mDirection == 0U) && mHaveHats) + if (mHaveHats) { // reading only hat 0 const uint8_t hat = SDL_JoystickGetHat(mJoystick, 0); + mHatPosition = 0; if ((hat & SDL_HAT_RIGHT) != 0) - mDirection |= RIGHT; + mHatPosition |= RIGHT; else if ((hat & SDL_HAT_LEFT) != 0) - mDirection |= LEFT; + mHatPosition |= LEFT; if ((hat & SDL_HAT_UP) != 0) - mDirection |= UP; + mHatPosition |= UP; else if ((hat & SDL_HAT_DOWN) != 0) - mDirection |= DOWN; + mHatPosition |= DOWN; } // Buttons @@ -293,15 +295,30 @@ void Joystick::logic() } else { + mHatPosition = 0; for (int i = 0; i < mButtonsNumber; i++) mActiveButtons[i] = false; } BLOCK_END("Joystick::logic") } -bool Joystick::buttonPressed(const unsigned char no) const +bool Joystick::buttonPressed(const int no) const { - return (mEnabled && no < MAX_BUTTONS) ? mActiveButtons[no] : false; + if (!mEnabled) + return false; + if (no < 0) + 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; + return false; } void Joystick::getNames(STD_VECTOR &names) @@ -320,18 +337,12 @@ void Joystick::update() KeysVector *Joystick::getActionVector(const SDL_Event &event) { const int i = getButtonFromEvent(event); - - if (i < 0 || i >= mButtonsNumber) - return nullptr; -// logger->log("button triggerAction: %d", i); - if (mKeyToAction.find(i) != mKeyToAction.end()) - return &mKeyToAction[i]; - return nullptr; + return getActionVectorByKey(i); } KeysVector *Joystick::getActionVectorByKey(const int i) { - if (i < 0 || i >= mButtonsNumber) + if (i < 0 || (i >= mButtonsNumber && i < MAX_BUTTONS) || i > KEY_LAST) return nullptr; // logger->log("button triggerAction: %d", i); if (mKeyToAction.find(i) != mKeyToAction.end()) @@ -341,9 +352,30 @@ KeysVector *Joystick::getActionVectorByKey(const int i) int Joystick::getButtonFromEvent(const SDL_Event &event) const { - if (event.jbutton.which != mNumber) - return -1; - return event.jbutton.button; + if (event.type == SDL_JOYBUTTONDOWN) + { + if (event.jbutton.which != mNumber) + return -1; + return event.jbutton.button; + } + if (event.type == SDL_JOYHATMOTION) + { + // reading only hat 0 + if (event.jhat.which != mNumber || event.jhat.hat != 0) + return -1; + const int value = event.jhat.value; + // SDL reports new hat position, not when d-pad button is pressed. + // because of that we have to compare it to previously known state. + if ((mHatPosition & UP) == 0 && (value & SDL_HAT_UP) != 0) + return KEY_UP; + if ((mHatPosition & DOWN) == 0 && (value & SDL_HAT_DOWN) != 0) + return KEY_DOWN; + if ((mHatPosition & LEFT) == 0 && (value & SDL_HAT_LEFT) != 0) + return KEY_LEFT; + if ((mHatPosition & RIGHT) == 0 && (value & SDL_HAT_RIGHT) != 0) + return KEY_RIGHT; + } + return -1; } bool Joystick::isActionActive(const InputActionT index) const @@ -357,12 +389,7 @@ bool Joystick::isActionActive(const InputActionT index) const const InputItem &val = key.values[i]; if (val.type != InputType::JOYSTICK) continue; - const int value = val.value; - if (value >= 0 && value < mButtonsNumber) - { - if (mActiveButtons[value]) - return true; - } + return buttonPressed(val.value); } return false; } @@ -388,6 +415,14 @@ 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 (repeat) { int &keyTime = (*it).second; diff --git a/src/input/joystick.h b/src/input/joystick.h index 32fdddf2e..bddf520a2 100644 --- a/src/input/joystick.h +++ b/src/input/joystick.h @@ -51,6 +51,18 @@ class Joystick final MAX_BUTTONS = 64 }; + /** + * Additional "buttons" for hat 0 (d-pad). + */ + enum + { + KEY_UP = MAX_BUTTONS, + KEY_DOWN, + KEY_LEFT, + KEY_RIGHT, + KEY_LAST = KEY_RIGHT + }; + /** * Directions, to be used as bitmask values. */ @@ -107,7 +119,7 @@ class Joystick final */ void logic(); - bool buttonPressed(const unsigned char no) const A_WARN_UNUSED; + bool buttonPressed(const int no) const A_WARN_UNUSED; bool isUp() const noexcept2 A_WARN_UNUSED { return mEnabled && ((mDirection & UP) != 0); } @@ -149,6 +161,7 @@ class Joystick final protected: unsigned char mDirection; + unsigned char mHatPosition; bool mActiveButtons[MAX_BUTTONS]; SDL_Joystick *mJoystick; -- 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(-) 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 1dc559144c8809e2d9ab91a5f90a2047fc0595bc Mon Sep 17 00:00:00 2001 From: ewewukek Date: Thu, 11 Jan 2024 13:50:03 +0300 Subject: Gui support for joystick buttons --- src/gui/sdlinput.cpp | 14 ++++++++++++++ src/input/inputmanager.cpp | 12 ++++++++++-- src/input/joystick.cpp | 10 ++++++++++ src/input/joystick.h | 2 ++ 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; -- cgit v1.2.3-70-g09d2 From 0ff6869e745b4a0174ab1ee116ba5d6ad08e4812 Mon Sep 17 00:00:00 2001 From: ewewukek Date: Thu, 11 Jan 2024 14:20:39 +0300 Subject: Extend gui support to joystick hat (d-pad) --- src/gui/sdlinput.cpp | 1 + src/input/inputmanager.cpp | 6 ++++-- src/input/joystick.cpp | 5 +++++ src/input/joystick.h | 2 ++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/gui/sdlinput.cpp b/src/gui/sdlinput.cpp index f9ff0f618..bf6f0536c 100644 --- a/src/gui/sdlinput.cpp +++ b/src/gui/sdlinput.cpp @@ -172,6 +172,7 @@ void SDLInput::pushInput(const SDL_Event &event) } case SDL_JOYBUTTONDOWN: + case SDL_JOYHATMOTION: { const InputActionT actionId = inputManager.getActionByKey(event); if (actionId > InputAction::NO_VALUE) diff --git a/src/input/inputmanager.cpp b/src/input/inputmanager.cpp index 0bffbe231..ac5325e3b 100644 --- a/src/input/inputmanager.cpp +++ b/src/input/inputmanager.cpp @@ -738,7 +738,9 @@ bool InputManager::handleEvent(const SDL_Event &restrict event) restrict2 if (gui != nullptr) { const bool res = gui->handleInput(); - if (res && (event.type == SDL_KEYDOWN || event.type == SDL_JOYBUTTONDOWN)) + const bool joystickActionEvent = + joystick != nullptr && joystick->isActionEvent(event); + if (res && (event.type == SDL_KEYDOWN || joystickActionEvent)) { BLOCK_END("InputManager::handleEvent") return true; @@ -1112,7 +1114,7 @@ InputActionT InputManager::getActionByKey(const SDL_Event &restrict event) return idx; } } - if (joystick != nullptr && event.type == SDL_JOYBUTTONDOWN) + if (joystick != nullptr && joystick->isActionEvent(event)) { const InputActionT idx = joystick->getActionId(event); if (CAST_S32(idx) >= 0 && diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp index 3a4a47fb1..1e375cb0a 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -341,6 +341,11 @@ void Joystick::update() mKeyTimeMap, InputType::JOYSTICK); } +bool Joystick::isActionEvent(const SDL_Event &event) +{ + return getButtonFromEvent(event) >= 0; +} + KeysVector *Joystick::getActionVector(const SDL_Event &event) { const int i = getButtonFromEvent(event); diff --git a/src/input/joystick.h b/src/input/joystick.h index 2a43d1a14..293e75a22 100644 --- a/src/input/joystick.h +++ b/src/input/joystick.h @@ -147,6 +147,8 @@ class Joystick final void update(); + bool isActionEvent(const SDL_Event &event) A_WARN_UNUSED; + KeysVector *getActionVector(const SDL_Event &event) A_WARN_UNUSED; KeysVector *getActionVectorByKey(const int i) A_WARN_UNUSED; -- cgit v1.2.3-70-g09d2 From 842b7fd4db44d85308afbb401023429621d9c586 Mon Sep 17 00:00:00 2001 From: ewewukek Date: Thu, 11 Jan 2024 15:04:28 +0300 Subject: Make sticks/triggers behave like regular buttons --- src/gui/sdlinput.cpp | 1 + src/input/inputmanager.cpp | 33 ++++++++++++++++++++++++- src/input/joystick.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++--- src/input/joystick.h | 15 ++++++++++-- 4 files changed, 103 insertions(+), 7 deletions(-) diff --git a/src/gui/sdlinput.cpp b/src/gui/sdlinput.cpp index bf6f0536c..8f3ca8bff 100644 --- a/src/gui/sdlinput.cpp +++ b/src/gui/sdlinput.cpp @@ -173,6 +173,7 @@ void SDLInput::pushInput(const SDL_Event &event) case SDL_JOYBUTTONDOWN: case SDL_JOYHATMOTION: + case SDL_JOYAXISMOTION: { const InputActionT actionId = inputManager.getActionByKey(event); if (actionId > InputAction::NO_VALUE) diff --git a/src/input/inputmanager.cpp b/src/input/inputmanager.cpp index ac5325e3b..fc2815b5e 100644 --- a/src/input/inputmanager.cpp +++ b/src/input/inputmanager.cpp @@ -441,6 +441,20 @@ std::string InputManager::getKeyStringLong(const InputActionT index) const // TRANSLATORS: joystick right button. must be short. str = _("JRight"); } + else if (key.value >= Joystick::KEY_NEGATIVE_AXIS_FIRST + && key.value < Joystick::KEY_POSITIVE_AXIS_FIRST) + { + // TRANSLATORS: joystick negative axis. must be short. + str = strprintf(_("JAxis%dMinus"), + key.value - Joystick::KEY_NEGATIVE_AXIS_FIRST); + } + else if (key.value >= Joystick::KEY_POSITIVE_AXIS_FIRST + && key.value < Joystick::KEY_END) + { + // TRANSLATORS: joystick positive axis. must be short. + str = strprintf(_("JAxis%dPlus"), + key.value - Joystick::KEY_POSITIVE_AXIS_FIRST); + } } if (!str.empty()) { @@ -507,6 +521,20 @@ void InputManager::updateKeyString(const InputFunction &ki, // TRANSLATORS: joystick right button. must be very short str = _("JRt"); } + else if (key.value >= Joystick::KEY_NEGATIVE_AXIS_FIRST + && key.value < Joystick::KEY_POSITIVE_AXIS_FIRST) + { + // TRANSLATORS: joystick negative axis. must be very short. + str = strprintf(_("JA%d-"), + key.value - Joystick::KEY_NEGATIVE_AXIS_FIRST); + } + else if (key.value >= Joystick::KEY_POSITIVE_AXIS_FIRST + && key.value < Joystick::KEY_END) + { + // TRANSLATORS: joystick positive axis. must be very short. + str = strprintf(_("JA%d+"), + key.value - Joystick::KEY_POSITIVE_AXIS_FIRST); + } } if (!str.empty()) { @@ -697,10 +725,12 @@ bool InputManager::handleEvent(const SDL_Event &restrict event) restrict2 } case SDL_JOYBUTTONDOWN: case SDL_JOYHATMOTION: + case SDL_JOYAXISMOTION: { updateConditionMask(true); // joystick.handleActicateButton(event); - if (handleAssignKey(event, InputType::JOYSTICK)) + if (joystick != nullptr && joystick->isActionEvent(event) + && handleAssignKey(event, InputType::JOYSTICK)) { BLOCK_END("InputManager::handleEvent") return true; @@ -774,6 +804,7 @@ bool InputManager::handleEvent(const SDL_Event &restrict event) restrict2 case SDL_JOYBUTTONDOWN: case SDL_JOYHATMOTION: + case SDL_JOYAXISMOTION: if ((joystick != nullptr) && joystick->validate()) { if (triggerAction(joystick->getActionVector(event))) diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp index 1e375cb0a..21de2ad3d 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -51,6 +51,7 @@ Joystick::Joystick(const int no) : mJoystick(nullptr), mTolerance(0), mNumber(no >= joystickCount ? joystickCount : no), + mAxesNumber(MAX_AXES), mButtonsNumber(MAX_BUTTONS), mUseHatForMovement(true), mUseInactive(false), @@ -59,6 +60,8 @@ Joystick::Joystick(const int no) : mKeyToId(), mKeyTimeMap() { + for (int i = 0; i < MAX_AXES; i++) + mAxesPositions[i] = 0; for (int i = 0; i < MAX_BUTTONS; i++) mActiveButtons[i] = false; } @@ -127,7 +130,9 @@ bool Joystick::open() return false; } + mAxesNumber = SDL_JoystickNumAxes(mJoystick); mButtonsNumber = SDL_JoystickNumButtons(mJoystick); + logger->log("Joystick: %i ", mNumber); #ifdef USE_SDL2 logger->log("Name: %s", SDL_JoystickName(mJoystick)); @@ -186,13 +191,16 @@ bool Joystick::open() logger->log("Name: %s", SDL_JoystickName(mNumber)); #endif // USE_SDL2 - logger->log("Axes: %i ", SDL_JoystickNumAxes(mJoystick)); + logger->log("Axes: %i ", mAxesNumber); logger->log("Balls: %i", SDL_JoystickNumBalls(mJoystick)); logger->log("Hats: %i", SDL_JoystickNumHats(mJoystick)); logger->log("Buttons: %i", mButtonsNumber); mHaveHats = (SDL_JoystickNumHats(mJoystick) > 0); + if (mAxesNumber > MAX_AXES) + mAxesNumber = mAxesNumber; + if (mButtonsNumber > MAX_BUTTONS) mButtonsNumber = MAX_BUTTONS; @@ -244,15 +252,18 @@ void Joystick::logic() if (mUseInactive || settings.inputFocused != KeyboardFocus::Unfocused) { + for (int i = 0; i < mAxesNumber; i++) + mAxesPositions[i] = SDL_JoystickGetAxis(mJoystick, i); + // X-Axis - int position = SDL_JoystickGetAxis(mJoystick, 0); + int position = mAxesPositions[0]; if (position >= mTolerance * SDL_JOYSTICK_AXIS_MAX) mDirection |= RIGHT; else if (position <= mTolerance * SDL_JOYSTICK_AXIS_MIN) mDirection |= LEFT; // Y-Axis - position = SDL_JoystickGetAxis(mJoystick, 1); + position = mAxesPositions[1]; if (position <= mTolerance * SDL_JOYSTICK_AXIS_MIN) mDirection |= UP; else if (position >= mTolerance * SDL_JOYSTICK_AXIS_MAX) @@ -300,6 +311,8 @@ void Joystick::logic() else { mHatPosition = 0; + for (int i = 0; i < mAxesNumber; i++) + mAxesPositions[i] = 0; for (int i = 0; i < mButtonsNumber; i++) mActiveButtons[i] = false; } @@ -325,6 +338,20 @@ bool Joystick::buttonPressed(const int no) const if (no == 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; + } return false; } @@ -354,7 +381,7 @@ KeysVector *Joystick::getActionVector(const SDL_Event &event) KeysVector *Joystick::getActionVectorByKey(const int i) { - if (i < 0 || (i >= mButtonsNumber && i < MAX_BUTTONS) || i > KEY_LAST) + if (i < 0 || (i >= mButtonsNumber && i < MAX_BUTTONS) || i >= KEY_END) return nullptr; // logger->log("button triggerAction: %d", i); if (mKeyToAction.find(i) != mKeyToAction.end()) @@ -397,6 +424,20 @@ int Joystick::getButtonFromEvent(const SDL_Event &event) const if ((mHatPosition & RIGHT) == 0 && (value & SDL_HAT_RIGHT) != 0) return KEY_RIGHT; } + if (event.type == SDL_JOYAXISMOTION) + { + if (event.jaxis.which != mNumber) + return -1; + const int axis = event.jaxis.axis; + if (axis < RESERVED_AXES) + return -1; + if (event.jaxis.value < 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 + && mAxesPositions[axis] < mTolerance * SDL_JOYSTICK_AXIS_MAX) + return KEY_POSITIVE_AXIS_FIRST + axis; + } return -1; } @@ -448,6 +489,18 @@ void Joystick::handleRepeat(const int time) 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) { int &keyTime = (*it).second; diff --git a/src/input/joystick.h b/src/input/joystick.h index 293e75a22..2a7e0f638 100644 --- a/src/input/joystick.h +++ b/src/input/joystick.h @@ -51,8 +51,15 @@ class Joystick final MAX_BUTTONS = 64 }; + enum + { + RESERVED_AXES = 2, // reserved for movement + MAX_AXES = 8 // number of axes we can handle + }; + /** - * Additional "buttons" for hat 0 (d-pad). + * Additional "buttons" for hat 0 (d-pad), + * sticks and triggers. */ enum { @@ -60,7 +67,9 @@ class Joystick final KEY_DOWN, KEY_LEFT, KEY_RIGHT, - KEY_LAST = KEY_RIGHT + KEY_NEGATIVE_AXIS_FIRST, + KEY_POSITIVE_AXIS_FIRST = KEY_NEGATIVE_AXIS_FIRST + MAX_AXES, + KEY_END = KEY_POSITIVE_AXIS_FIRST + MAX_AXES }; /** @@ -169,12 +178,14 @@ class Joystick final unsigned char mDirection; unsigned char mHatPosition; + int mAxesPositions[MAX_AXES]; bool mActiveButtons[MAX_BUTTONS]; SDL_Joystick *mJoystick; float mTolerance; int mNumber; + int mAxesNumber; int mButtonsNumber; bool mUseHatForMovement; bool mUseInactive; -- cgit v1.2.3-70-g09d2 From 45ce5db3a8b84e441ebeb10c54b2a2b7821e5c29 Mon Sep 17 00:00:00 2001 From: ewewukek Date: Thu, 11 Jan 2024 15:09:17 +0300 Subject: Refactor joystick input code --- src/input/joystick.cpp | 126 ++++++++++++++++++++++++++----------------------- src/input/joystick.h | 6 +++ 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; -- cgit v1.2.3-70-g09d2 From 4c6d99ef4e393f900c4acf521a376f6ea0e012f6 Mon Sep 17 00:00:00 2001 From: ewewukek Date: Thu, 11 Jan 2024 15:58:46 +0300 Subject: Heuristic to detect controller triggers --- src/input/joystick.cpp | 29 ++++++++++++++++++++++++++--- src/input/joystick.h | 1 + 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; -- cgit v1.2.3-70-g09d2 From 16fafb1516a346ee0553a329b0606c2bb55283d7 Mon Sep 17 00:00:00 2001 From: ewewukek Date: Tue, 2 Apr 2024 08:18:17 +0300 Subject: Consume input event in QuitDialog instead of relying on a hack --- src/gui/windows/quitdialog.cpp | 5 +++++ src/input/inputmanager.cpp | 22 +++++++--------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/gui/windows/quitdialog.cpp b/src/gui/windows/quitdialog.cpp index d1ceb1ad9..7ef2bc6e8 100644 --- a/src/gui/windows/quitdialog.cpp +++ b/src/gui/windows/quitdialog.cpp @@ -210,6 +210,7 @@ void QuitDialog::keyPressed(KeyEvent &event) { const InputActionT actionId = event.getActionId(); int dir = 0; + bool consume = true; PRAGMA45(GCC diagnostic push) PRAGMA45(GCC diagnostic ignored "-Wswitch-enum") @@ -229,10 +230,14 @@ void QuitDialog::keyPressed(KeyEvent &event) dir = 1; break; default: + consume = false; break; } PRAGMA45(GCC diagnostic pop) + if (consume) + event.consume(); + if (dir != 0) { STD_VECTOR::const_iterator it = mOptions.begin(); diff --git a/src/input/inputmanager.cpp b/src/input/inputmanager.cpp index fc2815b5e..8cb10041e 100644 --- a/src/input/inputmanager.cpp +++ b/src/input/inputmanager.cpp @@ -691,21 +691,7 @@ bool InputManager::handleEvent(const SDL_Event &restrict event) restrict2 BLOCK_END("InputManager::handleEvent") return true; } - keyboard.handleActivateKey(event); - // send straight to gui for certain windows -#ifndef DYECMD - if ((quitDialog != nullptr) || TextDialog::isActive()) - { - if (guiInput != nullptr) - guiInput->pushInput(event); - if (gui != nullptr) - gui->handleInput(); - BLOCK_END("InputManager::handleEvent") - return true; - } -#endif // DYECMD - break; } case SDL_KEYUP: @@ -767,9 +753,15 @@ bool InputManager::handleEvent(const SDL_Event &restrict event) restrict2 guiInput->pushInput(event); if (gui != nullptr) { - const bool res = gui->handleInput(); + bool res = gui->handleInput(); +#ifndef DYECMD + // always treat keyboard input as handled if a text dialog is active + if (event.type == SDL_KEYDOWN && TextDialog::isActive()) + res = true; +#endif // DYECMD const bool joystickActionEvent = joystick != nullptr && joystick->isActionEvent(event); + // stop processing input if event is consumed by the gui if (res && (event.type == SDL_KEYDOWN || joystickActionEvent)) { BLOCK_END("InputManager::handleEvent") -- cgit v1.2.3-70-g09d2 From a4d7bdc0bbd269d64fc885769908b79ff1b85041 Mon Sep 17 00:00:00 2001 From: ewewukek Date: Thu, 4 Apr 2024 14:30:14 +0300 Subject: Enable joystick by default, update default tolerance --- src/defaults.cpp | 6 +----- src/input/joystick.cpp | 3 --- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/defaults.cpp b/src/defaults.cpp index 2c990c42f..3c663309f 100644 --- a/src/defaults.cpp +++ b/src/defaults.cpp @@ -189,12 +189,8 @@ void setConfigDefaults(Configuration &cfg) AddDEF("screenshotDirectory3", ""); AddDEF("useScreenshotDirectorySuffix", true); AddDEF("screenshotDirectorySuffix", ""); -#ifdef __SWITCH__ AddDEF("joystickEnabled", true); -#else - AddDEF("joystickEnabled", false); -#endif - AddDEF("joystickTolerance", 0.1F); + AddDEF("joystickTolerance", 0.3F); AddDEF("useHatForMovement", true); AddDEF("logNpcInGui", true); AddDEF("download-music", true); diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp index 1072d3892..ea1883ad0 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -207,9 +207,6 @@ bool Joystick::open() if (mButtonsNumber > MAX_BUTTONS) mButtonsNumber = MAX_BUTTONS; -#ifdef __SWITCH__ - config.setValue("joystickTolerance", 0.1F); -#endif mTolerance = config.getFloatValue("joystickTolerance"); mUseHatForMovement = config.getBoolValue("useHatForMovement"); mUseInactive = config.getBoolValue("useInactiveJoystick"); -- 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(-) 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 From 95684834ecb45261da1640a4ad8143653698b192 Mon Sep 17 00:00:00 2001 From: Fedja Beader Date: Thu, 16 May 2024 13:14:34 +0000 Subject: Refactor loadFromLogFile calls into ChatTab contructor This has the side effect that now #Debug history is also loaded. **** mana/plus!84 --- src/game.cpp | 2 -- src/gui/widgets/tabs/chat/battletab.cpp | 2 -- src/gui/widgets/tabs/chat/chattab.cpp | 3 +++ src/gui/windows/chatwindow.cpp | 4 ---- src/net/ea/partyrecv.cpp | 2 -- src/net/eathena/clanrecv.cpp | 2 -- src/net/eathena/guildrecv.cpp | 2 -- src/net/tmwa/guildmanager.cpp | 2 -- 8 files changed, 3 insertions(+), 16 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 4e3c399cd..69daa254d 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -293,8 +293,6 @@ static void createGuiWindows() localChatTab = new ChatTab(chatWindow, _("General"), GENERAL_CHANNEL, "#General", ChatTabType::INPUT); localChatTab->setAllowHighlight(false); - if (config.getBoolValue("showChatHistory")) - localChatTab->loadFromLogFile("#General"); // TRANSLATORS: chat tab header debugChatTab = new ChatTab(chatWindow, _("Debug"), "", diff --git a/src/gui/widgets/tabs/chat/battletab.cpp b/src/gui/widgets/tabs/chat/battletab.cpp index 5dd5ff987..9fa18e945 100644 --- a/src/gui/widgets/tabs/chat/battletab.cpp +++ b/src/gui/widgets/tabs/chat/battletab.cpp @@ -36,8 +36,6 @@ BattleTab::BattleTab(const Widget2 *const widget) : ChatTab(widget, _("Battle"), "", "#Battle", ChatTabType::BATTLE) { setTabColors(ThemeColorId::BATTLE_CHAT_TAB); - if (config.getBoolValue("showChatHistory")) - loadFromLogFile("#Battle"); } BattleTab::~BattleTab() diff --git a/src/gui/widgets/tabs/chat/chattab.cpp b/src/gui/widgets/tabs/chat/chattab.cpp index ec9042694..9809287d2 100644 --- a/src/gui/widgets/tabs/chat/chattab.cpp +++ b/src/gui/widgets/tabs/chat/chattab.cpp @@ -97,6 +97,9 @@ ChatTab::ChatTab(const Widget2 *const widget, if (chatWindow != nullptr) chatWindow->addTab(this); mTextOutput->updateSize(true); + + if (config.getBoolValue("showChatHistory")) + loadFromLogFile(logName); } ChatTab::~ChatTab() diff --git a/src/gui/windows/chatwindow.cpp b/src/gui/windows/chatwindow.cpp index a3b777655..15363883f 100644 --- a/src/gui/windows/chatwindow.cpp +++ b/src/gui/windows/chatwindow.cpp @@ -1246,8 +1246,6 @@ WhisperTab *ChatWindow::addWhisperTab(const std::string &caption, if ((gui != nullptr) && !playerRelations.isGoodName(nick)) ret->setLabelFont(gui->getSecureFont()); mWhispers[tempNick] = ret; - if (config.getBoolValue("showChatHistory")) - ret->loadFromLogFile(nick); } if (switchTo) @@ -1330,8 +1328,6 @@ ChatTab *ChatWindow::addChannelTab(const std::string &name, ret = new ChannelTab(this, name); mChannels[tempName] = ret; ret->setAllowHighlight(false); - if (config.getBoolValue("showChatHistory")) - ret->loadFromLogFile(name); } if (switchTo) diff --git a/src/net/ea/partyrecv.cpp b/src/net/ea/partyrecv.cpp index 370cfed9c..53cb0227e 100644 --- a/src/net/ea/partyrecv.cpp +++ b/src/net/ea/partyrecv.cpp @@ -226,8 +226,6 @@ void PartyRecv::processPartyUpdateCoords(Net::MessageIn &msg) void PartyRecv::createTab() { partyTab = new PartyTab(chatWindow); - if (config.getBoolValue("showChatHistory")) - partyTab->loadFromLogFile("#Party"); } } // namespace Ea diff --git a/src/net/eathena/clanrecv.cpp b/src/net/eathena/clanrecv.cpp index 9ecb860a9..c686796dd 100644 --- a/src/net/eathena/clanrecv.cpp +++ b/src/net/eathena/clanrecv.cpp @@ -125,8 +125,6 @@ void ClanRecv::createTab() if (clanTab != nullptr) return; clanTab = new ClanTab(chatWindow); - if (config.getBoolValue("showChatHistory")) - clanTab->loadFromLogFile("#Clan"); } } // namespace EAthena diff --git a/src/net/eathena/guildrecv.cpp b/src/net/eathena/guildrecv.cpp index 74b8d9b8d..3d402c5b9 100644 --- a/src/net/eathena/guildrecv.cpp +++ b/src/net/eathena/guildrecv.cpp @@ -698,8 +698,6 @@ void GuildRecv::processGuildPositionInfo(Net::MessageIn &msg) if ((guildTab == nullptr) && (chatWindow != nullptr)) { guildTab = new GuildTab(chatWindow); - if (config.getBoolValue("showChatHistory")) - guildTab->loadFromLogFile("#Guild"); if (localPlayer != nullptr) localPlayer->addGuild(taGuild); guildHandler->memberList(); diff --git a/src/net/tmwa/guildmanager.cpp b/src/net/tmwa/guildmanager.cpp index f00e444db..52c056de6 100644 --- a/src/net/tmwa/guildmanager.cpp +++ b/src/net/tmwa/guildmanager.cpp @@ -234,8 +234,6 @@ void GuildManager::createTab(Guild *const guild) if (mTab == nullptr) { mTab = new EmulateGuildTab(chatWindow); - if (config.getBoolValue("showChatHistory")) - mTab->loadFromLogFile("#Guild"); if (localPlayer != nullptr) localPlayer->addGuild(guild); } -- cgit v1.2.3-70-g09d2 From 5570d9d1c2b1b5fba50283ec443088430332bb86 Mon Sep 17 00:00:00 2001 From: Fedja Beader Date: Fri, 17 May 2024 18:01:52 +0200 Subject: Fix oddity introduced by 'Download mplint release instead of recompiling every time' No error/warning? odd. --- .gitlab-ci.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f7b8e107c..6b59b54db 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -327,9 +327,6 @@ gcc-8_unittestsbin: tags: - docker - tags: - - docker - linters: stage: lint -- cgit v1.2.3-70-g09d2