Commit 49b0843c authored by Adam Barth's avatar Adam Barth

Add support for repeating performances

Fixes #447
parent 4d0520bb
...@@ -3,7 +3,9 @@ ...@@ -3,7 +3,9 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async'; import 'dart:async';
import 'dart:ui' show VoidCallback; import 'dart:ui' show VoidCallback, lerpDouble;
import 'package:newton/newton.dart';
import 'animated_value.dart'; import 'animated_value.dart';
import 'forces.dart'; import 'forces.dart';
...@@ -146,6 +148,27 @@ class ReversePerformance extends PerformanceView { ...@@ -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. /// A timeline that can be reversed and used to update [Animatable]s.
/// ///
...@@ -249,6 +272,12 @@ class Performance extends PerformanceView { ...@@ -249,6 +272,12 @@ class Performance extends PerformanceView {
return _timeline.animateWith(force.release(progress, velocity)); 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<VoidCallback> _listeners = new List<VoidCallback>(); final List<VoidCallback> _listeners = new List<VoidCallback>();
/// Calls the listener every time the progress of this performance changes /// Calls the listener every time the progress of this performance changes
......
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