animations.dart 12.7 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 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:ui' show VoidCallback;

7 8
import 'animation.dart';
import 'curves.dart';
9 10
import 'listener_helpers.dart';

11 12
class _AlwaysCompleteAnimation extends Animation<double> {
  const _AlwaysCompleteAnimation();
13

14
  @override
15
  void addListener(VoidCallback listener) { }
16 17

  @override
18
  void removeListener(VoidCallback listener) { }
19 20

  @override
21
  void addStatusListener(AnimationStatusListener listener) { }
22 23

  @override
24
  void removeStatusListener(AnimationStatusListener listener) { }
25 26

  @override
27
  AnimationStatus get status => AnimationStatus.completed;
28 29

  @override
30 31 32
  double get value => 1.0;
}

33 34 35 36 37 38
/// An animation that is always complete.
///
/// Using this constant involves less overhead than building an
/// [AnimationController] with an initial value of 1.0. This is useful when an
/// API expects an animation but you don't actually want to animate anything.
const Animation<double> kAlwaysCompleteAnimation = const _AlwaysCompleteAnimation();
39

40 41
class _AlwaysDismissedAnimation extends Animation<double> {
  const _AlwaysDismissedAnimation();
42

43
  @override
44
  void addListener(VoidCallback listener) { }
45 46

  @override
47
  void removeListener(VoidCallback listener) { }
48 49

  @override
50
  void addStatusListener(AnimationStatusListener listener) { }
51 52

  @override
53
  void removeStatusListener(AnimationStatusListener listener) { }
54 55

  @override
56
  AnimationStatus get status => AnimationStatus.dismissed;
57 58

  @override
59 60 61
  double get value => 0.0;
}

62 63 64 65 66 67
/// An animation that is always dismissed.
///
/// Using this constant involves less overhead than building an
/// [AnimationController] with an initial value of 0.0. This is useful when an
/// API expects an animation but you don't actually want to animate anything.
const Animation<double> kAlwaysDismissedAnimation = const _AlwaysDismissedAnimation();
68

69
/// An animation that is always stopped at a given value.
70
class AlwaysStoppedAnimation<T> extends Animation<T> {
71 72
  const AlwaysStoppedAnimation(this.value);

73
  @override
74
  final T value;
75

76
  @override
77
  void addListener(VoidCallback listener) { }
78 79

  @override
80
  void removeListener(VoidCallback listener) { }
81 82

  @override
83
  void addStatusListener(AnimationStatusListener listener) { }
84 85

  @override
86
  void removeStatusListener(AnimationStatusListener listener) { }
87 88

  @override
89 90 91
  AnimationStatus get status => AnimationStatus.forward;
}

92 93 94
/// Implements most of the [Animation] interface, by deferring its behavior to a
/// given [parent] Animation. To implement an [Animation] that proxies to a
/// parent, this class plus implementing "T get value" is all that is necessary.
95
abstract class AnimationWithParentMixin<T> {
96 97 98 99 100 101
  /// The animation whose value this animation will proxy.
  ///
  /// This animation must remain the same for the lifetime of this object. If
  /// you wish to proxy a different animation at different times, conside using
  /// [ProxyAnimation].
  Animation<T> get parent;
102 103 104 105 106 107 108 109 110

  void addListener(VoidCallback listener) => parent.addListener(listener);
  void removeListener(VoidCallback listener) => parent.removeListener(listener);
  void addStatusListener(AnimationStatusListener listener) => parent.addStatusListener(listener);
  void removeStatusListener(AnimationStatusListener listener) => parent.removeStatusListener(listener);

  AnimationStatus get status => parent.status;
}

111 112 113 114 115 116
/// An animation that is a proxy for another animation.
///
/// A proxy animation is useful because the parent animation can be mutated. For
/// example, one object can create a proxy animation, hand the proxy to another
/// object, and then later change the animation from which the proxy receieves
/// its value.
117
class ProxyAnimation extends Animation<double>
118
  with AnimationLazyListenerMixin, AnimationLocalListenersMixin, AnimationLocalStatusListenersMixin {
119
  ProxyAnimation([Animation<double> animation]) {
120 121
    _parent = animation;
    if (_parent == null) {
122
      _status = AnimationStatus.dismissed;
123 124 125 126
      _value = 0.0;
    }
  }

127
  AnimationStatus _status;
128 129
  double _value;

130 131 132 133
  /// The animation whose value this animation will proxy.
  ///
  /// This value is mutable. When mutated, the listeners on the proxy animation
  /// will be transparently updated to be listening to the new parent animation.
134 135 136 137
  Animation<double> get parent => _parent;
  Animation<double> _parent;
  void set parent(Animation<double> value) {
    if (value == _parent)
138
      return;
139 140 141
    if (_parent != null) {
      _status = _parent.status;
      _value = _parent.value;
142 143 144
      if (isListening)
        didStopListening();
    }
145 146
    _parent = value;
    if (_parent != null) {
147 148
      if (isListening)
        didStartListening();
149
      if (_value != _parent.value)
150
        notifyListeners();
151 152
      if (_status != _parent.status)
        notifyStatusListeners(_parent.status);
153 154 155 156 157
      _status = null;
      _value = null;
    }
  }

158
  @override
159
  void didStartListening() {
160 161 162
    if (_parent != null) {
      _parent.addListener(notifyListeners);
      _parent.addStatusListener(notifyStatusListeners);
163 164 165
    }
  }

166
  @override
167
  void didStopListening() {
168 169 170
    if (_parent != null) {
      _parent.removeListener(notifyListeners);
      _parent.removeStatusListener(notifyStatusListeners);
171 172 173
    }
  }

174
  @override
175
  AnimationStatus get status => _parent != null ? _parent.status : _status;
176 177

  @override
178
  double get value => _parent != null ? _parent.value : _value;
179 180
}

