73 lines
1.9 KiB
TypeScript
73 lines
1.9 KiB
TypeScript
import { EventEmitter } from "@angular/core";
|
|
import { InputScheme } from "./input-scheme";
|
|
import { InputAction } from "./input-action";
|
|
import { InputService } from "../services/input.service";
|
|
|
|
export interface InputKeyState {
|
|
key: string | number;
|
|
value: number;
|
|
angle?: number;
|
|
}
|
|
|
|
export abstract class InputDevice {
|
|
|
|
protected scheme?: InputScheme;
|
|
protected keyStates: InputKeyState[] = [];
|
|
public keyStateChanged = new EventEmitter<InputKeyState>();
|
|
public static KeyEventSafeModeEnabled = false;
|
|
|
|
public static get IsKeyEventSafeModeEnabled() {
|
|
if ((window as any).KeyEventSafeModeEnabled === true) {
|
|
return true;
|
|
}
|
|
if ((window as any).KeyEventSafeModeEnabled === false) {
|
|
return false;
|
|
}
|
|
|
|
return InputDevice.KeyEventSafeModeEnabled;
|
|
}
|
|
|
|
constructor(
|
|
public readonly name: string,
|
|
public readonly type = 'unknown',
|
|
) {
|
|
}
|
|
|
|
public mapKey(action: InputAction, keys: (string | number)[]): this {
|
|
this.scheme?.mapKey(action, keys);
|
|
return this;
|
|
}
|
|
|
|
public getScheme(): InputScheme | undefined {
|
|
return this.scheme;
|
|
}
|
|
|
|
public hasScheme(): boolean {
|
|
return !!this.scheme;
|
|
}
|
|
|
|
public setKey(key: string | number, value: number = 1, angle: number = 0): this {
|
|
let exists = this.keyStates.find(k => k.key === key);
|
|
if (!exists) {
|
|
exists = {
|
|
key,
|
|
value,
|
|
angle,
|
|
};
|
|
this.keyStates.push(exists);
|
|
} else {
|
|
exists.value = value;
|
|
exists.angle = angle;
|
|
}
|
|
|
|
this.keyStateChanged.emit(exists);
|
|
this.scheme?.setKeyState(key, value, angle);
|
|
return this;
|
|
}
|
|
|
|
public assignScheme(scheme: InputScheme): this {
|
|
this.scheme = scheme;
|
|
return this;
|
|
}
|
|
}
|