Split The code is public; the app answers only to the crew.

Play it fullscreen: the view and the touch pad together, on a phone and on a laptop #62

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/cs:scout/51-play-it-fullscreen-the-view-and-the-touc → main 1 commit fork

  • Merged. scout/51-play-it-fullscreen-the-view-and-the-touc was merged into main as fccb9eb.

Review

Sign in to review this change.
6 files changed +177 −2
README.md +8
@@ -108,6 +108,14 @@ the gun once it is gone), <kbd>Tab</kbd> shows
108108 the scoreboard, <kbd>Esc</kbd> pauses. Pausing also opens mute, volume and
109109 look sensitivity, which take effect at once and are remembered for next time.
110110
111 A **Fullscreen** button sits in the menu and in the pause overlay — wherever
112 the browser can do it at all; iOS Safari on an iPhone cannot, so there the
113 button is not shown and the game plays exactly as it does without one. It
114 fullscreens the view and the pad together, not the view alone, so a phone in
115 fullscreen keeps every button it had before. Leaving fullscreen with the
116 system gesture or <kbd>Esc</kbd> pauses a running match, the same as losing
117 pointer lock does, and puts the page back exactly as it was.
118
111119 On a touch screen the same actions are a pad, and the view is the aim:
112120
113121 | what | key | thumb |
src/client/fullscreen.test.ts added +21
@@ -0,0 +1,21 @@
1 import { expect, test } from "bun:test";
2 import { fullscreenControl } from "./fullscreen.ts";
3
4 test("a browser that cannot go fullscreen never shows the control", () => {
5 expect(fullscreenControl(false, false)).toEqual({ shown: false, label: "Fullscreen", pressed: false });
6 expect(fullscreenControl(false, true)).toEqual({ shown: false, label: "Fullscreen", pressed: false });
7 });
8
9 test("supported and not active: shown, unpressed, and says which way it will go", () => {
10 const state = fullscreenControl(true, false);
11 expect(state.shown).toBe(true);
12 expect(state.pressed).toBe(false);
13 expect(state.label).toBe("Fullscreen");
14 });
15
16 test("supported and active: shown, pressed, and the label reverses", () => {
17 const state = fullscreenControl(true, true);
18 expect(state.shown).toBe(true);
19 expect(state.pressed).toBe(true);
20 expect(state.label).toBe("Exit fullscreen");
21 });
src/client/fullscreen.ts added +75
@@ -0,0 +1,75 @@
1 /**
2 * Fullscreen: a pure decision — is the control shown, and what does it say —
3 * plus the thin DOM glue that asks for it. It always fullscreens the
4 * *wrapper*, the element holding both `#stage` and `#touch`, never `#stage`
5 * alone: fullscreening the canvas hides the pad and leaves a phone with no
6 * buttons at all.
7 */
8
9 export type FullscreenControl = {
10 /** False on a browser that cannot do it at all — iOS Safari on an iPhone. */
11 shown: boolean;
12 label: string;
13 pressed: boolean;
14 };
15
16 /** Pure: what the button shows, from support and the current element alone. */
17 export function fullscreenControl(supported: boolean, active: boolean): FullscreenControl {
18 if (!supported) return { shown: false, label: "Fullscreen", pressed: false };
19 return { shown: true, label: active ? "Exit fullscreen" : "Fullscreen", pressed: active };
20 }
21
22 export type FullscreenHandle = {
23 toggle(): void;
24 detach(): void;
25 };
26
27 /**
28 * Wires one or more buttons — the menu's and the pause overlay's are the same
29 * switch — to the wrapper's fullscreen state. `onExit` fires only when the
30 * wrapper itself drops out of fullscreen (the system gesture or `Esc`), never
31 * for some other element's fullscreen coming or going, and never on entry.
32 */
33 export function attachFullscreen(
34 wrapper: HTMLElement,
35 buttons: HTMLButtonElement[],
36 onExit: () => void,
37 ): FullscreenHandle {
38 const supported = document.fullscreenEnabled === true;
39 const isActive = (): boolean => document.fullscreenElement === wrapper;
40 let wasActive = isActive();
41
42 const render = (): void => {
43 const state = fullscreenControl(supported, isActive());
44 for (const button of buttons) {
45 button.hidden = !state.shown;
46 button.textContent = state.label;
47 button.setAttribute("aria-pressed", String(state.pressed));
48 }
49 };
50
51 const onChange = (): void => {
52 const active = isActive();
53 if (wasActive && !active) onExit();
54 wasActive = active;
55 render();
56 };
57
58 const toggle = (): void => {
59 if (!supported) return;
60 if (isActive()) void document.exitFullscreen();
61 else void wrapper.requestFullscreen().catch(() => {});
62 };
63
64 for (const button of buttons) button.addEventListener("click", toggle);
65 document.addEventListener("fullscreenchange", onChange);
66 render();
67
68 return {
69 toggle,
70 detach: () => {
71 document.removeEventListener("fullscreenchange", onChange);
72 for (const button of buttons) button.removeEventListener("click", toggle);
73 },
74 };
75 }
src/client/main.ts +8 −1
@@ -7,6 +7,7 @@
77 */
88
99 import { createAudio } from "./audio.ts";
10 import { attachFullscreen } from "./fullscreen.ts";
1011 import { drawHud } from "./hud.ts";
1112 import { attachButtons, createControls } from "./input.ts";
1213 import { clamp, dist } from "./math.ts";
@@ -36,6 +37,7 @@ const SCALE_EVERY_MS = 500;
3637 const $ = <T extends HTMLElement>(id: string): T => document.getElementById(id) as T;
3738
3839 function boot(): void {
40 const wrapper = $("wrapper");
3941 const scene = $<HTMLCanvasElement>("scene");
4042 const hudCanvas = $<HTMLCanvasElement>("hud");
4143 const menu = $("menu");
@@ -51,6 +53,8 @@ function boot(): void {
5153 const pauseSensField = $<HTMLInputElement>("pause-sens");
5254 const volumeField = $<HTMLInputElement>("volume");
5355 const muteButton = $<HTMLButtonElement>("mute");
56 const fullscreenMenuButton = $<HTMLButtonElement>("fullscreen-menu");
57 const fullscreenPauseButton = $<HTMLButtonElement>("fullscreen-pause");
5458
5559 const renderer = createRenderer(scene);
5660 if (renderer === null) {
@@ -106,7 +110,7 @@ function boot(): void {
106110 });
107111 // The pause dialog sits over a live match: a key typed into one of its
108112 // fields must never reach the window-level handler that drives the game.
109 for (const field of [pauseSensField, volumeField, muteButton])
113 for (const field of [pauseSensField, volumeField, muteButton, fullscreenPauseButton])
110114 field.addEventListener("keydown", (event) => event.stopPropagation());
111115
112116 let world: World | null = null;
@@ -195,6 +199,9 @@ function boot(): void {
195199 $("resume").focus();
196200 }
197201 };
202 // Leaving fullscreen — the system gesture or Esc — gets the same pause a
203 // lost pointer lock already gets: a match is never left running unseen.
204 attachFullscreen(wrapper, [fullscreenMenuButton, fullscreenPauseButton], () => controls.onPause());
198205
199206 // --- results, sent as the match goes rather than only at the end
200207 async function postResult(final: boolean): Promise<void> {
src/pages/game.test.ts added +48
@@ -0,0 +1,48 @@
1 import { expect, test } from "bun:test";
2 import { mkdtempSync } from "node:fs";
3 import { tmpdir } from "node:os";
4 import { join } from "node:path";
5 import { openDatabase } from "../db.ts";
6 import { gamePage } from "./game.ts";
7
8 const db = openDatabase(mkdtempSync(join(tmpdir(), "game-page-")));
9 const options = { env: "development", pull: undefined, commit: "", script: "/static/app.js", ssh: undefined };
10
11 /** A `<div id="X" ...>`'s own body, whatever else is on its tag, to its matching `</div>`. */
12 function element(html: string, id: string): string {
13 const marker = html.indexOf(`id="${id}"`);
14 expect(marker).toBeGreaterThan(-1);
15 const start = html.lastIndexOf("<div", marker);
16 expect(start).toBeGreaterThan(-1);
17 let depth = 0;
18 let i = start;
19 while (i < html.length) {
20 if (html.startsWith("<div", i)) depth += 1;
21 else if (html.startsWith("</div>", i)) {
22 depth -= 1;
23 if (depth === 0) return html.slice(start, i + "</div>".length);
24 }
25 i += 1;
26 }
27 throw new Error(`unclosed #${id}`);
28 }
29
30 test("the wrapper — not the stage alone — holds both the view and the pad", () => {
31 const html = gamePage(db, options);
32 const wrapper = element(html, "wrapper");
33 expect(wrapper).toContain(`<div id="stage">`);
34 expect(wrapper).toContain(`<div id="touch" hidden>`);
35 // Nesting, not just presence: a stage or a pad that escaped the wrapper
36 // would still match `toContain` but would not go fullscreen together.
37 expect(wrapper.indexOf(`<div id="stage">`)).toBeLessThan(wrapper.indexOf(`<div id="touch" hidden>`));
38 });
39
40 test("a fullscreen control sits in the menu and in the pause overlay", () => {
41 const html = gamePage(db, options);
42 const menu = element(html, "menu");
43 const pause = element(html, "pause");
44 expect(menu).toContain(`id="fullscreen-menu"`);
45 expect(menu).toContain(`aria-pressed="false"`);
46 expect(pause).toContain(`id="fullscreen-pause"`);
47 expect(pause).toContain(`aria-pressed="false"`);
48 });
src/pages/game.ts +17 −1
@@ -61,6 +61,18 @@ h1 { font-size:1.4rem; margin:0; letter-spacing:.02em; }
6161 small.commit { color:var(--dim); font-size:.75rem; }
6262 #stage { position:relative; width:100%; aspect-ratio:16/9; max-height:78vh; margin:1rem auto; background:#000;
6363 border:1px solid var(--line); border-radius:.6rem; overflow:hidden; touch-action:none; }
64 /* Fullscreen goes on #wrapper, not #stage: the pad has to come along, or a
65 phone in fullscreen has a view and no buttons. */
66 #wrapper:fullscreen, #wrapper:-webkit-full-screen {
67 display:flex; flex-direction:column; height:100dvh; width:100vw; background:#000;
68 padding-bottom:env(safe-area-inset-bottom);
69 }
70 #wrapper:fullscreen #stage, #wrapper:-webkit-full-screen #stage {
71 flex:1 1 auto; min-height:0; max-height:none; aspect-ratio:auto; margin:0; border:none; border-radius:0;
72 }
73 #wrapper:fullscreen #touch, #wrapper:-webkit-full-screen #touch {
74 flex:0 0 auto; margin:0 auto; padding-bottom:env(safe-area-inset-bottom);
75 }
6476 #scene, #hud { position:absolute; inset:0; width:100%; height:100%; display:block; }
6577 #hud { pointer-events:none; }
6678 .overlay { position:absolute; inset:0; display:flex; flex-direction:column; align-items:center; justify-content:center;
@@ -142,6 +154,7 @@ export function gamePage(db: Database, options: PageOptions): string {
142154 ${options.commit ? `<small class="commit">@ ${escape(options.commit)}</small>` : ""}
143155 </header>
144156 <main>
157 <div id="wrapper">
145158 <div id="stage">
146159 <canvas id="scene"></canvas>
147160 <canvas id="hud"></canvas>
@@ -155,7 +168,8 @@ export function gamePage(db: Database, options: PageOptions): string {
155168 <label>Side<select id="side"><option value="ct">Counter-Terrorists</option><option value="t">Terrorists</option></select></label>
156169 <label>Sensitivity<input id="sens" type="number" min="0.2" max="12" step="0.1" value="2.2"></label>
157170 </div>
158 <div class="row"><button type="button" class="primary" id="play">Play</button></div>
171 <div class="row"><button type="button" class="primary" id="play">Play</button>
172 <button type="button" id="fullscreen-menu" aria-pressed="false" hidden>Fullscreen</button></div>
159173 <p class="hint"><kbd>WASD</kbd> move · <kbd>Space</kbd> jump · <kbd>Ctrl</kbd> crouch · <kbd>Shift</kbd> walk ·
160174 mouse to aim and fire · <kbd>R</kbd> reload · <kbd>E</kbd> plant, defuse or take a gun · <kbd>B</kbd> buy ·
161175 right click or <kbd>Q</kbd> to scope · <kbd>1</kbd><kbd>2</kbd><kbd>3</kbd> guns ·
@@ -171,6 +185,7 @@ export function gamePage(db: Database, options: PageOptions): string {
171185 <label>Sensitivity<input id="pause-sens" type="number" min="0.2" max="12" step="0.1"></label>
172186 <label>Volume<input id="volume" type="range" min="0" max="100" step="1"></label>
173187 <button type="button" id="mute" aria-pressed="false">Mute</button>
188 <button type="button" id="fullscreen-pause" aria-pressed="false" hidden>Fullscreen</button>
174189 </div>
175190 <div class="row"><button type="button" class="primary" id="resume">Resume</button><button type="button" id="quit">Leave match</button></div>
176191 </div>
@@ -210,6 +225,7 @@ export function gamePage(db: Database, options: PageOptions): string {
210225 <p class="hint">Left of the view is the stick, the right aims — and a second
211226 finger anywhere on the right fires without moving your aim.</p>
212227 </div>
228 </div>
213229
214230 <section id="stats">${statsHtml(db)}</section>
215231

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs