# Code coverage report for src/animation/TrackClip.js

# all files / src/animation/ TrackClip.js

20.69% Statements 18/87
0% Branches 0/18
0% Functions 0/14
20.69% Lines 18/87

    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

    1×
    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     import Clip from './Clip';

    import glMatrix from '../dep/glmatrix';
    var quat = glMatrix.quat;
    var vec3 = glMatrix.vec3;

    /**
     *
     * Animation clip that manage a collection of {@link clay.animation.SamplerTrack}
     * @constructor
     * @alias clay.animation.TrackClip
     *
     * @extends clay.animation.Clip
     * @param {Object} [opts]
     * @param {string} [opts.name]
     * @param {Object} [opts.target]
     * @param {number} [opts.life]
     * @param {number} [opts.delay]
     * @param {number} [opts.gap]
     * @param {number} [opts.playbackRatio]
     * @param {boolean|number} [opts.loop] If loop is a number, it indicate the loop count of animation
     * @param {string|Function} [opts.easing]
     * @param {Function} [opts.onframe]
     * @param {Function} [opts.onfinish]
     * @param {Function} [opts.onrestart]
     * @param {Array.<clay.animation.SamplerTrack>} [opts.tracks]
     */
    var TrackClip = function (opts) {

        opts = opts || {};

        Clip.call(this, opts);

        /**
         *
         * @type {clay.animation.SamplerTrack[]}
         */
        this.tracks = opts.tracks || [];

        this.calcLifeFromTracks();
    };

    TrackClip.prototype = Object.create(Clip.prototype);

    TrackClip.prototype.constructor = TrackClip;

    TrackClip.prototype.step = function (time, dTime, silent) {

        var ret = Clip.prototype.step.call(this, time, dTime, true);

        if (ret !== 'finish') {
            var time = this.getElapsedTime();
            // TODO life may be changed.
            if (this._range) {
                time = this._range[0] + time;
            }
            this.setTime(time);
        }

        // PENDING Schedule
        if (!silent && ret !== 'paused') {
            this.fire('frame');
        }

        return ret;
    };

    /**
     * @param {Array.<number>} range
     */
    TrackClip.prototype.setRange = function (range) {
        this.calcLifeFromTracks();
        this._range = range;
        if (range) {
            range[1] = Math.min(range[1], this.life);
            range[0] = Math.min(range[0], this.life);
            this.life = (range[1] - range[0]);
        }
    };

    TrackClip.prototype.setTime = function (time) {
        for (var i = 0; i < this.tracks.length; i++) {
            this.tracks[i].setTime(time);
        }
    };

    TrackClip.prototype.calcLifeFromTracks = function () {
        this.life = 0;
        for (var i = 0; i < this.tracks.length; i++) {
            this.life = Math.max(this.life, this.tracks[i].getMaxTime());
        }
    };

    /**
     * @param {clay.animation.SamplerTrack} track
     */
    TrackClip.prototype.addTrack = function (track) {
        this.tracks.push(track);
        this.calcLifeFromTracks();
    };

    /**
     * @param {clay.animation.SamplerTrack} track
     */
    TrackClip.prototype.removeTarck = function (track) {
        var idx = this.tracks.indexOf(track);
        if (idx >= 0) {
            this.tracks.splice(idx, 1);
        }
    };

    /**
     * @param {number} startTime
     * @param {number} endTime
     * @param {boolean} isLoop
     * @return {clay.animation.TrackClip}
     */
    TrackClip.prototype.getSubClip = function (startTime, endTime, isLoop) {
        var subClip = new TrackClip({
            name: this.name
        });

        for (var i = 0; i < this.tracks.length; i++) {
            var subTrack = this.tracks[i].getSubTrack(startTime, endTime);
            subClip.addTrack(subTrack);
        }

        if (isLoop !== undefined) {
            subClip.setLoop(isLoop);
        }

        subClip.life = endTime - startTime;

        return subClip;
    };

    /**
     * 1d blending from two skinning clips
     * @param  {clay.animation.TrackClip} clip1
     * @param  {clay.animation.TrackClip} clip2
     * @param  {number} w
     */
    TrackClip.prototype.blend1D = function (clip1, clip2, w) {
        for (var i = 0; i < this.tracks.length; i++) {
            var c1 = clip1.tracks[i];
            var c2 = clip2.tracks[i];
            var tClip = this.tracks[i];

            tClip.blend1D(c1, c2, w);
        }
    };

    /**
     * Additive blending from two skinning clips
     * @param  {clay.animation.TrackClip} clip1
     * @param  {clay.animation.TrackClip} clip2
     */
    TrackClip.prototype.additiveBlend = function (clip1, clip2) {
        for (var i = 0; i < this.tracks.length; i++) {
            var c1 = clip1.tracks[i];
            var c2 = clip2.tracks[i];
            var tClip = this.tracks[i];

            tClip.additiveBlend(c1, c2);
        }
    };

    /**
     * Subtractive blending from two skinning clips
     * @param  {clay.animation.TrackClip} clip1
     * @param  {clay.animation.TrackClip} clip2
     */
    TrackClip.prototype.subtractiveBlend = function (clip1, clip2) {
        for (var i = 0; i < this.tracks.length; i++) {
            var c1 = clip1.tracks[i];
            var c2 = clip2.tracks[i];
            var tClip = this.tracks[i];

            tClip.subtractiveBlend(c1, c2);
        }
    };

    /**
     * 2D blending from three skinning clips
     * @param  {clay.animation.TrackClip} clip1
     * @param  {clay.animation.TrackClip} clip2
     * @param  {clay.animation.TrackClip} clip3
     * @param  {number} f
     * @param  {number} g
     */
    TrackClip.prototype.blend2D = function (clip1, clip2, clip3, f, g) {
        for (var i = 0; i < this.tracks.length; i++) {
            var c1 = clip1.tracks[i];
            var c2 = clip2.tracks[i];
            var c3 = clip3.tracks[i];
            var tClip = this.tracks[i];

            tClip.blend2D(c1, c2, c3, f, g);
        }
    };

    /**
     * Copy SRT of all joints clips from another TrackClip
     * @param  {clay.animation.TrackClip} clip
     */
    TrackClip.prototype.copy = function (clip) {
        for (var i = 0; i < this.tracks.length; i++) {
            var sTrack = clip.tracks[i];
            var tTrack = this.tracks[i];

            vec3.copy(tTrack.position, sTrack.position);
            vec3.copy(tTrack.scale, sTrack.scale);
            quat.copy(tTrack.rotation, sTrack.rotation);
        }
    };

    TrackClip.prototype.clone = function () {
        var clip = Clip.prototype.clone.call(this);
        for (var i = 0; i < this.tracks.length; i++) {
            clip.addTrack(this.tracks[i].clone());
        }
        clip.life = this.life;
        return clip;
    };

    export default TrackClip;