Commit 203e6fd7 authored by Adam Barth's avatar Adam Barth

Make Ticker start ticking at zero Duration

The only client wants a zero-based duration.
parent db191e96
...@@ -7,7 +7,7 @@ import 'dart:async'; ...@@ -7,7 +7,7 @@ import 'dart:async';
import 'package:newton/newton.dart'; import 'package:newton/newton.dart';
import 'package:sky/src/animation/scheduler.dart'; import 'package:sky/src/animation/scheduler.dart';
typedef _TickerCallback(Duration timeStamp); typedef _TickerCallback(Duration elapsed);
/// Calls its callback once per animation frame /// Calls its callback once per animation frame
class Ticker { class Ticker {
...@@ -18,12 +18,14 @@ class Ticker { ...@@ -18,12 +18,14 @@ class Ticker {
Completer _completer; Completer _completer;
int _animationId; int _animationId;
Duration _startTime;
/// Start calling onTick once per animation frame /// Start calling onTick once per animation frame
/// ///
/// The returned future resolves once the ticker stops ticking. /// The returned future resolves once the ticker stops ticking.
Future start() { Future start() {
assert(!isTicking); assert(!isTicking);
assert(_startTime == null);
_completer = new Completer(); _completer = new Completer();
_scheduleTick(); _scheduleTick();
return _completer.future; return _completer.future;
...@@ -36,6 +38,8 @@ class Ticker { ...@@ -36,6 +38,8 @@ class Ticker {
if (!isTicking) if (!isTicking)
return; return;
_startTime = null;
if (_animationId != null) { if (_animationId != null) {
scheduler.cancelAnimationFrame(_animationId); scheduler.cancelAnimationFrame(_animationId);
_animationId = null; _animationId = null;
...@@ -58,7 +62,10 @@ class Ticker { ...@@ -58,7 +62,10 @@ class Ticker {
assert(_animationId != null); assert(_animationId != null);
_animationId = null; _animationId = null;
_onTick(timeStamp); if (_startTime == null)
_startTime = timeStamp;
_onTick(timeStamp - _startTime);
// The onTick callback may have scheduled another tick already. // The onTick callback may have scheduled another tick already.
if (isTicking && _animationId == null) if (isTicking && _animationId == null)
...@@ -83,7 +90,6 @@ class AnimatedSimulation { ...@@ -83,7 +90,6 @@ class AnimatedSimulation {
Ticker _ticker; Ticker _ticker;
Simulation _simulation; Simulation _simulation;
Duration _startTime;
double _value = 0.0; double _value = 0.0;
/// The current value of the simulation /// The current value of the simulation
...@@ -101,7 +107,6 @@ class AnimatedSimulation { ...@@ -101,7 +107,6 @@ class AnimatedSimulation {
assert(simulation != null); assert(simulation != null);
assert(!_ticker.isTicking); assert(!_ticker.isTicking);
_simulation = simulation; _simulation = simulation;
_startTime = null;
_value = simulation.x(0.0); _value = simulation.x(0.0);
return _ticker.start(); return _ticker.start();
} }
...@@ -109,23 +114,18 @@ class AnimatedSimulation { ...@@ -109,23 +114,18 @@ class AnimatedSimulation {
/// Stop ticking the current simulation /// Stop ticking the current simulation
void stop() { void stop() {
_simulation = null; _simulation = null;
_startTime = null;
_ticker.stop(); _ticker.stop();
} }
/// Whether this object is currently ticking a simulation /// Whether this object is currently ticking a simulation
bool get isAnimating => _ticker.isTicking; bool get isAnimating => _ticker.isTicking;
void _tick(Duration timeStamp) { void _tick(Duration elapsed) {
if (_startTime == null)
_startTime = timeStamp;
double timeInMicroseconds = (timeStamp - _startTime).inMicroseconds.toDouble(); double elapsedInSeconds = elapsed.inMicroseconds.toDouble() / Duration.MICROSECONDS_PER_SECOND;
double timeInSeconds = timeInMicroseconds / Duration.MICROSECONDS_PER_SECOND; _value = _simulation.x(elapsedInSeconds);
_value = _simulation.x(timeInSeconds);
final bool isLastTick = _simulation.isDone(timeInSeconds);
if (isLastTick) if (_simulation.isDone(elapsedInSeconds))
stop(); stop();
_onTick(_value); _onTick(_value);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment