import { getUUID } from '../../utils/get-uuid'; import { illusionService } from '../illusion-service'; import { FillType } from '../../enums/fill-type'; class Source { /** * @param cSource - 帧数据源实例 * @param imageDataPtr - 图片像素数据头指针地址 */ constructor(public cSource: CSource, public imageDataPtr: number) {} } /** * 帧数据源工具类 */ export class SourceHandler { map: Map; constructor() { this.map = new Map(); } /** * 构建底层帧数据源实例 * @returns 帧数据源 id */ create(): { id: string} { const id = getUUID(); const cSource = new illusionService.module.Source(); this.map.set(id, { cSource: cSource, imageDataPtr: -1 }); return { id }; } /** * 获取帧数据源 * @param id - 帧数据源 id */ getSource(id: string): Source { if(!this.map.has(id)) { throw new Error(`source: ${id} is not exist`); } return this.map.get(id)!; } /** * 设置帧数据源的像素数据 * @param id - 帧数据源 id * @param pixels - 像素数据 * @param width - 宽度 * @param height - 高度 */ setPixels(id: string, pixels: ArrayLike, width: number, height: number) { const { module } = illusionService; const source = this.getSource(id); if(source.imageDataPtr > 0) { module._free(source.imageDataPtr); } const ptr = module._malloc(pixels.length); module.HEAPU8.set(pixels, ptr); source.cSource.setPixels(ptr, width, height); source.imageDataPtr = ptr; } /** * 销毁帧数据源实例 * @param id - 帧数据源 id */ destory(id: string) { const source = this.getSource(id); if(source.imageDataPtr > 0) { illusionService.module._free(source.imageDataPtr); } source.cSource.delete(); this.map.delete(id); } /** * 设置填充方式 * @param id - 帧数据源 id * @param fillType - 填充方式 */ setFillType(id: string, fillType: FillType): void { const source = this.getSource(id); source.cSource.setFillType(fillType); } /** * 设置基于画布宽高与目标宽高比例的裁剪模式 * @param id - 帧数据源 id * @param ratio - 宽高比例 */ setClipRatio(id: string, ratio: number): void { const source = this.getSource(id); source.cSource.setClipRatio(ratio); } /** * 平移数据源 * @param id - 帧数据源 id * @param x - x 轴平移量 * @param y - y 轴平移量 * @param z - z 轴平移量 */ translate(id: string, x: number, y: number, z: number): void { const source = this.getSource(id); source.cSource.translate(x, y, z); } /** * 缩放数据源 * @param id - 帧数据源 id * @param x - x 轴缩放比 * @param y - y 轴缩放比 * @param z - z 轴缩放比 */ scale(id: string, x: number, y: number, z: number): void { const source = this.getSource(id); source.cSource.scale(x, y, z); } /** * 旋转数据源 * @param id - 帧数据源 id * @param x - x 轴旋转角度 * @param y - y 轴旋转角度 * @param z - z 轴旋转角度 */ rotate(id: string, x: number, y: number, z: number): void { const source = this.getSource(id); source.cSource.rotate(x, y, z); } /** * 数据变更时候需要调用该接口通知基类 * @param id - 帧数据源 id */ notifyRefresh(id: string): void { const source = this.getSource(id); source.cSource.notifyRefresh(); } } export const sourceHandler = new SourceHandler();