Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: sky/sdk/lib/animation/animation_performance.dart

Issue 1211603003: Baby steps towards an odeon-like animation system. First victim: Drawer. (Closed) Base URL: git@github.com:/domokit/mojo.git@master
Patch Set: better lerp Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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);
Hixie 2015/06/30 23:11:56 If the range is 0.0..1.0, it should probably be ca
12 }
13
14 class AnimatedType<T> extends AnimatedVariable {
15 T value;
16 T begin, end;
17 Curve curve;
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
Hixie 2015/06/30 23:11:56 Is the idea that for each possible physical simula
Matt Perry 2015/07/01 18:17:43 Dunno. odeon has "disengageWithForce", which is wh
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;
Hixie 2015/06/30 23:11:56 Or use a Duration?
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;
Hixie 2015/06/30 23:11:56 0.0
58 bool get isPresented => timeline.value == 1;
Hixie 2015/06/30 23:11:56 1.0
59 bool get isAnimating => timeline.isAnimating;
60
61 void present() {
62 _animateTo(1.0);
63 }
64 void dismiss() {
65 _animateTo(0.0);
66 }
Hixie 2015/06/30 23:11:56 present and dismiss don't sound like opposites. Ma
Matt Perry 2015/07/01 18:17:43 Went with "play" and "reverse".
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.
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)
81 timeline.animateTo(target, duration, curve: linear);
82 }
83
84 // TODO(mpcomplete): fix animateTo to better handle resuming an animation,
Hixie 2015/06/30 23:11:56 "better" how?
Matt Perry 2015/07/01 18:17:43 Hmm.. I forgot what I meant by this. I'll take it
85 // with timelines. Also need to support forces.
86 void _animateTo(double target) {
Hixie 2015/06/30 23:11:56 move this closer to present/dismiss, since they're
Matt Perry 2015/07/01 18:17:43 Done.
87 double remainingDistance = (target - timeline.value).abs();
88 timeline.stop();
89 if (remainingDistance != 0)
90 timeline.animateTo(target, remainingDistance * durationMS);
91 }
92 }
93
94
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698