move pan input into an InputMapper

This commit is contained in:
2026-08-05 19:50:23 +02:00
parent f6df95abb2
commit caa810f66d
7 changed files with 168 additions and 28 deletions

70
src/ui/InputMapper.cpp Normal file
View File

@@ -0,0 +1,70 @@
#include "InputMapper.h"
#include <memory>
#include <QKeyEvent>
#include "EventManager.h"
#include "PanDirectionChangedEvent.h"
bool InputMapper::handleKeyPress(QKeyEvent* event)
{
// Auto-repeat says nothing new about which keys are down, and a held action is
// already held.
if (event->isAutoRepeat()) { return false; }
switch (event->key())
{
case Qt::Key_A:
m_panLeftHeld = true;
updatePanDirection();
return true;
case Qt::Key_D:
m_panRightHeld = true;
updatePanDirection();
return true;
default:
return false;
}
}
bool InputMapper::handleKeyRelease(QKeyEvent* event)
{
if (event->isAutoRepeat()) { return false; }
switch (event->key())
{
case Qt::Key_A:
m_panLeftHeld = false;
updatePanDirection();
return true;
case Qt::Key_D:
m_panRightHeld = false;
updatePanDirection();
return true;
default:
return false;
}
}
void InputMapper::releaseAll()
{
m_panLeftHeld = false;
m_panRightHeld = false;
updatePanDirection();
}
void InputMapper::updatePanDirection()
{
// Holding both keys cancels out rather than favouring one.
PanDirection direction = PanDirection::None;
if (m_panLeftHeld != m_panRightHeld)
{
direction = m_panLeftHeld ? PanDirection::Left : PanDirection::Right;
}
if (direction == m_panDirection) { return; }
m_panDirection = direction;
EventManager::getInstance()->sendEventImmediately(
std::make_shared<PanDirectionChangedEvent>(direction));
}