| 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): make this an interface. |
| 9 // TODO(mpcomplete): find a better name, or reconcile this with AnimatedValue. |
| 10 class AnimationVariable { |
| 11 double value; |
| 12 double begin, end; |
| 13 Curve curve = linear; |
| 14 |
| 15 AnimationVariable(this.begin, this.end, {this.curve}) { |
| 16 value = begin; |
| 17 } |
| 18 |
| 19 void setPercentage(double t) { |
| 20 // TODO(mpcomplete): Reverse the timeline and curve. |
| 21 value = begin + (end - begin) * curve.transform(t); |
| 22 } |
| 23 } |
| 24 |
| 25 // This class manages a "performance" - a collection of values that change |
| 26 // based on a timeline. For example, a performance may handle an animation |
| 27 // of a menu opening by sliding and fading in (changing Y value and opacity) |
| 28 // over .5 seconds. The performance can move forwards (present) or backwards |
| 29 // (dismiss). A consumer may also take direct control of the timeline by |
| 30 // manipulating |percentage|, or |fling| the timeline causing a physics-based |
| 31 // simulation to take over the progression. |
| 32 class AnimationPerformance { |
| 33 // TODO(mpcomplete): make this a list, or composable somehow. |
| 34 AnimationVariable variable; |
| 35 // Advances from 0 to 1. On each tick, we'll update our variable's values. |
| 36 AnimatedValue timeline = new AnimatedValue(0.0); |
| 37 // TODO(mpcomplete): duration should be on a director. |
| 38 // TODO(mpcomplete): maybe this should be in seconds? |
| 39 double durationMS; |
| 40 |
| 41 AnimationPerformance() { |
| 42 timeline.onValueChanged.listen((double t) { |
| 43 variable.setPercentage(t); |
| 44 }); |
| 45 } |
| 46 |
| 47 double get percentage => timeline.value; |
| 48 void set percentage(double t) { |
| 49 stop(); |
| 50 timeline.value = t.clamp(0.0, 1.0); |
| 51 } |
| 52 |
| 53 bool get isDismissed => timeline.value == 0; |
| 54 bool get isPresented => timeline.value == 1; |
| 55 bool get isAnimating => timeline.isAnimating; |
| 56 |
| 57 void present() { |
| 58 _animateTo(1.0); |
| 59 } |
| 60 void dismiss() { |
| 61 _animateTo(0.0); |
| 62 } |
| 63 |
| 64 void stop() { |
| 65 timeline.stop(); |
| 66 } |
| 67 |
| 68 // Resume animating in a direction, with the given velocity. |
| 69 // TODO(mpcomplete): this should be a force with friction so it slows over |
| 70 // time. |
| 71 void fling({double velocity: 1.0}) { |
| 72 double target = velocity.sign < 0.0 ? 0.0 : 1.0; |
| 73 double distance = (target - timeline.value).abs(); |
| 74 double duration = distance / velocity.abs(); |
| 75 |
| 76 if (distance > 0) |
| 77 timeline.animateTo(target, duration, curve: linear); |
| 78 } |
| 79 |
| 80 // TODO(mpcomplete): fix animateTo to better handle resuming an animation, |
| 81 // with timelines. Also need to support forces. |
| 82 void _animateTo(double target) { |
| 83 double remainingDistance = (target - timeline.value).abs(); |
| 84 timeline.stop(); |
| 85 if (remainingDistance != 0) |
| 86 timeline.animateTo(target, remainingDistance * durationMS); |
| 87 } |
| 88 } |
| 89 |
| 90 |
| OLD | NEW |