All files / ts/controller/input/types inputTypeKeyboard.ts

100% Statements 31/31
100% Branches 10/10
100% Functions 11/11
100% Lines 27/27

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 53 54 55 56 57 58 59 60 61 62 63 64 65  1x                 1x           15x 15x 15x       14x 14x 14x       1x 1x       5x 5x 5x       5x 5x 5x       10x 10x 4x   6x         12x 6x 6x 6x 6x 4x   2x      
import { IDestroyable } from "../../../environment/objectStates.js";
import { IInputType, InputType } from "./inputType.js";
import { IInputReceiver } from "../inputReceiver.js";
import { IEntityInput } from "../../../components/entities/entityInput.js";
import { IEntity } from "../../../components/entities/entity.js";
import { IScene } from "../../../components/scene.js";
import { IEngineCore } from "../../../engineCore.js";
import { EventKey } from "../events/eventKey.js";
 
export interface IInputTypeKeyboard extends IInputType, IDestroyable { }
export class InputTypeKeyboard extends InputType implements IInputTypeKeyboard {
 
    private readonly _keyUp: (e: Event) => void;
    private readonly _keyDown: (e: Event) => void;
 
    public constructor() {
        super();
        this._keyUp = (e: Event): void => this.onKeyboardEventUp(<EventKey>e);
        this._keyDown = (e: Event): void => this.onKeyboardEventDown(<EventKey>e);
    }
 
    public override initialize(engineCore: IEngineCore, inputReceiver: IInputReceiver): void {
        super.initialize(engineCore, inputReceiver);
        this.inputReceiver.addEventListener("keydown", this._keyDown);
        this.inputReceiver.addEventListener("keyup", this._keyUp);
    }
 
    public destroy(): void {
        this.inputReceiver.removeEventListener("keydown", this._keyDown);
        this.inputReceiver.removeEventListener("keyup", this._keyUp);
    }
 
    protected onKeyboardEventDown(event: EventKey): void {
        const scenes = this.engineCore.children;
        const action = (inputEntity: IEntityInput): void => inputEntity.keyDown(event);
        this.updateScenes(scenes, action);
    }
 
    protected onKeyboardEventUp(event: EventKey): void {
        const scenes = this.engineCore.children;
        const action = (inputEntity: IEntityInput): void => inputEntity.keyUp(event);
        this.updateScenes(scenes, action);
    }
 
    private updateScenes(scenes: ReadonlyArray<IScene>, action: (inputEntity: IEntityInput) => void): void {
        for (const scene of scenes) {
            if (!scene.isVisible || !scene.isEnabled)
                continue;
 
            this.updateEntities(scene.children, action);
        }
    }
 
    private updateEntities(entities: ReadonlyArray<IEntity>, action: (inputEntity: IEntityInput) => void): void {
        for (const entity of entities) {
            const inputEntity = entity as IEntityInput;
            const receivePointerInput = inputEntity.receiveKeyboardInput ?? false;
            this.updateEntities(entity.children, action);
            if (!receivePointerInput)
                continue;
 
            action(inputEntity);
        }
    }
}