yzt
2023-05-05 4c558c77a6a9d23f057f094c4dc3e315eabef497
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
import clone from "../Core/clone.js";
import defaultValue from "../Core/defaultValue.js";
import defined from "../Core/defined.js";
import DeveloperError from "../Core/DeveloperError.js";
import EasingFunction from "../Core/EasingFunction.js";
import getTimestamp from "../Core/getTimestamp.js";
import TimeConstants from "../Core/TimeConstants.js";
import TweenJS from "../ThirdParty/Tween.js";
 
/**
 * A tween is an animation that interpolates the properties of two objects using an {@link EasingFunction}.  Create
 * one using {@link Scene#tweens} and {@link TweenCollection#add} and related add functions.
 *
 * @alias Tween
 * @constructor
 *
 * @private
 */
function Tween(
  tweens,
  tweenjs,
  startObject,
  stopObject,
  duration,
  delay,
  easingFunction,
  update,
  complete,
  cancel
) {
  this._tweens = tweens;
  this._tweenjs = tweenjs;
 
  this._startObject = clone(startObject);
  this._stopObject = clone(stopObject);
 
  this._duration = duration;
  this._delay = delay;
  this._easingFunction = easingFunction;
 
  this._update = update;
  this._complete = complete;
 
  /**
   * The callback to call if the tween is canceled either because {@link Tween#cancelTween}
   * was called or because the tween was removed from the collection.
   *
   * @type {TweenCollection.TweenCancelledCallback}
   */
  this.cancel = cancel;
 
  /**
   * @private
   */
  this.needsStart = true;
}
 
Object.defineProperties(Tween.prototype, {
  /**
   * An object with properties for initial values of the tween.  The properties of this object are changed during the tween's animation.
   * @memberof Tween.prototype
   *
   * @type {Object}
   * @readonly
   */
  startObject: {
    get: function () {
      return this._startObject;
    },
  },
 
  /**
   * An object with properties for the final values of the tween.
   * @memberof Tween.prototype
   *
   * @type {Object}
   * @readonly
   */
  stopObject: {
    get: function () {
      return this._stopObject;
    },
  },
 
  /**
   * The duration, in seconds, for the tween.  The tween is automatically removed from the collection when it stops.
   * @memberof Tween.prototype
   *
   * @type {Number}
   * @readonly
   */
  duration: {
    get: function () {
      return this._duration;
    },
  },
 
  /**
   * The delay, in seconds, before the tween starts animating.
   * @memberof Tween.prototype
   *
   * @type {Number}
   * @readonly
   */
  delay: {
    get: function () {
      return this._delay;
    },
  },
 
  /**
   * Determines the curve for animtion.
   * @memberof Tween.prototype
   *
   * @type {EasingFunction}
   * @readonly
   */
  easingFunction: {
    get: function () {
      return this._easingFunction;
    },
  },
 
  /**
   * The callback to call at each animation update (usually tied to the a rendered frame).
   * @memberof Tween.prototype
   *
   * @type {TweenCollection.TweenUpdateCallback}
   * @readonly
   */
  update: {
    get: function () {
      return this._update;
    },
  },
 
  /**
   * The callback to call when the tween finishes animating.
   * @memberof Tween.prototype
   *
   * @type {TweenCollection.TweenCompleteCallback}
   * @readonly
   */
  complete: {
    get: function () {
      return this._complete;
    },
  },
 
  /**
   * @memberof Tween.prototype
   *
   * @private
   */
  tweenjs: {
    get: function () {
      return this._tweenjs;
    },
  },
});
 
/**
 * Cancels the tween calling the {@link Tween#cancel} callback if one exists.  This
 * has no effect if the tween finished or was already canceled.
 */
Tween.prototype.cancelTween = function () {
  this._tweens.remove(this);
};
 
/**
 * A collection of tweens for animating properties.  Commonly accessed using {@link Scene#tweens}.
 *
 * @alias TweenCollection
 * @constructor
 *
 * @private
 */
function TweenCollection() {
  this._tweens = [];
}
 
Object.defineProperties(TweenCollection.prototype, {
  /**
   * The number of tweens in the collection.
   * @memberof TweenCollection.prototype
   *
   * @type {Number}
   * @readonly
   */
  length: {
    get: function () {
      return this._tweens.length;
    },
  },
});
 
