try new Input style

This commit is contained in:
Tangent Wantwight 2020-02-15 23:15:59 -05:00
parent c77499a577
commit 5ca9397621
2 changed files with 28 additions and 1 deletions

View file

@ -12,6 +12,9 @@ export interface KeyHandler {
/// A key has been released /// A key has been released
release?(key: KeyName): void, 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 /// You are receiving control now, perhaps indicate this in the UI
activate?(): void, activate?(): void,
@ -52,7 +55,7 @@ export class KeyControl {
this.dispatch(evt, "press"); this.dispatch(evt, "press");
}; };
private blur = (evt: FocusEvent) => { private blur = (evt: FocusEvent) => {
this.handler && this.handler.block && this.handler.block(); this.handler?.block?.();
}; };
constructor(private element: HTMLElement, tabindex: number = -1) { constructor(private element: HTMLElement, tabindex: number = -1) {
@ -85,6 +88,7 @@ export class KeyControl {
} else if(state == "release" && this.handler.release) { } else if(state == "release" && this.handler.release) {
this.handler.release(name); this.handler.release(name);
} }
this.handler.update?.(name, state);
} }
}; };

23
src/Game/Input.ts Normal file
View file

@ -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();
}
}