From 0c1d1df080bb335928e0e793b9a2f12ff02f3678 Mon Sep 17 00:00:00 2001 From: Hixie Date: Mon, 14 Sep 2015 14:04:22 -0700 Subject: [PATCH] Clean up animation-related files. Surface all the constructor arguments of AnimationTiming in all its subclasses. Remove some pointless casts. Fix some typos. Put constructors first in class declarations. Remove some blank lines where they just confused the structure of the code. --- .../sky/lib/src/animation/animated_value.dart | 16 +++++----- .../src/animation/animation_performance.dart | 7 +++-- .../sky/lib/src/animation/curves.dart | 31 ++++++------------- 3 files changed, 22 insertions(+), 32 deletions(-) diff --git a/sky/packages/sky/lib/src/animation/animated_value.dart b/sky/packages/sky/lib/src/animation/animated_value.dart index f5854f46906..ff1fba65046 100644 --- a/sky/packages/sky/lib/src/animation/animated_value.dart +++ b/sky/packages/sky/lib/src/animation/animated_value.dart @@ -77,8 +77,8 @@ class AnimationTiming { /// An animated variable with a concrete type class AnimatedValue extends AnimationTiming implements AnimatedVariable { - AnimatedValue(this.begin, { this.end, Interval interval, Curve curve, Curve reverseCurve }) - : super(interval: interval, curve: curve, reverseCurve: reverseCurve) { + AnimatedValue(this.begin, { this.end, Interval interval, Interval reverseInterval, Curve curve, Curve reverseCurve }) + : super(interval: interval, reverseInterval: reverseInterval, curve: curve, reverseCurve: reverseCurve) { value = begin; } @@ -110,8 +110,8 @@ class AnimatedList extends AnimationTiming implements AnimatedVariable { /// The list of variables contained in the list List variables; - AnimatedList(this.variables, { Interval interval, Curve curve, Curve reverseCurve }) - : super(interval: interval, curve: curve, reverseCurve: reverseCurve); + AnimatedList(this.variables, { Interval interval, Interval reverseInterval, Curve curve, Curve reverseCurve }) + : super(interval: interval, reverseInterval: reverseInterval, curve: curve, reverseCurve: reverseCurve); // Updates the value of all the variables in the list according to the given animation clock value and direction void setProgress(double t, Direction direction) { @@ -128,8 +128,8 @@ class AnimatedList extends AnimationTiming implements AnimatedVariable { /// This class specializes the interpolation of AnimatedValue to be /// appropriate for colors. class AnimatedColorValue extends AnimatedValue { - AnimatedColorValue(Color begin, { Color end, Curve curve }) - : super(begin, end: end, curve: curve); + AnimatedColorValue(Color begin, { Color end, Interval interval, Interval reverseInterval, Curve curve, Curve reverseCurve }) + : super(begin, end: end, interval: interval, reverseInterval: reverseInterval, curve: curve, reverseCurve: reverseCurve); Color lerp(double t) => Color.lerp(begin, end, t); } @@ -139,8 +139,8 @@ class AnimatedColorValue extends AnimatedValue { /// This class specializes the interpolation of AnimatedValue to be /// appropriate for rectangles. class AnimatedRect extends AnimatedValue { - AnimatedRect(Rect begin, { Rect end, Curve curve }) - : super(begin, end: end, curve: curve); + AnimatedRect(Rect begin, { Rect end, Interval interval, Interval reverseInterval, Curve curve, Curve reverseCurve }) + : super(begin, end: end, interval: interval, reverseInterval: reverseInterval, curve: curve, reverseCurve: reverseCurve); Rect lerp(double t) => Rect.lerp(begin, end, t); } diff --git a/sky/packages/sky/lib/src/animation/animation_performance.dart b/sky/packages/sky/lib/src/animation/animation_performance.dart index 92046edd4b2..d862f7e1f2c 100644 --- a/sky/packages/sky/lib/src/animation/animation_performance.dart +++ b/sky/packages/sky/lib/src/animation/animation_performance.dart @@ -32,7 +32,7 @@ enum AnimationStatus { /// [fling] the timeline causing a physics-based simulation to take over the /// progression. class AnimationPerformance { - AnimationPerformance({AnimatedVariable variable, this.duration}) : + AnimationPerformance({ AnimatedVariable variable, this.duration }) : _variable = variable { _timeline = new Timeline(_tick); } @@ -68,7 +68,8 @@ class AnimationPerformance { if (variable == null) { variable = newVariable; } else if (variable is AnimatedList) { - (variable as AnimatedList).variables.add(newVariable); + final AnimatedList variable = this.variable; // TODO(ianh): Remove this line when the analyzer is cleverer + variable.variables.add(newVariable); } else { variable = new AnimatedList([variable, newVariable]); } @@ -219,7 +220,7 @@ class AnimationPerformance { /// An animation performance with an animated variable with a concrete type class ValueAnimation extends AnimationPerformance { - ValueAnimation({AnimatedValue variable, Duration duration}) : + ValueAnimation({ AnimatedValue variable, Duration duration }) : super(variable: variable, duration: duration); AnimatedValue get variable => _variable as AnimatedValue; diff --git a/sky/packages/sky/lib/src/animation/curves.dart b/sky/packages/sky/lib/src/animation/curves.dart index d7dd325efdb..791f45bcc14 100644 --- a/sky/packages/sky/lib/src/animation/curves.dart +++ b/sky/packages/sky/lib/src/animation/curves.dart @@ -21,23 +21,14 @@ abstract class Curve { double transform(double t); } -/// The idenity map over the unit interval +/// The identity map over the unit interval class Linear implements Curve { const Linear(); - - double transform(double t) { - return t; - } + double transform(double t) => t; } /// A curve that is initially 0.0, then linear, then 1.0 class Interval implements Curve { - /// The smallest value for which this interval is 0.0 - final double start; - - /// The smallest value for which this interval is 1.0 - final double end; - Interval(this.start, this.end) { assert(start >= 0.0); assert(start <= 1.0); @@ -45,6 +36,12 @@ class Interval implements Curve { assert(end <= 1.0); } + /// The smallest value for which this interval is 0.0 + final double start; + + /// The smallest value for which this interval is 1.0 + final double end; + double transform(double t) { return ((t - start) / (end - start)).clamp(0.0, 1.0); } @@ -52,23 +49,21 @@ class Interval implements Curve { /// A cubic polynomial mapping of the unit interval class Cubic implements Curve { + const Cubic(this.a, this.b, this.c, this.d); + final double a; final double b; final double c; final double d; - const Cubic(this.a, this.b, this.c, this.d); - double transform(double t) { double start = 0.0; double end = 1.0; while (true) { double midpoint = (start + end) / 2; double estimate = _evaluateCubic(a, c, midpoint); - if ((t - estimate).abs() < _kCubicErrorBound) return _evaluateCubic(b, d, midpoint); - if (estimate < t) start = midpoint; else @@ -94,7 +89,6 @@ double _bounce(double t) { /// An oscillating curve that grows in magnitude class BounceInCurve implements Curve { const BounceInCurve(); - double transform(double t) { return 1.0 - _bounce(1.0 - t); } @@ -103,7 +97,6 @@ class BounceInCurve implements Curve { /// An oscillating curve that shrink in magnitude class BounceOutCurve implements Curve { const BounceOutCurve(); - double transform(double t) { return _bounce(t); } @@ -112,7 +105,6 @@ class BounceOutCurve implements Curve { /// An oscillating curve that first grows and then shrink in magnitude class BounceInOutCurve implements Curve { const BounceInOutCurve(); - double transform(double t) { if (t < 0.5) return (1.0 - _bounce(1.0 - t)) * 0.5; @@ -125,7 +117,6 @@ class BounceInOutCurve implements Curve { class ElasticInCurve implements Curve { const ElasticInCurve([this.period = 0.4]); final double period; - double transform(double t) { double s = period / 4.0; t = t - 1.0; @@ -137,7 +128,6 @@ class ElasticInCurve implements Curve { class ElasticOutCurve implements Curve { const ElasticOutCurve([this.period = 0.4]); final double period; - double transform(double t) { double s = period / 4.0; return math.pow(2.0, -10 * t) * math.sin((t - s) * (math.PI * 2.0) / period) + 1.0; @@ -148,7 +138,6 @@ class ElasticOutCurve implements Curve { class ElasticInOutCurve implements Curve { const ElasticInOutCurve([this.period = 0.4]); final double period; - double transform(double t) { double s = period / 4.0; t = 2.0 * t - 1.0;