explodorama / files / src / ui / keyCursor.ts
keyCursor.ts
| 1 | import type { ActionId, Vec } from '../sim/types' |
| 2 | import { eq } from '../sim/state' |
| 3 | import { BAR_BUTTONS, ghostAction, type BarButton, type TileAction } from './padTiles' |
| 4 | import type { Step } from './spatial' |
| 5 | |
| 6 | /** |
| 7 | * The keyboard's tile-first cursor (decisions.md 2026-11-02, Q65): the controller's cursor |
| 8 | * (padTiles.ts, padTarget.ts) woken by Shift + an arrow (or W A S D), so a player with only a |
| 9 | * keyboard can reach every tile, not just the four straight lines the plain arrows look along. |
| 10 | * Pure: main.ts hands in what a key event says, and the state. |
| 11 | */ |
| 12 | |
| 13 | /** Who has the board: the controller, the keyboard's cursor (and aim), or the mouse / touch / plain keys (null). */ |
| 14 | export type InputMode = 'pad' | 'keys' | null |
| 15 | |
| 16 | /** The parts of a key event the rules need (a `KeyboardEvent` fits). */ |
| 17 | export interface KeyInfo { |
| 18 | code: string |
| 19 | key: string |
| 20 | shiftKey: boolean |
| 21 | altKey: boolean |
| 22 | ctrlKey: boolean |
| 23 | metaKey: boolean |
| 24 | } |
| 25 | |
| 26 | /** What a key event is to the cursor. */ |
| 27 | export type KeyRole = |
| 28 | /** Shift, Alt, Control, Meta on their own: pressed on the way to a combination. */ |
| 29 | | 'modifier' |
| 30 | /** Shift + an arrow or W A S D: hops the cursor (and wakes it). */ |
| 31 | | 'hop' |
| 32 | /** Alt + an arrow or W A S D: pans the view. */ |
| 33 | | 'pan' |
| 34 | /** Home: the view follows the player again. */ |
| 35 | | 'recenter' |
| 36 | /** "What is that?" (`?`): aims, so it wakes the cursor's mode too. */ |
| 37 | | 'inspect' |
| 38 | /** A plain arrow, W A S D: today's "nearest valid tile that way". */ |
| 39 | | 'direction' |
| 40 | /** Q / E / Z / C: a diagonal step. */ |
| 41 | | 'diagonal' |
| 42 | | 'other' |
| 43 | |
| 44 | const DIRECTION_CODES: Record<string, { dx: Step; dy: Step }> = { |
| 45 | ArrowUp: { dx: 0, dy: -1 }, KeyW: { dx: 0, dy: -1 }, |
| 46 | ArrowDown: { dx: 0, dy: 1 }, KeyS: { dx: 0, dy: 1 }, |
| 47 | ArrowLeft: { dx: -1, dy: 0 }, KeyA: { dx: -1, dy: 0 }, |
| 48 | ArrowRight: { dx: 1, dy: 0 }, KeyD: { dx: 1, dy: 0 }, |
| 49 | } |
| 50 | const DIAGONAL_CODES = ['KeyQ', 'KeyE', 'KeyZ', 'KeyC'] |
| 51 | const MODIFIER_CODES = ['ShiftLeft', 'ShiftRight', 'AltLeft', 'AltRight', 'ControlLeft', 'ControlRight', 'MetaLeft', 'MetaRight', 'OS'] |
| 52 | |
| 53 | /** The board direction (y grows down, as the screen's) an arrow or W A S D key points, or null. */ |
| 54 | export function keyDirection(code: string): { dx: Step; dy: Step } | null { |
| 55 | return DIRECTION_CODES[code] ?? null |
| 56 | } |
| 57 | |
| 58 | export function keyRole(k: KeyInfo): KeyRole { |
| 59 | if (MODIFIER_CODES.includes(k.code)) return 'modifier' |
| 60 | // Control and Meta combinations belong to the browser (Ctrl+R, Cmd+L, Cmd+/ ...): never the cursor's. |
| 61 | if (k.ctrlKey || k.metaKey) return 'other' |
| 62 | // `?` is what the key types, not where it sits: other layouts have it elsewhere, and their Slash key types something else. |
| 63 | if (k.key === '?') return 'inspect' |
| 64 | if (k.code === 'Home') return 'recenter' |
| 65 | const dir = keyDirection(k.code) |
| 66 | if (dir && k.altKey) return 'pan' |
| 67 | if (dir && k.shiftKey) return 'hop' |
| 68 | if (dir) return 'direction' |
| 69 | if (DIAGONAL_CODES.includes(k.code)) return 'diagonal' |
| 70 | return 'other' |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Who has the board after a key: Shift + arrow and `?` give the keyboard's cursor; a pan, a recentre |
| 75 | * and the modifier keys change nothing for the keyboard (they only hand a controller's board back, |
| 76 | * as every key does); a plain arrow or diagonal step ends the cursor (the board is |
| 77 | * the old one again), except while "What is that?" is on, when the arrows aim; any other key |
| 78 | * (M, B, Space, Esc, Enter ...) is used by the cursor while it shows. |
| 79 | * `locked` (a dialog is open, or the demo plays): the keys belong to it, so none wakes the keyboard's |
| 80 | * cursor (Shift + arrow in the seed field, `?` typed in a dialog); a controller's board is still handed back. |
| 81 | */ |
| 82 | export function modeAfterKey(mode: InputMode, k: KeyInfo, inspecting: boolean, locked = false): InputMode { |
| 83 | const role = keyRole(k) |
| 84 | if (locked) return mode === 'keys' ? 'keys' : null |
| 85 | if (role === 'hop' || role === 'inspect') return 'keys' |
| 86 | // While "What is that?" is on, the plain arrows aim it, whoever had the board. |
| 87 | if ((role === 'direction' || role === 'diagonal') && inspecting) return 'keys' |
| 88 | if (mode !== 'keys') return null |
| 89 | return role === 'direction' || role === 'diagonal' ? null : 'keys' |
| 90 | } |
| 91 | |
| 92 | /** Keys that are no play: a modifier pressed alone, or anything with Cmd held (the OS's and browser's shortcuts, such as a screenshot). They never change who has the board. */ |
| 93 | export function ignoredForMode(k: KeyInfo): boolean { |
| 94 | return k.metaKey || ['Shift', 'Control', 'Alt', 'Meta', 'CapsLock', 'OS'].includes(k.key) |
| 95 | } |
| 96 | |
| 97 | /** Alt + arrow: how far the view moves for one key press (or repeat), in tiles, y up (camera.ts `panView`). */ |
| 98 | export const PAN_TILES_PER_KEY = 0.5 |
| 99 | |
| 100 | export function panStep(code: string): { x: number; y: number } | null { |
| 101 | const dir = keyDirection(code) |
| 102 | return dir ? { x: dir.dx * PAN_TILES_PER_KEY, y: -dir.dy * PAN_TILES_PER_KEY } : null |
| 103 | } |
| 104 | |
| 105 | /** An element a key press already means something to: Enter or Space on it presses it, so the game stays out. */ |
| 106 | export function targetTakesKey(target: { tagName?: string; isContentEditable?: boolean } | null): boolean { |
| 107 | if (!target) return false |
| 108 | if (target.isContentEditable) return true |
| 109 | return ['BUTTON', 'A', 'INPUT', 'TEXTAREA', 'SELECT', 'SUMMARY'].includes((target.tagName ?? '').toUpperCase()) |
| 110 | } |
| 111 | |
| 112 | /** The key cap shown for each bar button in the cursor's label. */ |
| 113 | export const KEY_CAPS: Record<BarButton, string> = { move: 'M', bomb: 'B', charged: 'V', wait: 'Space' } |
| 114 | |
| 115 | /** |
| 116 | * Which bar button an action key means on the cursor's tile: its own button (M, B, V, Space) when |
| 117 | * that is possible there, else the button whose action there is the key's (R is Move's run, K its |
| 118 | * kick, T the throw ...). Null when the tile offers nothing for the key: the key then picks the |
| 119 | * action as it always did (then Enter, or a plain arrow, aims it). |
| 120 | */ |
| 121 | export function keyButton(actions: readonly TileAction[], id: ActionId): BarButton | null { |
| 122 | const own = (BAR_BUTTONS as readonly string[]).includes(id) ? actions.find((a) => a.possible && a.button === id) : undefined |
| 123 | if (own) return own.button |
| 124 | const byAction = actions.find((a) => a.possible && a.id === id) |
| 125 | return byAction ? byAction.button : null |
| 126 | } |
| 127 | |
| 128 | /** The button Enter presses on the cursor's tile: the first action the ghost previews (never Wait; nothing on the player's own tile, as a tap there does nothing). */ |
| 129 | export function defaultButton(actions: readonly TileAction[], tile: Vec, player: Vec): BarButton | null { |
| 130 | if (eq(tile, player)) return null |
| 131 | // A faded tile (auto-move's walk): Enter walks, as Move does. |
| 132 | const walk = actions.find((a) => a.far) |
| 133 | if (walk) return walk.button |
| 134 | const first = ghostAction(actions, tile, player) |
| 135 | return first ? (actions.find((a) => a.possible && a.id === first.id)?.button ?? null) : null |
| 136 | } |
| 137 | |
| 138 | /** What Esc does with the cursor showing, nearest first: put away "What is that?", a picked action or the flag; else bring the cursor home; else put the cursor away. */ |
| 139 | export function escStep(c: { inspecting: boolean; picked: boolean; onPlayer: boolean }): 'cancel' | 'home' | 'hide' { |
| 140 | if (c.inspecting || c.picked) return 'cancel' |
| 141 | return c.onPlayer ? 'hide' : 'home' |
| 142 | } |