From 1742e4d7fdc0076e093af6d4c8b527efaba814ad Mon Sep 17 00:00:00 2001 From: Maschell Date: Sun, 5 Jan 2025 13:34:53 +0100 Subject: [PATCH] Fix press down detection if more than one controller is connected --- source/ButtonComboInfo.cpp | 36 ++++++++++++++++++++++++++++++ source/ButtonComboInfo.h | 2 ++ source/ButtonComboInfoDown.cpp | 15 +++++++++---- source/ButtonComboInfoDown.h | 6 ++++- source/ButtonComboInfoHold.cpp | 40 +--------------------------------- 5 files changed, 55 insertions(+), 44 deletions(-) diff --git a/source/ButtonComboInfo.cpp b/source/ButtonComboInfo.cpp index ae07cc6..5ae3632 100644 --- a/source/ButtonComboInfo.cpp +++ b/source/ButtonComboInfo.cpp @@ -103,4 +103,40 @@ bool ButtonComboInfoIF::conflictsWith(const ButtonComboModule_ButtonComboOptions bool ButtonComboInfoIF::conflictsWith(const ButtonComboInfoIF &other) const { return conflictsWith({other.mControllerMask, other.mCombo}); +} + +int32_t ButtonComboInfoIF::ControllerTypeToChanIndex(const ButtonComboModule_ControllerTypes type) { + switch (type) { + case BUTTON_COMBO_MODULE_CONTROLLER_VPAD_0: { + return 0; + } + case BUTTON_COMBO_MODULE_CONTROLLER_VPAD_1: { + return 1; + } + case BUTTON_COMBO_MODULE_CONTROLLER_WPAD_0: { + return 2; + } + case BUTTON_COMBO_MODULE_CONTROLLER_WPAD_1: { + return 3; + } + case BUTTON_COMBO_MODULE_CONTROLLER_WPAD_2: { + return 4; + } + case BUTTON_COMBO_MODULE_CONTROLLER_WPAD_3: { + return 5; + } + case BUTTON_COMBO_MODULE_CONTROLLER_WPAD_4: { + return 6; + } + case BUTTON_COMBO_MODULE_CONTROLLER_WPAD_5: { + return 7; + } + case BUTTON_COMBO_MODULE_CONTROLLER_WPAD_6: { + return 8; + } + default: + break; + } + + return -1; } \ No newline at end of file diff --git a/source/ButtonComboInfo.h b/source/ButtonComboInfo.h index 3db119b..70e8bfb 100644 --- a/source/ButtonComboInfo.h +++ b/source/ButtonComboInfo.h @@ -47,6 +47,8 @@ class ButtonComboInfoIF { [[nodiscard]] virtual ButtonComboModule_ButtonComboInfoEx getComboInfoEx() const = 0; protected: + static int32_t ControllerTypeToChanIndex(ButtonComboModule_ControllerTypes type); + ButtonComboModule_ComboStatus mStatus = BUTTON_COMBO_MODULE_COMBO_STATUS_INVALID_STATUS; std::string mLabel; ButtonComboModule_ControllerTypes mControllerMask = {}; diff --git a/source/ButtonComboInfoDown.cpp b/source/ButtonComboInfoDown.cpp index f5826c2..2f26e13 100644 --- a/source/ButtonComboInfoDown.cpp +++ b/source/ButtonComboInfoDown.cpp @@ -22,13 +22,20 @@ void ButtonComboInfoDown::UpdateInput( if ((mControllerMask & controller) == 0) { return; } + const auto chanIndex = ControllerTypeToChanIndex(controller); + if (chanIndex < 0 || static_cast(chanIndex) >= std::size(mHoldInformation)) { + DEBUG_FUNCTION_LINE_WARN("ChanIndex is out of bounds %d", chanIndex); + return; + } + + auto &[prevButtonCombo] = mHoldInformation[chanIndex]; DEBUG_FUNCTION_LINE_VERBOSE("[PRESS DOWN] Check button combo %08X on controller %08X (lastItem im pressedButtons (size %d) is %08X) for %s [%08X]", mCombo, controller, pressedButtons.size(), pressedButtons.back(), mLabel.c_str(), getHandle().handle); for (const auto &pressedButton : pressedButtons) { - const bool prevButtonsIncludedCombo = (mPrevButtonPress & mCombo) == mCombo; // Make sure the combo can't be triggered on releasing - const bool buttonsPressedChanged = mPrevButtonPress != pressedButton; // Avoid "holding" the combo - const bool buttonsPressedMatchCombo = pressedButton == mCombo; // detect the actual combo + const bool prevButtonsIncludedCombo = (prevButtonCombo & mCombo) == mCombo; // Make sure the combo can't be triggered on releasing + const bool buttonsPressedChanged = prevButtonCombo != pressedButton; // Avoid "holding" the combo + const bool buttonsPressedMatchCombo = pressedButton == mCombo; // detect the actual combo if (buttonsPressedChanged && buttonsPressedMatchCombo && !prevButtonsIncludedCombo) { if (mCallback != nullptr) { @@ -38,7 +45,7 @@ void ButtonComboInfoDown::UpdateInput( DEBUG_FUNCTION_LINE_WARN("Callback was null for combo %08X", getHandle()); } } - mPrevButtonPress = pressedButton; + prevButtonCombo = pressedButton; } } diff --git a/source/ButtonComboInfoDown.h b/source/ButtonComboInfoDown.h index 0c8e3c6..4521227 100644 --- a/source/ButtonComboInfoDown.h +++ b/source/ButtonComboInfoDown.h @@ -19,11 +19,15 @@ class ButtonComboInfoDown final : public ButtonComboInfoIF { ~ButtonComboInfoDown() override; private: + typedef struct { + uint32_t prevButtonCombo; + } HoldInformation; + void UpdateInput(ButtonComboModule_ControllerTypes controller, std::span pressedButtons) override; ButtonComboModule_Error setHoldDuration(uint32_t uint32) override; [[nodiscard]] ButtonComboModule_ButtonComboInfoEx getComboInfoEx() const override; - uint32_t mPrevButtonPress = {}; + HoldInformation mHoldInformation[9] = {}; // one for each controller }; \ No newline at end of file diff --git a/source/ButtonComboInfoHold.cpp b/source/ButtonComboInfoHold.cpp index df6b6e2..26e1f68 100644 --- a/source/ButtonComboInfoHold.cpp +++ b/source/ButtonComboInfoHold.cpp @@ -2,44 +2,6 @@ #include -namespace { - int32_t controllerTypeToChanIndex(const ButtonComboModule_ControllerTypes type) { - switch (type) { - case BUTTON_COMBO_MODULE_CONTROLLER_VPAD_0: { - return 0; - } - case BUTTON_COMBO_MODULE_CONTROLLER_VPAD_1: { - return 1; - } - case BUTTON_COMBO_MODULE_CONTROLLER_WPAD_0: { - return 2; - } - case BUTTON_COMBO_MODULE_CONTROLLER_WPAD_1: { - return 3; - } - case BUTTON_COMBO_MODULE_CONTROLLER_WPAD_2: { - return 4; - } - case BUTTON_COMBO_MODULE_CONTROLLER_WPAD_3: { - return 5; - } - case BUTTON_COMBO_MODULE_CONTROLLER_WPAD_4: { - return 6; - } - case BUTTON_COMBO_MODULE_CONTROLLER_WPAD_5: { - return 7; - } - case BUTTON_COMBO_MODULE_CONTROLLER_WPAD_6: { - return 8; - } - default: - break; - } - - return -1; - } -} // namespace - ButtonComboInfoHold::ButtonComboInfoHold(std::string label, const ButtonComboModule_ControllerTypes controllerMask, const ButtonComboModule_Buttons combo, @@ -64,7 +26,7 @@ void ButtonComboInfoHold::UpdateInput(const ButtonComboModule_ControllerTypes co if ((mControllerMask & controller) == 0) { return; } - const auto chanIndex = controllerTypeToChanIndex(controller); + const auto chanIndex = ControllerTypeToChanIndex(controller); if (chanIndex < 0 || static_cast(chanIndex) >= std::size(mHoldInformation)) { DEBUG_FUNCTION_LINE_WARN("ChanIndex is out of bounds %d", chanIndex); return;