/**
 * Creates a tween for animating between two sets of properties.  The tween starts animating at the next call to {@link TweenCollection#update}, which
 * is implicit when {@link Viewer} or {@link CesiumWidget} render the scene.
 *
 * @param {Object} [options] Object with the following properties:
 * @param {Object} options.startObject An object with properties for initial values of the tween.  The properties of this object are changed during the tween's animation.
 * @param {Object} options.stopObject An object with properties for the final values of the tween.
 * @param {Number} options.duration The duration, in seconds, for the tween.  The tween is automatically removed from the collection when it stops.
 * @param {Number} [options.delay=0.0] The delay, in seconds, before the tween starts animating.
 * @param {EasingFunction} [options.easingFunction=EasingFunction.LINEAR_NONE] Determines the curve for animtion.
 * @param {TweenCollection.TweenUpdateCallback} [options.update] The callback to call at each animation update (usually tied to the a rendered frame).
 * @param {TweenCollection.TweenCompleteCallback} [options.complete] The callback to call when the tween finishes animating.
 * @param {TweenCollection.TweenCancelledCallback} [options.cancel] The callback to call if the tween is canceled either because {@link Tween#cancelTween} was called or because the tween was removed from the collection.
 * @returns {Tween} The tween.
 *
 * @exception {DeveloperError} options.duration must be positive.
 */
TweenCollection.prototype.add = function (options) {
  options = defaultValue(options, defaultValue.EMPTY_OBJECT);
 
  //>>includeStart('debug', pragmas.debug);
  if (!defined(options.startObject) || !defined(options.stopObject)) {
    throw new DeveloperError(
      "options.startObject and options.stopObject are required."
    );
  }
 
  if (!defined(options.duration) || options.duration < 0.0) {
    throw new DeveloperError(
      "options.duration is required and must be positive."
    );
  }
  //>>includeEnd('debug');
 
  if (options.duration === 0.0) {
    if (defined(options.complete)) {
      options.complete();
    }
    return new Tween(this);
  }
 
  var duration = options.duration / TimeConstants.SECONDS_PER_MILLISECOND;
  var delayInSeconds = defaultValue(options.delay, 0.0);
  var delay = delayInSeconds / TimeConstants.SECONDS_PER_MILLISECOND;
  var easingFunction = defaultValue(
    options.easingFunction,
    EasingFunction.LINEAR_NONE
  );
 
  var value = options.startObject;
  var tweenjs = new TweenJS.Tween(value);
  tweenjs.to(clone(options.stopObject), duration);
  tweenjs.delay(delay);
  tweenjs.easing(easingFunction);
  if (defined(options.update)) {
    tweenjs.onUpdate(function () {
      options.update(value);
    });
  }
  tweenjs.onComplete(defaultValue(options.complete, null));
  tweenjs.repeat(defaultValue(options._repeat, 0.0));
 
  var tween = new Tween(
    this,
    tweenjs,
    options.startObject,
    options.stopObject,
    options.duration,
    delayInSeconds,
    easingFunction,
    options.update,
    options.complete,
    options.cancel
  );
  this._tweens.push(tween);
  return tween;
};
 
/**
 * Creates a tween for animating a scalar property on the given object.  The tween starts animating at the next call to {@link TweenCollection#update}, which
 * is implicit when {@link Viewer} or {@link CesiumWidget} render the scene.
 *
 * @param {Object} [options] Object with the following properties:
 * @param {Object} options.object The object containing the property to animate.
 * @param {String} options.property The name of the property to animate.
 * @param {Number} options.startValue The initial value.
 * @param {Number} options.stopValue The final value.
 * @param {Number} [options.duration=3.0] The duration, in seconds, for the tween.  The tween is automatically removed from the collection when it stops.
 * @param {Number} [options.delay=0.0] The delay, in seconds, before the tween starts animating.
 * @param {EasingFunction} [options.easingFunction=EasingFunction.LINEAR_NONE] Determines the curve for animtion.
 * @param {TweenCollection.TweenUpdateCallback} [options.update] The callback to call at each animation update (usually tied to the a rendered frame).
 * @param {TweenCollection.TweenCompleteCallback} [options.complete] The callback to call when the tween finishes animating.
 * @param {TweenCollection.TweenCancelledCallback} [options.cancel] The callback to call if the tween is canceled either because {@link Tween#cancelTween} was called or because the tween was removed from the collection.
 * @returns {Tween} The tween.
 *
 * @exception {DeveloperError} options.object must have the specified property.
 * @exception {DeveloperError} options.duration must be positive.
 */
