Drop deno desktop
This commit is contained in:
+1
-43
@@ -24,17 +24,6 @@ let pointer_lock_waiting = false;
|
||||
let skip_look_event = false;
|
||||
// the browser let go of the mouse while the game wanted it, like escape or switching windows
|
||||
let lost_pointer_lock = false;
|
||||
// the canvas had the pointer lock at the last pointerlockchange
|
||||
let 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
|
||||
let centering = Promise.resolve();
|
||||
let 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
|
||||
let ignore_look_until = 0;
|
||||
// what the game last asked for, it can change while grabbing waits
|
||||
let want_grab = false;
|
||||
|
||||
export function listen_to_page(page_canvas: HTMLCanvasElement) {
|
||||
canvas = page_canvas;
|
||||
@@ -84,7 +73,6 @@ export function listen_to_page(page_canvas: HTMLCanvasElement) {
|
||||
skip_look_event = false;
|
||||
return;
|
||||
}
|
||||
if (performance.now() < ignore_look_until) return;
|
||||
InputManager.look(e.movementX, e.movementY);
|
||||
} else {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
@@ -93,15 +81,7 @@ export function listen_to_page(page_canvas: HTMLCanvasElement) {
|
||||
});
|
||||
|
||||
document.addEventListener("pointerlockchange", () => {
|
||||
const locked = document.pointerLockElement === canvas;
|
||||
if (was_locked && !locked) {
|
||||
ignore_look_until = Infinity;
|
||||
centering = center_desktop_mouse().then(() => {
|
||||
ignore_look_until = performance.now() + CENTERING_SETTLE_MS;
|
||||
});
|
||||
}
|
||||
was_locked = locked;
|
||||
skip_look_event = locked;
|
||||
skip_look_event = document.pointerLockElement === canvas;
|
||||
self.setTimeout(() => {
|
||||
const grab = document.pointerLockElement === canvas;
|
||||
if (!grab) {
|
||||
@@ -140,7 +120,6 @@ function is_shortcut(e: KeyboardEvent) {
|
||||
|
||||
export async function set_mouse_grabbed(grab: boolean) {
|
||||
pointer_lock_flag = grab;
|
||||
want_grab = grab;
|
||||
// a retry to grab it again mustn't happen once it shouldn't be grabbed
|
||||
if (!grab && mouse_ungrab_timeout !== -1) {
|
||||
self.clearTimeout(mouse_ungrab_timeout);
|
||||
@@ -151,15 +130,6 @@ export async function set_mouse_grabbed(grab: boolean) {
|
||||
if (grab ? locked : !locked && !pointer_lock_waiting) {
|
||||
return;
|
||||
}
|
||||
if (grab) {
|
||||
// called every frame, once is enough
|
||||
if (awaiting_centering) return;
|
||||
awaiting_centering = true;
|
||||
await centering;
|
||||
awaiting_centering = false;
|
||||
// a gui opened again meanwhile
|
||||
if (!want_grab || document.pointerLockElement === canvas) return;
|
||||
}
|
||||
const t = performance.now();
|
||||
if (grab) {
|
||||
InputManager.mouse_x = -1;
|
||||
@@ -204,15 +174,3 @@ export function toggle_fullscreen() {
|
||||
document.exitFullscreen();
|
||||
}
|
||||
}
|
||||
|
||||
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