PC controls: keyboard pan/zoom, Tab cycle, Escape stack, right-click deselect

Adds full PC keyboard+mouse support on top of existing touch controls. Touch
paths untouched. All input goes through named actions in project.godot.

Bindings:
- WASD / arrows: camera pan (speed scales with zoom)
- = / -: keyboard zoom in/out
- C / Home: center on selected pawn
- Tab / Shift+Tab: cycle through pawns (pans camera to selection)
- B / L / P / ,: toggle BuildDrawer / AlertsLog / WorkPriorityMatrix / Settings
- Escape: cancel active designation tool > close topmost panel > deselect pawn
- Right-click: cancel active tool or deselect pawn (RTS convention)
- F: speed_cycle (action restored; handler still TODO)
- pawn_prev action removed; Shift+Tab read via event.shift_pressed inline

Escape priority enforced by Designation._input running before _unhandled_input
plus each panel consuming its own cancel action when visible.

Also fixes a pre-existing pre-Phase-17 bug: WorkPriorityMatrix, AlertsLog,
StorytellerModal, LoadMenu, and SettingsMenu had MOUSE_FILTER_STOP Controls
(Backdrop / Dim) that remained input-active when the panel was "closed" —
their open/close paths only toggled _root.visible / _panel.visible, never
CanvasLayer.visible. World mouse events (right-click deselect, left-click
pawn-select) were silently eaten. Now each _set_visible / open / close
toggles self.visible (the CanvasLayer) so input dispatch shuts off properly.

Verified end-to-end via MCP runtime: WASD pan, zoom keys, Tab+Shift+Tab
cycle, B-open + Escape-close, right-click deselect, left-click pawn-select
all working in sequence with no input bleed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
megaproxy 2026-05-12 12:06:38 +01:00
parent b9093dd24b
commit 0b2e0fcd03
11 changed files with 300 additions and 1 deletions

View file

@ -14,6 +14,7 @@ const CLICK_MAX_DURATION_MS: int = 300
var _pathfinder: Pathfinder = null
var _selected_pawn: Pawn = null
var _camera = null # Camera2D (CameraRig) — set via bind_camera(); duck-typed to avoid circular preload
# When Designation paint mode is active this flag is raised by Designation so
# Selection does not also try to select/move on the same click.
@ -29,13 +30,57 @@ func bind(pathfinder: Pathfinder) -> void:
_pathfinder = pathfinder
## Inject the CameraRig so Tab-cycle and center-on-selection can pan to the
## selected pawn's tile. Call from world.gd after selection.bind().
func bind_camera(rig) -> void:
_camera = rig
func selected() -> Pawn:
return _selected_pawn
func _unhandled_input(event: InputEvent) -> void:
# ── Keyboard: Tab → cycle pawns (Shift+Tab reverses) ───────────────────────
if event.is_action_pressed("pawn_next"):
# Read shift from the event itself (set by the OS on the key event); more
# reliable than Input.is_key_pressed(KEY_SHIFT) which doesn't reflect modifier
# state from synthetic input events (MCP test injections).
var reverse: bool = event is InputEventKey and event.shift_pressed
_cycle_pawn(-1 if reverse else 1)
get_viewport().set_input_as_handled()
return
# ── Keyboard: C / Home → center camera on selected pawn ─────────────────────
if event.is_action_pressed("center_on_selection"):
if _selected_pawn != null and _camera != null:
_camera.pan_to_tile(_selected_pawn.tile)
Audit.log("selection", "center_on_selection → %s" % _selected_pawn.tile)
get_viewport().set_input_as_handled()
return
# ── Keyboard: Escape → deselect (lowest-priority; consumed last) ─────────────
# Designation._input handles Escape first; panels handle it in _unhandled_input
# before reaching here. If we still see it and have a selection, consume it.
if event.is_action_pressed("cancel") and _selected_pawn != null:
_deselect()
get_viewport().set_input_as_handled()
Audit.log("selection", "escape: deselected")
return
# ── Mouse: only handle button events below ───────────────────────────────────
if not (event is InputEventMouseButton):
return
# ── Right-click: cancel designation (if active) or deselect pawn ─────────────
if event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
# Designation cancellation is handled by Designation._input; if we see
# this right-click, no designation was active. Deselect any selected pawn.
if _selected_pawn != null:
_deselect()
get_viewport().set_input_as_handled()
return
if event.button_index != MOUSE_BUTTON_LEFT:
return
# Designation paint mode owns input while active; Selection steps aside.
@ -102,3 +147,36 @@ func _select(pawn: Pawn) -> void:
pawn.set_selected(true)
EventBus.pawn_selected.emit(pawn)
Audit.log("selection", "selected %s at %s" % [pawn.pawn_name, pawn.tile])
## Clear the current selection without selecting another pawn.
func _deselect() -> void:
if _selected_pawn == null:
return
_selected_pawn.set_selected(false)
EventBus.pawn_deselected.emit()
Audit.log("selection", "deselected %s" % _selected_pawn.pawn_name)
_selected_pawn = null
## Cycle the selection forward (dir=1) or backward (dir=-1) through World.pawns.
## Wraps around. If no pawn currently selected, picks World.pawns[0].
## Pans the camera to the newly selected pawn's tile.
func _cycle_pawn(dir: int) -> void:
var pawns: Array = World.pawns
if pawns.is_empty():
return
var next_pawn: Pawn
if _selected_pawn == null:
next_pawn = pawns[0]
else:
var idx: int = pawns.find(_selected_pawn)
if idx == -1:
next_pawn = pawns[0]
else:
idx = posmod(idx + dir, pawns.size())
next_pawn = pawns[idx]
_select(next_pawn)
if _camera != null:
_camera.pan_to_tile(next_pawn.tile)
Audit.log("selection", "pawn_cycle dir=%d%s" % [dir, next_pawn.pawn_name])