TweenCollection.prototype.addProperty = function (options) {
  options = defaultValue(options, defaultValue.EMPTY_OBJECT);
 
  var object = options.object;
  var property = options.property;
  var startValue = options.startValue;
  var stopValue = options.stopValue;
 
  //>>includeStart('debug', pragmas.debug);
  if (!defined(object) || !defined(options.property)) {
    throw new DeveloperError(
      "options.object and options.property are required."
    );
  }
  if (!defined(object[property])) {
    throw new DeveloperError(
      "options.object must have the specified property."
    );
  }
  if (!defined(startValue) || !defined(stopValue)) {
    throw new DeveloperError(
      "options.startValue and options.stopValue are required."
    );
  }
  //>>includeEnd('debug');
 
  function update(value) {
    object[property] = value.value;
  }
 
  return this.add({
    startObject: {
      value: startValue,
    },
    stopObject: {
      value: stopValue,
    },
    duration: defaultValue(options.duration, 3.0),
    delay: options.delay,
    easingFunction: options.easingFunction,
    update: update,
    complete: options.complete,
    cancel: options.cancel,
    _repeat: options._repeat,
  });
};
 
/**
 * Creates a tween for animating the alpha of all color uniforms on a {@link Material}.  The tween starts animating at the next call to {@link TweenCollection#update}, which
 * is implicit when {@link Viewer} or {@link CesiumWidget} render the scene.
 *
 * @param {Object} [options] Object with the following properties:
 * @param {Material} options.material The material to animate.
 * @param {Number} [options.startValue=0.0] The initial alpha value.
 * @param {Number} [options.stopValue=1.0] The final alpha value.
 * @param {Number} [options.duration=3.0] The duration, in seconds, for the tween.  The tween is automatically removed from the collection when it stops.
 * @param {Number} [options.delay=0.0] The delay, in seconds, before the tween starts animating.
 * @param {EasingFunction} [options.easingFunction=EasingFunction.LINEAR_NONE] Determines the curve for animtion.
 * @param {TweenCollection.TweenUpdateCallback} [options.update] The callback to call at each animation update (usually tied to the a rendered frame).
 * @param {TweenCollection.TweenCompleteCallback} [options.complete] The callback to call when the tween finishes animating.
 * @param {TweenCollection.TweenCancelledCallback} [options.cancel] The callback to call if the tween is canceled either because {@link Tween#cancelTween} was called or because the tween was removed from the collection.
 * @returns {Tween} The tween.
 *
 * @exception {DeveloperError} material has no properties with alpha components.
 * @exception {DeveloperError} options.duration must be positive.
 */
TweenCollection.prototype.addAlpha = function (options) {
  options = defaultValue(options, defaultValue.EMPTY_OBJECT);
 
  var material = options.material;
 
  //>>includeStart('debug', pragmas.debug);
  if (!defined(material)) {
    throw new DeveloperError("options.material is required.");
  }
  //>>includeEnd('debug');
 
  var properties = [];
 
  for (var property in material.uniforms) {
    if (
      material.uniforms.hasOwnProperty(property) &&
      defined(material.uniforms[property]) &&
      defined(material.uniforms[property].alpha)
    ) {
      properties.push(property);
    }
  }
 
  //>>includeStart('debug', pragmas.debug);
  if (properties.length === 0) {
    throw new DeveloperError(
      "material has no properties with alpha components."
    );
  }
  //>>includeEnd('debug');
 
  function update(value) {
    var length = properties.length;
    for (var i = 0; i < length; ++i) {
      material.uniforms[properties[i]].alpha = value.alpha;
    }
  }
 
  return this.add({
    startObject: {
      alpha: defaultValue(options.startValue, 0.0), // Default to fade in
    },
    stopObject: {
      alpha: defaultValue(options.stopValue, 1.0),
    },
    duration: defaultValue(options.duration, 3.0),
    delay: options.delay,
    easingFunction: options.easingFunction,
    update: update,
    complete: options.complete,
    cancel: options.cancel,
  });
};
 
/**
 * Creates a tween for animating the offset uniform of a {@link Material}.  The tween starts animating at the next call to {@link TweenCollection#update}, which
 * is implicit when {@link Viewer} or {@link CesiumWidget} render the scene.
 *
 * @param {Object} [options] Object with the following properties:
 * @param {Material} options.material The material to animate.
 * @param {Number} options.startValue The initial alpha value.
 * @param {Number} options.stopValue The final alpha value.
 * @param {Number} [options.duration=3.0] The duration, in seconds, for the tween.  The tween is automatically removed from the collection when it stops.
 * @param {Number} [options.delay=0.0] The delay, in seconds, before the tween starts animating.
 * @param {EasingFunction} [options.easingFunction=EasingFunction.LINEAR_NONE] Determines the curve for animtion.
 * @param {TweenCollection.TweenUpdateCallback} [options.update] The callback to call at each animation update (usually tied to the a rendered frame).
 * @param {TweenCollection.TweenCancelledCallback} [options.cancel] The callback to call if the tween is canceled either because {@link Tween#cancelTween} was called or because the tween was removed from the collection.
 * @returns {Tween} The tween.
 *
 * @exception {DeveloperError} material.uniforms must have an offset property.
 * @exception {DeveloperError} options.duration must be positive.
 */
