From 2680f3d82b2569c06bda4872690a9744c5cee4fa Mon Sep 17 00:00:00 2001 From: Charlie Allom Date: Tue, 3 Nov 2026 10:31:42 +0000 Subject: [PATCH] The arrow keys and W A S D move the map while held The map moves at 700 px a second on screen at any zoom. The keys run their own frame loop only while one is down, so the game loop does no extra work. Letting go, a window opening or leaving the browser window stops it, and a slow frame moves at most 50 ms worth. Arrows still scroll the NOC dashboard when it is open, and Alt or Cmd with an arrow stays the browser's. --- CHANGELOG.md | 2 +- site/i18n/de.js | 1 + site/i18n/es.js | 1 + site/i18n/fr.js | 1 + site/i18n/it.js | 1 + site/i18n/pt.js | 1 + src/game/93-ui/82-keys.js | 16 ++++++++++++++-- src/i18n/en.js | 1 + tests/keyboard-shortcuts.test.js | 33 ++++++++++++++++++++++++++++++-- 9 files changed, 52 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe14d8c..bf07b1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ All notable changes to this project are documented here. The project follows ## [Unreleased] -- New keyboard shortcuts: [ and ] step through your POPs, - and = zoom the map out and in, n opens the NOC dashboard, and ? lists every shortcut. A letter right after n opens one of the dashboard's tabs, such as n c for Connections, and Help links to the list too. +- New keyboard shortcuts: the arrow keys and W A S D move the map, [ and ] step through your POPs, - and = zoom out and in, n opens the NOC dashboard, and ? lists every shortcut. A letter right after n opens one of the dashboard's tabs, such as n c for Connections, and Help links to the list too. ## [2.6.0] — 2026-11-03 diff --git a/site/i18n/de.js b/site/i18n/de.js index 149fc64..5a3312c 100644 --- a/site/i18n/de.js +++ b/site/i18n/de.js @@ -2374,6 +2374,7 @@ "keys.esc":"Esc", "keys.game_speed":"Spielgeschwindigkeit: ½×, 1×, 2×, 4×, 8×", "keys.keyboard_shortcuts":"Tastenkürzel", +"keys.move_around_the_map":"Karte verschieben", "keys.noc_dashboard_a_letter_right":"NOC-Dashboard. Ein Buchstabe direkt danach öffnet einen Tab:", "keys.pause_or_play":"Pause oder weiter", "keys.previous_or_next_of_your":"Vorheriger oder nächster deiner POPs", diff --git a/site/i18n/es.js b/site/i18n/es.js index c6d55a7..0f9312e 100644 --- a/site/i18n/es.js +++ b/site/i18n/es.js @@ -2372,6 +2372,7 @@ "keys.esc":"Esc", "keys.game_speed":"Velocidad del juego: ½×, 1×, 2×, 4×, 8×", "keys.keyboard_shortcuts":"Atajos de teclado", +"keys.move_around_the_map":"Mover el mapa", "keys.noc_dashboard_a_letter_right":"Panel del NOC. Una letra justo después abre una pestaña:", "keys.pause_or_play":"Pausar o reanudar", "keys.previous_or_next_of_your":"Tu POP anterior o siguiente", diff --git a/site/i18n/fr.js b/site/i18n/fr.js index 43d5054..c0c9f01 100644 --- a/site/i18n/fr.js +++ b/site/i18n/fr.js @@ -2372,6 +2372,7 @@ "keys.esc":"Échap", "keys.game_speed":"Vitesse du jeu : ½×, 1×, 2×, 4×, 8×", "keys.keyboard_shortcuts":"Raccourcis clavier", +"keys.move_around_the_map":"Déplacer la carte", "keys.noc_dashboard_a_letter_right":"Tableau de bord NOC. Une lettre juste après ouvre un onglet :", "keys.pause_or_play":"Pause ou reprise", "keys.previous_or_next_of_your":"Ton POP précédent ou suivant", diff --git a/site/i18n/it.js b/site/i18n/it.js index 0ac558c..1881b8e 100644 --- a/site/i18n/it.js +++ b/site/i18n/it.js @@ -2372,6 +2372,7 @@ "keys.esc":"Esc", "keys.game_speed":"Velocità di gioco: ½×, 1×, 2×, 4×, 8×", "keys.keyboard_shortcuts":"Scorciatoie da tastiera", +"keys.move_around_the_map":"Sposta la mappa", "keys.noc_dashboard_a_letter_right":"Dashboard NOC. Una lettera subito dopo apre una scheda:", "keys.pause_or_play":"Pausa o riprendi", "keys.previous_or_next_of_your":"Il tuo POP precedente o successivo", diff --git a/site/i18n/pt.js b/site/i18n/pt.js index 422017c..5478bed 100644 --- a/site/i18n/pt.js +++ b/site/i18n/pt.js @@ -2372,6 +2372,7 @@ "keys.esc":"Esc", "keys.game_speed":"Velocidade do jogo: ½×, 1×, 2×, 4×, 8×", "keys.keyboard_shortcuts":"Atalhos de teclado", +"keys.move_around_the_map":"Mover o mapa", "keys.noc_dashboard_a_letter_right":"Dashboard do NOC. Uma letra logo depois abre uma aba:", "keys.pause_or_play":"Pausar ou continuar", "keys.previous_or_next_of_your":"Seu POP anterior ou próximo", diff --git a/src/game/93-ui/82-keys.js b/src/game/93-ui/82-keys.js index 334a05c..8bbde0c 100644 --- a/src/game/93-ui/82-keys.js +++ b/src/game/93-ui/82-keys.js @@ -1,13 +1,21 @@ -/* ---------- Keyboard shortcuts: [ ] step through your POPs, - = zoom, n (then a letter) the NOC, ? lists every key ---------- +/* ---------- Keyboard shortcuts: arrows/WASD move, [ ] step through your POPs, - = zoom, n (then a letter) the NOC, ? lists every key ---------- Space, 0–4 and Escape live with the map's input (96-map/90-input.js), / with Find a city. */ // Your POPs in the order Find a city lists them, so [ and ] walk the same list. const popOrder=()=>S&&S.pops?Object.keys(S.pops).filter(isCity).sort((a,b)=>CITY[a].name.localeCompare(CITY[b].name)):[]; // n opens the NOC dashboard, and a letter within KEY_LEAD ms of it picks a tab (n c: Connections). n never closes the // dashboard, so n c switches tabs without it flashing shut; Escape closes it. const DASH_KEYS={o:'overview',u:'cust',c:'conn',t:'traffic',r:'infra',l:'lease'},KEY_LEAD=1000;let keyLead=0; +// Arrow keys and WASD move the map while held, at a steady speed on screen. Their own frame loop runs only while one is +// down, so the game loop does no extra work; letting go, a window opening or leaving the browser window stops it. +const PAN_KEYS={ArrowUp:[0,-1],w:[0,-1],ArrowDown:[0,1],s:[0,1],ArrowLeft:[-1,0],a:[-1,0],ArrowRight:[1,0],d:[1,0]},PAN_SPEED=700; +const panHeld=new Set();let panAt=null; +function panFrame(t){if(!panHeld.size||!$('modal').hidden||!$('dash').hidden){panHeld.clear();return;} + // A slow frame (a busy day, a background tab) moves at most 50 ms worth, not a jump across the map. + const dt=panAt==null?0:Math.min(0.05,(t-panAt)/1000);panAt=t;let dx=0,dy=0;for(const k of panHeld){dx+=PAN_KEYS[k][0];dy+=PAN_KEYS[k][1];} + cam.x+=dx*PAN_SPEED*dt/cam.z;cam.y+=dy*PAN_SPEED*dt/cam.z;wrapCam();requestAnimationFrame(panFrame);} function showKeys(){const k=s=>`${s}`,TN={overview:tr('common.overview'),cust:tr('common.customers'),conn:tr('common.connections'),traffic:tr('common.traffic'),infra:tr('common.links_routers'),lease:tr('common.leased_lines')}; const tabs=Object.entries(DASH_KEYS).map(([l,t])=>`${k(l)} ${TN[t]}`).join(' '); - const rows=[[k(tr('keys.space')),tr('keys.pause_or_play')],[k('0')+' – '+k('4'),tr('keys.game_speed')],[k('/'),tr('find.find_a_city')],[k('[')+' '+k(']'),tr('keys.previous_or_next_of_your')],[k('-')+' '+k('='),tr('keys.zoom_the_map_out_or')],[k('n'),`${tr('keys.noc_dashboard_a_letter_right')}
${tabs}`],[k(tr('keys.esc')),tr('keys.close_a_window_or_deselect')],[k('?'),tr('keys.show_this_list')]]; + const rows=[[[...'←↑↓→'].map(k).join(' ')+'
'+[...'wasd'].map(k).join(' '),tr('keys.move_around_the_map')],[k(tr('keys.space')),tr('keys.pause_or_play')],[k('0')+' – '+k('4'),tr('keys.game_speed')],[k('/'),tr('find.find_a_city')],[k('[')+' '+k(']'),tr('keys.previous_or_next_of_your')],[k('-')+' '+k('='),tr('keys.zoom_the_map_out_or')],[k('n'),`${tr('keys.noc_dashboard_a_letter_right')}
${tabs}`],[k(tr('keys.esc')),tr('keys.close_a_window_or_deselect')],[k('?'),tr('keys.show_this_list')]]; openModal(`

