| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'curves.dart'; | 5 import 'curves.dart'; |
| 6 import 'dart:async'; | 6 import 'dart:async'; |
| 7 import 'dart:math' as math; | 7 import 'dart:math' as math; |
| 8 import 'dart:sky' as sky; | 8 import 'dart:sky' as sky; |
| 9 | 9 |
| 10 class FrameGenerator { | 10 class FrameGenerator { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 _animationId = 0; | 46 _animationId = 0; |
| 47 _controller.add(timeStamp); | 47 _controller.add(timeStamp); |
| 48 if (!_cancelled) { | 48 if (!_cancelled) { |
| 49 _scheduleTick(); | 49 _scheduleTick(); |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 } | 52 } |
| 53 | 53 |
| 54 class AnimationGenerator extends FrameGenerator { | 54 class AnimationGenerator extends FrameGenerator { |
| 55 Stream<double> get onTick => _stream; | 55 Stream<double> get onTick => _stream; |
| 56 final double initialDelay; |
| 56 final double duration; | 57 final double duration; |
| 57 final double begin; | 58 final double begin; |
| 58 final double end; | 59 final double end; |
| 59 final Curve curve; | 60 final Curve curve; |
| 60 Stream<double> _stream; | 61 Stream<double> _stream; |
| 61 bool _done = false; | 62 bool _done = false; |
| 62 | 63 |
| 63 AnimationGenerator(this.duration, { | 64 AnimationGenerator(this.duration, { |
| 65 this.initialDelay: 0.0, |
| 64 this.begin: 0.0, | 66 this.begin: 0.0, |
| 65 this.end: 1.0, | 67 this.end: 1.0, |
| 66 this.curve: linear, | 68 this.curve: linear, |
| 67 Function onDone | 69 Function onDone |
| 68 }):super(onDone: onDone) { | 70 }):super(onDone: onDone) { |
| 69 assert(duration > 0); | 71 assert(duration > 0); |
| 70 double startTime = 0.0; | 72 double startTime = 0.0; |
| 71 _stream = super.onTick.map((timeStamp) { | 73 _stream = super.onTick.map((timeStamp) { |
| 72 if (startTime == 0.0) | 74 if (startTime == 0.0) |
| 73 startTime = timeStamp; | 75 startTime = timeStamp; |
| 74 return math.min((timeStamp - startTime) / duration, 1.0); | 76 |
| 77 double t = (timeStamp - (startTime + initialDelay)) / duration; |
| 78 return math.max(0.0, math.min(t, 1.0)); |
| 75 }) | 79 }) |
| 76 .takeWhile(_checkForCompletion) | 80 .takeWhile(_checkForCompletion) // |
| 81 .where((t) => t >= 0.0) |
| 77 .map(_transform); | 82 .map(_transform); |
| 78 } | 83 } |
| 79 | 84 |
| 80 double _transform(double t) { | 85 double _transform(double t) { |
| 81 if (_done) | 86 if (_done) |
| 82 return end; | 87 return end; |
| 83 return begin + (end - begin) * curve.transform(t); | 88 return begin + (end - begin) * curve.transform(t); |
| 84 } | 89 } |
| 85 | 90 |
| 91 // This is required because Dart Streams don't have takeUntil (inclusive). |
| 86 bool _checkForCompletion(double t) { | 92 bool _checkForCompletion(double t) { |
| 87 if (_done) | 93 if (_done) |
| 88 return false; | 94 return false; |
| 95 |
| 89 _done = t >= 1; | 96 _done = t >= 1; |
| 90 return true; | 97 return true; |
| 91 } | 98 } |
| 92 } | 99 } |
| 100 |
| 101 class Animation { |
| 102 Stream<double> get onValueChanged => _controller.stream; |
| 103 |
| 104 double get value => _value; |
| 105 |
| 106 void set value(double value) { |
| 107 stop(); |
| 108 _setValue(value); |
| 109 } |
| 110 |
| 111 bool get isAnimating => _animation != null; |
| 112 |
| 113 StreamController _controller = new StreamController(sync: true); |
| 114 |
| 115 AnimationGenerator _animation; |
| 116 |
| 117 double _value; |
| 118 |
| 119 void _setValue(double value) { |
| 120 _value = value; |
| 121 _controller.add(_value); |
| 122 } |
| 123 |
| 124 void stop() { |
| 125 if (_animation != null) { |
| 126 _animation.cancel(); |
| 127 _animation = null; |
| 128 } |
| 129 } |
| 130 |
| 131 void animateTo(double newValue, double duration, |
| 132 { Curve curve: linear, double initialDelay: 0.0 }) { |
| 133 stop(); |
| 134 |
| 135 _animation = new AnimationGenerator(duration, begin: _value, end: newValue, |
| 136 curve: curve, initialDelay: initialDelay); |
| 137 |
| 138 _animation.onTick.listen(_setValue, onDone: () { |
| 139 _animation = null; |
| 140 }); |
| 141 } |
| 142 } |
| OLD | NEW |