TweenCollection.prototype.addOffsetIncrement = function (options) {
  options = defaultValue(options, defaultValue.EMPTY_OBJECT);
 
  var material = options.material;
 
  //>>includeStart('debug', pragmas.debug);
  if (!defined(material)) {
    throw new DeveloperError("material is required.");
  }
  if (!defined(material.uniforms.offset)) {
    throw new DeveloperError("material.uniforms must have an offset property.");
  }
  //>>includeEnd('debug');
 
  var uniforms = material.uniforms;
  return this.addProperty({
    object: uniforms,
    property: "offset",
    startValue: uniforms.offset,
    stopValue: uniforms.offset + 1,
    duration: options.duration,
    delay: options.delay,
    easingFunction: options.easingFunction,
    update: options.update,
    cancel: options.cancel,
    _repeat: Infinity,
  });
};
 
/**
 * Removes a tween from the collection.
 * <p>
 * This calls the {@link Tween#cancel} callback if the tween has one.
 * </p>
 *
 * @param {Tween} tween The tween to remove.
 * @returns {Boolean} <code>true</code> if the tween was removed; <code>false</code> if the tween was not found in the collection.
 */
TweenCollection.prototype.remove = function (tween) {
  if (!defined(tween)) {
    return false;
  }
 
  var index = this._tweens.indexOf(tween);
  if (index !== -1) {
    tween.tweenjs.stop();
    if (defined(tween.cancel)) {
      tween.cancel();
    }
    this._tweens.splice(index, 1);
    return true;
  }
 
  return false;
};
 
/**
 * Removes all tweens from the collection.
 * <p>
 * This calls the {@link Tween#cancel} callback for each tween that has one.
 * </p>
 */
TweenCollection.prototype.removeAll = function () {
  var tweens = this._tweens;
 
  for (var i = 0; i < tweens.length; ++i) {
    var tween = tweens[i];
    tween.tweenjs.stop();
    if (defined(tween.cancel)) {
      tween.cancel();
    }
  }
  tweens.length = 0;
};
 
/**
 * Determines whether this collection contains a given tween.
 *
 * @param {Tween} tween The tween to check for.
 * @returns {Boolean} <code>true</code> if this collection contains the tween, <code>false</code> otherwise.
 */
TweenCollection.prototype.contains = function (tween) {
  return defined(tween) && this._tweens.indexOf(tween) !== -1;
};
 
/**
 * Returns the tween in the collection at the specified index.  Indices are zero-based
 * and increase as tweens are added.  Removing a tween shifts all tweens after
 * it to the left, changing their indices.  This function is commonly used to iterate over
 * all the tween in the collection.
 *
 * @param {Number} index The zero-based index of the tween.
 * @returns {Tween} The tween at the specified index.
 *
 * @example
 * // Output the duration of all the tweens in the collection.
 * var tweens = scene.tweens;
 * var length = tweens.length;
 * for (var i = 0; i < length; ++i) {
 *   console.log(tweens.get(i).duration);
 * }
 */
TweenCollection.prototype.get = function (index) {
  //>>includeStart('debug', pragmas.debug);
  if (!defined(index)) {
    throw new DeveloperError("index is required.");
  }
  //>>includeEnd('debug');
 
  return this._tweens[index];
};
 
/**
 * Updates the tweens in the collection to be at the provide time.  When a tween finishes, it is removed
 * from the collection.
 *
 * @param {Number} [time=getTimestamp()] The time in seconds.  By default tweens are synced to the system clock.
 */
TweenCollection.prototype.update = function (time) {
  var tweens = this._tweens;
 
  var i = 0;
  time = defined(time)
    ? time / TimeConstants.SECONDS_PER_MILLISECOND
    : getTimestamp();
  while (i < tweens.length) {
    var tween = tweens[i];
    var tweenjs = tween.tweenjs;
 
    if (tween.needsStart) {
      tween.needsStart = false;
      tweenjs.start(time);
    } else if (tweenjs.update(time)) {
      i++;
    } else {
      tweenjs.stop();
      tweens.splice(i, 1);
    }
  }
};
 
/**
 * A function that will execute when a tween completes.
 * @callback TweenCollection.TweenCompleteCallback
 */
 
/**
 * A function that will execute when a tween updates.
 * @callback TweenCollection.TweenUpdateCallback
 */
 
/**
 * A function that will execute when a tween is cancelled.
 * @callback TweenCollection.TweenCancelledCallback
 */
export default TweenCollection;