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

Unified 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, 6 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 side-by-side diff with in-line comments
Download patch
Index: sky/sdk/lib/animation/animation_performance.dart
diff --git a/sky/sdk/lib/animation/animation_performance.dart b/sky/sdk/lib/animation/animation_performance.dart
new file mode 100644
index 0000000000000000000000000000000000000000..6ebbb141aabf3af219243a76939863149b70cfca
--- /dev/null
+++ b/sky/sdk/lib/animation/animation_performance.dart
@@ -0,0 +1,94 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'animated_value.dart';
+import 'curves.dart';
+
+// TODO(mpcomplete): merge this stuff with AnimatedValue somehow. We shouldn't
+// have 2 different ways to animate values.
+abstract class AnimatedVariable {
+ void setPercentage(double t);
Hixie 2015/06/30 23:11:56 If the range is 0.0..1.0, it should probably be ca
+}
+
+class AnimatedType<T> extends AnimatedVariable {
+ T value;
+ T begin, end;
+ Curve curve;
+
+ AnimatedType(this.begin, this.end, {this.curve: linear}) {
+ value = begin;
+ }
+
+ void setPercentage(double t) {
+ // TODO(mpcomplete): Reverse the timeline and curve.
+ value = begin + (end - begin) * curve.transform(t);
+ }
+}
+
+// This class manages a "performance" - a collection of values that change
+// based on a timeline. For example, a performance may handle an animation
+// of a menu opening by sliding and fading in (changing Y value and opacity)
+// over .5 seconds. The performance can move forwards (present) or backwards
+// (dismiss). A consumer may also take direct control of the timeline by
+// 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
+// simulation to take over the progression.
+class AnimationPerformance {
+ // TODO(mpcomplete): make this a list, or composable somehow.
+ AnimatedVariable variable;
+ // Advances from 0 to 1. On each tick, we'll update our variable's values.
+ AnimatedValue timeline = new AnimatedValue(0.0);
+ // TODO(mpcomplete): duration should be on a director.
+ // TODO(mpcomplete): maybe this should be in seconds?
+ double durationMS;
Hixie 2015/06/30 23:11:56 Or use a Duration?
+
+ AnimationPerformance() {
+ timeline.onValueChanged.listen((double t) {
+ variable.setPercentage(t);
+ });
+ }
+
+ double get percentage => timeline.value;
+ void set percentage(double t) {
+ stop();
+ timeline.value = t.clamp(0.0, 1.0);
+ }
+
+ bool get isDismissed => timeline.value == 0;
Hixie 2015/06/30 23:11:56 0.0
+ bool get isPresented => timeline.value == 1;
Hixie 2015/06/30 23:11:56 1.0
+ bool get isAnimating => timeline.isAnimating;
+
+ void present() {
+ _animateTo(1.0);
+ }
+ void dismiss() {
+ _animateTo(0.0);
+ }
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".
+
+ void stop() {
+ timeline.stop();
+ }
+
+ // Resume animating in a direction, with the given velocity.
+ // TODO(mpcomplete): this should be a force with friction so it slows over
+ // time.
+ void fling({double velocity: 1.0}) {
+ double target = velocity.sign < 0.0 ? 0.0 : 1.0;
+ double distance = (target - timeline.value).abs();
+ double duration = distance / velocity.abs();
+
+ if (distance > 0)
+ timeline.animateTo(target, duration, curve: linear);
+ }
+
+ // 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
+ // with timelines. Also need to support forces.
+ 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.
+ double remainingDistance = (target - timeline.value).abs();
+ timeline.stop();
+ if (remainingDistance != 0)
+ timeline.animateTo(target, remainingDistance * durationMS);
+ }
+}
+
+

Powered by Google App Engine
This is Rietveld 408576698