Chromium Code Reviews| 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 setPercentage(double t); | |
|
abarth-chromium
2015/06/30 22:26:09
Does t go from 0 to 100 or from 0 to 1? If its th
Matt Perry
2015/07/01 18:17:42
Done.
| |
| 12 } | |
| 13 | |
| 14 class AnimatedType<T> extends AnimatedVariable { | |
| 15 T value; | |
| 16 T begin, end; | |
|
abarth-chromium
2015/06/30 22:26:08
final?
Matt Perry
2015/07/01 18:17:43
Done.
| |
| 17 Curve curve; | |
|
abarth-chromium
2015/06/30 22:26:08
final?
Matt Perry
2015/07/01 18:17:43
Done.
| |
| 18 | |
| 19 AnimatedType(this.begin, this.end, {this.curve: linear}) { | |
| 20 value = begin; | |
| 21 } | |
| 22 | |
| 23 void setPercentage(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 |percentage|, 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 // TODO(mpcomplete): maybe this should be in seconds? | |
| 43 double durationMS; | |
|
abarth-chromium
2015/06/30 22:26:08
Maybe this should be of type Duration?
https://ap
Matt Perry
2015/07/01 18:17:42
Done.
| |
| 44 | |
| 45 AnimationPerformance() { | |
| 46 timeline.onValueChanged.listen((double t) { | |
| 47 variable.setPercentage(t); | |
| 48 }); | |
| 49 } | |
| 50 | |
| 51 double get percentage => timeline.value; | |
| 52 void set percentage(double t) { | |
| 53 stop(); | |
| 54 timeline.value = t.clamp(0.0, 1.0); | |
| 55 } | |
| 56 | |
| 57 bool get isDismissed => timeline.value == 0; | |
| 58 bool get isPresented => timeline.value == 1; | |
|
abarth-chromium
2015/06/30 22:26:09
0.0 and 1.0
Matt Perry
2015/07/01 18:17:42
Done.
| |
| 59 bool get isAnimating => timeline.isAnimating; | |
| 60 | |
| 61 void present() { | |
| 62 _animateTo(1.0); | |
| 63 } | |
| 64 void dismiss() { | |
| 65 _animateTo(0.0); | |
| 66 } | |
| 67 | |
| 68 void stop() { | |
| 69 timeline.stop(); | |
| 70 } | |
| 71 | |
| 72 // Resume animating in a direction, with the given velocity. | |
| 73 // TODO(mpcomplete): this should be a force with friction so it slows over | |
| 74 // time. | |
|
abarth-chromium
2015/06/30 22:26:09
Have you looked at ParticleInBoxWithFriction from
Matt Perry
2015/07/01 18:17:42
Yeah, that looks promising. I'd have to hook it up
| |
| 75 void fling({double velocity: 1.0}) { | |
| 76 double target = velocity.sign < 0.0 ? 0.0 : 1.0; | |
| 77 double distance = (target - timeline.value).abs(); | |
| 78 double duration = distance / velocity.abs(); | |
| 79 | |
| 80 if (distance > 0) | |
|
abarth-chromium
2015/06/30 22:26:08
0.0
Matt Perry
2015/07/01 18:17:42
Done.
| |
| 81 timeline.animateTo(target, duration, curve: linear); | |
| 82 } | |
| 83 | |
| 84 // TODO(mpcomplete): fix animateTo to better handle resuming an animation, | |
| 85 // with timelines. Also need to support forces. | |
| 86 void _animateTo(double target) { | |
| 87 double remainingDistance = (target - timeline.value).abs(); | |
| 88 timeline.stop(); | |
| 89 if (remainingDistance != 0) | |
|
abarth-chromium
2015/06/30 22:26:08
0.0
Matt Perry
2015/07/01 18:17:43
Done.
| |
| 90 timeline.animateTo(target, remainingDistance * durationMS); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 | |
| OLD | NEW |