| 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 |
| 11 abstract class Generator { | 11 abstract class Generator { |
| 12 Stream<double> get onTick; // TODO(ianh): rename this to tickStream | 12 Stream<double> get onTick; // TODO(ianh): rename this to tickStream |
| 13 void cancel(); | 13 void cancel(); |
| 14 } | 14 } |
| 15 | 15 |
| 16 class FrameGenerator extends Generator { | 16 class FrameGenerator extends Generator { |
| 17 Function onDone; | 17 Function onDone; |
| 18 StreamController _controller; | 18 StreamController _controller; |
| 19 | 19 |
| 20 @override |
| 20 Stream<double> get onTick => _controller.stream; | 21 Stream<double> get onTick => _controller.stream; |
| 21 | 22 |
| 22 int _animationId = 0; | 23 int _animationId = 0; |
| 23 bool _cancelled = false; | 24 bool _cancelled = false; |
| 24 | 25 |
| 25 FrameGenerator({this.onDone}) { | 26 FrameGenerator({this.onDone}) { |
| 26 _controller = new StreamController( | 27 _controller = new StreamController( |
| 27 sync: true, | 28 sync: true, |
| 28 onListen: _scheduleTick, | 29 onListen: _scheduleTick, |
| 29 onCancel: cancel); | 30 onCancel: cancel); |
| 30 } | 31 } |
| 31 | 32 |
| 33 @override |
| 32 void cancel() { | 34 void cancel() { |
| 33 if (_cancelled) { | 35 if (_cancelled) { |
| 34 return; | 36 return; |
| 35 } | 37 } |
| 36 if (_animationId != 0) { | 38 if (_animationId != 0) { |
| 37 scheduler.cancelAnimationFrame(_animationId); | 39 scheduler.cancelAnimationFrame(_animationId); |
| 38 } | 40 } |
| 39 _animationId = 0; | 41 _animationId = 0; |
| 40 _cancelled = true; | 42 _cancelled = true; |
| 41 if (onDone != null) { | 43 if (onDone != null) { |
| 42 onDone(); | 44 onDone(); |
| 43 } | 45 } |
| 44 } | 46 } |
| 45 | 47 |
| 46 void _scheduleTick() { | 48 void _scheduleTick() { |
| 47 assert(_animationId == 0); | 49 assert(_animationId == 0); |
| 48 _animationId = scheduler.requestAnimationFrame(_tick); | 50 _animationId = scheduler.requestAnimationFrame(_tick); |
| 49 } | 51 } |
| 50 | 52 |
| 51 void _tick(double timeStamp) { | 53 void _tick(double timeStamp) { |
| 52 _animationId = 0; | 54 _animationId = 0; |
| 53 _controller.add(timeStamp); | 55 _controller.add(timeStamp); |
| 54 if (!_cancelled) { | 56 if (!_cancelled) { |
| 55 _scheduleTick(); | 57 _scheduleTick(); |
| 56 } | 58 } |
| 57 } | 59 } |
| 58 } | 60 } |
| 59 | 61 |
| 60 class AnimationGenerator extends Generator { | 62 class AnimationGenerator extends Generator { |
| 63 @override |
| 61 Stream<double> get onTick => _stream; | 64 Stream<double> get onTick => _stream; |
| 62 final double initialDelay; | 65 final double initialDelay; |
| 63 final double duration; | 66 final double duration; |
| 64 final double begin; | 67 final double begin; |
| 65 final double end; | 68 final double end; |
| 66 final Curve curve; | 69 final Curve curve; |
| 67 | 70 |
| 68 FrameGenerator _generator; | 71 FrameGenerator _generator; |
| 69 Stream<double> _stream; | 72 Stream<double> _stream; |
| 70 bool _done = false; | 73 bool _done = false; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 95 .where((t) => t >= 0.0) | 98 .where((t) => t >= 0.0) |
| 96 .map(_transform); | 99 .map(_transform); |
| 97 } | 100 } |
| 98 | 101 |
| 99 double get remainingTime { | 102 double get remainingTime { |
| 100 if (_lastTime == null) | 103 if (_lastTime == null) |
| 101 return duration; | 104 return duration; |
| 102 return duration - _lastTime; | 105 return duration - _lastTime; |
| 103 } | 106 } |
| 104 | 107 |
| 108 @override |
| 105 void cancel() { | 109 void cancel() { |
| 106 _generator.cancel(); | 110 _generator.cancel(); |
| 107 } | 111 } |
| 108 | 112 |
| 109 double _transform(double t) { | 113 double _transform(double t) { |
| 110 if (_done) | 114 if (_done) |
| 111 return end; | 115 return end; |
| 112 return begin + (end - begin) * curve.transform(t); | 116 return begin + (end - begin) * curve.transform(t); |
| 113 } | 117 } |
| 114 | 118 |
| 115 // This is required because Dart Streams don't have takeUntil (inclusive). | 119 // This is required because Dart Streams don't have takeUntil (inclusive). |
| 116 bool _checkForCompletion(double t) { | 120 bool _checkForCompletion(double t) { |
| 117 if (_done) | 121 if (_done) |
| 118 return false; | 122 return false; |
| 119 | 123 |
| 120 _done = t >= 1; | 124 _done = t >= 1; |
| 121 return true; | 125 return true; |
| 122 } | 126 } |
| 123 } | 127 } |
| 124 | 128 |
| 125 class Simulation extends Generator { | 129 class Simulation extends Generator { |
| 130 @override |
| 126 Stream<double> get onTick => _stream; | 131 Stream<double> get onTick => _stream; |
| 127 final System system; | 132 final System system; |
| 128 | 133 |
| 129 FrameGenerator _generator; | 134 FrameGenerator _generator; |
| 130 Stream<double> _stream; | 135 Stream<double> _stream; |
| 131 double _previousTime = 0.0; | 136 double _previousTime = 0.0; |
| 132 | 137 |
| 133 Simulation(this.system, {Function terminationCondition, Function onDone}) { | 138 Simulation(this.system, {Function terminationCondition, Function onDone}) { |
| 134 _generator = new FrameGenerator(onDone: onDone); | 139 _generator = new FrameGenerator(onDone: onDone); |
| 135 _stream = _generator.onTick.map(_update); | 140 _stream = _generator.onTick.map(_update); |
| 136 | 141 |
| 137 if (terminationCondition != null) { | 142 if (terminationCondition != null) { |
| 138 bool done = false; | 143 bool done = false; |
| 139 _stream = _stream.takeWhile((_) { | 144 _stream = _stream.takeWhile((_) { |
| 140 if (done) | 145 if (done) |
| 141 return false; | 146 return false; |
| 142 done = terminationCondition(); | 147 done = terminationCondition(); |
| 143 return true; | 148 return true; |
| 144 }); | 149 }); |
| 145 } | 150 } |
| 146 } | 151 } |
| 147 | 152 |
| 153 @override |
| 148 void cancel() { | 154 void cancel() { |
| 149 _generator.cancel(); | 155 _generator.cancel(); |
| 150 } | 156 } |
| 151 | 157 |
| 152 double _update(double timeStamp) { | 158 double _update(double timeStamp) { |
| 153 double previousTime = _previousTime; | 159 double previousTime = _previousTime; |
| 154 _previousTime = timeStamp; | 160 _previousTime = timeStamp; |
| 155 if (previousTime == 0.0) | 161 if (previousTime == 0.0) |
| 156 return timeStamp; | 162 return timeStamp; |
| 157 double deltaT = timeStamp - previousTime; | 163 double deltaT = timeStamp - previousTime; |
| 158 system.update(deltaT); | 164 system.update(deltaT); |
| 159 return timeStamp; | 165 return timeStamp; |
| 160 } | 166 } |
| 161 } | 167 } |
| OLD | NEW |