From de16ea49c7244ec85bb3f4a0c0ef542b37b8f858 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Mon, 28 Jun 2021 22:38:25 -0700 Subject: [PATCH] Qt: Redo sensor binding to be less fragile --- CHANGES | 1 + src/platform/qt/SensorView.cpp | 17 +++++++++-------- src/platform/qt/SensorView.h | 4 +++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CHANGES b/CHANGES index 27931a979..525d2e94b 100644 --- a/CHANGES +++ b/CHANGES @@ -34,6 +34,7 @@ Other fixes: - Qt: Fix applying savetype-only overrides - Qt: Fix crash in sprite view for partially out-of-bounds sprites (fixes mgba.io/i/2165) - Qt: Fix having to press controller buttons twice for menu items (fixes mgba.io/i/2143) + - Qt: Redo sensor binding to be less fragile - Util: Fix loading UPS patches that affect the last byte of the file Misc: - Core: Suspend runloop when a core crashes diff --git a/src/platform/qt/SensorView.cpp b/src/platform/qt/SensorView.cpp index ba8f87da2..1742a26ee 100644 --- a/src/platform/qt/SensorView.cpp +++ b/src/platform/qt/SensorView.cpp @@ -79,14 +79,11 @@ void SensorView::setController(std::shared_ptr controller) { void SensorView::jiggerer(QAbstractButton* button, void (InputController::*setter)(int)) { connect(button, &QAbstractButton::toggled, [this, button, setter](bool checked) { if (!checked) { - m_jiggered = nullptr; + m_button = nullptr; } else { button->setFocus(); - m_jiggered = [this, button, setter](int axis) { - (m_input->*setter)(axis); - button->setChecked(false); - button->clearFocus(); - }; + m_button = button; + m_setter = setter; } }); button->installEventFilter(this); @@ -106,8 +103,12 @@ bool SensorView::eventFilter(QObject*, QEvent* event) { if (event->type() == GamepadAxisEvent::Type()) { GamepadAxisEvent* gae = static_cast(event); gae->accept(); - if (m_jiggered && gae->direction() != GamepadAxisEvent::NEUTRAL && gae->isNew()) { - m_jiggered(gae->axis()); + if (m_button && gae->direction() != GamepadAxisEvent::NEUTRAL && gae->isNew()) { + m_button->removeEventFilter(this); + m_button->clearFocus(); + m_button->setChecked(false); + (m_input->*m_setter)(gae->axis()); + m_button = nullptr; } return true; } diff --git a/src/platform/qt/SensorView.h b/src/platform/qt/SensorView.h index 4cead7d11..5ccc40ec8 100644 --- a/src/platform/qt/SensorView.h +++ b/src/platform/qt/SensorView.h @@ -42,7 +42,9 @@ private slots: private: Ui::SensorView m_ui; - std::function m_jiggered; + QAbstractButton* m_button = nullptr; + void (InputController::*m_setter)(int); + std::shared_ptr m_controller; InputController* m_input; mRotationSource* m_rotation;