import merge from 'lodash/merge'; import { BaseModel } from './base-model'; import { Servicer } from '../servicer/servicer'; import { Source } from './source'; import { MessageType } from '../core/message-type'; import { Filter } from './filter'; import { MixerType } from '../enums/mixer-type'; import { FilterType } from '../enums/filter-type'; const commands = MessageType.layer; interface Mixer { type: MixerType; }; interface Animation { type: FilterType; progress: number; }; export class Layer extends BaseModel { // 数据源 public source?: Source; // 滤镜列表 public filters: Array // 开始时间 public startTime = 0; // 持续时间 public duration = Infinity; public zOrder = 0; // 不透明度 public opacity = 1; // 混合模式 public mixer: Mixer = { type: MixerType.NORMAL }; // 入场动画 public enterAnimation: Animation | null = null; // 出场动画 public exitAnimation: Animation | null = null; // 滤镜强度 public intensity: number = 1; constructor( id: string, servicer: Servicer ) { super(id, servicer); this.filters = []; } async destory() { await this.servicer.do(MessageType.layer.destory, [this.id]); } /** * 设置图层数据源 * @param source - 数据源 */ async setSource(source: Source) { await this.servicer.do(commands.setSource, [this.id, source.id]); this.source = source; } /** * 设置滤镜时间段,不在时间周期内时不作渲染,默认全程渲染 * @param startTime * @param duration */ async setTimeRange(startTime: number, duration: number) { await this.servicer.do(commands.setTimeRange, [this.id, startTime, duration]); this.startTime = startTime; this.duration = duration; } /** * 设置图层顺序 * @param zOrder - 越高的图层在越上面 */ async setZOrder(zOrder: number) { await this.servicer.do(commands.setZOrder, [this.id, zOrder]); this.zOrder = zOrder; } /** * 设置不透明度 * @param opacity - 不透明度 */ async setOpacity(opacity: number) { await this.servicer.do(commands.setOpacity, [this.id, opacity]); this.opacity = opacity; } /** * 平移图层 * @param x - x 轴平移量 * @param y - y 轴平移量 * @param z - z 轴平移量 */ async translate(x: number, y: number, z: number) { await this.servicer.do(commands.translate, [this.id, x, y, z]); } /** * 缩放图层 * @param x - x 轴缩放比 * @param y - y 轴缩放比 * @param z - z 轴缩放比 */ async scale(x: number, y: number, z: number) { await this.servicer.do(commands.scale, [this.id, x, y, z]); } /** * 旋转图层 * @param x - x 轴旋转角度 * @param y - y 轴旋转角度 * @param z - z 轴旋转角度 */ async rotate(x: number, y: number, z: number) { await this.servicer.do(commands.rotate, [this.id, x, y, z]); } /** * 设置图层混合类型 * @param type - 混合类型 */ async setMixerType(type: MixerType) { await this.servicer.do(commands.setMixerType, [this.id, type]); this.mixer.type = type; } /** * 设置混合模式的参数 * @param name - 参数名 * @param value - 参数值 */ async updateMixerValue(name: string, value: number) { await this.servicer.do(commands.updateMixerValue, [this.id, name, value]); } /** * 设置图层入场动画 * @param type - 动画类型 */ async setEnterAnimation(type: FilterType) { await this.servicer.do(commands.setEnterAnimation, [this.id, type]); this.enterAnimation = merge({ progress: 0 }, this.enterAnimation, { type: type }); } /** * 更新进场动画进度 * @param progress - 进度 [0.0, 1.0] */ async updateEnterAnimationProgress(progress: number) { await this.servicer.do(commands.updateEnterAnimationProgress, [this.id, progress]); this.enterAnimation = merge({ progress: 0 }, this.enterAnimation, { progress }); } /** * 设置图层出场动画 * @param type - 动画类型 */ async setExitAnimation(type: any) { await this.servicer.do(commands.setExitAnimation, [this.id, type]); this.exitAnimation = merge({ progress: 0 }, this.exitAnimation, { type: type }); } /** * 更新出场动画进度 * @param progress - 进度 [0.0, 1.0] */ async updateExitAnimationProgress(progress: number) { await this.servicer.do(commands.updateExitAnimationProgress, [this.id, progress]); this.exitAnimation = merge({ progress: 0 }, this.exitAnimation, { progress }); } /** * 添加图层的叠加效果(滤镜) * @param filter - 叠加效果(滤镜) * @param wrapPixelAround 是否重复边缘像素,[是]则在transform之前叠加效果,[否]则在transform之后叠加效果 */ async addEffect(filter: Filter, wrapPixelAround: boolean = true) { if(this.filters.includes(filter)) { return; } await this.servicer.do(commands.addEffect, [this.id, filter.id, wrapPixelAround]); this.filters.push(filter); } /** * 移除指定效果(滤镜) * @param filter - 叠加效果(滤镜) */ async removeEffect(filter: Filter) { const index = this.filters.indexOf(filter); if(index >= 0) { await this.servicer.do(commands.removeEffect, [this.id, filter.id]); this.filters.splice(index, 1); } } /** * 清空所有特效列表 */ async clearEffects() { await this.servicer.do(commands.clearEffects, [this.id]); this.filters = []; } /** * 更新图层最终效果强度 * @param intensity 强度,[0.0, 1.0] */ async updateEffectIntensity(intensity: number) { await this.servicer.do(commands.updateEffectIntensity, [this.id, intensity]); this.intensity = intensity; } }