| 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 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import '../base/scheduler.dart' as scheduler; | 7 import '../base/scheduler.dart' as scheduler; |
| 8 import 'curves.dart'; | 8 import 'curves.dart'; |
| 9 import 'mechanics.dart'; | 9 import 'mechanics.dart'; |
| 10 | 10 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 | 50 |
| 51 void _tick(double timeStamp) { | 51 void _tick(double timeStamp) { |
| 52 _animationId = 0; | 52 _animationId = 0; |
| 53 _controller.add(timeStamp); | 53 _controller.add(timeStamp); |
| 54 if (!_cancelled) { | 54 if (!_cancelled) { |
| 55 _scheduleTick(); | 55 _scheduleTick(); |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 | 59 |
| 60 class AnimationGenerator extends Generator { | |
| 61 Stream<double> get onTick => _stream; | |
| 62 final double initialDelay; | |
| 63 final double duration; | |
| 64 final double begin; | |
| 65 final double end; | |
| 66 final Curve curve; | |
| 67 | |
| 68 FrameGenerator _generator; | |
| 69 Stream<double> _stream; | |
| 70 bool _done = false; | |
| 71 double _lastTime; | |
| 72 | |
| 73 AnimationGenerator({ | |
| 74 this.initialDelay: 0.0, | |
| 75 this.duration, | |
| 76 this.begin: 0.0, | |
| 77 this.end: 1.0, | |
| 78 this.curve: linear, | |
| 79 Function onDone | |
| 80 }) { | |
| 81 assert(curve != null); | |
| 82 assert(duration != null && duration > 0.0); | |
| 83 _generator = new FrameGenerator(onDone: onDone); | |
| 84 | |
| 85 double startTime = 0.0; | |
| 86 _stream = _generator.onTick.map((timeStamp) { | |
| 87 if (startTime == 0.0) | |
| 88 startTime = timeStamp; | |
| 89 | |
| 90 double t = (timeStamp - (startTime + initialDelay)) / duration; | |
| 91 _lastTime = t.clamp(0.0, 1.0); | |
| 92 return _lastTime; | |
| 93 }) | |
| 94 .takeWhile(_checkForCompletion) | |
| 95 .where((t) => t >= 0.0) | |
| 96 .map(_transform); | |
| 97 } | |
| 98 | |
| 99 double get remainingTime { | |
| 100 if (_lastTime == null) | |
| 101 return duration; | |
| 102 return duration - _lastTime; | |
| 103 } | |
| 104 | |
| 105 void cancel() { | |
| 106 _generator.cancel(); | |
| 107 } | |
| 108 | |
| 109 double _transform(double t) { | |
| 110 if (_done) | |
| 111 return end; | |
| 112 return begin + (end - begin) * curve.transform(t); | |
| 113 } | |
| 114 | |
| 115 // This is required because Dart Streams don't have takeUntil (inclusive). | |
| 116 bool _checkForCompletion(double t) { | |
| 117 if (_done) | |
| 118 return false; | |
| 119 | |
| 120 _done = t >= 1; | |
| 121 return true; | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 class Simulation extends Generator { | 60 class Simulation extends Generator { |
| 126 Stream<double> get onTick => _stream; | 61 Stream<double> get onTick => _stream; |
| 127 final System system; | 62 final System system; |
| 128 | 63 |
| 129 FrameGenerator _generator; | 64 FrameGenerator _generator; |
| 130 Stream<double> _stream; | 65 Stream<double> _stream; |
| 131 double _previousTime = 0.0; | 66 double _previousTime = 0.0; |
| 132 | 67 |
| 133 Simulation(this.system, {Function terminationCondition, Function onDone}) { | 68 Simulation(this.system, {Function terminationCondition, Function onDone}) { |
| 134 _generator = new FrameGenerator(onDone: onDone); | 69 _generator = new FrameGenerator(onDone: onDone); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 152 double _update(double timeStamp) { | 87 double _update(double timeStamp) { |
| 153 double previousTime = _previousTime; | 88 double previousTime = _previousTime; |
| 154 _previousTime = timeStamp; | 89 _previousTime = timeStamp; |
| 155 if (previousTime == 0.0) | 90 if (previousTime == 0.0) |
| 156 return timeStamp; | 91 return timeStamp; |
| 157 double deltaT = timeStamp - previousTime; | 92 double deltaT = timeStamp - previousTime; |
| 158 system.update(deltaT); | 93 system.update(deltaT); |
| 159 return timeStamp; | 94 return timeStamp; |
| 160 } | 95 } |
| 161 } | 96 } |
| OLD | NEW |