| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'animated_value.dart'; | 5 import 'timeline.dart'; |
| 6 import 'curves.dart'; | 6 import 'curves.dart'; |
| 7 | 7 |
| 8 // TODO(mpcomplete): merge this stuff with AnimatedValue somehow. We shouldn't | 8 // TODO(mpcomplete): merge this stuff with AnimatedValue somehow. We shouldn't |
| 9 // have 2 different ways to animate values. | 9 // have 2 different ways to animate values. |
| 10 abstract class AnimatedVariable { | 10 abstract class AnimatedVariable { |
| 11 void setFraction(double t); | 11 void setFraction(double t); |
| 12 } | 12 } |
| 13 | 13 |
| 14 class AnimatedType<T extends dynamic> extends AnimatedVariable { | 14 class AnimatedType<T extends dynamic> extends AnimatedVariable { |
| 15 T value; | 15 T value; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 27 } | 27 } |
| 28 | 28 |
| 29 // This class manages a "performance" - a collection of values that change | 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 | 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) | 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 | 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 | 33 // (dismiss). A consumer may also take direct control of the timeline by |
| 34 // manipulating |progress|, or |fling| the timeline causing a physics-based | 34 // manipulating |progress|, or |fling| the timeline causing a physics-based |
| 35 // simulation to take over the progression. | 35 // simulation to take over the progression. |
| 36 class AnimationPerformance { | 36 class AnimationPerformance { |
| 37 AnimationPerformance() { |
| 38 _timeline = new Timeline(_tick); |
| 39 } |
| 40 |
| 37 // TODO(mpcomplete): make this a list, or composable somehow. | 41 // TODO(mpcomplete): make this a list, or composable somehow. |
| 38 AnimatedVariable variable; | 42 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. | 43 // TODO(mpcomplete): duration should be on a director. |
| 42 Duration duration; | 44 Duration duration; |
| 43 | 45 |
| 44 AnimationPerformance() { | 46 // Advances from 0 to 1. On each tick, we'll update our variable's values. |
| 45 timeline.onValueChanged.listen((double t) { | 47 Timeline _timeline; |
| 46 variable.setFraction(t); | 48 Timeline get timeline => _timeline; |
| 47 }); | |
| 48 } | |
| 49 | 49 |
| 50 double get progress => timeline.value; | 50 double get progress => timeline.value; |
| 51 void set progress(double t) { | 51 void set progress(double t) { |
| 52 stop(); | 52 stop(); |
| 53 timeline.value = t.clamp(0.0, 1.0); | 53 timeline.value = t.clamp(0.0, 1.0); |
| 54 } | 54 } |
| 55 | 55 |
| 56 bool get isDismissed => progress == 0.0; | 56 bool get isDismissed => progress == 0.0; |
| 57 bool get isCompleted => progress == 1.0; | 57 bool get isCompleted => progress == 1.0; |
| 58 bool get isAnimating => timeline.isAnimating; | 58 bool get isAnimating => timeline.isAnimating; |
| 59 | 59 |
| 60 void play() { | 60 void play() { |
| 61 _animateTo(1.0); | 61 _animateTo(1.0); |
| 62 } | 62 } |
| 63 void reverse() { | 63 void reverse() { |
| 64 _animateTo(0.0); | 64 _animateTo(0.0); |
| 65 } | 65 } |
| 66 | 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() { | 67 void stop() { |
| 75 timeline.stop(); | 68 timeline.stop(); |
| 76 } | 69 } |
| 77 | 70 |
| 78 // Resume animating in a direction, with the given velocity. | 71 // Resume animating in a direction, with the given velocity. |
| 79 // TODO(mpcomplete): this should be a force with friction so it slows over | 72 // TODO(mpcomplete): this should be a force with friction so it slows over |
| 80 // time. | 73 // time. |
| 81 void fling({double velocity: 1.0}) { | 74 void fling({double velocity: 1.0}) { |
| 82 double target = velocity.sign < 0.0 ? 0.0 : 1.0; | 75 double target = velocity.sign < 0.0 ? 0.0 : 1.0; |
| 83 double distance = (target - timeline.value).abs(); | 76 double distance = (target - timeline.value).abs(); |
| 84 double duration = distance / velocity.abs(); | 77 double duration = distance / velocity.abs(); |
| 85 | 78 |
| 86 if (distance > 0.0) | 79 if (distance > 0.0) |
| 87 timeline.animateTo(target, duration, curve: linear); | 80 timeline.animateTo(target, duration: duration); |
| 81 } |
| 82 |
| 83 final List<Function> _listeners = new List<Function>(); |
| 84 |
| 85 void addListener(Function listener) { |
| 86 _listeners.add(listener); |
| 87 } |
| 88 |
| 89 void removeListener(Function listener) { |
| 90 _listeners.remove(listener); |
| 91 } |
| 92 |
| 93 void _notifyListeners() { |
| 94 List<Function> localListeners = new List<Function>.from(_listeners); |
| 95 for (Function listener in localListeners) |
| 96 listener(); |
| 97 } |
| 98 |
| 99 void _animateTo(double target) { |
| 100 double remainingDistance = (target - timeline.value).abs(); |
| 101 timeline.stop(); |
| 102 if (remainingDistance != 0.0) |
| 103 timeline.animateTo(target, duration: remainingDistance * duration.inMillis
econds); |
| 104 } |
| 105 |
| 106 void _tick(double t) { |
| 107 variable.setFraction(t); |
| 108 _notifyListeners(); |
| 88 } | 109 } |
| 89 } | 110 } |
| OLD | NEW |