| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 import 'animated_value.dart'; |
| 6 import 'curves.dart'; |
| 7 |
| 8 // TODO(mpcomplete): merge this stuff with AnimatedValue somehow. We shouldn't |
| 9 // have 2 different ways to animate values. |
| 10 abstract class AnimatedVariable { |
| 11 void setFraction(double t); |
| 12 } |
| 13 |
| 14 class AnimatedType<T> extends AnimatedVariable { |
| 15 T value; |
| 16 final T begin, end; |
| 17 final Curve curve; |
| 18 |
| 19 AnimatedType(this.begin, this.end, {this.curve: linear}) { |
| 20 value = begin; |
| 21 } |
| 22 |
| 23 void setFraction(double t) { |
| 24 // TODO(mpcomplete): Reverse the timeline and curve. |
| 25 value = begin + (end - begin) * curve.transform(t); |
| 26 } |
| 27 } |
| 28 |
| 29 // This class manages a "performance" - a collection of values that change |
| 30 // based on a timeline. For example, a performance may handle an animation |
| 31 // of a menu opening by sliding and fading in (changing Y value and opacity) |
| 32 // over .5 seconds. The performance can move forwards (present) or backwards |
| 33 // (dismiss). A consumer may also take direct control of the timeline by |
| 34 // manipulating |progress|, or |fling| the timeline causing a physics-based |
| 35 // simulation to take over the progression. |
| 36 class AnimationPerformance { |
| 37 // TODO(mpcomplete): make this a list, or composable somehow. |
| 38 AnimatedVariable variable; |
| 39 // Advances from 0 to 1. On each tick, we'll update our variable's values. |
| 40 AnimatedValue timeline = new AnimatedValue(0.0); |
| 41 // TODO(mpcomplete): duration should be on a director. |
| 42 Duration duration; |
| 43 |
| 44 AnimationPerformance() { |
| 45 timeline.onValueChanged.listen((double t) { |
| 46 variable.setFraction(t); |
| 47 }); |
| 48 } |
| 49 |
| 50 double get progress => timeline.value; |
| 51 void set progress(double t) { |
| 52 stop(); |
| 53 timeline.value = t.clamp(0.0, 1.0); |
| 54 } |
| 55 |
| 56 bool get isDismissed => progress == 0.0; |
| 57 bool get isCompleted => progress == 1.0; |
| 58 bool get isAnimating => timeline.isAnimating; |
| 59 |
| 60 void play() { |
| 61 _animateTo(1.0); |
| 62 } |
| 63 void reverse() { |
| 64 _animateTo(0.0); |
| 65 } |
| 66 |
| 67 void _animateTo(double target) { |
| 68 double remainingDistance = (target - timeline.value).abs(); |
| 69 timeline.stop(); |
| 70 if (remainingDistance != 0.0) |
| 71 timeline.animateTo(target, remainingDistance * duration.inMilliseconds); |
| 72 } |
| 73 |
| 74 void stop() { |
| 75 timeline.stop(); |
| 76 } |
| 77 |
| 78 // Resume animating in a direction, with the given velocity. |
| 79 // TODO(mpcomplete): this should be a force with friction so it slows over |
| 80 // time. |
| 81 void fling({double velocity: 1.0}) { |
| 82 double target = velocity.sign < 0.0 ? 0.0 : 1.0; |
| 83 double distance = (target - timeline.value).abs(); |
| 84 double duration = distance / velocity.abs(); |
| 85 |
| 86 if (distance > 0.0) |
| 87 timeline.animateTo(target, duration, curve: linear); |
| 88 } |
| 89 } |
| OLD | NEW |