diff --git a/controls/touch.js b/controls/touch.js index 4ad30a6..906b696 100644 --- a/controls/touch.js +++ b/controls/touch.js @@ -15,10 +15,11 @@ // holds the beak open (axes.jaw = 1) while held. // // The overlay DOM lives in index.html (#touch-ui); this module only wires -// pointer events to it. `connected` mirrors (pointer: coarse) - rl.js -// reflects it onto body.touch-mode, which is what actually shows the -// overlay - so a desktop never sees the thumbs and a phone always does, -// even before the first touch. +// pointer events to it. `connected` (mirrored onto body.touch-mode by +// rl.js, which is what actually shows the overlay) arms two ways: +// - (pointer: coarse) matches: phones/tablets, before any touch; +// - OR the first real touch anywhere: catches DevTools device modes and +// hybrid laptops the media query misses. Latching, like a gamepad. const TOUCH_ALPHA = 0.18; // EMA smoothing toward the stick target const TOUCH_DEADZONE = 0.12; // normalized deflection ignored around center @@ -44,9 +45,16 @@ export class TouchSource { init() { this.#mq = window.matchMedia("(pointer: coarse)"); - this.#onMq = () => { this.connected = this.#mq.matches; }; + // Latching: once a device has proven it can touch, keep the thumbs. + // __microduckTouched carries a touch seen by the title page before + // this module booted. + if (window.__microduckTouched) this.connected = true; + this.#onMq = () => { this.connected = this.connected || this.#mq.matches; }; this.#mq.addEventListener("change", this.#onMq); this.#onMq(); + const armTouch = () => { this.connected = true; }; + window.addEventListener("touchstart", armTouch, { once: true, passive: true }); + this.#disposers.push(() => window.removeEventListener("touchstart", armTouch)); const zone = document.getElementById("touch-zone"); const stick = document.getElementById("touch-stick"); diff --git a/index.html b/index.html index 15f0811..13ab2bd 100644 --- a/index.html +++ b/index.html @@ -1280,12 +1280,20 @@ pollPad(); // Touch tutorial variant needs the class before rl.js boots; the - // game loop keeps mirroring the same media query afterwards. + // game loop keeps mirroring the touch source afterwards. Latching: + // (pointer: coarse) OR any real touch proves the device (DevTools + // device modes and hybrid laptops often fail the media query), and + // the __microduckTouched flag hands the proof to controls/touch.js. const coarse = matchMedia("(pointer: coarse)"); - const pollTouch = () => - document.body.classList.toggle("touch-mode", coarse.matches); - coarse.addEventListener("change", pollTouch); - pollTouch(); + const armTouch = () => { + window.__microduckTouched = true; + document.body.classList.add("touch-mode"); + }; + if (coarse.matches) armTouch(); + else coarse.addEventListener("change", () => coarse.matches && armTouch()); + window.addEventListener("touchstart", armTouch, { once: true, passive: true }); + // ?touch=1 forces the thumbs on any device - desktop testing hook. + if (new URLSearchParams(location.search).get("touch") === "1") armTouch(); // ── Menu open / close ──────────────────────────────────────────── const open = () => { @@ -1368,7 +1376,7 @@ appended: a plain src attribute can 401 when the browser blocks the *.static.hf.space auth cookie inside the hub iframe. -->