explodorama / files / src / ui / demoInput.ts
demoInput.ts
| 1 | import type { PadFamily } from './padMap' |
| 2 | |
| 3 | /** |
| 4 | * The input the demo is shown for (Q61): a button on the demo screen cycles through them, so |
| 5 | * anyone can see how the game plays by touch, by mouse, by keyboard (Q65) or by each kind of |
| 6 | * controller. The pick overrides what the page detected (a pad in use or not) for the demo only; |
| 7 | * nothing is saved. Pure: main.ts keeps the pick and does the drawing. |
| 8 | */ |
| 9 | export type DemoInput = 'touch' | 'mouse' | 'keyboard' | 'ps5' | 'xbox' | 'switch' | 'generic' |
| 10 | |
| 11 | /** The cycle, in order, with each entry's button label and, for a controller, its picture set (ui/padMap.ts). */ |
| 12 | const INPUTS: readonly { id: DemoInput; label: string; family: PadFamily | null }[] = [ |
| 13 | { id: 'touch', label: 'Input: touch', family: null }, |
| 14 | { id: 'mouse', label: 'Input: mouse', family: null }, |
| 15 | { id: 'keyboard', label: 'Input: keyboard', family: null }, |
| 16 | { id: 'ps5', label: 'Input: controller: ps5', family: 'playstation' }, |
| 17 | { id: 'xbox', label: 'Input: controller: xbox', family: 'xbox' }, |
| 18 | { id: 'switch', label: 'Input: controller: switch', family: 'nintendo' }, |
| 19 | { id: 'generic', label: 'Input: controller: generic', family: 'generic' }, |
| 20 | ] |
| 21 | export const DEMO_INPUTS: readonly DemoInput[] = INPUTS.map((i) => i.id) |
| 22 | |
| 23 | const entry = (id: DemoInput) => INPUTS.find((i) => i.id === id)! |
| 24 | |
| 25 | export const demoInputLabel = (id: DemoInput): string => entry(id).label |
| 26 | |
| 27 | /** The controller family an input shows (null for touch and mouse). */ |
| 28 | export const demoInputFamily = (id: DemoInput): PadFamily | null => entry(id).family |
| 29 | |
| 30 | export const isPadInput = (id: DemoInput): boolean => entry(id).family !== null |
| 31 | |
| 32 | /** The keyboard demo: its own order of scenes and the key overlay (ui/keyDemo.ts, keyOverlay.ts). */ |
| 33 | export const isKeysInput = (id: DemoInput): boolean => id === 'keyboard' |
| 34 | |
| 35 | /** What one click on the button picks next: the entry after `id`, wrapping round. */ |
| 36 | export const nextDemoInput = (id: DemoInput): DemoInput => DEMO_INPUTS[(DEMO_INPUTS.indexOf(id) + 1) % DEMO_INPUTS.length]! |
| 37 | |
| 38 | /** The entry for the pad family in use (several entries never share one). */ |
| 39 | export const inputForFamily = (family: PadFamily): DemoInput => INPUTS.find((i) => i.family === family)!.id |
| 40 | |
| 41 | /** |
| 42 | * What the demo is shown for now: the pick if there is one, else what was detected (a controller |
| 43 | * in use, with its family, or otherwise the touch demo, as before Q61). |
| 44 | */ |
| 45 | export function effectiveDemoInput(forced: DemoInput | null, padActive: boolean, padKind: PadFamily): DemoInput { |
| 46 | return forced ?? (padActive ? inputForFamily(padKind) : 'touch') |
| 47 | } |
| 48 | |
| 49 | /** The demo's last line for an input: a controller holds, a keyboard presses Enter or Esc, a mouse clicks, a finger taps. `{move}` is the confirm face button's picture (bottom; a Nintendo pad's right-hand A) (ui/padCaption). */ |
| 50 | export function demoHintText(id: DemoInput): string { |
| 51 | if (isPadInput(id)) return 'Hold {move} to play' |
| 52 | if (isKeysInput(id)) return 'Press Enter or Esc to play' |
| 53 | return id === 'mouse' ? 'Click anywhere to play' : 'Tap anywhere to play' |
| 54 | } |