All files / ts/controller/input inputController.ts

100% Statements 18/18
100% Branches 2/2
100% Functions 9/9
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 531x                     1x             10x 10x 10x       11x 11x       4x       2x 2x 1x 1x         2x 2x       1x       1x    
import { IRefreshable, IDestroyable, tryRefresh, tryDestroy } from "../../environment/objectStates.js";
import { IInputReceiver } from "./inputReceiver.js";
import { IEngineCore } from "../../engineCore.js";
import { IInputType } from "./types/inputType.js";
 
export interface IInputController extends IRefreshable, IDestroyable {
    add(inputName: string, inputType: IInputType): void;
    get<T extends IInputType>(inputName: string): T | undefined;
    remove(inputName: string): void;
    clear(): void;
}
export class InputController implements IInputController {
 
    private readonly _engineCore: IEngineCore;
    private readonly _inputReceiver: IInputReceiver;
    private readonly _inputTypes: Map<string, IInputType>;
 
    public constructor(engineCore: IEngineCore, inputReceiver: IInputReceiver) {
        this._engineCore = engineCore;
        this._inputReceiver = inputReceiver;
        this._inputTypes = new Map<string, IInputType>();
    }
 
    public add(inputName: string, inputType: IInputType): void {
        this._inputTypes.set(inputName, inputType);
        inputType.initialize(this._engineCore, this._inputReceiver);
    }
 
    public get<T extends IInputType>(inputName: string): T | undefined {
        return this._inputTypes.get(inputName) as T;
    }
 
    public remove(inputName: string): void {
        const type = this._inputTypes.get(inputName);
        if (type) {
            tryDestroy(type);
            this._inputTypes.delete(inputName);
        }
    }
 
    public clear(): void {
        this._inputTypes.forEach(type => tryDestroy(type));
        this._inputTypes.clear();
    }
 
    public refresh(): void {
        this._inputTypes.forEach(type => tryRefresh(type));
    }
 
    public destroy(): void {
        this.clear();
    }
}