OLD | NEW |
| (Empty) |
1 part of widgets; | |
2 | |
3 class FrameGenerator { | |
4 | |
5 Function onDone; | |
6 StreamController _controller; | |
7 | |
8 Stream<double> get onTick => _controller.stream; | |
9 | |
10 int _animationId = 0; | |
11 bool _cancelled = false; | |
12 | |
13 FrameGenerator({this.onDone}) { | |
14 _controller = new StreamController( | |
15 sync: true, | |
16 onListen: _scheduleTick, | |
17 onCancel: cancel); | |
18 } | |
19 | |
20 void cancel() { | |
21 if (_cancelled) { | |
22 return; | |
23 } | |
24 if (_animationId != 0) { | |
25 sky.window.cancelAnimationFrame(_animationId); | |
26 } | |
27 _animationId = 0; | |
28 _cancelled = true; | |
29 if (onDone != null) { | |
30 onDone(); | |
31 } | |
32 } | |
33 | |
34 void _scheduleTick() { | |
35 assert(_animationId == 0); | |
36 _animationId = sky.window.requestAnimationFrame(_tick); | |
37 } | |
38 | |
39 void _tick(double timeStamp) { | |
40 _animationId = 0; | |
41 _controller.add(timeStamp); | |
42 if (!_cancelled) { | |
43 _scheduleTick(); | |
44 } | |
45 } | |
46 } | |
47 | |
48 class AnimationGenerator extends FrameGenerator { | |
49 | |
50 Stream<double> get onTick => _stream; | |
51 final double duration; | |
52 final double begin; | |
53 final double end; | |
54 final Curve curve; | |
55 Stream<double> _stream; | |
56 bool _done = false; | |
57 | |
58 AnimationGenerator(this.duration, { | |
59 this.begin: 0.0, | |
60 this.end: 1.0, | |
61 this.curve: linear, | |
62 Function onDone | |
63 }):super(onDone: onDone) { | |
64 double startTime = 0.0; | |
65 double targetTime = 0.0; | |
66 _stream = super.onTick.map((timeStamp) { | |
67 if (startTime == 0.0) { | |
68 startTime = timeStamp; | |
69 targetTime = startTime + duration; | |
70 } | |
71 return math.min((timeStamp - startTime) / duration, 1.0); | |
72 }) | |
73 .takeWhile(_checkForCompletion) | |
74 .map((t) => begin + (end - begin) * curve.transform(t)); | |
75 } | |
76 | |
77 bool _checkForCompletion(double t) { | |
78 if (_done) | |
79 return false; | |
80 _done = t >= 1; | |
81 return true; | |
82 } | |
83 } | |
OLD | NEW |