Skip to content

Commit

Permalink
feat(input): add isDown to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaisthorpe committed Oct 2, 2024
1 parent 1ad2c48 commit a97f15f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-windows-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tedengine/ted': minor
---

Add isDown to controllers
37 changes: 37 additions & 0 deletions packages/ted/src/input/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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<TKeyDownEvent>(
TEventTypesInput.KeyDown,
this.handleKeyDown,
);

this.inputEventQueue.addListener<TKeyUpEvent>(
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
Expand Down Expand Up @@ -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;
});
}

/**
Expand All @@ -231,5 +258,15 @@ export default class TController {
TEventTypesWindow.Blur,
this.resetAxisValues,
);

this.inputEventQueue.removeListener<TKeyDownEvent>(
TEventTypesInput.KeyDown,
this.handleKeyDown,
);

this.inputEventQueue.removeListener<TKeyUpEvent>(
TEventTypesInput.KeyUp,
this.handleKeyUp,
);
}
}

0 comments on commit a97f15f

Please sign in to comment.