animated_value.dart 2.86 KB
Newer Older
1 2 3 4 5 6 7
// 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 "dart:sky";

import 'package:sky/animation/curves.dart';
8
import 'package:sky/animation/direction.dart';
9 10
import 'package:sky/base/lerp.dart';

11 12
export 'package:sky/animation/curves.dart' show Interval;

13
abstract class AnimatedVariable {
14
  void setProgress(double t, Direction direction);
15 16 17
  String toString();
}

18 19
class AnimationTiming {
  AnimationTiming({this.interval, this.reverseInterval, this.curve, this.reverseCurve});
20 21 22 23 24 25

  Interval interval;
  Interval reverseInterval;
  Curve curve;
  Curve reverseCurve;

26
  double transform(double t, Direction direction) {
27 28 29 30 31 32 33 34 35 36
    Interval interval = _getInterval(direction);
    if (interval != null)
      t = interval.transform(t);
    if (t == 1.0) // Or should we support inverse curves?
      return t;
    Curve curve = _getCurve(direction);
    if (curve != null)
      t = curve.transform(t);
    return t;
  }
37

38 39 40 41
  Interval _getInterval(Direction direction) {
    if (direction == Direction.forward || reverseInterval == null)
      return interval;
    return reverseInterval;
42 43
  }

44 45 46 47
  Curve _getCurve(Direction direction) {
    if (direction == Direction.forward || reverseCurve == null)
      return curve;
    return reverseCurve;
48 49 50
  }
}

51
class AnimatedValue<T extends dynamic> extends AnimationTiming implements AnimatedVariable {
52 53
  AnimatedValue(this.begin, { this.end, Interval interval, Curve curve, Curve reverseCurve })
    : super(interval: interval, curve: curve, reverseCurve: reverseCurve) {
54 55 56 57 58 59 60
    value = begin;
  }

  T value;
  T begin;
  T end;

61 62 63
  T lerp(double t) => begin + (end - begin) * t;

  void setProgress(double t, Direction direction) {
64
    if (end != null) {
65
      t = transform(t, direction);
66
      value = (t == 1.0) ? end : lerp(t);
67 68 69 70 71 72
    }
  }

  String toString() => 'AnimatedValue(begin=$begin, end=$end, value=$value)';
}

73
class AnimatedList extends AnimationTiming implements AnimatedVariable {
74 75
  List<AnimatedVariable> variables;

76 77
  AnimatedList(this.variables, { Interval interval, Curve curve, Curve reverseCurve })
    : super(interval: interval, curve: curve, reverseCurve: reverseCurve);
78

79
  void setProgress(double t, Direction direction) {
80
    double adjustedTime = transform(t, direction);
81
    for (AnimatedVariable variable in variables)
82
      variable.setProgress(adjustedTime, direction);
83 84 85 86 87
  }

  String toString() => 'AnimatedList([$variables])';
}

88
class AnimatedColorValue extends AnimatedValue<Color> {
89
  AnimatedColorValue(Color begin, { Color end, Curve curve })
90 91
    : super(begin, end: end, curve: curve);

92
  Color lerp(double t) => lerpColor(begin, end, t);
93
}
94 95

class AnimatedRect extends AnimatedValue<Rect> {
96
  AnimatedRect(Rect begin, { Rect end, Curve curve })
97 98
    : super(begin, end: end, curve: curve);

99
  Rect lerp(double t) => lerpRect(begin, end, t);
100
}