| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 import 'curves.dart'; | |
| 6 import 'dart:async'; | |
| 7 import 'dart:math' as math; | |
| 8 import 'dart:sky' as sky; | |
| 9 | |
| 10 class FrameGenerator { | |
| 11 Function onDone; | |
| 12 StreamController _controller; | |
| 13 | |
| 14 Stream<double> get onTick => _controller.stream; | |
| 15 | |
| 16 int _animationId = 0; | |
| 17 bool _cancelled = false; | |
| 18 | |
| 19 FrameGenerator({this.onDone}) { | |
| 20 _controller = new StreamController( | |
| 21 sync: true, | |
| 22 onListen: _scheduleTick, | |
| 23 onCancel: cancel); | |
| 24 } | |
| 25 | |
| 26 void cancel() { | |
| 27 if (_cancelled) { | |
| 28 return; | |
| 29 } | |
| 30 if (_animationId != 0) { | |
| 31 sky.window.cancelAnimationFrame(_animationId); | |
| 32 } | |
| 33 _animationId = 0; | |
| 34 _cancelled = true; | |
| 35 if (onDone != null) { | |
| 36 onDone(); | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 void _scheduleTick() { | |
| 41 assert(_animationId == 0); | |
| 42 _animationId = sky.window.requestAnimationFrame(_tick); | |
| 43 } | |
| 44 | |
| 45 void _tick(double timeStamp) { | |
| 46 _animationId = 0; | |
| 47 _controller.add(timeStamp); | |
| 48 if (!_cancelled) { | |
| 49 _scheduleTick(); | |
| 50 } | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 class AnimationGenerator { | |
| 55 Stream<double> get onTick => _stream; | |
| 56 final double initialDelay; | |
| 57 final double duration; | |
| 58 final double begin; | |
| 59 final double end; | |
| 60 final Curve curve; | |
| 61 | |
| 62 FrameGenerator _generator; | |
| 63 Stream<double> _stream; | |
| 64 bool _done = false; | |
| 65 | |
| 66 AnimationGenerator({ | |
| 67 this.initialDelay: 0.0, | |
| 68 this.duration, | |
| 69 this.begin: 0.0, | |
| 70 this.end: 1.0, | |
| 71 this.curve: linear, | |
| 72 Function onDone | |
| 73 }) { | |
| 74 assert(duration != null && duration > 0.0); | |
| 75 _generator = new FrameGenerator(onDone: onDone); | |
| 76 | |
| 77 double startTime = 0.0; | |
| 78 _stream = _generator.onTick.map((timeStamp) { | |
| 79 if (startTime == 0.0) | |
| 80 startTime = timeStamp; | |
| 81 | |
| 82 double t = (timeStamp - (startTime + initialDelay)) / duration; | |
| 83 return math.max(0.0, math.min(t, 1.0)); | |
| 84 }) | |
| 85 .takeWhile(_checkForCompletion) | |
| 86 .where((t) => t >= 0.0) | |
| 87 .map(_transform); | |
| 88 } | |
| 89 | |
| 90 void cancel() { | |
| 91 _generator.cancel(); | |
| 92 } | |
| 93 | |
| 94 double _transform(double t) { | |
| 95 if (_done) | |
| 96 return end; | |
| 97 return begin + (end - begin) * curve.transform(t); | |
| 98 } | |
| 99 | |
| 100 // This is required because Dart Streams don't have takeUntil (inclusive). | |
| 101 bool _checkForCompletion(double t) { | |
| 102 if (_done) | |
| 103 return false; | |
| 104 | |
| 105 _done = t >= 1; | |
| 106 return true; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 class Animation { | |
| 111 Stream<double> get onValueChanged => _controller.stream; | |
| 112 | |
| 113 double get value => _value; | |
| 114 | |
| 115 void set value(double value) { | |
| 116 stop(); | |
| 117 _setValue(value); | |
| 118 } | |
| 119 | |
| 120 bool get isAnimating => _animation != null; | |
| 121 | |
| 122 StreamController _controller = new StreamController(sync: true); | |
| 123 | |
| 124 AnimationGenerator _animation; | |
| 125 | |
| 126 double _value; | |
| 127 | |
| 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 } | |
| 138 } | |
| 139 | |
| 140 void animateTo(double newValue, double duration, | |
| 141 { Curve curve: linear, double initialDelay: 0.0 }) { | |
| 142 stop(); | |
| 143 | |
| 144 _animation = new AnimationGenerator( | |
| 145 duration: duration, | |
| 146 begin: _value, | |
| 147 end: newValue, | |
| 148 curve: curve, | |
| 149 initialDelay: initialDelay); | |
| 150 | |
| 151 _animation.onTick.listen(_setValue, onDone: () { | |
| 152 _animation = null; | |
| 153 }); | |
| 154 } | |
| 155 } | |
| OLD | NEW |