Qt: Bind controllers to specific windows

This commit is contained in:
Jeffrey Pfau 2015-07-18 00:20:54 -07:00
parent 85c4162ad1
commit 75fb2548bb
3 changed files with 23 additions and 7 deletions

View File

@ -25,7 +25,7 @@ int InputController::s_sdlInited = 0;
GBASDLEvents InputController::s_sdlEvents; GBASDLEvents InputController::s_sdlEvents;
#endif #endif
InputController::InputController(int playerId, QObject* parent) InputController::InputController(int playerId, QWidget* topLevel, QObject* parent)
: QObject(parent) : QObject(parent)
, m_playerId(playerId) , m_playerId(playerId)
, m_config(nullptr) , m_config(nullptr)
@ -34,6 +34,7 @@ InputController::InputController(int playerId, QObject* parent)
, m_playerAttached(false) , m_playerAttached(false)
#endif #endif
, m_allowOpposing(false) , m_allowOpposing(false)
, m_topLevel(topLevel)
{ {
GBAInputMapInit(&m_inputMap); GBAInputMapInit(&m_inputMap);
@ -432,7 +433,7 @@ void InputController::testGamepad(int type) {
if (newlyAboveThreshold) { if (newlyAboveThreshold) {
GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, newlyAboveThreshold, type, this); GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, newlyAboveThreshold, type, this);
postPendingEvent(event->gbaKey()); postPendingEvent(event->gbaKey());
QApplication::sendEvent(QApplication::focusWidget(), event); sendGamepadEvent(event);
if (!event->isAccepted()) { if (!event->isAccepted()) {
clearPendingEvent(event->gbaKey()); clearPendingEvent(event->gbaKey());
} }
@ -441,7 +442,7 @@ void InputController::testGamepad(int type) {
for (auto axis : oldAxes) { for (auto axis : oldAxes) {
GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, false, type, this); GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, false, type, this);
clearPendingEvent(event->gbaKey()); clearPendingEvent(event->gbaKey());
QApplication::sendEvent(QApplication::focusWidget(), event); sendGamepadEvent(event);
} }
if (!QApplication::focusWidget()) { if (!QApplication::focusWidget()) {
@ -454,7 +455,7 @@ void InputController::testGamepad(int type) {
for (int button : activeButtons) { for (int button : activeButtons) {
GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Down(), button, type, this); GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Down(), button, type, this);
postPendingEvent(event->gbaKey()); postPendingEvent(event->gbaKey());
QApplication::sendEvent(QApplication::focusWidget(), event); sendGamepadEvent(event);
if (!event->isAccepted()) { if (!event->isAccepted()) {
clearPendingEvent(event->gbaKey()); clearPendingEvent(event->gbaKey());
} }
@ -462,10 +463,23 @@ void InputController::testGamepad(int type) {
for (int button : oldButtons) { for (int button : oldButtons) {
GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Up(), button, type, this); GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Up(), button, type, this);
clearPendingEvent(event->gbaKey()); clearPendingEvent(event->gbaKey());
QApplication::sendEvent(QApplication::focusWidget(), event); sendGamepadEvent(event);
} }
} }
void InputController::sendGamepadEvent(QEvent* event) {
QWidget* focusWidget = nullptr;
if (m_topLevel) {
focusWidget = m_topLevel->focusWidget();
if (!focusWidget) {
focusWidget = m_topLevel;
}
} else {
focusWidget = QApplication::focusWidget();
}
QApplication::sendEvent(focusWidget, event);
}
void InputController::postPendingEvent(GBAKey key) { void InputController::postPendingEvent(GBAKey key) {
m_pendingEvents.insert(key); m_pendingEvents.insert(key);
} }

View File

@ -32,7 +32,7 @@ Q_OBJECT
public: public:
static const uint32_t KEYBOARD = 0x51545F4B; static const uint32_t KEYBOARD = 0x51545F4B;
InputController(int playerId = 0, QObject* parent = nullptr); InputController(int playerId = 0, QWidget* topLevel = nullptr, QObject* parent = nullptr);
~InputController(); ~InputController();
void setConfiguration(ConfigController* config); void setConfiguration(ConfigController* config);
@ -94,11 +94,13 @@ private:
void postPendingEvent(GBAKey); void postPendingEvent(GBAKey);
void clearPendingEvent(GBAKey); void clearPendingEvent(GBAKey);
bool hasPendingEvent(GBAKey) const; bool hasPendingEvent(GBAKey) const;
void sendGamepadEvent(QEvent*);
GBAInputMap m_inputMap; GBAInputMap m_inputMap;
ConfigController* m_config; ConfigController* m_config;
int m_playerId; int m_playerId;
bool m_allowOpposing; bool m_allowOpposing;
QWidget* m_topLevel;
#ifdef BUILD_SDL #ifdef BUILD_SDL
static int s_sdlInited; static int s_sdlInited;

View File

@ -53,7 +53,7 @@ Window::Window(ConfigController* config, int playerId, QWidget* parent)
, m_screenWidget(new WindowBackground()) , m_screenWidget(new WindowBackground())
, m_logo(":/res/mgba-1024.png") , m_logo(":/res/mgba-1024.png")
, m_config(config) , m_config(config)
, m_inputController(playerId) , m_inputController(playerId, this)
#ifdef USE_FFMPEG #ifdef USE_FFMPEG
, m_videoView(nullptr) , m_videoView(nullptr)
#endif #endif