From 49b0843c84bff83f3d747845fdaa3d5576229090 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Tue, 17 Nov 2015 17:02:49 -0800 Subject: [PATCH] Add support for repeating performances Fixes #447 --- .../lib/src/animation/performance.dart | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/animation/performance.dart b/packages/flutter/lib/src/animation/performance.dart index 879d1c0cf2..49e4f57806 100644 --- a/packages/flutter/lib/src/animation/performance.dart +++ b/packages/flutter/lib/src/animation/performance.dart @@ -3,7 +3,9 @@ // found in the LICENSE file. import 'dart:async'; -import 'dart:ui' show VoidCallback; +import 'dart:ui' show VoidCallback, lerpDouble; + +import 'package:newton/newton.dart'; import 'animated_value.dart'; import 'forces.dart'; @@ -146,6 +148,27 @@ class ReversePerformance extends PerformanceView { } } +class _RepeatingSimulation extends Simulation { + _RepeatingSimulation(this.min, this.max, Duration period) + : _periodInSeconds = period.inMicroseconds.toDouble() / Duration.MICROSECONDS_PER_SECOND { + assert(_periodInSeconds > 0.0); + } + + final double min; + final double max; + + final double _periodInSeconds; + + double x(double timeInSeconds) { + assert(timeInSeconds >= 0.0); + final double t = (timeInSeconds / _periodInSeconds) % 1.0; + return lerpDouble(min, max, t); + } + + double dx(double timeInSeconds) => 1.0; + + bool isDone(double timeInSeconds) => false; +} /// A timeline that can be reversed and used to update [Animatable]s. /// @@ -249,6 +272,12 @@ class Performance extends PerformanceView { return _timeline.animateWith(force.release(progress, velocity)); } + Future repeat({ double min: 0.0, double max: 1.0, Duration period }) { + if (period == null) + period = duration; + return _timeline.animateWith(new _RepeatingSimulation(min, max, period)); + } + final List _listeners = new List(); /// Calls the listener every time the progress of this performance changes