123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- 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";
- 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;
-
- this.cancel = cancel;
-
- this.needsStart = true;
- }
- Object.defineProperties(Tween.prototype, {
-
- startObject: {
- get: function () {
- return this._startObject;
- },
- },
-
- stopObject: {
- get: function () {
- return this._stopObject;
- },
- },
-
- duration: {
- get: function () {
- return this._duration;
- },
- },
-
- delay: {
- get: function () {
- return this._delay;
- },
- },
-
- easingFunction: {
- get: function () {
- return this._easingFunction;
- },
- },
-
- update: {
- get: function () {
- return this._update;
- },
- },
-
- complete: {
- get: function () {
- return this._complete;
- },
- },
-
- tweenjs: {
- get: function () {
- return this._tweenjs;
- },
- },
- });
- Tween.prototype.cancelTween = function () {
- this._tweens.remove(this);
- };
- function TweenCollection() {
- this._tweens = [];
- }
- Object.defineProperties(TweenCollection.prototype, {
-
- length: {
- get: function () {
- return this._tweens.length;
- },
- },
- });
- TweenCollection.prototype.add = function (options) {
- options = defaultValue(options, defaultValue.EMPTY_OBJECT);
-
- 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."
- );
- }
-
- 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;
- };
- 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;
-
- 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."
- );
- }
-
- 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,
- });
- };
- TweenCollection.prototype.addAlpha = function (options) {
- options = defaultValue(options, defaultValue.EMPTY_OBJECT);
- var material = options.material;
-
- if (!defined(material)) {
- throw new DeveloperError("options.material is required.");
- }
-
- 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);
- }
- }
-
- if (properties.length === 0) {
- throw new DeveloperError(
- "material has no properties with alpha components."
- );
- }
-
- 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),
- },
- 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,
- });
- };
- TweenCollection.prototype.addOffsetIncrement = function (options) {
- options = defaultValue(options, defaultValue.EMPTY_OBJECT);
- var material = options.material;
-
- if (!defined(material)) {
- throw new DeveloperError("material is required.");
- }
- if (!defined(material.uniforms.offset)) {
- throw new DeveloperError("material.uniforms must have an offset property.");
- }
-
- 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,
- });
- };
- 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;
- };
- 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;
- };
- TweenCollection.prototype.contains = function (tween) {
- return defined(tween) && this._tweens.indexOf(tween) !== -1;
- };
- TweenCollection.prototype.get = function (index) {
-
- if (!defined(index)) {
- throw new DeveloperError("index is required.");
- }
-
- return this._tweens[index];
- };
- 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);
- }
- }
- };
- export default TweenCollection;
|