All files / core/handlers engine-handler.ts

100% Statements 123/123
95.65% Branches 22/23
100% Functions 10/10
100% Lines 123/123

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 1231x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 14x 14x 14x 14x 4x 4x 4x 4x 4x 7x 1x 1x 6x 6x 6x 4x 4x 4x 4x 4x 4x 1x 1x 3x 3x 3x 3x 4x 4x 4x 4x 4x 4x 2x 1x 1x 1x 1x 4x 4x 4x 4x 4x 2x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 1x 1x 1x 4x 4x 4x 4x 4x 4x 5x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 4x 4x 1x 1x
import { illusionService } from "../illusion-service";
import { viewTargetHandler } from "./view-target-handler";
import { layerHandler } from "./layer-handler";
import { EngineStatus } from "../../enums/engine-status";
 
 
export class EngineHandler {
    layers: Set<string>
    targetId: string;
    status: EngineStatus;
 
    constructor() {
        this.layers = new Set();
        this.targetId = '';
        this.status = EngineStatus.Shutdown;
    }
 
    /**
     * 初始化渲染引擎
     */
    init(workDir: string, enableLog: boolean = false): void {
        if(this.status !== EngineStatus.Shutdown) {
            throw new Error(`invalid engine status: ${this.status}`);
        }
        illusionService.module.engineInit(workDir, enableLog);
        this.status = EngineStatus.Stop;
    }
 
    /**
     * 启动渲染引擎
     */
    prepare(targetId: string): void {
        if(this.status !== EngineStatus.Stop) {
            throw new Error(`invalid engine status: ${this.status}`);
        }
        this.setTarget(targetId);
        illusionService.module.enginePrepare();
        this.status = EngineStatus.Running;
    }
 
    /**
     * 渲染一帧
     * @param clock - 帧时间
     */
    submit(clock: number): void {
        if(this.status !== EngineStatus.Running) {
            throw new Error(`invalid engine status: ${this.status}`);
        }
        illusionService.module.engineSubmit(clock);
    }
 
    /**
     * 关闭渲染引擎
     */
    stop(): void {
        if(this.status !== EngineStatus.Running) {
            throw new Error(`invalid engine status: ${this.status}`);
        }
        illusionService.module.engineStop();
        this.status = EngineStatus.Stop;
    }
 
    /**
     * 销毁引擎
     */
    shutdown() {
        illusionService.module.engineShutdown();
        this.status = EngineStatus.Shutdown;
    }
 
    /**
     * 设置渲染目标
     * @param targetId - 渲染目标 id
     */
    setTarget(targetId: string): void {
        if(targetId === this.targetId) {
            return;
        }
 
        const target = viewTargetHandler.getViewTarget(targetId);
        illusionService.module.engineSetTarget(target);
        this.targetId = targetId;
    }
 
    /**
     * 添加图层
     * @param layerId - 图层 id
     */
    addLayer(layerId: string) {
        if(this.layers.has(layerId)) {
            return ;
        }
 
        const layer = layerHandler.getLayer(layerId);
        illusionService.module.engineAddLayer(layer.cLayer);
        this.layers.add(layerId);
    }
 
    /**
     * 移除图层
     * @param layerId - 图层 id
     */
    removeLayer(layerId: string) {
        if(this.layers.has(layerId)) {
            const layer = layerHandler.getLayer(layerId);
            illusionService.module.engineRemoveLayer(layer.cLayer);
            this.layers.delete(layerId);
        }
    }
 
    /**
     * 注册离屏画布
     * @param id - 画布 id
     * @param offscreenCanvas - 离屏画布实例
     */
    registerOffscreenCanvas(id: string, offscreenCanvas: OffscreenCanvas) {
        const module = illusionService.module;
        module.GL.offscreenCanvases[id] = offscreenCanvas;
    }
 
}
 
export const engineHandler = new EngineHandler();