⌨️ ${tr('keys.keyboard_shortcuts')}

${rows.map(([a,b])=>`
${a}
${b}
`).join('')}
${btn(UI.started?tr('common.back_to_the_map'):tr('common.got_it'),'helpClose',{},'big')}
`,'keys');} document.addEventListener('keydown',e=>{ @@ -18,5 +26,9 @@ document.addEventListener('keydown',e=>{ if(lead&&DASH_KEYS[k]&&!$('dash').hidden){e.preventDefault();ACT.dashTab({k:DASH_KEYS[k]});return;} if(k==='n'){if(!S||!UI.started||!$('modal').hidden)return;e.preventDefault();if($('dash').hidden)ACT.dash();if(!$('dash').hidden)keyLead=Date.now();return;} if(k==='?'){if(e.repeat)return;if($('modal').hidden){if(S&&UI.started){e.preventDefault();ACT.keys();}}else if($('modal').className==='keys'){e.preventDefault();closeModal();}return;} + // Alt with an arrow is the browser's Back and Forward. + if(!e.altKey&&PAN_KEYS[k]){if(!S||!UI.started||!$('modal').hidden||!$('dash').hidden)return;e.preventDefault();if(!panHeld.size){panAt=null;requestAnimationFrame(panFrame);}panHeld.add(k);return;} if(!(step||zoom)||!S||!UI.started||!$('modal').hidden)return; e.preventDefault();if(step)ACT.popStep({d:step});else ACT.zoomStep({d:zoom});}); +document.addEventListener('keyup',e=>panHeld.delete(e.key.length===1?e.key.toLowerCase():e.key)); +addEventListener('blur',()=>panHeld.clear()); diff --git a/src/i18n/en.js b/src/i18n/en.js index 6c710c1..beef4d8 100644 --- a/src/i18n/en.js +++ b/src/i18n/en.js @@ -2373,6 +2373,7 @@ "keys.esc":"Esc", "keys.game_speed":"Game speed: ½×, 1×, 2×, 4×, 8×", "keys.keyboard_shortcuts":"Keyboard shortcuts", +"keys.move_around_the_map":"Move around the map", "keys.noc_dashboard_a_letter_right":"NOC dashboard. A letter right after it opens a tab:", "keys.pause_or_play":"Pause or play", "keys.previous_or_next_of_your":"Previous or next of your POPs", diff --git a/tests/keyboard-shortcuts.test.js b/tests/keyboard-shortcuts.test.js index 0590076..0b78ce9 100644 --- a/tests/keyboard-shortcuts.test.js +++ b/tests/keyboard-shortcuts.test.js @@ -51,6 +51,35 @@ test('- and = zoom the map out and in around its middle, within the zoom limits' assert.deepEqual(errors, []); }); +test('the arrow keys and W A S D move the map while held, at the same speed on screen at any zoom', (t) => { + const { ev, dom, errors } = load(); + // A key left held (a failed assertion) would keep the page's frame loop, and this test file, running. + t.after(() => dom.window.dispatchEvent(new dom.window.Event('blur'))); + const doc = dom.window.document, key = keyer(dom); + const up = k => doc.body.dispatchEvent(new dom.window.KeyboardEvent('keyup', { key: k, bubbles: true })); + const at = () => [ev('cam.x'), ev('cam.y')].map(v => Math.round(v * 100) / 100); + // Frames are driven by hand: panAt=null makes the first one a start, the next one 50 ms later moves. + const frames = (a, b) => ev(`panAt=null;panFrame(${a});panFrame(${b})`); + ev(start + ';cam.z=1;cam.x=1000;cam.y=500'); + key('ArrowRight'); frames(1000, 1050); assert.deepEqual(at(), [1035, 500], '700 px a second: 35 px in 50 ms'); + up('ArrowRight'); frames(1000, 1050); assert.deepEqual(at(), [1035, 500], 'letting go stops it'); + key('w'); key('a'); frames(0, 50); assert.deepEqual(at(), [1000, 465], 'W and A together: up and left'); + up('w'); up('A'); + assert.equal(ev('panHeld.size'), 0, 'a key let go with Shift held still counts'); + ev('cam.z=2'); key('s'); frames(0, 50); assert.deepEqual(at(), [1000, 482.5], 'zoomed in, the same 35 px on screen'); + frames(0, 2000); assert.deepEqual(at(), [1000, 500], 'a slow frame moves at most 50 ms worth'); + up('s'); + key('ArrowDown'); key('ArrowDown', null, { repeat: true }); assert.equal(ev('panHeld.size'), 1, 'key repeat is the same key'); + dom.window.dispatchEvent(new dom.window.Event('blur')); assert.equal(ev('panHeld.size'), 0, 'leaving the window lets go'); + key('d'); ev('ACT.keys()'); frames(0, 50); assert.deepEqual(at(), [1000, 500], 'a window opening stops it'); + assert.equal(ev('panHeld.size'), 0); + ev('closeModal();ACT.dash()'); key('ArrowLeft'); assert.equal(ev('panHeld.size'), 0, 'arrows scroll the NOC dashboard instead'); + ev("$('dash').hidden=true;ACT.rename()"); key('a', doc.getElementById('rnIn')); key('a'); assert.equal(ev('panHeld.size'), 0, 'not while typing or behind a window'); + ev('closeModal()'); key('ArrowLeft', null, { altKey: true }); key('ArrowLeft', null, { metaKey: true }); + assert.equal(ev('panHeld.size'), 0, 'Alt or Cmd with an arrow is the browser\'s'); + assert.deepEqual(errors, []); +}); + test('n opens the NOC dashboard, and n followed by a letter opens one of its tabs', () => { const { ev, dom, errors } = load(); const key = keyer(dom), open = () => !ev("$('dash').hidden"), tab = () => ev("UI.dtab||'overview'"); @@ -79,9 +108,9 @@ test('? shows every keyboard shortcut and closes again; Help links to it on a co key('?'); assert.equal(ev("$('modal').className"), 'keys', '? opens the list'); const txt = doc.getElementById('modalCard').textContent; - for (const s of ['Keyboard shortcuts', 'Space', 'Pause or play', 'Find a city', 'Previous or next of your POPs', 'Zoom the map out or in', 'NOC dashboard', 'Connections', 'Leased lines', 'Esc', 'Show this list']) + for (const s of ['Keyboard shortcuts', 'Move around the map', 'Space', 'Pause or play', 'Find a city', 'Previous or next of your POPs', 'Zoom the map out or in', 'NOC dashboard', 'Connections', 'Leased lines', 'Esc', 'Show this list']) assert.ok(txt.includes(s), s); - for (const k of ['[', ']', '-', '=', '/', '?', '0', '4', 'n', 'o', 'u', 'c', 't', 'r', 'l']) assert.ok([...doc.querySelectorAll('#modalCard .kbd')].some(e => e.textContent === k), k); + for (const k of ['←', '↑', '↓', '→', 'w', 'a', 's', 'd', '[', ']', '-', '=', '/', '?', '0', '4', 'n', 'o', 'u', 'c', 't', 'r', 'l']) assert.ok([...doc.querySelectorAll('#modalCard .kbd')].some(e => e.textContent === k), k); key('?', null, { repeat: true }); assert.equal(ev("$('modal').hidden"), false, 'holding ? does not flicker it'); key('?'); assert.equal(ev("$('modal').hidden"), true, '? again closes it'); key('?'); key('Escape'); assert.equal(ev("$('modal').hidden"), true, 'so does Escape');