181 182 183 184 185 186 187
/// An animation that is the reverse of another animation.
///
/// If the parent animation is running forward from 0.0 to 1.0, this animation
/// is running in reverse from 1.0 to 0.0. Notice that using a ReverseAnimation
/// is different from simply using a [Tween] with a begin of 1.0 and an end of
/// 0.0 because the tween does not change the status or direction of the
/// animation.
188
class ReverseAnimation extends Animation<double>
189
  with AnimationLazyListenerMixin, AnimationLocalStatusListenersMixin {
190
  ReverseAnimation(this.parent);
191

192
  /// The animation whose value and direction this animation is reversing.
193
  final Animation<double> parent;
194

195
  @override
196 197
  void addListener(VoidCallback listener) {
    didRegisterListener();
198
    parent.addListener(listener);
199
  }
200 201

  @override
202
  void removeListener(VoidCallback listener) {
203
    parent.removeListener(listener);
204 205 206
    didUnregisterListener();
  }

207
  @override
208
  void didStartListening() {
209
    parent.addStatusListener(_statusChangeHandler);
210 211
  }

212
  @override
213
  void didStopListening() {
214
    parent.removeStatusListener(_statusChangeHandler);
215 216
  }

217
  void _statusChangeHandler(AnimationStatus status) {
218 219 220
    notifyStatusListeners(_reverseStatus(status));
  }

221
  @override
222
  AnimationStatus get status => _reverseStatus(parent.status);
223 224

  @override
225
  double get value => 1.0 - parent.value;
226

227
  AnimationStatus _reverseStatus(AnimationStatus status) {
228
    switch (status) {
229 230 231 232
      case AnimationStatus.forward: return AnimationStatus.reverse;
      case AnimationStatus.reverse: return AnimationStatus.forward;
      case AnimationStatus.completed: return AnimationStatus.dismissed;
      case AnimationStatus.dismissed: return AnimationStatus.completed;
233 234 235 236
    }
  }
}

237 238 239 240 241
/// An animation that applies a curve to another animation.
///
/// [CurvedAnimation] is useful when you wish to apply a [Curve] and you already
/// have the underlying animation object. If you don't yet have an animation and
/// want to apply a [Curve] to a [Tween], consider using [CurveTween].
242
class CurvedAnimation extends Animation<double> with AnimationWithParentMixin<double> {
243 244 245 246 247 248 249 250 251 252
  CurvedAnimation({
    this.parent,
    this.curve: Curves.linear,
    this.reverseCurve
  }) {
    assert(parent != null);
    assert(curve != null);
    parent.addStatusListener(_handleStatusChanged);
  }

253
  /// The animation to which this animation applies a curve.
254
  @override
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
  final Animation<double> parent;

  /// The curve to use in the forward direction.
  Curve curve;

  /// The curve to use in the reverse direction.
  ///
  /// If this field is null, uses [curve] in both directions.
  Curve reverseCurve;

  /// The direction used to select the current curve.
  ///
  /// The curve direction is only reset when we hit the beginning or the end of
  /// the timeline to avoid discontinuities in the value of any variables this
  /// a animation is used to animate.
Adam Barth's avatar
Adam Barth committed
270
  AnimationStatus _curveDirection;
271 272 273 274 275 276 277 278

