mirror of https://github.com/mgba-emu/mgba.git
Qt: Hacky way to swap out focus for a gamepad (fixes #64)
This commit is contained in:
parent
0ecdc1ac44
commit
b9c9425464
|
@ -32,6 +32,7 @@ GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, const QString&
|
||||||
setMinimumSize(300, 300);
|
setMinimumSize(300, 300);
|
||||||
|
|
||||||
const GBAInputMap* map = controller->map();
|
const GBAInputMap* map = controller->map();
|
||||||
|
controller->stealFocus(this);
|
||||||
|
|
||||||
m_keyDU = new KeyEditor(this);
|
m_keyDU = new KeyEditor(this);
|
||||||
m_keyDD = new KeyEditor(this);
|
m_keyDD = new KeyEditor(this);
|
||||||
|
@ -151,6 +152,19 @@ void GBAKeyEditor::paintEvent(QPaintEvent* event) {
|
||||||
painter.drawPicture(0, 0, m_background);
|
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() {
|
void GBAKeyEditor::setNext() {
|
||||||
findFocus();
|
findFocus();
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,8 @@ public slots:
|
||||||
protected:
|
protected:
|
||||||
virtual void resizeEvent(QResizeEvent*) override;
|
virtual void resizeEvent(QResizeEvent*) override;
|
||||||
virtual void paintEvent(QPaintEvent*) override;
|
virtual void paintEvent(QPaintEvent*) override;
|
||||||
|
virtual bool event(QEvent*) override;
|
||||||
|
virtual void closeEvent(QCloseEvent*) override;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void setNext();
|
void setNext();
|
||||||
|
|
|
@ -35,6 +35,7 @@ InputController::InputController(int playerId, QWidget* topLevel, QObject* paren
|
||||||
#endif
|
#endif
|
||||||
, m_allowOpposing(false)
|
, m_allowOpposing(false)
|
||||||
, m_topLevel(topLevel)
|
, m_topLevel(topLevel)
|
||||||
|
, m_focusParent(topLevel)
|
||||||
{
|
{
|
||||||
GBAInputMapInit(&m_inputMap);
|
GBAInputMapInit(&m_inputMap);
|
||||||
|
|
||||||
|
@ -469,10 +470,10 @@ void InputController::testGamepad(int type) {
|
||||||
|
|
||||||
void InputController::sendGamepadEvent(QEvent* event) {
|
void InputController::sendGamepadEvent(QEvent* event) {
|
||||||
QWidget* focusWidget = nullptr;
|
QWidget* focusWidget = nullptr;
|
||||||
if (m_topLevel) {
|
if (m_focusParent) {
|
||||||
focusWidget = m_topLevel->focusWidget();
|
focusWidget = m_focusParent->focusWidget();
|
||||||
if (!focusWidget) {
|
if (!focusWidget) {
|
||||||
focusWidget = m_topLevel;
|
focusWidget = m_focusParent;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
focusWidget = QApplication::focusWidget();
|
focusWidget = QApplication::focusWidget();
|
||||||
|
@ -505,3 +506,13 @@ void InputController::setScreensaverSuspendable(bool suspendable) {
|
||||||
GBASDLSetScreensaverSuspendable(&s_sdlEvents, suspendable);
|
GBASDLSetScreensaverSuspendable(&s_sdlEvents, suspendable);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
void InputController::stealFocus(QWidget* focus) {
|
||||||
|
m_focusParent = focus;
|
||||||
|
}
|
||||||
|
|
||||||
|
void InputController::releaseFocus(QWidget* focus) {
|
||||||
|
if (focus == m_focusParent) {
|
||||||
|
m_focusParent = m_topLevel;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -74,6 +74,9 @@ public:
|
||||||
float gyroSensitivity() const;
|
float gyroSensitivity() const;
|
||||||
void setGyroSensitivity(float sensitivity);
|
void setGyroSensitivity(float sensitivity);
|
||||||
|
|
||||||
|
void stealFocus(QWidget* focus);
|
||||||
|
void releaseFocus(QWidget* focus);
|
||||||
|
|
||||||
GBARumble* rumble();
|
GBARumble* rumble();
|
||||||
GBARotationSource* rotationSource();
|
GBARotationSource* rotationSource();
|
||||||
|
|
||||||
|
@ -101,6 +104,7 @@ private:
|
||||||
int m_playerId;
|
int m_playerId;
|
||||||
bool m_allowOpposing;
|
bool m_allowOpposing;
|
||||||
QWidget* m_topLevel;
|
QWidget* m_topLevel;
|
||||||
|
QWidget* m_focusParent;
|
||||||
|
|
||||||
#ifdef BUILD_SDL
|
#ifdef BUILD_SDL
|
||||||
static int s_sdlInited;
|
static int s_sdlInited;
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include "ShortcutView.h"
|
#include "ShortcutView.h"
|
||||||
|
|
||||||
#include "GamepadButtonEvent.h"
|
#include "GamepadButtonEvent.h"
|
||||||
|
#include "InputController.h"
|
||||||
#include "ShortcutController.h"
|
#include "ShortcutController.h"
|
||||||
|
|
||||||
#include <QKeyEvent>
|
#include <QKeyEvent>
|
||||||
|
@ -15,6 +16,7 @@ using namespace QGBA;
|
||||||
ShortcutView::ShortcutView(QWidget* parent)
|
ShortcutView::ShortcutView(QWidget* parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
, m_controller(nullptr)
|
, m_controller(nullptr)
|
||||||
|
, m_input(nullptr)
|
||||||
{
|
{
|
||||||
m_ui.setupUi(this);
|
m_ui.setupUi(this);
|
||||||
m_ui.keyEdit->setValueButton(-1);
|
m_ui.keyEdit->setValueButton(-1);
|
||||||
|
@ -32,6 +34,14 @@ void ShortcutView::setController(ShortcutController* controller) {
|
||||||
m_ui.shortcutTable->setModel(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) {
|
bool ShortcutView::eventFilter(QObject*, QEvent* event) {
|
||||||
if (event->type() == QEvent::KeyPress) {
|
if (event->type() == QEvent::KeyPress) {
|
||||||
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
|
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,
|
m_controller->updateAxis(m_ui.shortcutTable->selectionModel()->currentIndex(), axis,
|
||||||
static_cast<GamepadAxisEvent::Direction>(direction));
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
|
|
||||||
namespace QGBA {
|
namespace QGBA {
|
||||||
|
|
||||||
|
class InputController;
|
||||||
class ShortcutController;
|
class ShortcutController;
|
||||||
|
|
||||||
class ShortcutView : public QWidget {
|
class ShortcutView : public QWidget {
|
||||||
|
@ -23,9 +24,12 @@ public:
|
||||||
ShortcutView(QWidget* parent = nullptr);
|
ShortcutView(QWidget* parent = nullptr);
|
||||||
|
|
||||||
void setController(ShortcutController* controller);
|
void setController(ShortcutController* controller);
|
||||||
|
void setInputController(InputController* input);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual bool eventFilter(QObject* obj, QEvent* event) override;
|
virtual bool eventFilter(QObject* obj, QEvent* event) override;
|
||||||
|
virtual bool event(QEvent*) override;
|
||||||
|
virtual void closeEvent(QCloseEvent*) override;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void load(const QModelIndex&);
|
void load(const QModelIndex&);
|
||||||
|
@ -38,6 +42,7 @@ private:
|
||||||
Ui::ShortcutView m_ui;
|
Ui::ShortcutView m_ui;
|
||||||
|
|
||||||
ShortcutController* m_controller;
|
ShortcutController* m_controller;
|
||||||
|
InputController* m_input;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -315,6 +315,7 @@ void Window::openShortcutWindow() {
|
||||||
#endif
|
#endif
|
||||||
ShortcutView* shortcutView = new ShortcutView();
|
ShortcutView* shortcutView = new ShortcutView();
|
||||||
shortcutView->setController(m_shortcutController);
|
shortcutView->setController(m_shortcutController);
|
||||||
|
shortcutView->setInputController(&m_inputController);
|
||||||
openView(shortcutView);
|
openView(shortcutView);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue