update method
@returns true if the tween is still playing after the update, false otherwise (calling update on a paused tween still returns true because it is still playing, just paused).
Implementation
bool update([int? time, bool autoStart = true]) {
var _this = this;
int? _a;
time ??= now();
if (this._isPaused)
return true;
var endTime = this._startTime + this._duration;
if (!this._goToEnd && !this._isPlaying) {
if (time > endTime)
return false;
if (autoStart)
this.start(time, true);
}
this._goToEnd = false;
if (time < this._startTime) {
return true;
}
if (this._onStartCallbackFired == false) {
this._onStartCallback?.call(this._object);
this._onStartCallbackFired = true;
}
if (this._onEveryStartCallbackFired == false) {
this._onEveryStartCallback?.call(this._object);
this._onEveryStartCallbackFired = true;
}
var elapsedTime = time - this._startTime;
int durationAndDelay = this._duration + ((_a = this._repeatDelayTime) != null && _a != null ? _a : this._delayTime);
var totalTime = this._duration + this._repeat * durationAndDelay;
calculateElapsedPortion() {
if (_this._duration == 0)
return 1;
if (elapsedTime > totalTime) {
return 1;
}
var timesRepeated = (elapsedTime / durationAndDelay).truncate();
var timeIntoCurrentRepeat = elapsedTime - timesRepeated * durationAndDelay;
// TODO use %?
// const timeIntoCurrentRepeat = elapsedTime % durationAndDelay
var portion = math.min<num>(timeIntoCurrentRepeat / _this._duration, 1);
if (portion == 0 && elapsedTime == _this._duration) {
return 1;
}
return portion;
};
var elapsed = calculateElapsedPortion();
var value = this._easing(elapsed);
// properties transformations
this._updateProperties(this._object, this._valuesStart, this._valuesEnd, value);
this._onUpdateCallback?.call(this._object, elapsed);
if (this._duration == 0 || elapsedTime >= this._duration) {
if (this._repeat > 0) {
int completeCount = math.min<int>(((elapsedTime - this._duration) / durationAndDelay).truncate() + 1, this._repeat);
if (this._repeat.isFinite) {
this._repeat -= completeCount;
}
// Reassign starting values, restart by making startTime = now
for (final property in this._valuesStartRepeat.keys) {
if (!this._yoyo && this._valuesEnd[property] is String) {
this._valuesStartRepeat[property] =
// eslint-disable-next-line
// @ts-ignore FIXME?
this._valuesStartRepeat[property] + double.parse(this._valuesEnd[property]);
}
if (this._yoyo) {
this._swapEndStartRepeatValues(property);
}
this._valuesStart[property] = this._valuesStartRepeat[property];
}
if (this._yoyo) {
this._reversed = !this._reversed;
}
this._startTime += durationAndDelay * completeCount;
this._onRepeatCallback?.call(this._object);
this._onEveryStartCallbackFired = false;
return true;
}
else {
this._onCompleteCallback?.call(this._object);
for (var i = 0, numChainedTweens = this._chainedTweens.length; i < numChainedTweens; i++) {
// Make the chained tweens start exactly at the time they should,
// even if the `update()` method was called way past the duration of the tween
this._chainedTweens[i].start(this._startTime + this._duration, false);
}
this._isPlaying = false;
return false;
}
}
return true;
}