Skip to content

Commit

Permalink
added pause when mouse leaves canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
BarthPaleologue committed Aug 3, 2023
1 parent 2b23167 commit 85615b9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/ts/controller/inputs/mouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ export class Mouse implements Input {
deadAreaRadius = 100;
private canvas: HTMLCanvasElement;

private onMouseEnterListeners: (() => void)[] = [];
private onMouseLeaveListeners: (() => void)[] = [];

constructor(canvas: HTMLCanvasElement, deadAreaRadius = 50) {
this.deadAreaRadius = deadAreaRadius;
this.canvas = canvas;

window.addEventListener("mousemove", (e) => {
document.addEventListener("mousemove", (e) => {
this.dx = (e.x - this.x) / this.canvas.width;
this.dy = (e.y - this.y) / this.canvas.height;

Expand All @@ -29,6 +32,20 @@ export class Mouse implements Input {
this.dxToCenter = e.x - this.canvas.width / 2;
this.dyToCenter = e.y - this.canvas.height / 2;
});

document.addEventListener("mouseenter", () => {
this.onMouseEnterListeners.forEach((listener) => listener());
});
document.addEventListener("mouseleave", () => {
this.onMouseLeaveListeners.forEach((listener) => listener());
});
}

addOnMouseEnterListener(listener: () => void) {
this.onMouseEnterListeners.push(listener);
}
addOnMouseLeaveListener(listener: () => void) {
this.onMouseLeaveListeners.push(listener);
}

getRoll() {
Expand Down
17 changes: 17 additions & 0 deletions src/ts/controller/planetEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import { SystemUI } from "../ui/systemUI";
import { BlackHole } from "../view/bodies/stellarObjects/blackHole";
import { ShipController } from "../spaceship/shipController";

enum EngineState {
RUNNING,
PAUSED
}

export class PlanetEngine {
// UI
private readonly helmetOverlay: HelmetOverlay;
Expand All @@ -50,6 +55,8 @@ export class PlanetEngine {

private readonly collisionWorker = new CollisionWorker();

private state = EngineState.RUNNING;

constructor() {
this.helmetOverlay = new HelmetOverlay();
this.bodyEditor = new BodyEditor();
Expand Down Expand Up @@ -91,6 +98,14 @@ export class PlanetEngine {
});
}

pause(): void {
this.state = EngineState.PAUSED;
}

resume(): void {
this.state = EngineState.RUNNING;
}

/**
* Toggles the star map
* @throws Error if the star map is null
Expand Down Expand Up @@ -151,6 +166,8 @@ export class PlanetEngine {
});

this.starSystemScene.registerBeforeRender(() => {
if (this.state === EngineState.PAUSED) return;

const starSystemScene = this.getStarSystemScene();
const starSystem = this.getStarSystem();
const activeController = starSystemScene.getActiveController();
Expand Down
7 changes: 7 additions & 0 deletions src/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ spaceshipController.addInput(keyboard);
spaceshipController.addInput(gamepad);
spaceshipController.addInput(mouse);

mouse.addOnMouseEnterListener(() => {
if (scene.getActiveController() === spaceshipController) engine.resume();
});
mouse.addOnMouseLeaveListener(() => {
if (scene.getActiveController() === spaceshipController) engine.pause();
});

scene.setActiveController(spaceshipController);

engine.registerStarSystemUpdateCallback(() => {
Expand Down

0 comments on commit 85615b9

Please sign in to comment.