From 81458c7925418ed2cca6970866a1e622f318afa9 Mon Sep 17 00:00:00 2001 From: Hans Muller Date: Mon, 6 Aug 2018 08:53:06 -0700 Subject: [PATCH] Added TweenSequence, TweenSequenceItem, ConstantTween (#20056) Make it possible to define an animation by stringing together a sequence of tweens. --- packages/flutter/lib/animation.dart | 1 + packages/flutter/lib/src/animation/tween.dart | 13 ++ .../lib/src/animation/tween_sequence.dart | 131 ++++++++++++++++++ .../test/animation/animations_test.dart | 107 ++++++++++++++ .../flutter/test/animation/tween_test.dart | 9 ++ 5 files changed, 261 insertions(+) create mode 100644 packages/flutter/lib/src/animation/tween_sequence.dart diff --git a/packages/flutter/lib/animation.dart b/packages/flutter/lib/animation.dart index 444d52ef83..d7b4fcc46f 100644 --- a/packages/flutter/lib/animation.dart +++ b/packages/flutter/lib/animation.dart @@ -17,3 +17,4 @@ export 'src/animation/animations.dart'; export 'src/animation/curves.dart'; export 'src/animation/listener_helpers.dart'; export 'src/animation/tween.dart'; +export 'src/animation/tween_sequence.dart'; diff --git a/packages/flutter/lib/src/animation/tween.dart b/packages/flutter/lib/src/animation/tween.dart index 02b86a2eaf..9b5ae1158c 100644 --- a/packages/flutter/lib/src/animation/tween.dart +++ b/packages/flutter/lib/src/animation/tween.dart @@ -303,6 +303,19 @@ class StepTween extends Tween { int lerp(double t) => (begin + (end - begin) * t).floor(); } +/// A tween with a constant value. +class ConstantTween extends Tween { + /// Create a tween whose [begin] and [end] values equal [value]. + ConstantTween(T value) : super(begin: value, end: value); + + /// This tween doesn't interpolate, it always returns [value]. + @override + T lerp(double t) => begin; + + @override + String toString() => '$runtimeType(value: begin)'; +} + /// Transforms the value of the given animation by the given curve. /// /// This class differs from [CurvedAnimation] in that [CurvedAnimation] applies diff --git a/packages/flutter/lib/src/animation/tween_sequence.dart b/packages/flutter/lib/src/animation/tween_sequence.dart new file mode 100644 index 0000000000..c1f8fd04a5 --- /dev/null +++ b/packages/flutter/lib/src/animation/tween_sequence.dart @@ -0,0 +1,131 @@ +import 'package:flutter/foundation.dart'; + +import 'animation.dart'; +import 'animations.dart'; +import 'tween.dart'; + +/// Enables creating an [Animation] whose value is defined by a +/// sequence of [Tween]s. +/// +/// Each [TweenSequenceItem] has a weight that defines its percentage +/// of the animation's duration. Each tween defines the animation's value +/// during the interval indicated by its weight. +/// +/// For example, to define an animation that uses an easing curve to +/// interpolate between 5.0 and 10.0 during the first 40% of the +/// animation, remain at 10.0 for the next 20%, and then return to +/// 10.0 for the final 40%: +/// +/// ``` +/// final Animation = new TweenSequence( +/// >[ +/// new TweenSequenceItem( +/// tween: new Tween(begin: 5.0, end: 10.0) +/// .chain(new CurveTween(curve: Curves.ease)), +/// weight: 40.0, +/// ), +/// new TweenSequenceItem( +/// tween: new ConstantTween(10.0), +/// weight: 20.0, +/// ), +/// new TweenSequenceItem( +/// tween: new Tween(begin: 10.0, end: 5.0) +/// .chain(new CurveTween(curve: Curves.ease)), +/// weight: 40.0, +/// ), +/// ], +/// ).animate(myAnimationController); +///``` +class TweenSequence extends Animatable { + /// Construct a TweenSequence. + /// + /// The [items] parameter must be a list of one or more + /// [TweenSequenceItem]s. + /// + /// There's a small cost associated with building a `TweenSequence` so + /// it's best to reuse one, rather than rebuilding it on every frame, + /// when that's possible. + TweenSequence(List> items) : assert(items != null), assert(items.isNotEmpty) { + _items.addAll(items); + + double totalWeight = 0.0; + for (TweenSequenceItem item in _items) + totalWeight += item.weight; + assert(totalWeight > 0.0); + + double start = 0.0; + for (int i = 0; i < _items.length; i += 1) { + final double end = i == _items.length - 1 ? 1.0 : start + _items[i].weight / totalWeight; + _intervals.add(new _Interval(start, end)); + start = end; + } + } + + final List> _items = >[]; + final List<_Interval> _intervals = <_Interval>[]; + + T _evaluateAt(double t, int index) { + final TweenSequenceItem element = _items[index]; + final double tInterval = _intervals[index].value(t); + return element.tween.evaluate(new AlwaysStoppedAnimation(tInterval)); + } + + @override + T evaluate(Animation animation) { + final double t = animation.value; + assert(t >= 0.0 && t <= 1.0); + if (t == 1.0) + return _evaluateAt(t, _items.length - 1); + for (int index = 0; index < _items.length; index++) { + if (_intervals[index].contains(t)) + return _evaluateAt(t, index); + } + // Should be unreachable. + assert(false, 'TweenSequence.evaluate() could not find a interval for $t'); + return null; + } +} + +/// A simple holder for one element of a [TweenSequence]. +class TweenSequenceItem { + /// Construct a TweenSequenceItem. + /// + /// The [tween] must not be null and [weight] must be greater than 0.0. + const TweenSequenceItem({ + @required this.tween, + @required this.weight, + }) : assert(tween != null), assert(weight != null), assert(weight > 0.0); + + /// Defines the value of the [TweenSequence] for the interval within the + /// animation's duration indicated by [weight] and this item's position + /// in the list of items. + /// + /// The value of this item can be "curved" by chaining it to a [CurveTween]. + /// For example to create a tween that eases from 0.0 to 10.0: + /// ``` + /// new Tween(begin: 0.0, end: 10.0) + /// .chain(new CurveTween(curve: Curves.ease)) + /// ``` + final Animatable tween; + + /// An abitrary value that indicates the relative percentage of a + /// [TweenSequence] animation's duration when [tween] will be used. + /// + /// The percentage for an individual item is the item's weight divided + /// by the sum of all of the items' weights. + final double weight; +} + +class _Interval { + const _Interval(this.start, this.end) : assert(end > start); + + final double start; + final double end; + + bool contains(double t) => t >= start && t < end; + + double value(double t) => (t - start) / (end - start); + + @override + String toString() => '<$start, $end>'; +} diff --git a/packages/flutter/test/animation/animations_test.dart b/packages/flutter/test/animation/animations_test.dart index c9f2ecabe8..a5e7148891 100644 --- a/packages/flutter/test/animation/animations_test.dart +++ b/packages/flutter/test/animation/animations_test.dart @@ -231,4 +231,111 @@ void main() { expect(() { curved.value; }, throwsFlutterError); }); + + test('TweenSequence', () { + final AnimationController controller = new AnimationController( + vsync: const TestVSync(), + ); + + final Animation animation = new TweenSequence( + >[ + new TweenSequenceItem( + tween: new Tween(begin: 5.0, end: 10.0), + weight: 4.0, + ), + new TweenSequenceItem( + tween: new ConstantTween(10.0), + weight: 2.0, + ), + new TweenSequenceItem( + tween: new Tween(begin: 10.0, end: 5.0), + weight: 4.0, + ), + ], + ).animate(controller); + + expect(animation.value, 5.0); + + controller.value = 0.2; + expect(animation.value, 7.5); + + controller.value = 0.4; + expect(animation.value, 10.0); + + controller.value = 0.6; + expect(animation.value, 10.0); + + controller.value = 0.8; + expect(animation.value, 7.5); + + controller.value = 1.0; + expect(animation.value, 5.0); + }); + + test('TweenSequence with curves', () { + final AnimationController controller = new AnimationController( + vsync: const TestVSync(), + ); + + final Animation animation = new TweenSequence( + >[ + new TweenSequenceItem( + tween: new Tween(begin: 5.0, end: 10.0) + .chain(new CurveTween(curve: const Interval(0.5, 1.0))), + weight: 4.0, + ), + new TweenSequenceItem( + tween: new ConstantTween(10.0) + .chain(new CurveTween(curve: Curves.linear)), // linear is a no-op + weight: 2.0, + ), + new TweenSequenceItem( + tween: new Tween(begin: 10.0, end: 5.0) + .chain(new CurveTween(curve: const Interval(0.0, 0.5))), + weight: 4.0, + ), + ], + ).animate(controller); + + expect(animation.value, 5.0); + + controller.value = 0.2; + expect(animation.value, 5.0); + + controller.value = 0.4; + expect(animation.value, 10.0); + + controller.value = 0.6; + expect(animation.value, 10.0); + + controller.value = 0.8; + expect(animation.value, 5.0); + + controller.value = 1.0; + expect(animation.value, 5.0); + }); + + test('TweenSequence, one tween', () { + final AnimationController controller = new AnimationController( + vsync: const TestVSync(), + ); + + final Animation animation = new TweenSequence( + >[ + new TweenSequenceItem( + tween: new Tween(begin: 5.0, end: 10.0), + weight: 1.0, + ), + ], + ).animate(controller); + + expect(animation.value, 5.0); + + controller.value = 0.5; + expect(animation.value, 7.5); + + controller.value = 1.0; + expect(animation.value, 10.0); + }); + } diff --git a/packages/flutter/test/animation/tween_test.dart b/packages/flutter/test/animation/tween_test.dart index 19cf7c2836..f35bfd27c6 100644 --- a/packages/flutter/test/animation/tween_test.dart +++ b/packages/flutter/test/animation/tween_test.dart @@ -74,4 +74,13 @@ void main() { moreOrLessEquals(0.0) ); }, skip: Platform.isWindows); // floating point math not quite deterministic on Windows? + + test('ConstantTween', () { + final ConstantTween tween = new ConstantTween(100.0); + expect(tween.begin, 100.0); + expect(tween.end, 100.0); + expect(tween.lerp(0.0), 100.0); + expect(tween.lerp(0.5), 100.0); + expect(tween.lerp(1.0), 100.0); + }); }