| 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..627e70d0d5c4f138bfe7c787f8acf7f92b00b10a
|
| --- /dev/null
|
| +++ b/sky/sdk/lib/animation/animation_performance.dart
|
| @@ -0,0 +1,90 @@
|
| +// 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): make this an interface.
|
| +// TODO(mpcomplete): find a better name, or reconcile this with AnimatedValue.
|
| +class AnimationVariable {
|
| + double value;
|
| + double begin, end;
|
| + Curve curve = linear;
|
| +
|
| + AnimationVariable(this.begin, this.end, {this.curve}) {
|
| + 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
|
| +// simulation to take over the progression.
|
| +class AnimationPerformance {
|
| + // TODO(mpcomplete): make this a list, or composable somehow.
|
| + AnimationVariable 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;
|
| +
|
| + 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;
|
| + bool get isPresented => timeline.value == 1;
|
| + bool get isAnimating => timeline.isAnimating;
|
| +
|
| + void present() {
|
| + _animateTo(1.0);
|
| + }
|
| + void dismiss() {
|
| + _animateTo(0.0);
|
| + }
|
| +
|
| + 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,
|
| + // with timelines. Also need to support forces.
|
| + void _animateTo(double target) {
|
| + double remainingDistance = (target - timeline.value).abs();
|
| + timeline.stop();
|
| + if (remainingDistance != 0)
|
| + timeline.animateTo(target, remainingDistance * durationMS);
|
| + }
|
| +}
|
| +
|
| +
|
|
|