Qt: Hacky way to swap out focus for a gamepad (fixes #64)
Jeffrey Pfau jeffrey@endrift.com
Wed, 22 Jul 2015 21:16:28 -0700
7 files changed,
67 insertions(+),
3 deletions(-)
M
src/platform/qt/GBAKeyEditor.cpp
→
src/platform/qt/GBAKeyEditor.cpp
@@ -32,6 +32,7 @@ setWindowFlags(windowFlags() & ~Qt::WindowFullscreenButtonHint);
setMinimumSize(300, 300); const GBAInputMap* map = controller->map(); + controller->stealFocus(this); m_keyDU = new KeyEditor(this); m_keyDD = new KeyEditor(this);@@ -149,6 +150,19 @@ void GBAKeyEditor::paintEvent(QPaintEvent* event) {
QPainter painter(this); painter.scale(width() / 480.0, height() / 480.0); painter.drawPicture(0, 0, m_background); +} + +void GBAKeyEditor::closeEvent(QCloseEvent*) { + m_controller->releaseFocus(this); +} + +bool GBAKeyEditor::event(QEvent* event) { + if (event->type() == QEvent::WindowActivate) { + m_controller->stealFocus(this); + } else if (event->type() == QEvent::WindowDeactivate) { + m_controller->releaseFocus(this); + } + return QWidget::event(event); } void GBAKeyEditor::setNext() {
M
src/platform/qt/GBAKeyEditor.h
→
src/platform/qt/GBAKeyEditor.h
@@ -35,6 +35,8 @@
protected: virtual void resizeEvent(QResizeEvent*) override; virtual void paintEvent(QPaintEvent*) override; + virtual bool event(QEvent*) override; + virtual void closeEvent(QCloseEvent*) override; private slots: void setNext();
M
src/platform/qt/InputController.cpp
→
src/platform/qt/InputController.cpp
@@ -35,6 +35,7 @@ , m_playerAttached(false)
#endif , m_allowOpposing(false) , m_topLevel(topLevel) + , m_focusParent(topLevel) { GBAInputMapInit(&m_inputMap);@@ -469,10 +470,10 @@ }
void InputController::sendGamepadEvent(QEvent* event) { QWidget* focusWidget = nullptr; - if (m_topLevel) { - focusWidget = m_topLevel->focusWidget(); + if (m_focusParent) { + focusWidget = m_focusParent->focusWidget(); if (!focusWidget) { - focusWidget = m_topLevel; + focusWidget = m_focusParent; } } else { focusWidget = QApplication::focusWidget();@@ -505,3 +506,13 @@ void InputController::setScreensaverSuspendable(bool suspendable) {
GBASDLSetScreensaverSuspendable(&s_sdlEvents, suspendable); } #endif + +void InputController::stealFocus(QWidget* focus) { + m_focusParent = focus; +} + +void InputController::releaseFocus(QWidget* focus) { + if (focus == m_focusParent) { + m_focusParent = m_topLevel; + } +}
M
src/platform/qt/InputController.h
→
src/platform/qt/InputController.h
@@ -74,6 +74,9 @@
float gyroSensitivity() const; void setGyroSensitivity(float sensitivity); + void stealFocus(QWidget* focus); + void releaseFocus(QWidget* focus); + GBARumble* rumble(); GBARotationSource* rotationSource();@@ -101,6 +104,7 @@ ConfigController* m_config;
int m_playerId; bool m_allowOpposing; QWidget* m_topLevel; + QWidget* m_focusParent; #ifdef BUILD_SDL static int s_sdlInited;
M
src/platform/qt/ShortcutView.cpp
→
src/platform/qt/ShortcutView.cpp
@@ -6,6 +6,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ShortcutView.h" #include "GamepadButtonEvent.h" +#include "InputController.h" #include "ShortcutController.h" #include <QKeyEvent>@@ -15,6 +16,7 @@
ShortcutView::ShortcutView(QWidget* parent) : QWidget(parent) , m_controller(nullptr) + , m_input(nullptr) { m_ui.setupUi(this); m_ui.keyEdit->setValueButton(-1);@@ -30,6 +32,14 @@
void ShortcutView::setController(ShortcutController* controller) { m_controller = controller; m_ui.shortcutTable->setModel(controller); +} + +void ShortcutView::setInputController(InputController* controller) { + if (m_input) { + m_input->releaseFocus(this); + } + m_input = controller; + m_input->stealFocus(this); } bool ShortcutView::eventFilter(QObject*, QEvent* event) {@@ -111,3 +121,20 @@ }
m_controller->updateAxis(m_ui.shortcutTable->selectionModel()->currentIndex(), axis, static_cast<GamepadAxisEvent::Direction>(direction)); } + +void ShortcutView::closeEvent(QCloseEvent*) { + if (m_input) { + m_input->releaseFocus(this); + } +} + +bool ShortcutView::event(QEvent* event) { + if (m_input) { + if (event->type() == QEvent::WindowActivate) { + m_input->stealFocus(this); + } else if (event->type() == QEvent::WindowDeactivate) { + m_input->releaseFocus(this); + } + } + return QWidget::event(event); +}
M
src/platform/qt/ShortcutView.h
→
src/platform/qt/ShortcutView.h
@@ -14,6 +14,7 @@ #include "ui_ShortcutView.h"
namespace QGBA { +class InputController; class ShortcutController; class ShortcutView : public QWidget {@@ -23,9 +24,12 @@ public:
ShortcutView(QWidget* parent = nullptr); void setController(ShortcutController* controller); + void setInputController(InputController* input); protected: virtual bool eventFilter(QObject* obj, QEvent* event) override; + virtual bool event(QEvent*) override; + virtual void closeEvent(QCloseEvent*) override; private slots: void load(const QModelIndex&);@@ -38,6 +42,7 @@ private:
Ui::ShortcutView m_ui; ShortcutController* m_controller; + InputController* m_input; }; }
M
src/platform/qt/Window.cpp
→
src/platform/qt/Window.cpp
@@ -315,6 +315,7 @@ m_inputController.recalibrateAxes();
#endif ShortcutView* shortcutView = new ShortcutView(); shortcutView->setController(m_shortcutController); + shortcutView->setInputController(&m_inputController); openView(shortcutView); }