From a97f15f568b8088cf122153bb50faf0589950f0e Mon Sep 17 00:00:00 2001 From: Tom Aisthorpe Date: Wed, 2 Oct 2024 21:11:40 +0100 Subject: [PATCH] feat(input): add isDown to controller --- .changeset/many-windows-divide.md | 5 ++++ packages/ted/src/input/controller.ts | 37 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 .changeset/many-windows-divide.md diff --git a/.changeset/many-windows-divide.md b/.changeset/many-windows-divide.md new file mode 100644 index 0000000..56ff276 --- /dev/null +++ b/.changeset/many-windows-divide.md @@ -0,0 +1,5 @@ +--- +'@tedengine/ted': minor +--- + +Add isDown to controllers diff --git a/packages/ted/src/input/controller.ts b/packages/ted/src/input/controller.ts index a5901ff..583a5f2 100644 --- a/packages/ted/src/input/controller.ts +++ b/packages/ted/src/input/controller.ts @@ -36,6 +36,8 @@ export default class TController { public mouseMovement?: TMouseMovement; public pointerLocked = false; + public isDown: { [key: string]: boolean } = {}; + constructor(private inputEventQueue: TEventQueue) { this.resetAxisValues = this.resetAxisValues.bind(this); @@ -45,6 +47,27 @@ export default class TController { TEventTypesWindow.Blur, this.resetAxisValues, ); + + this.handleKeyDown = this.handleKeyDown.bind(this); + this.handleKeyUp = this.handleKeyUp.bind(this); + + this.inputEventQueue.addListener( + TEventTypesInput.KeyDown, + this.handleKeyDown, + ); + + this.inputEventQueue.addListener( + TEventTypesInput.KeyUp, + this.handleKeyUp, + ); + } + + private handleKeyDown(e: TKeyDownEvent) { + this.isDown[e.subType] = true; + } + + private handleKeyUp(e: TKeyUpEvent) { + this.isDown[e.subType] = false; } // @todo add validation on button @@ -219,6 +242,10 @@ export default class TController { Object.keys(this.axes).forEach((key) => { this.axes[key] = 0; }); + + Object.keys(this.isDown).forEach((key) => { + this.isDown[key] = false; + }); } /** @@ -231,5 +258,15 @@ export default class TController { TEventTypesWindow.Blur, this.resetAxisValues, ); + + this.inputEventQueue.removeListener( + TEventTypesInput.KeyDown, + this.handleKeyDown, + ); + + this.inputEventQueue.removeListener( + TEventTypesInput.KeyUp, + this.handleKeyUp, + ); } }