mirror of
https://github.com/moonlight-stream/moonlight-qt
synced 2024-12-14 13:22:27 +00:00
Allow gamepad navigation with the left analog stick
This commit is contained in:
parent
162a16ed3a
commit
6d382078d3
2 changed files with 66 additions and 1 deletions
|
@ -6,9 +6,12 @@
|
||||||
|
|
||||||
#include "settings/mappingmanager.h"
|
#include "settings/mappingmanager.h"
|
||||||
|
|
||||||
|
#define AXIS_NAVIGATION_REPEAT_DELAY 150
|
||||||
|
|
||||||
SdlGamepadKeyNavigation::SdlGamepadKeyNavigation()
|
SdlGamepadKeyNavigation::SdlGamepadKeyNavigation()
|
||||||
: m_Enabled(false),
|
: m_Enabled(false),
|
||||||
m_SettingsMode(false)
|
m_SettingsMode(false),
|
||||||
|
m_LastAxisNavigationEventTime(0)
|
||||||
{
|
{
|
||||||
m_PollingTimer = new QTimer(this);
|
m_PollingTimer = new QTimer(this);
|
||||||
connect(m_PollingTimer, SIGNAL(timeout()), this, SLOT(onPollingTimerFired()));
|
connect(m_PollingTimer, SIGNAL(timeout()), this, SLOT(onPollingTimerFired()));
|
||||||
|
@ -45,6 +48,7 @@ void SdlGamepadKeyNavigation::enable()
|
||||||
// on first init of the GC subsystem. We can't depend on them due to
|
// on first init of the GC subsystem. We can't depend on them due to
|
||||||
// overlapping lifetimes of SdlGamepadKeyNavigation instances, so we
|
// overlapping lifetimes of SdlGamepadKeyNavigation instances, so we
|
||||||
// will attach ourselves.
|
// will attach ourselves.
|
||||||
|
SDL_PumpEvents();
|
||||||
SDL_FlushEvent(SDL_CONTROLLERDEVICEADDED);
|
SDL_FlushEvent(SDL_CONTROLLERDEVICEADDED);
|
||||||
|
|
||||||
// Open all currently attached game controllers
|
// Open all currently attached game controllers
|
||||||
|
@ -164,6 +168,66 @@ void SdlGamepadKeyNavigation::onPollingTimerFired()
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle analog sticks by polling
|
||||||
|
for (auto gc : m_Gamepads) {
|
||||||
|
short leftX = SDL_GameControllerGetAxis(gc, SDL_CONTROLLER_AXIS_LEFTX);
|
||||||
|
short leftY = SDL_GameControllerGetAxis(gc, SDL_CONTROLLER_AXIS_LEFTY);
|
||||||
|
if (SDL_GetTicks() - m_LastAxisNavigationEventTime < AXIS_NAVIGATION_REPEAT_DELAY) {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
else if (leftY < -30000) {
|
||||||
|
if (m_SettingsMode) {
|
||||||
|
// Back-tab
|
||||||
|
sendKey(QEvent::Type::KeyPress, Qt::Key_Tab, Qt::ShiftModifier);
|
||||||
|
sendKey(QEvent::Type::KeyRelease, Qt::Key_Tab, Qt::ShiftModifier);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sendKey(QEvent::Type::KeyPress, Qt::Key_Up);
|
||||||
|
sendKey(QEvent::Type::KeyRelease, Qt::Key_Up);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_LastAxisNavigationEventTime = SDL_GetTicks();
|
||||||
|
}
|
||||||
|
else if (leftY > 30000) {
|
||||||
|
if (m_SettingsMode) {
|
||||||
|
sendKey(QEvent::Type::KeyPress, Qt::Key_Tab);
|
||||||
|
sendKey(QEvent::Type::KeyRelease, Qt::Key_Tab);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sendKey(QEvent::Type::KeyPress, Qt::Key_Down);
|
||||||
|
sendKey(QEvent::Type::KeyRelease, Qt::Key_Down);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_LastAxisNavigationEventTime = SDL_GetTicks();
|
||||||
|
}
|
||||||
|
else if (leftX < -30000) {
|
||||||
|
sendKey(QEvent::Type::KeyPress, Qt::Key_Left);
|
||||||
|
sendKey(QEvent::Type::KeyRelease, Qt::Key_Left);
|
||||||
|
if (m_SettingsMode) {
|
||||||
|
// Some settings controls respond to left/right (like the slider)
|
||||||
|
// and others respond to up/down (like combo boxes). They seem to
|
||||||
|
// be mutually exclusive though so let's just send both.
|
||||||
|
sendKey(QEvent::Type::KeyPress, Qt::Key_Up);
|
||||||
|
sendKey(QEvent::Type::KeyRelease, Qt::Key_Up);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_LastAxisNavigationEventTime = SDL_GetTicks();
|
||||||
|
}
|
||||||
|
else if (leftX > 30000) {
|
||||||
|
sendKey(QEvent::Type::KeyPress, Qt::Key_Right);
|
||||||
|
sendKey(QEvent::Type::KeyRelease, Qt::Key_Right);
|
||||||
|
if (m_SettingsMode) {
|
||||||
|
// Some settings controls respond to left/right (like the slider)
|
||||||
|
// and others respond to up/down (like combo boxes). They seem to
|
||||||
|
// be mutually exclusive though so let's just send both.
|
||||||
|
sendKey(QEvent::Type::KeyPress, Qt::Key_Down);
|
||||||
|
sendKey(QEvent::Type::KeyRelease, Qt::Key_Down);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_LastAxisNavigationEventTime = SDL_GetTicks();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SdlGamepadKeyNavigation::sendKey(QEvent::Type type, Qt::Key key, Qt::KeyboardModifiers modifiers)
|
void SdlGamepadKeyNavigation::sendKey(QEvent::Type type, Qt::Key key, Qt::KeyboardModifiers modifiers)
|
||||||
|
|
|
@ -31,4 +31,5 @@ private:
|
||||||
QList<SDL_GameController*> m_Gamepads;
|
QList<SDL_GameController*> m_Gamepads;
|
||||||
bool m_Enabled;
|
bool m_Enabled;
|
||||||
bool m_SettingsMode;
|
bool m_SettingsMode;
|
||||||
|
Uint32 m_LastAxisNavigationEventTime;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue