Improve input
This commit is contained in:
+43
-1
@@ -121,6 +121,17 @@ export class InputManager {
|
||||
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) => {
|
||||
@@ -187,6 +198,7 @@ export class InputManager {
|
||||
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;
|
||||
} else {
|
||||
@@ -206,7 +218,15 @@ export class InputManager {
|
||||
});
|
||||
|
||||
document.addEventListener("pointerlockchange", () => {
|
||||
this.#skip_look_event = document.pointerLockElement === canvas;
|
||||
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) {
|
||||
@@ -316,6 +336,7 @@ export class InputManager {
|
||||
|
||||
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);
|
||||
@@ -326,6 +347,15 @@ export class InputManager {
|
||||
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) {
|
||||
@@ -372,3 +402,15 @@ export class InputManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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