Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(826)

Unified Diff: sky/sdk/lib/animation/animated_simulation.dart

Issue 1233653003: Add support for "flinging" the animation timeline with a spring force. So far, (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: . Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « DEPS ('k') | sky/sdk/lib/animation/animation_performance.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
}
}
« no previous file with comments | « DEPS ('k') | sky/sdk/lib/animation/animation_performance.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698