resolve keyboard shortcuts through one action table instead of a switch
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include "BlueprintSelectionRequestedEvent.h"
|
||||
#include "BuildHotkeyPressedEvent.h"
|
||||
#include "BuildingType.h"
|
||||
#include "ControlAction.h"
|
||||
#include "DebugDrawToggleRequestedEvent.h"
|
||||
#include "EscapeMenuRequestedEvent.h"
|
||||
#include "EventManager.h"
|
||||
@@ -77,7 +78,7 @@ QString InputMapper::getBuildHotkeyLabel(BuildingType type)
|
||||
return QString();
|
||||
}
|
||||
|
||||
bool InputMapper::handleKeyPress(QKeyEvent* event)
|
||||
bool InputMapper::handleKeyPress(QKeyEvent* event, const ControlContext& context)
|
||||
{
|
||||
// Auto-repeat says nothing new about which keys are down, and a held action is
|
||||
// already held.
|
||||
@@ -86,6 +87,9 @@ bool InputMapper::handleKeyPress(QKeyEvent* event)
|
||||
// Number-key build-mode hotkeys (REQ-UI-HOTKEYS). nativeVirtualKey gives the
|
||||
// physical digit independent of keyboard layout and Shift (with Shift held, key()
|
||||
// for the number row can arrive as Key_Exclam etc.). VK_1..VK_9 = 0x31..0x39.
|
||||
// Not part of the ControlAction table: these are advertised on the build buttons
|
||||
// rather than in the controls panel, and getBuildHotkeyLabel already reads the
|
||||
// same binding table this does (REQ-UI-CONTROLS-ACCURACY).
|
||||
const quint32 virtualKey = event->nativeVirtualKey();
|
||||
if (virtualKey >= 0x31 && virtualKey <= 0x39)
|
||||
{
|
||||
@@ -100,107 +104,102 @@ bool InputMapper::handleKeyPress(QKeyEvent* event)
|
||||
}
|
||||
}
|
||||
|
||||
// Blueprint chords (REQ-UI-HOTKEYS). Checked ahead of the plain-key switch below,
|
||||
// which binds bare A/D/W/S/R/Q/C/V and must not fire on a Ctrl chord -- bare C and V
|
||||
// are the temporary-blueprint counterparts of these two (REQ-UI-BLUEPRINT-TEMP). Both
|
||||
// requests are decided by MainWindow, the only widget that can pause the game and dim
|
||||
// the window for a modal.
|
||||
if ((event->modifiers() & Qt::ControlModifier) != 0)
|
||||
{
|
||||
switch (event->key())
|
||||
{
|
||||
case Qt::Key_C:
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<BlueprintSaveRequestedEvent>());
|
||||
return true;
|
||||
case Qt::Key_V:
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<BlueprintSelectionRequestedEvent>());
|
||||
return true;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Development controls, deliberately outside the table so they are never offered
|
||||
// to the player (REQ-UI-CONTROLS-ACCURACY).
|
||||
switch (event->key())
|
||||
{
|
||||
case Qt::Key_A:
|
||||
m_panLeftHeld = true;
|
||||
updatePanDirection();
|
||||
return true;
|
||||
case Qt::Key_D:
|
||||
m_panRightHeld = true;
|
||||
updatePanDirection();
|
||||
return true;
|
||||
case Qt::Key_Space:
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<PauseToggleRequestedEvent>());
|
||||
return true;
|
||||
case Qt::Key_W:
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<SpeedStepRequestedEvent>(+1));
|
||||
return true;
|
||||
case Qt::Key_S:
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<SpeedStepRequestedEvent>(-1));
|
||||
return true;
|
||||
case Qt::Key_R:
|
||||
// Shift reverses the rotation direction (REQ-BLD-ROTATE).
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<GhostRotationRequestedEvent>(
|
||||
(event->modifiers() & Qt::ShiftModifier) != 0));
|
||||
return true;
|
||||
case Qt::Key_Q:
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<ModeCancelRequestedEvent>());
|
||||
return true;
|
||||
case Qt::Key_C:
|
||||
// Capture a temporary blueprint from the current selection (REQ-UI-BLUEPRINT-TEMP).
|
||||
// The BlueprintLibrary owns the selection and blueprint-capture logic; it decides
|
||||
// whether anything placeable is selected and drives placement mode from there.
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<TemporaryBlueprintCaptureRequestedEvent>());
|
||||
return true;
|
||||
case Qt::Key_V:
|
||||
// Re-enter placement mode for the temporary blueprint captured with C, if there is
|
||||
// one (REQ-UI-BLUEPRINT-TEMP). The library holds it; nothing is captured here.
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<TemporaryBlueprintPlaceRequestedEvent>());
|
||||
return true;
|
||||
case Qt::Key_F3:
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<DebugDrawToggleRequestedEvent>());
|
||||
return true;
|
||||
case Qt::Key_Escape:
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<EscapeMenuRequestedEvent>());
|
||||
return true;
|
||||
case Qt::Key_F4:
|
||||
EventManager::getInstance()->addEvent(
|
||||
std::make_shared<TracePrintRequestedEvent>());
|
||||
return true;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Everything else: the key names an action, the action names an event. Which key
|
||||
// is bound to what, and whether it does anything in this situation, are both the
|
||||
// table's business -- this switch only knows what each action means
|
||||
// (REQ-UI-HOTKEYS, REQ-UI-CONTROLS-ACCURACY).
|
||||
switch (resolveKeyAction(event->key(), event->modifiers(), context))
|
||||
{
|
||||
case ControlAction::Move:
|
||||
// A parameter of the action rather than an action of its own: the table binds
|
||||
// both keys to Move and the direction is read off the key here, as the rotation
|
||||
// direction and the build hotkey's digit are.
|
||||
if (event->key() == Qt::Key_A) { m_panLeftHeld = true; }
|
||||
else { m_panRightHeld = true; }
|
||||
updatePanDirection();
|
||||
return true;
|
||||
case ControlAction::GameSpeed:
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<SpeedStepRequestedEvent>(event->key() == Qt::Key_W ? +1 : -1));
|
||||
return true;
|
||||
case ControlAction::TogglePause:
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<PauseToggleRequestedEvent>());
|
||||
return true;
|
||||
case ControlAction::Rotate:
|
||||
// Shift reverses the rotation direction (REQ-BLD-ROTATE).
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<GhostRotationRequestedEvent>(
|
||||
(event->modifiers() & Qt::ShiftModifier) != 0));
|
||||
return true;
|
||||
case ControlAction::EnterDeconstruct:
|
||||
case ControlAction::ExitMode:
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<ModeCancelRequestedEvent>());
|
||||
return true;
|
||||
case ControlAction::CopyTemporary:
|
||||
// The BlueprintLibrary owns the selection and blueprint-capture logic; it drives
|
||||
// placement mode from there (REQ-UI-BLUEPRINT-TEMP).
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<TemporaryBlueprintCaptureRequestedEvent>());
|
||||
return true;
|
||||
case ControlAction::PasteTemporary:
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<TemporaryBlueprintPlaceRequestedEvent>());
|
||||
return true;
|
||||
case ControlAction::CreateBlueprint:
|
||||
// Decided by MainWindow, the only widget that can pause the game and dim the
|
||||
// window for a modal.
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<BlueprintSaveRequestedEvent>());
|
||||
return true;
|
||||
case ControlAction::OpenBlueprints:
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<BlueprintSelectionRequestedEvent>());
|
||||
return true;
|
||||
case ControlAction::OpenMenu:
|
||||
EventManager::getInstance()->sendEventImmediately(
|
||||
std::make_shared<EscapeMenuRequestedEvent>());
|
||||
return true;
|
||||
default:
|
||||
// Either nothing is bound to the key here, or what is bound is a mouse gesture
|
||||
// the view handles. Unconsumed, so ordinary Qt shortcuts keep working.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool InputMapper::handleKeyRelease(QKeyEvent* event)
|
||||
bool InputMapper::handleKeyRelease(QKeyEvent* event, const ControlContext& context)
|
||||
{
|
||||
if (event->isAutoRepeat()) { return false; }
|
||||
|
||||
switch (event->key())
|
||||
// Only held actions have a release worth acting on. Resolved through the table
|
||||
// rather than matched against A and D directly, so the keys stay rebindable in one
|
||||
// place rather than two.
|
||||
if (resolveKeyAction(event->key(), event->modifiers(), context) != ControlAction::Move)
|
||||
{
|
||||
case Qt::Key_A:
|
||||
m_panLeftHeld = false;
|
||||
updatePanDirection();
|
||||
return true;
|
||||
case Qt::Key_D:
|
||||
m_panRightHeld = false;
|
||||
updatePanDirection();
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->key() == Qt::Key_A) { m_panLeftHeld = false; }
|
||||
else { m_panRightHeld = false; }
|
||||
updatePanDirection();
|
||||
return true;
|
||||
}
|
||||
|
||||
void InputMapper::releaseAll()
|
||||
|
||||
Reference in New Issue
Block a user