Skip to content

Commit

Permalink
Refactor pointerLock
Browse files Browse the repository at this point in the history
  • Loading branch information
caiiiycuk committed Jul 4, 2024
1 parent 004fc5f commit 6675f7e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 42 deletions.
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
// url: "https://br.cdn.dos.zone/ipx/doom_dm/doom_dm.jsdos",
// url: "https://cdn.dos.zone/original/2X/b/b4b5275904d86a4ab8a20917b2b7e34f0df47bf7.jsdos", // dhry2
// url: "https://cdn.dos.zone/original/2X/7/744842062905f72648a4d492ccc2526d039b3702.jsdos", // sim-city
// url: "https://cdn.dos.zone/custom/dos/war2.jsdos", // warcraft-2
// dosboxConf: diabloConf,
// loginUrl: "http://localhost:8080/subscription.html",
// autoStart: true,
Expand All @@ -49,9 +50,10 @@
// name: "makevm",
// host: "backend.make-vm.com:8001",
// },
onEvent: (event) => {
onEvent: (event, ci) => {
console.log("Event", event)
if (event === "ci-ready") {
window.ci = ci;
// props.setFrame("network");
// props.setFullScreen(true);
}
Expand Down
18 changes: 17 additions & 1 deletion src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,20 @@
background: black;
z-index: 999;
}
}
}

.jsdos-rso {
* {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;

-ms-touch-action: none;
-ms-content-zooming: none;
touch-action: none;
outline: none;
}
}
10 changes: 8 additions & 2 deletions src/window/dos/controls/mouse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommandInterface } from "emulators";
import { mouseCapture } from "./mouse/mouse-locked";
import { mousePointerLock } from "./mouse/mouse-locked";
import { mouseDefault } from "./mouse/mouse-not-locked";
import { mouseSwipe } from "./mouse/mouse-swipe";
import { pointer } from "./mouse/pointer";
Expand All @@ -14,7 +14,13 @@ export function mouse(lock: boolean,
}

if (lock) {
return mouseCapture(sensitivity, pointerButton, el, ci);
const unlock = mousePointerLock(el);
const umount = mouseSwipe(sensitivity, pointerButton, el, ci);

return () => {
umount();
unlock();
};
}

return mouseDefault(pointerButton, el, ci);
Expand Down
48 changes: 13 additions & 35 deletions src/window/dos/controls/mouse/mouse-locked.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import { CommandInterface } from "emulators";
import { mount } from "./mount";
import { pointer } from "./pointer";

export function mouseCapture(sensitivity: number,
pointerButton: number,
el: HTMLElement,
ci: CommandInterface) {
function isNotLocked() {
return document.pointerLockElement !== el;
}

function onMouseDown(x: number, y: number, button: number) {
if (isNotLocked()) {
export function mousePointerLock(el: HTMLElement) {
function requestLock() {
if (document.pointerLockElement !== el) {
const requestPointerLock = el.requestPointerLock ||
(el as any).mozRequestPointerLock ||
(el as any).webkitRequestPointerLock;
Expand All @@ -19,33 +11,19 @@ export function mouseCapture(sensitivity: number,

return;
}

ci.sendMouseButton(button, true);
}

function onMouseUp(x: number, y: number, button: number) {
if (isNotLocked()) {
return;
}
const options = {
capture: true,
};

ci.sendMouseButton(button, false);
for (const next of pointer.starters) {
el.addEventListener(next, requestLock, options);
}

function onMouseMove(x: number, y: number, mX: number, mY: number) {
if (isNotLocked()) {
return;
}

if (mX === 0 && mY === 0) {
return;
return () => {
for (const next of pointer.starters) {
el.removeEventListener(next, requestLock, options);
}

(ci as any).sendMouseRelativeMotion(mX * sensitivity, mY * sensitivity);
}

function onMouseLeave(x: number, y: number) {
// nothing to do
}

return mount(el, pointerButton, onMouseDown, onMouseMove, onMouseUp, onMouseLeave);
};
}
1 change: 0 additions & 1 deletion src/window/dos/controls/mouse/mouse-swipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export function mouseSwipe(sensitivity: number,
}

acc += Math.abs(mX) + Math.abs(mY);

(ci as any).sendMouseRelativeMotion(mX * sensitivity * 2, mY * sensitivity * 2);
}

Expand Down
5 changes: 3 additions & 2 deletions src/window/dos/dos-window.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ export function DosWindow(props: {
console.log("wsServer:", version, " expected:", actualWsVersion);
}), { token });
}
}
};

return (emulators as any)[backend + (worker ? "Worker" : "Direct")](bundles, {
return (emulators as any)[((backend !== "dosbox" && backend !== "dosboxX") ? "dosbox" : backend) +
(worker ? "Worker" : "Direct")](bundles, {
token,
});
})();
Expand Down

0 comments on commit 6675f7e

Please sign in to comment.