| Index: sky/sdk/lib/animation/animated_simulation.dart
|
| diff --git a/sky/sdk/lib/animation/animated_simulation.dart b/sky/sdk/lib/animation/animated_simulation.dart
|
| index 3d40f880f37cbea4d6038cc1549768c7363a142f..f2afb896bdad7a96859f367e6f7f051bd5fff9a3 100644
|
| --- a/sky/sdk/lib/animation/animated_simulation.dart
|
| +++ b/sky/sdk/lib/animation/animated_simulation.dart
|
| @@ -5,10 +5,63 @@
|
| import 'dart:async';
|
|
|
| import 'package:newton/newton.dart';
|
| -import 'package:sky/animation/timeline.dart';
|
| +import 'package:sky/base/scheduler.dart' as scheduler;
|
|
|
| const double _kSecondsPerMillisecond = 1000.0;
|
|
|
| +class Ticker {
|
| + Ticker(Function onTick) : _onTick = onTick;
|
| +
|
| + final Function _onTick;
|
| +
|
| + Completer _completer;
|
| + int _animationId;
|
| +
|
| + Future start() {
|
| + assert(!isTicking);
|
| + _completer = new Completer();
|
| + _scheduleTick();
|
| + return _completer.future;
|
| + }
|
| +
|
| + void stop() {
|
| + if (!isTicking)
|
| + return;
|
| +
|
| + if (_animationId != null) {
|
| + scheduler.cancelAnimationFrame(_animationId);
|
| + _animationId = null;
|
| + }
|
| +
|
| + Completer localCompleter = _completer;
|
| + _completer = null;
|
| +
|
| + // We take the _completer into a local variable so that !isTicking when we
|
| + // actually complete the future.
|
| + assert(!isTicking);
|
| + localCompleter.complete();
|
| + }
|
| +
|
| + bool get isTicking => _completer != null;
|
| +
|
| + void _tick(double timeStamp) {
|
| + assert(isTicking);
|
| + assert(_animationId != null);
|
| + _animationId = null;
|
| +
|
| + _onTick(timeStamp);
|
| +
|
| + if (isTicking)
|
| + _scheduleTick();
|
| + }
|
| +
|
| + void _scheduleTick() {
|
| + assert(isTicking);
|
| + assert(_animationId == null);
|
| + _animationId = scheduler.requestAnimationFrame(_tick);
|
| + }
|
| +}
|
| +
|
| class AnimatedSimulation {
|
|
|
| AnimatedSimulation(Function onTick) : _onTick = onTick {
|
| @@ -23,6 +76,11 @@ class AnimatedSimulation {
|
|
|
| double _value = 0.0;
|
| double get value => _value;
|
| + void set value(double newValue) {
|
| + assert(!_ticker.isTicking);
|
| + _value = newValue;
|
| + _onTick(_value);
|
| + }
|
|
|
| Future start(Simulation simulation) {
|
| assert(simulation != null);
|
| @@ -36,7 +94,6 @@ class AnimatedSimulation {
|
| void stop() {
|
| _simulation = null;
|
| _startTime = null;
|
| - _value = 0.0;
|
| _ticker.stop();
|
| }
|
|
|
| @@ -50,10 +107,10 @@ class AnimatedSimulation {
|
| _value = _simulation.x(timeInSeconds);
|
| final bool isLastTick = _simulation.isDone(timeInSeconds);
|
|
|
| - _onTick(_value);
|
| -
|
| if (isLastTick)
|
| stop();
|
| +
|
| + _onTick(_value);
|
| }
|
|
|
| }
|
|
|