From 5ca939762151fc40c848cc8438e33ad5cee20dcf Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Sat, 15 Feb 2020 23:15:59 -0500 Subject: [PATCH] try new Input style --- src/Applet/Keyboard.ts | 6 +++++- src/Game/Input.ts | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/Game/Input.ts diff --git a/src/Applet/Keyboard.ts b/src/Applet/Keyboard.ts index e41248f..59d1e29 100644 --- a/src/Applet/Keyboard.ts +++ b/src/Applet/Keyboard.ts @@ -12,6 +12,9 @@ export interface KeyHandler { /// A key has been released release?(key: KeyName): void, + /// A key has been pressed or released + update?(key: KeyName, state: "press" | "release"): void, + /// You are receiving control now, perhaps indicate this in the UI activate?(): void, @@ -52,7 +55,7 @@ export class KeyControl { this.dispatch(evt, "press"); }; private blur = (evt: FocusEvent) => { - this.handler && this.handler.block && this.handler.block(); + this.handler?.block?.(); }; constructor(private element: HTMLElement, tabindex: number = -1) { @@ -85,6 +88,7 @@ export class KeyControl { } else if(state == "release" && this.handler.release) { this.handler.release(name); } + this.handler.update?.(name, state); } }; diff --git a/src/Game/Input.ts b/src/Game/Input.ts new file mode 100644 index 0000000..c08cd4a --- /dev/null +++ b/src/Game/Input.ts @@ -0,0 +1,23 @@ +import { KeyHandler, KeyName } from "../Applet/Keyboard"; + +export class Buttons implements KeyHandler { + + keys: KeyName[] = []; + + public update(key: KeyName, state: "press" | "release") { + const newKeys = this.keys.filter(k => k != key); + if(state == "press") { + newKeys.push(key); + newKeys.sort(); + } + this.keys = newKeys; + } + + public block() { + this.keys = []; + } + + public getPressed(): KeyName[] { + return this.keys.slice(); + } +}