Start maybe working on a native app
This commit is contained in:
+70
-224
@@ -1,7 +1,7 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import { canvas } from "./renderer/core.ts";
|
||||
import { platform } from "./platform.ts";
|
||||
|
||||
export type KeyCode =
|
||||
// letters
|
||||
@@ -79,16 +79,8 @@ export type KeyCode =
|
||||
| "F11"
|
||||
| "F12";
|
||||
|
||||
// the keys the game keeps in fullscreen, see "fullscreenchange". not every key: with all of them locked, chromium
|
||||
// can keep alt+tab and the windows key from the os too
|
||||
const FULLSCREEN_LOCKED_KEYS = ["Escape", "KeyW"];
|
||||
|
||||
// chromium's keyboard lock api, typescript's dom types don't have it yet
|
||||
interface KeyboardLock {
|
||||
lock(codes?: string[]): Promise<void>;
|
||||
unlock(): void;
|
||||
}
|
||||
|
||||
// what the player presses and how the mouse moves, kept for the frame. where it runs feeds it events: the page's
|
||||
// (browser_input.ts) or the desktop app's window (native/), and grabs the mouse for it (see platform.ts)
|
||||
export class InputManager {
|
||||
static keys_down = new Set<string>();
|
||||
static keys_pressed = new Set<string>();
|
||||
@@ -108,8 +100,6 @@ export class InputManager {
|
||||
// around a screen can never turn it
|
||||
static look_delta_x = 0;
|
||||
static look_delta_y = 0;
|
||||
// chrome sometimes sends a huge movement with the first mouse event after the lock starts
|
||||
static #skip_look_event = false;
|
||||
static wheel_delta = 0;
|
||||
|
||||
// in the order they were typed. a list, since the same letter can be typed twice in one frame
|
||||
@@ -117,147 +107,71 @@ export class InputManager {
|
||||
// letters pressed with ctrl (cmd on macs) held, like "a" for select all
|
||||
static shortcuts: string[] = [];
|
||||
|
||||
static pointer_lock_flag = false;
|
||||
static mouse_grab_timer = 0;
|
||||
static mouse_ungrab_timer = 0;
|
||||
static mouse_ungrab_timeout = -1;
|
||||
static pointer_lock_waiting = false;
|
||||
// the browser let go of the mouse while the game wanted it, like escape or switching windows
|
||||
static #lost_pointer_lock = false;
|
||||
// the canvas had the pointer lock at the last pointerlockchange
|
||||
static #was_locked = false;
|
||||
// the desktop app moving the cursor after the mouse was let go, see center_desktop_mouse. grabbing it again waits
|
||||
// for that, or the move would come while it's locked and turn the camera
|
||||
static #centering = Promise.resolve();
|
||||
static #awaiting_centering = false;
|
||||
// the locked mouse doesn't turn the camera until then, the move can still reach chromium after the app answered,
|
||||
// or the mouse was locked again before chromium said it let go
|
||||
static #ignore_look_until = 0;
|
||||
// what the game last asked for, it can change while grabbing waits
|
||||
static #want_grab = false;
|
||||
|
||||
static initialize(canvas: HTMLCanvasElement) {
|
||||
self.addEventListener("keydown", (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
if (!this.keys_down.has(e.code)) {
|
||||
this.keys_pressed.add(e.code);
|
||||
}
|
||||
this.keys_down.add(e.code);
|
||||
if (e.key.length === 1) {
|
||||
if (this.#is_shortcut(e)) {
|
||||
this.shortcuts.push(e.key.toLowerCase());
|
||||
} else {
|
||||
this.typed_characters.push(e.key);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// keys let go while the window isn't focused never send keyup, so they'd stay held
|
||||
self.addEventListener("blur", () => {
|
||||
this.keys_down.clear();
|
||||
this.mouse_buttons_down.clear();
|
||||
});
|
||||
|
||||
self.addEventListener("keyup", (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
this.keys_down.delete(e.code);
|
||||
this.keys_released.add(e.code);
|
||||
});
|
||||
|
||||
self.addEventListener("mousedown", (e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
if (!document.hasFocus() && document.pointerLockElement !== canvas) {
|
||||
this.set_mouse_grabbed(true);
|
||||
return;
|
||||
}
|
||||
if (!this.mouse_buttons_down.has(e.button)) {
|
||||
this.mouse_buttons_pressed.add(e.button);
|
||||
}
|
||||
this.mouse_buttons_down.add(e.button);
|
||||
});
|
||||
|
||||
self.addEventListener("mouseup", (e) => {
|
||||
e.preventDefault();
|
||||
this.mouse_buttons_down.delete(e.button);
|
||||
this.mouse_buttons_released.add(e.button);
|
||||
});
|
||||
|
||||
// ctrl+scroll (and pinching a trackpad, which browsers send as that) would zoom the page. only a listener that
|
||||
// isn't passive can stop it, and on the window they're passive unless told otherwise
|
||||
self.addEventListener("wheel", (e) => {
|
||||
if (e.ctrlKey) {
|
||||
e.preventDefault();
|
||||
}
|
||||
this.wheel_delta += e.deltaY;
|
||||
}, { passive: false });
|
||||
|
||||
document.addEventListener("mousemove", (e) => {
|
||||
e.preventDefault();
|
||||
if (document.pointerLockElement === canvas) {
|
||||
if (this.#skip_look_event) {
|
||||
this.#skip_look_event = false;
|
||||
return;
|
||||
}
|
||||
if (performance.now() < this.#ignore_look_until) return;
|
||||
this.look_delta_x += e.movementX;
|
||||
this.look_delta_y += e.movementY;
|
||||
// code is a KeyCode, key what it types: a character, or a name like "Enter". shortcut is whether ctrl (cmd on
|
||||
// macs) is held for one, then a letter is a shortcut instead of being typed
|
||||
static key_down(code: string, key: string, shortcut: boolean) {
|
||||
if (!this.keys_down.has(code)) {
|
||||
this.keys_pressed.add(code);
|
||||
}
|
||||
this.keys_down.add(code);
|
||||
if (key.length === 1) {
|
||||
if (shortcut) {
|
||||
this.shortcuts.push(key.toLowerCase());
|
||||
} else {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
const new_x = e.clientX - rect.left;
|
||||
const new_y = e.clientY - rect.top;
|
||||
|
||||
// not from where it was before the mouse was grabbed, that's -1
|
||||
if (this.mouse_x >= 0 && this.mouse_y >= 0) {
|
||||
this.mouse_delta_x += new_x - this.mouse_x;
|
||||
this.mouse_delta_y += new_y - this.mouse_y;
|
||||
}
|
||||
|
||||
this.mouse_x = new_x;
|
||||
this.mouse_y = new_y;
|
||||
this.typed_characters.push(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener("pointerlockchange", () => {
|
||||
const locked = document.pointerLockElement === canvas;
|
||||
if (this.#was_locked && !locked) {
|
||||
this.#ignore_look_until = Infinity;
|
||||
this.#centering = center_desktop_mouse().then(() => {
|
||||
this.#ignore_look_until = performance.now() + CENTERING_SETTLE_MS;
|
||||
});
|
||||
}
|
||||
this.#was_locked = locked;
|
||||
this.#skip_look_event = locked;
|
||||
self.setTimeout(() => {
|
||||
const grab = document.pointerLockElement === canvas;
|
||||
if (!grab) {
|
||||
if (this.pointer_lock_flag) {
|
||||
this.mouse_ungrab_timer = performance.now();
|
||||
this.#lost_pointer_lock = true;
|
||||
}
|
||||
}
|
||||
this.pointer_lock_flag = grab;
|
||||
}, 60);
|
||||
this.pointer_lock_waiting = false;
|
||||
});
|
||||
// for where typing comes apart from keys, the desktop app's window
|
||||
static type_text(text: string) {
|
||||
this.typed_characters.push(...text);
|
||||
}
|
||||
|
||||
// in fullscreen, chromium's keyboard lock gives the game escape (holding it still leaves fullscreen) and ctrl+w,
|
||||
// instead of the browser leaving fullscreen or closing the tab. other browsers don't have it and work as before
|
||||
document.addEventListener("fullscreenchange", () => {
|
||||
const keyboard = (navigator as Navigator & { keyboard?: KeyboardLock }).keyboard;
|
||||
if (!keyboard?.lock) return;
|
||||
if (document.fullscreenElement) {
|
||||
keyboard.lock(FULLSCREEN_LOCKED_KEYS).catch(() => {});
|
||||
} else {
|
||||
keyboard.unlock();
|
||||
}
|
||||
});
|
||||
static key_up(code: string) {
|
||||
this.keys_down.delete(code);
|
||||
this.keys_released.add(code);
|
||||
}
|
||||
|
||||
document.addEventListener("pointerlockerror", () => {
|
||||
this.pointer_lock_waiting = false;
|
||||
});
|
||||
// buttons are numbered like the dom's: 0 left, 1 middle, 2 right
|
||||
static mouse_down(button: number) {
|
||||
if (!this.mouse_buttons_down.has(button)) {
|
||||
this.mouse_buttons_pressed.add(button);
|
||||
}
|
||||
this.mouse_buttons_down.add(button);
|
||||
}
|
||||
|
||||
static mouse_up(button: number) {
|
||||
this.mouse_buttons_down.delete(button);
|
||||
this.mouse_buttons_released.add(button);
|
||||
}
|
||||
|
||||
// the cursor at x y, in the canvas's pixels
|
||||
static move_cursor(x: number, y: number) {
|
||||
// not from where it was before the mouse was grabbed, that's -1
|
||||
if (this.mouse_x >= 0 && this.mouse_y >= 0) {
|
||||
this.mouse_delta_x += x - this.mouse_x;
|
||||
this.mouse_delta_y += y - this.mouse_y;
|
||||
}
|
||||
this.mouse_x = x;
|
||||
this.mouse_y = y;
|
||||
}
|
||||
|
||||
// the grabbed mouse moved
|
||||
static look(x: number, y: number) {
|
||||
this.look_delta_x += x;
|
||||
this.look_delta_y += y;
|
||||
}
|
||||
|
||||
// down is positive, about 100 for a notch of a wheel
|
||||
static scroll(amount: number) {
|
||||
this.wheel_delta += amount;
|
||||
}
|
||||
|
||||
// keys and buttons let go while the game didn't have focus never come up, so they'd stay held
|
||||
static release_all() {
|
||||
this.keys_down.clear();
|
||||
this.mouse_buttons_down.clear();
|
||||
}
|
||||
|
||||
static is_key_down(code: KeyCode): boolean {
|
||||
@@ -306,12 +220,6 @@ export class InputManager {
|
||||
this.mouse_buttons_consumed.add(button);
|
||||
}
|
||||
|
||||
// ctrl (cmd on macs) held for a shortcut. altgr types characters like @ and comes as ctrl+alt on windows, so it
|
||||
// isn't one. getModifierState("AltGraph") isn't used, some linux layouts report it along with plain ctrl
|
||||
static #is_shortcut(e: KeyboardEvent) {
|
||||
return e.metaKey || (e.ctrlKey && !e.altKey);
|
||||
}
|
||||
|
||||
static is_shortcut_pressed(letter: string): boolean {
|
||||
return this.shortcuts.includes(letter);
|
||||
}
|
||||
@@ -337,83 +245,21 @@ export class InputManager {
|
||||
this.mouse_buttons_consumed.clear();
|
||||
}
|
||||
|
||||
static async set_mouse_grabbed(grab: boolean) {
|
||||
this.pointer_lock_flag = grab;
|
||||
this.#want_grab = grab;
|
||||
// a retry to grab it again mustn't happen once it shouldn't be grabbed
|
||||
if (!grab && this.mouse_ungrab_timeout !== -1) {
|
||||
self.clearTimeout(this.mouse_ungrab_timeout);
|
||||
this.mouse_ungrab_timeout = -1;
|
||||
}
|
||||
// already how it should be, the game calls this every frame
|
||||
const locked = document.pointerLockElement === canvas;
|
||||
if (grab ? locked : !locked && !this.pointer_lock_waiting) {
|
||||
return;
|
||||
}
|
||||
if (grab) {
|
||||
// called every frame, once is enough
|
||||
if (this.#awaiting_centering) return;
|
||||
this.#awaiting_centering = true;
|
||||
await this.#centering;
|
||||
this.#awaiting_centering = false;
|
||||
// a gui opened again meanwhile
|
||||
if (!this.#want_grab || document.pointerLockElement === canvas) return;
|
||||
}
|
||||
const t = performance.now();
|
||||
this.mouse_grab_timer = t;
|
||||
if (grab) {
|
||||
this.mouse_x = -1;
|
||||
this.mouse_y = -1;
|
||||
this.pointer_lock_waiting = true;
|
||||
try {
|
||||
await canvas.requestPointerLock();
|
||||
} catch (_) { /* */ }
|
||||
if (this.mouse_ungrab_timeout !== -1) {
|
||||
self.clearTimeout(this.mouse_ungrab_timeout);
|
||||
}
|
||||
this.mouse_ungrab_timeout = -1;
|
||||
if (t - this.mouse_ungrab_timer < 3000) {
|
||||
this.mouse_ungrab_timeout = self.setTimeout(async () => {
|
||||
try {
|
||||
await canvas.requestPointerLock();
|
||||
} catch (_) { /* */ }
|
||||
}, 3100 - (t - this.mouse_ungrab_timer));
|
||||
}
|
||||
} else {
|
||||
if (!this.pointer_lock_waiting) {
|
||||
document.exitPointerLock();
|
||||
}
|
||||
}
|
||||
// the game calls this every frame
|
||||
static set_mouse_grabbed(grab: boolean) {
|
||||
platform.set_mouse_grabbed(grab);
|
||||
}
|
||||
|
||||
// whether the mouse was taken away since the last call, browsers eat the escape that does it
|
||||
// whether the mouse was taken away since the last call, like browsers do with escape
|
||||
static take_lost_pointer_lock() {
|
||||
const lost = this.#lost_pointer_lock;
|
||||
this.#lost_pointer_lock = false;
|
||||
return lost;
|
||||
return platform.take_lost_mouse();
|
||||
}
|
||||
|
||||
static is_mouse_grabbed() {
|
||||
return document.hasFocus() && document.pointerLockElement === canvas;
|
||||
return platform.is_mouse_grabbed();
|
||||
}
|
||||
|
||||
static toggle_fullscreen() {
|
||||
if (!document.fullscreenElement) {
|
||||
canvas.requestFullscreen();
|
||||
} else {
|
||||
document.exitFullscreen();
|
||||
}
|
||||
platform.toggle_fullscreen();
|
||||
}
|
||||
}
|
||||
|
||||
const CENTERING_TIMEOUT_MS = 250;
|
||||
const CENTERING_SETTLE_MS = 100;
|
||||
|
||||
// the desktop app (the page has ?singleplayer there, see desktop/main.ts) moves the cursor to the middle of the
|
||||
// window when the mouse is let go to open a gui, like minecraft. chromium would put it back where it was grabbed.
|
||||
// resolves once the cursor was moved, or after CENTERING_TIMEOUT_MS if the app is slow, so grabbing isn't held up
|
||||
function center_desktop_mouse(): Promise<void> {
|
||||
if (!new URL(location.href).searchParams.has("singleplayer")) return Promise.resolve();
|
||||
const moved = fetch("/desktop/center_mouse", { method: "POST" }).then(() => {}, () => {});
|
||||
return Promise.race([moved, new Promise<void>((resolve) => self.setTimeout(resolve, CENTERING_TIMEOUT_MS))]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user