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 abstract class Generator { |
| 11 Stream<double> get onTick; |
| 12 void cancel(); |
| 13 } |
| 14 |
| 15 class FrameGenerator extends Generator { |
11 Function onDone; | 16 Function onDone; |
12 StreamController _controller; | 17 StreamController _controller; |
13 | 18 |
14 Stream<double> get onTick => _controller.stream; | 19 Stream<double> get onTick => _controller.stream; |
15 | 20 |
16 int _animationId = 0; | 21 int _animationId = 0; |
17 bool _cancelled = false; | 22 bool _cancelled = false; |
18 | 23 |
19 FrameGenerator({this.onDone}) { | 24 FrameGenerator({this.onDone}) { |
20 _controller = new StreamController( | 25 _controller = new StreamController( |
(...skipping 23 matching lines...) Expand all Loading... |
44 | 49 |
45 void _tick(double timeStamp) { | 50 void _tick(double timeStamp) { |
46 _animationId = 0; | 51 _animationId = 0; |
47 _controller.add(timeStamp); | 52 _controller.add(timeStamp); |
48 if (!_cancelled) { | 53 if (!_cancelled) { |
49 _scheduleTick(); | 54 _scheduleTick(); |
50 } | 55 } |
51 } | 56 } |
52 } | 57 } |
53 | 58 |
54 class AnimationGenerator { | 59 class AnimationGenerator extends Generator { |
55 Stream<double> get onTick => _stream; | 60 Stream<double> get onTick => _stream; |
56 final double initialDelay; | 61 final double initialDelay; |
57 final double duration; | 62 final double duration; |
58 final double begin; | 63 final double begin; |
59 final double end; | 64 final double end; |
60 final Curve curve; | 65 final Curve curve; |
61 | 66 |
62 FrameGenerator _generator; | 67 FrameGenerator _generator; |
63 Stream<double> _stream; | 68 Stream<double> _stream; |
64 bool _done = false; | 69 bool _done = false; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 // This is required because Dart Streams don't have takeUntil (inclusive). | 105 // This is required because Dart Streams don't have takeUntil (inclusive). |
101 bool _checkForCompletion(double t) { | 106 bool _checkForCompletion(double t) { |
102 if (_done) | 107 if (_done) |
103 return false; | 108 return false; |
104 | 109 |
105 _done = t >= 1; | 110 _done = t >= 1; |
106 return true; | 111 return true; |
107 } | 112 } |
108 } | 113 } |
109 | 114 |
110 class Animation { | 115 class Simulation extends Generator { |
111 Stream<double> get onValueChanged => _controller.stream; | 116 Stream<double> get onTick => _stream; |
| 117 final System system; |
112 | 118 |
113 double get value => _value; | 119 FrameGenerator _generator; |
| 120 Stream<double> _stream; |
| 121 double _previousTime = 0.0; |
114 | 122 |
115 void set value(double value) { | 123 Simulation(this.system, {Function terminationCondition, Function onDone}) { |
116 stop(); | 124 _generator = new FrameGenerator(onDone: onDone); |
117 _setValue(value); | 125 _stream = _generator.onTick.map(_update); |
118 } | |
119 | 126 |
120 bool get isAnimating => _animation != null; | 127 if (terminationCondition != null) { |
121 | 128 bool done = false; |
122 StreamController _controller = new StreamController(sync: true); | 129 _stream = _stream.takeWhile((_) { |
123 | 130 if (done) |
124 AnimationGenerator _animation; | 131 return false; |
125 | 132 done = terminationCondition(); |
126 double _value; | 133 return true; |
127 | 134 }); |
128 void _setValue(double value) { | |
129 _value = value; | |
130 _controller.add(_value); | |
131 } | |
132 | |
133 void stop() { | |
134 if (_animation != null) { | |
135 _animation.cancel(); | |
136 _animation = null; | |
137 } | 135 } |
138 } | 136 } |
139 | 137 |
140 void animateTo(double newValue, double duration, | 138 void cancel() { |
141 { Curve curve: linear, double initialDelay: 0.0 }) { | 139 _generator.cancel(); |
142 stop(); | 140 } |
143 | 141 |
144 _animation = new AnimationGenerator( | 142 double _update(double timeStamp) { |
145 duration: duration, | 143 double previousTime = _previousTime; |
146 begin: _value, | 144 _previousTime = timeStamp; |
147 end: newValue, | 145 if (previousTime == 0.0) |
148 curve: curve, | 146 return timeStamp; |
149 initialDelay: initialDelay); | 147 double deltaT = timeStamp - previousTime; |
150 | 148 system.update(deltaT); |
151 _animation.onTick.listen(_setValue, onDone: () { | 149 return timeStamp; |
152 _animation = null; | |
153 }); | |
154 } | 150 } |
155 } | 151 } |
OLD | NEW |