Index: sky/framework/animation/generator.dart |
diff --git a/sky/framework/animation/generator.dart b/sky/framework/animation/generator.dart |
index b24c46227dff4e88f9f8ec82c97aaa2a6b70b85e..65c373f548a7029301c7b55d5f3ece344a7b6236 100644 |
--- a/sky/framework/animation/generator.dart |
+++ b/sky/framework/animation/generator.dart |
@@ -53,6 +53,7 @@ class FrameGenerator { |
class AnimationGenerator extends FrameGenerator { |
Stream<double> get onTick => _stream; |
+ final double initialDelay; |
final double duration; |
final double begin; |
final double end; |
@@ -61,6 +62,7 @@ class AnimationGenerator extends FrameGenerator { |
bool _done = false; |
AnimationGenerator(this.duration, { |
+ this.initialDelay: 0.0, |
this.begin: 0.0, |
this.end: 1.0, |
this.curve: linear, |
@@ -71,9 +73,12 @@ class AnimationGenerator extends FrameGenerator { |
_stream = super.onTick.map((timeStamp) { |
if (startTime == 0.0) |
startTime = timeStamp; |
- return math.min((timeStamp - startTime) / duration, 1.0); |
+ |
+ double t = (timeStamp - (startTime + initialDelay)) / duration; |
+ return math.max(0.0, math.min(t, 1.0)); |
}) |
- .takeWhile(_checkForCompletion) |
+ .takeWhile(_checkForCompletion) // |
+ .where((t) => t >= 0.0) |
.map(_transform); |
} |
@@ -83,10 +88,55 @@ class AnimationGenerator extends FrameGenerator { |
return begin + (end - begin) * curve.transform(t); |
} |
+ // This is required because Dart Streams don't have takeUntil (inclusive). |
bool _checkForCompletion(double t) { |
if (_done) |
return false; |
+ |
_done = t >= 1; |
return true; |
} |
} |
+ |
+class Animation { |
+ Stream<double> get onValueChanged => _controller.stream; |
+ |
+ double get value => _value; |
+ |
+ void set value(double value) { |
+ stop(); |
+ _setValue(value); |
+ } |
+ |
+ bool get isAnimating => _animation != null; |
+ |
+ StreamController _controller = new StreamController(sync: true); |
+ |
+ AnimationGenerator _animation; |
+ |
+ double _value; |
+ |
+ void _setValue(double value) { |
+ _value = value; |
+ _controller.add(_value); |
+ } |
+ |
+ void stop() { |
+ if (_animation != null) { |
+ _animation.cancel(); |
+ _animation = null; |
+ } |
+ } |
+ |
+ void animateTo(double newValue, double duration, |
+ { Curve curve: linear, double initialDelay: 0.0 }) { |
+ stop(); |
+ |
+ _animation = new AnimationGenerator(duration, begin: _value, end: newValue, |
+ curve: curve, initialDelay: initialDelay); |
+ |
+ _animation.onTick.listen(_setValue, onDone: () { |
+ _animation = null; |
+ }); |
+ } |
+} |