  void _handleStatusChanged(AnimationStatus status) {
    switch (status) {
      case AnimationStatus.dismissed:
      case AnimationStatus.completed:
        _curveDirection = null;
        break;
      case AnimationStatus.forward:
Adam Barth's avatar
Adam Barth committed
279
        _curveDirection ??= AnimationStatus.forward;
280 281
        break;
      case AnimationStatus.reverse:
Adam Barth's avatar
Adam Barth committed
282
        _curveDirection ??= AnimationStatus.reverse;
283 284 285 286
        break;
    }
  }

287
  @override
288
  double get value {
Adam Barth's avatar
Adam Barth committed
289
    final bool useForwardCurve = reverseCurve == null || (_curveDirection ?? parent.status) != AnimationStatus.reverse;
290 291 292 293 294 295 296 297 298 299 300 301 302
    Curve activeCurve = useForwardCurve ? curve : reverseCurve;

    double t = parent.value;
    if (activeCurve == null)
      return t;
    if (t == 0.0 || t == 1.0) {
      assert(activeCurve.transform(t).round() == t);
      return t;
    }
    return activeCurve.transform(t);
  }
}

303 304 305 306 307 308 309 310 311 312 313 314
enum _TrainHoppingMode { minimize, maximize }

/// This animation starts by proxying one animation, but can be given a
/// second animation. When their times cross (either because the second is
/// going in the opposite direction, or because the one overtakes the other),
/// the animation hops over to proxying the second animation, and the second
/// animation becomes the new "first" performance.
///
/// Since this object must track the two animations even when it has no
/// listeners of its own, instead of shutting down when all its listeners are
/// removed, it exposes a [dispose()] method. Call this method to shut this
/// object down.
315
class TrainHoppingAnimation extends Animation<double>
316
  with AnimationEagerListenerMixin, AnimationLocalListenersMixin, AnimationLocalStatusListenersMixin {
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
  TrainHoppingAnimation(this._currentTrain, this._nextTrain, { this.onSwitchedTrain }) {
    assert(_currentTrain != null);
    if (_nextTrain != null) {
      if (_currentTrain.value > _nextTrain.value) {
        _mode = _TrainHoppingMode.maximize;
      } else {
        _mode = _TrainHoppingMode.minimize;
        if (_currentTrain.value == _nextTrain.value) {
          _currentTrain = _nextTrain;
          _nextTrain = null;
        }
      }
    }
    _currentTrain.addStatusListener(_statusChangeHandler);
    _currentTrain.addListener(_valueChangeHandler);
    if (_nextTrain != null)
      _nextTrain.addListener(_valueChangeHandler);
    assert(_mode != null);
  }

337
  /// The animation that is current driving this animation.
338 339 340
  Animation<double> get currentTrain => _currentTrain;
  Animation<double> _currentTrain;
  Animation<double> _nextTrain;
341 342
  _TrainHoppingMode _mode;

343
  /// Called when this animation switches to be driven by a different animation.
344 345
  VoidCallback onSwitchedTrain;

346 347
  AnimationStatus _lastStatus;
  void _statusChangeHandler(AnimationStatus status) {
348 349 350 351 352 353 354 355
    assert(_currentTrain != null);
    if (status != _lastStatus) {
      notifyListeners();
      _lastStatus = status;
    }
    assert(_lastStatus != null);
  }

356
  @override
357
  AnimationStatus get status => _currentTrain.status;
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

  double _lastValue;
  void _valueChangeHandler() {
    assert(_currentTrain != null);
    bool hop = false;
    if (_nextTrain != null) {
      switch (_mode) {
        case _TrainHoppingMode.minimize:
          hop = _nextTrain.value <= _currentTrain.value;
          break;
        case _TrainHoppingMode.maximize:
          hop = _nextTrain.value >= _currentTrain.value;
          break;
      }
      if (hop) {
        _currentTrain.removeStatusListener(_statusChangeHandler);
        _currentTrain.removeListener(_valueChangeHandler);
        _currentTrain = _nextTrain;
376 377 378
        // TODO(hixie): This should be setting a status listener on the next
        // train, not a value listener, and it should pass in _statusChangeHandler,
        // not _valueChangeHandler
379 380 381 382 383 384 385 386 387 388 389 390 391 392
        _nextTrain.addListener(_valueChangeHandler);
        _statusChangeHandler(_nextTrain.status);
      }
    }
    double newValue = value;
    if (newValue != _lastValue) {
      notifyListeners();
      _lastValue = newValue;
    }
    assert(_lastValue != null);
    if (hop && onSwitchedTrain != null)
      onSwitchedTrain();
  }

393
  @override
394 395 396 397
  double get value => _currentTrain.value;

  /// Frees all the resources used by this performance.
  /// After this is called, this object is no longer usable.
398
  @override
399 400 401 402 403 404 405 406 407 408 409
  void dispose() {
    assert(_currentTrain != null);
    _currentTrain.removeStatusListener(_statusChangeHandler);
    _currentTrain.removeListener(_valueChangeHandler);
    _currentTrain = null;
    if (_nextTrain != null) {
      _nextTrain.removeListener(_valueChangeHandler);
      _nextTrain = null;
    }
  }
}