mirror of https://github.com/mgba-emu/mgba.git
Qt: Bind controllers to specific windows
This commit is contained in:
parent
85c4162ad1
commit
75fb2548bb
|
@ -25,7 +25,7 @@ int InputController::s_sdlInited = 0;
|
|||
GBASDLEvents InputController::s_sdlEvents;
|
||||
#endif
|
||||
|
||||
InputController::InputController(int playerId, QObject* parent)
|
||||
InputController::InputController(int playerId, QWidget* topLevel, QObject* parent)
|
||||
: QObject(parent)
|
||||
, m_playerId(playerId)
|
||||
, m_config(nullptr)
|
||||
|
@ -34,6 +34,7 @@ InputController::InputController(int playerId, QObject* parent)
|
|||
, m_playerAttached(false)
|
||||
#endif
|
||||
, m_allowOpposing(false)
|
||||
, m_topLevel(topLevel)
|
||||
{
|
||||
GBAInputMapInit(&m_inputMap);
|
||||
|
||||
|
@ -432,7 +433,7 @@ void InputController::testGamepad(int type) {
|
|||
if (newlyAboveThreshold) {
|
||||
GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, newlyAboveThreshold, type, this);
|
||||
postPendingEvent(event->gbaKey());
|
||||
QApplication::sendEvent(QApplication::focusWidget(), event);
|
||||
sendGamepadEvent(event);
|
||||
if (!event->isAccepted()) {
|
||||
clearPendingEvent(event->gbaKey());
|
||||
}
|
||||
|
@ -441,7 +442,7 @@ void InputController::testGamepad(int type) {
|
|||
for (auto axis : oldAxes) {
|
||||
GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, false, type, this);
|
||||
clearPendingEvent(event->gbaKey());
|
||||
QApplication::sendEvent(QApplication::focusWidget(), event);
|
||||
sendGamepadEvent(event);
|
||||
}
|
||||
|
||||
if (!QApplication::focusWidget()) {
|
||||
|
@ -454,7 +455,7 @@ void InputController::testGamepad(int type) {
|
|||
for (int button : activeButtons) {
|
||||
GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Down(), button, type, this);
|
||||
postPendingEvent(event->gbaKey());
|
||||
QApplication::sendEvent(QApplication::focusWidget(), event);
|
||||
sendGamepadEvent(event);
|
||||
if (!event->isAccepted()) {
|
||||
clearPendingEvent(event->gbaKey());
|
||||
}
|
||||
|
@ -462,10 +463,23 @@ void InputController::testGamepad(int type) {
|
|||
for (int button : oldButtons) {
|
||||
GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Up(), button, type, this);
|
||||
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) {
|
||||
m_pendingEvents.insert(key);
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ Q_OBJECT
|
|||
public:
|
||||
static const uint32_t KEYBOARD = 0x51545F4B;
|
||||
|
||||
InputController(int playerId = 0, QObject* parent = nullptr);
|
||||
InputController(int playerId = 0, QWidget* topLevel = nullptr, QObject* parent = nullptr);
|
||||
~InputController();
|
||||
|
||||
void setConfiguration(ConfigController* config);
|
||||
|
@ -94,11 +94,13 @@ private:
|
|||
void postPendingEvent(GBAKey);
|
||||
void clearPendingEvent(GBAKey);
|
||||
bool hasPendingEvent(GBAKey) const;
|
||||
void sendGamepadEvent(QEvent*);
|
||||
|
||||
GBAInputMap m_inputMap;
|
||||
ConfigController* m_config;
|
||||
int m_playerId;
|
||||
bool m_allowOpposing;
|
||||
QWidget* m_topLevel;
|
||||
|
||||
#ifdef BUILD_SDL
|
||||
static int s_sdlInited;
|
||||
|
|
|
@ -53,7 +53,7 @@ Window::Window(ConfigController* config, int playerId, QWidget* parent)
|
|||
, m_screenWidget(new WindowBackground())
|
||||
, m_logo(":/res/mgba-1024.png")
|
||||
, m_config(config)
|
||||
, m_inputController(playerId)
|
||||
, m_inputController(playerId, this)
|
||||
#ifdef USE_FFMPEG
|
||||
, m_videoView(nullptr)
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue