From d11e903cbf2c47c042e73a125f583b44ec959cc3 Mon Sep 17 00:00:00 2001 From: Tom Aisthorpe Date: Fri, 5 Jul 2024 18:28:42 +0100 Subject: [PATCH] feat(input): reset axis values on window blur --- .changeset/curvy-numbers-eat.md | 5 ++++ packages/ted/src/input/controller.ts | 36 +++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 .changeset/curvy-numbers-eat.md diff --git a/.changeset/curvy-numbers-eat.md b/.changeset/curvy-numbers-eat.md new file mode 100644 index 0000000..edc83c6 --- /dev/null +++ b/.changeset/curvy-numbers-eat.md @@ -0,0 +1,5 @@ +--- +'@tedengine/ted': minor +--- + +Reset controller axis values on window blur diff --git a/packages/ted/src/input/controller.ts b/packages/ted/src/input/controller.ts index 1eac081..0fa1ca4 100644 --- a/packages/ted/src/input/controller.ts +++ b/packages/ted/src/input/controller.ts @@ -1,5 +1,7 @@ import TEventQueue from '../core/event-queue'; import type TPawn from '../core/pawn'; +import type { TWindowBlurEvent } from '../fred/events'; +import { TEventTypesWindow } from '../fred/events'; import { TEventTypesInput } from './events'; import type { TMouseLocation, TMouseMoveEvent } from './events'; import type { @@ -25,7 +27,16 @@ export default class TController { public mouseLocation?: TMouseLocation; - constructor(private engineEventQueue: TEventQueue) {} + constructor(private engineEventQueue: TEventQueue) { + this.resetAxisValues = this.resetAxisValues.bind(this); + + // Reset all axis values when the window loses focus + // This is to prevent axis getting stuck in a pressed state + engineEventQueue.addListener( + TEventTypesWindow.Blur, + this.resetAxisValues, + ); + } // @todo add validation on button // @todo add support for different event types @@ -160,4 +171,27 @@ export default class TController { public update() { this.events.update(); } + + /** + * Resets all current axis values to 0. + */ + private resetAxisValues() { + Object.keys(this.axes).forEach((key) => { + this.axes[key] = 0; + }); + + console.log('reset axis values'); + } + + /** + * Tears down the controller and removes all event listeners. + * + * @todo add rest of event listeners + */ + public destroy() { + this.engineEventQueue.removeListener( + TEventTypesWindow.Blur, + this.resetAxisValues, + ); + } }