diff options
author | Fedja Beader <fedja@protonmail.ch> | 2024-05-15 19:42:10 +0000 |
---|---|---|
committer | Fedja Beader <fedja@protonmail.ch> | 2024-05-15 19:42:10 +0000 |
commit | 99f361699e1c8bbc1e69f4d199863b836b3528a3 (patch) | |
tree | a0223df3b3a8ce2d7adfa270f45a25f930309d42 /src | |
parent | 4337008fdeef5bb01be2fc6fed1683d5941f23ae (diff) | |
parent | ce7efa33a54fdf37416631057dc13799dc2f847f (diff) | |
download | plus-99f361699e1c8bbc1e69f4d199863b836b3528a3.tar.gz plus-99f361699e1c8bbc1e69f4d199863b836b3528a3.tar.bz2 plus-99f361699e1c8bbc1e69f4d199863b836b3528a3.tar.xz plus-99f361699e1c8bbc1e69f4d199863b836b3528a3.zip |
Merge branch 'improved_controller' into 'master'
Controller improvements
See merge request mana/plus!52
Diffstat (limited to 'src')
-rw-r--r-- | src/defaults.cpp | 10 | ||||
-rw-r--r-- | src/gui/sdlinput.cpp | 16 | ||||
-rw-r--r-- | src/gui/widgets/tabs/setup_joystick.cpp | 82 | ||||
-rw-r--r-- | src/gui/widgets/tabs/setup_joystick.h | 6 | ||||
-rw-r--r-- | src/gui/windows/quitdialog.cpp | 5 | ||||
-rw-r--r-- | src/input/inputmanager.cpp | 125 | ||||
-rw-r--r-- | src/input/joystick.cpp | 291 | ||||
-rw-r--r-- | src/input/joystick.h | 67 |
8 files changed, 413 insertions, 189 deletions
diff --git a/src/defaults.cpp b/src/defaults.cpp index 4869de8ef..3c663309f 100644 --- a/src/defaults.cpp +++ b/src/defaults.cpp @@ -189,15 +189,9 @@ void setConfigDefaults(Configuration &cfg) AddDEF("screenshotDirectory3", ""); AddDEF("useScreenshotDirectorySuffix", true); AddDEF("screenshotDirectorySuffix", ""); -#ifdef __SWITCH__ AddDEF("joystickEnabled", true); -#else - AddDEF("joystickEnabled", false); -#endif - AddDEF("upTolerance", 100); - AddDEF("downTolerance", 100); - AddDEF("leftTolerance", 100); - AddDEF("rightTolerance", 100); + AddDEF("joystickTolerance", 0.3F); + AddDEF("useHatForMovement", true); AddDEF("logNpcInGui", true); AddDEF("download-music", true); AddDEF("guialpha", 0.8F); diff --git a/src/gui/sdlinput.cpp b/src/gui/sdlinput.cpp index 9b8b1ec6a..8f3ca8bff 100644 --- a/src/gui/sdlinput.cpp +++ b/src/gui/sdlinput.cpp @@ -171,6 +171,22 @@ void SDLInput::pushInput(const SDL_Event &event) break; } + case SDL_JOYBUTTONDOWN: + case SDL_JOYHATMOTION: + case SDL_JOYAXISMOTION: + { + 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/gui/widgets/tabs/setup_joystick.cpp b/src/gui/widgets/tabs/setup_joystick.cpp index ab623b901..297f5bafc 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" @@ -43,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)), @@ -58,6 +53,13 @@ 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, 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"), @@ -73,10 +75,19 @@ Setup_Joystick::Setup_Joystick(const Widget2 *const widget) : mJoystickEnabled->setSelected(mOriginalJoystickEnabled); mJoystickEnabled->setActionEventId("joystick"); mJoystickEnabled->addActionListener(this); - mCalibrateButton->setEnabled(mOriginalJoystickEnabled); + + const float tolerance = config.getFloatValue("joystickTolerance"); + mToleranceSlider->setValue(tolerance); + // TRANSLATORS: joystick settings tab label + mToleranceLabel->setCaption(_("joystick dead zone: ") + + strprintf("%.2f", tolerance)); + mToleranceLabel->setWidth(150); + mToleranceLabel->setHeight(20); mNamesDropDown->setActionEventId("name"); mNamesDropDown->addActionListener(this); + mToleranceSlider->setActionEventId("toleranceslider"); + mToleranceSlider->addActionListener(this); if (joystick != nullptr) { @@ -96,10 +107,13 @@ 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); + + place(0, 2, mToleranceSlider, 1, 1); + place(1, 2, mToleranceLabel, 1, 1).setPadding(3); + + place(0, 3, mUseHatForMovementCheckBox, 1, 1); + place(0, 4, mUseInactiveCheckBox, 1, 1); + place(0, 5, mDetectButton, 1, 1); setDimension(Rect(0, 0, 365, 75)); } @@ -121,6 +135,13 @@ void Setup_Joystick::action(const ActionEvent &event) if (joystick != nullptr) joystick->setNumber(mNamesDropDown->getSelected()); } + else if (source == mToleranceSlider) + { + const float tolerance = mToleranceSlider->getValue(); + // TRANSLATORS: joystick settings tab label + mToleranceLabel->setCaption(_("joystick dead zone: ") + + strprintf("%.2f", tolerance)); + } else if (source == mDetectButton) { Joystick::detect(); @@ -130,36 +151,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) @@ -186,6 +182,16 @@ 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); + joystick->setTolerance(tolerance); } diff --git a/src/gui/widgets/tabs/setup_joystick.h b/src/gui/widgets/tabs/setup_joystick.h index 174cd82b2..2c138daf5 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 { @@ -50,12 +51,13 @@ 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; 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/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<RadioButton*>::const_iterator it = mOptions.begin(); diff --git a/src/input/inputmanager.cpp b/src/input/inputmanager.cpp index a379e0e86..8cb10041e 100644 --- a/src/input/inputmanager.cpp +++ b/src/input/inputmanager.cpp @@ -416,8 +416,45 @@ 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"); + } + 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()) { @@ -459,8 +496,45 @@ 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"); + } + 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()) { @@ -617,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: @@ -650,10 +710,13 @@ bool InputManager::handleEvent(const SDL_Event &restrict event) restrict2 break; } 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; @@ -690,8 +753,16 @@ bool InputManager::handleEvent(const SDL_Event &restrict event) restrict2 guiInput->pushInput(event); if (gui != nullptr) { - const bool res = gui->handleInput(); - if (res && event.type == SDL_KEYDOWN) + 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") return true; @@ -724,6 +795,8 @@ bool InputManager::handleEvent(const SDL_Event &restrict event) restrict2 // break; case SDL_JOYBUTTONDOWN: + case SDL_JOYHATMOTION: + case SDL_JOYAXISMOTION: if ((joystick != nullptr) && joystick->validate()) { if (triggerAction(joystick->getActionVector(event))) @@ -1055,7 +1128,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); @@ -1065,6 +1137,15 @@ InputActionT InputManager::getActionByKey(const SDL_Event &restrict event) return idx; } } + if (joystick != nullptr && joystick->isActionEvent(event)) + { + 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 52d9a7525..ea1883ad0 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -47,21 +47,24 @@ bool Joystick::mInitialized = false; Joystick::Joystick(const int no) : mDirection(0), + mHatPosition(0), mJoystick(nullptr), - mUpTolerance(0), - mDownTolerance(0), - mLeftTolerance(0), - mRightTolerance(0), - mCalibrating(false), + mTolerance(0), mNumber(no >= joystickCount ? joystickCount : no), - mCalibrated(false), + mAxesNumber(MAX_AXES), mButtonsNumber(MAX_BUTTONS), + mUseHatForMovement(true), mUseInactive(false), mHaveHats(false), mKeyToAction(), mKeyToId(), 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; } @@ -130,7 +133,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)); @@ -189,32 +194,31 @@ 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; -#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); -#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.getFloatValue("joystickTolerance"); + 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; } @@ -245,15 +249,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; @@ -264,18 +260,28 @@ void Joystick::logic() if (mUseInactive || settings.inputFocused != KeyboardFocus::Unfocused) { + for (int i = 0; i < mAxesNumber; 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 = SDL_JoystickGetAxis(mJoystick, 0); - if (position >= mRightTolerance) + int position = mAxesPositions[0]; + if (position >= mTolerance * SDL_JOYSTICK_AXIS_MAX) mDirection |= RIGHT; - else if (position <= mLeftTolerance) + else if (position <= mTolerance * SDL_JOYSTICK_AXIS_MIN) mDirection |= LEFT; // Y-Axis - position = SDL_JoystickGetAxis(mJoystick, 1); - if (position <= mUpTolerance) + position = mAxesPositions[1]; + if (position <= mTolerance * SDL_JOYSTICK_AXIS_MIN) mDirection |= UP; - else if (position >= mDownTolerance) + else if (position >= mTolerance * SDL_JOYSTICK_AXIS_MAX) mDirection |= DOWN; #ifdef DEBUG_JOYSTICK @@ -287,18 +293,21 @@ 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; + if ((mDirection == 0U) && mUseHatForMovement) + mDirection = mHatPosition; } // Buttons @@ -316,52 +325,42 @@ 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; } BLOCK_END("Joystick::logic") } -void Joystick::startCalibration() -{ - mUpTolerance = 0; - mDownTolerance = 0; - mLeftTolerance = 0; - mRightTolerance = 0; - mCalibrating = true; -} - -void Joystick::doCalibration() +bool Joystick::buttonPressed(const int no) const { - // X-Axis - int position = SDL_JoystickGetAxis(mJoystick, 0); - if (position > mRightTolerance) - mRightTolerance = position; - else if (position < mLeftTolerance) - mLeftTolerance = position; - - // Y-Axis - position = SDL_JoystickGetAxis(mJoystick, 1); - if (position > mDownTolerance) - mDownTolerance = position; - else if (position < mUpTolerance) - mUpTolerance = position; -} + if (!mEnabled) + return false; + const int button = getButton(no); + if (button >= 0) + { + if (button < mButtonsNumber) + return mActiveButtons[button]; + if (button == KEY_UP) + return (mHatPosition & UP) != 0; + if (button == KEY_DOWN) + return (mHatPosition & DOWN) != 0; + if (button == KEY_LEFT) + return (mHatPosition & LEFT) != 0; + if (button == KEY_RIGHT) + return (mHatPosition & RIGHT) != 0; + } + const int naxis = getNegativeAxis(no); + if (naxis >= 0) + return mAxesPositions[naxis] < mTolerance * SDL_JOYSTICK_AXIS_MIN; -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); -} + const int paxis = getPositiveAxis(no); + if (paxis >= 0) + return mAxesPositions[paxis] > mTolerance * SDL_JOYSTICK_AXIS_MAX; -bool Joystick::buttonPressed(const unsigned char no) const -{ - return (mEnabled && no < MAX_BUTTONS) ? mActiveButtons[no] : false; + return false; } void Joystick::getNames(STD_VECTOR <std::string> &names) @@ -377,33 +376,128 @@ 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); - - 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 (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; } +InputActionT Joystick::getActionId(const SDL_Event &event) +{ + const int i = getButtonFromEvent(event); + if (i < 0) + return InputAction::NO_VALUE; + + KeyToIdMapIter it = mKeyToId.find(i); + if (it != mKeyToId.end()) + return it->second; + + return InputAction::NO_VALUE; +} + int Joystick::getButtonFromEvent(const SDL_Event &event) const { - if (event.jbutton.which != mNumber) + if (event.type == SDL_JOYBUTTONDOWN) + { + if (event.jbutton.which != mNumber) + return -1; + return event.jbutton.button; + } + if (!mUseHatForMovement && 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; + } + if (event.type == SDL_JOYAXISMOTION) + { + if (event.jaxis.which != mNumber) + return -1; + const int axis = event.jaxis.axis; + if (axis < RESERVED_AXES) + return -1; + 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 (position > mTolerance * SDL_JOYSTICK_AXIS_MAX + && mAxesPositions[axis] < mTolerance * SDL_JOYSTICK_AXIS_MAX) + return KEY_POSITIVE_AXIS_FIRST + axis; + } + 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; - return event.jbutton.button; + + 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 @@ -417,19 +511,14 @@ 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; } bool Joystick::validate() const { - if (mCalibrating || !mEnabled || !mCalibrated) + if (!mEnabled) return false; return mUseInactive || @@ -441,14 +530,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 (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 a3c129950..49c4e553a 100644 --- a/src/input/joystick.h +++ b/src/input/joystick.h @@ -31,6 +31,15 @@ PRAGMA48(GCC diagnostic ignored "-Wshadow") #include <SDL_events.h> 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: @@ -42,6 +51,27 @@ 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), + * sticks and triggers. + */ + enum + { + KEY_UP = MAX_BUTTONS, + KEY_DOWN, + KEY_LEFT, + KEY_RIGHT, + KEY_NEGATIVE_AXIS_FIRST, + KEY_POSITIVE_AXIS_FIRST = KEY_NEGATIVE_AXIS_FIRST + MAX_AXES, + KEY_END = KEY_POSITIVE_AXIS_FIRST + MAX_AXES + }; + /** * Directions, to be used as bitmask values. */ @@ -98,14 +128,7 @@ 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 buttonPressed(const int no) const A_WARN_UNUSED; bool isUp() const noexcept2 A_WARN_UNUSED { return mEnabled && ((mDirection & UP) != 0); } @@ -122,15 +145,25 @@ class Joystick final int getNumber() const noexcept2 A_WARN_UNUSED { return mNumber; } + void setTolerance(const float tolerance) + { mTolerance = tolerance; } + + void setUseHatForMovement(const bool b) + { mUseHatForMovement = b; } + void setUseInactive(const bool b) { mUseInactive = b; } 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; + 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; @@ -144,18 +177,18 @@ class Joystick final protected: unsigned char mDirection; + unsigned char mHatPosition; + int mAxesPositions[MAX_AXES]; + bool mIsTrigger[MAX_AXES]; bool mActiveButtons[MAX_BUTTONS]; SDL_Joystick *mJoystick; - int mUpTolerance; - int mDownTolerance; - int mLeftTolerance; - int mRightTolerance; - bool mCalibrating; + float mTolerance; int mNumber; - bool mCalibrated; + int mAxesNumber; int mButtonsNumber; + bool mUseHatForMovement; bool mUseInactive; bool mHaveHats; @@ -172,7 +205,11 @@ class Joystick final static bool mInitialized; static int joystickCount; - void doCalibration(); + 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; |