All files / src/model layer.ts

100% Statements 236/236
68.45% Branches 128/187
88.57% Functions 31/35
100% Lines 236/236

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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 2361x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 26x 26x 26x 26x 26x 26x 31x 31x 1x 1x 31x 31x 31x 31x 31x 31x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 1x 1x 1x 31x 31x 31x 31x 31x 31x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 1x 1x 31x 31x 31x 31x 31x 31x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 1x 1x 31x 31x 31x 31x 31x 31x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 7x 1x 1x 6x 6x 7x 7x 31x 31x 31x 31x 31x 31x 3x 3x 2x 3x 3x 3x 31x 31x 31x 31x 31x 1x 1x 1x 31x 31x 31x 31x 31x 31x 1x 1x 1x 31x
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<Filter>
    // 开始时间
    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;
    }
}