handle errors within event handlers

This commit is contained in:
Reimar 2025-10-15 10:25:47 +02:00
parent 01e5a5c7e8
commit 09dda08f52

View File

@ -63,12 +63,22 @@ export class Gamelib {
keydownListener(ev) {
this.keysPressed.add(ev.key);
try {
this.keyPressHandlers.get(ev.key)?.();
} catch (e) {
this.console.error(e);
}
}
keyupListener(ev) {
this.keysPressed.delete(ev.key);
try {
this.keyReleaseHandlers.get(ev.key)?.();
} catch (e) {
this.console.error(e);
}
}
mousemoveListener(ev) {
@ -78,12 +88,16 @@ export class Gamelib {
this.mouseX = ev.offsetX * ratioX;
this.mouseY = ev.offsetY * ratioY;
try {
this.mouseMoveHandler?.(
ev.offsetX * ratioX,
ev.offsetY * ratioY,
ev.movementX * ratioX,
ev.movementY * ratioY,
);
} catch (e) {
this.console.error(e);
}
}
mousedownListener(ev) {
@ -91,7 +105,12 @@ export class Gamelib {
const ratioY = this.canvas.height / this.canvas.clientHeight;
this.mouseButtonsPressed.add(ev.button);
try {
this.mouseDownHandlers.get(ev.button)?.(ev.offsetX * ratioX, ev.offsetY * ratioY);
} catch (e) {
this.console.error(e);
}
}
mouseupListener(ev) {
@ -99,7 +118,12 @@ export class Gamelib {
const ratioY = this.canvas.height / this.canvas.clientHeight;
this.mouseButtonsPressed.delete(ev.button);
try {
this.mouseUpHandlers.get(ev.button)?.(ev.offsetX * ratioX, ev.offsetY * ratioY);
} catch (e) {
this.console.error(e);
}
}
contextMenuListener(ev) {