Qt: Hacky way to swap out focus for a gamepad (fixes #64)

This commit is contained in:
Jeffrey Pfau 2015-07-22 21:16:28 -07:00
parent 0ecdc1ac44
commit b9c9425464
7 changed files with 67 additions and 3 deletions

View File

@ -32,6 +32,7 @@ GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, const QString&
setMinimumSize(300, 300);
const GBAInputMap* map = controller->map();
controller->stealFocus(this);
m_keyDU = new KeyEditor(this);
m_keyDD = new KeyEditor(this);
@ -151,6 +152,19 @@ void GBAKeyEditor::paintEvent(QPaintEvent* event) {
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() {
findFocus();

View File

@ -35,6 +35,8 @@ public slots:
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();

View File

@ -35,6 +35,7 @@ InputController::InputController(int playerId, QWidget* topLevel, QObject* paren
#endif
, m_allowOpposing(false)
, m_topLevel(topLevel)
, m_focusParent(topLevel)
{
GBAInputMapInit(&m_inputMap);
@ -469,10 +470,10 @@ void InputController::testGamepad(int type) {
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;
}
}

View File

@ -74,6 +74,9 @@ public:
float gyroSensitivity() const;
void setGyroSensitivity(float sensitivity);
void stealFocus(QWidget* focus);
void releaseFocus(QWidget* focus);
GBARumble* rumble();
GBARotationSource* rotationSource();
@ -101,6 +104,7 @@ private:
int m_playerId;
bool m_allowOpposing;
QWidget* m_topLevel;
QWidget* m_focusParent;
#ifdef BUILD_SDL
static int s_sdlInited;

View File

@ -6,6 +6,7 @@
#include "ShortcutView.h"
#include "GamepadButtonEvent.h"
#include "InputController.h"
#include "ShortcutController.h"
#include <QKeyEvent>
@ -15,6 +16,7 @@ using namespace QGBA;
ShortcutView::ShortcutView(QWidget* parent)
: QWidget(parent)
, m_controller(nullptr)
, m_input(nullptr)
{
m_ui.setupUi(this);
m_ui.keyEdit->setValueButton(-1);
@ -32,6 +34,14 @@ void ShortcutView::setController(ShortcutController* 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) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
@ -111,3 +121,20 @@ void ShortcutView::updateAxis(int axis, int direction) {
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);
}

View File

@ -14,6 +14,7 @@
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;
};
}

View File

@ -315,6 +315,7 @@ void Window::openShortcutWindow() {
#endif
ShortcutView* shortcutView = new ShortcutView();
shortcutView->setController(m_shortcutController);
shortcutView->setInputController(&m_inputController);
openView(shortcutView);
}