Chromium Code Reviews| 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 'dart:async'; | |
| 6 | |
| 7 import '../base/scheduler.dart' as scheduler; | |
| 8 import 'curves.dart'; | |
| 9 | |
| 10 class Ticker { | |
| 11 Ticker(Function onTick) : _onTick = onTick; | |
| 12 | |
| 13 final Function _onTick; | |
| 14 | |
| 15 Completer _completer; | |
| 16 int _id; | |
|
eseidel
2015/07/09 16:55:17
Better name maybe? Didn't realize this was the ca
| |
| 17 | |
| 18 Future start() { | |
| 19 assert(!isTicking); | |
| 20 _completer = new Completer(); | |
| 21 _scheduleTick(); | |
| 22 return _completer.future; | |
| 23 } | |
| 24 | |
| 25 void stop() { | |
| 26 if (!isTicking) | |
| 27 return; | |
| 28 | |
| 29 if (_id != null) { | |
| 30 scheduler.cancelAnimationFrame(_id); | |
| 31 _id = null; | |
| 32 } | |
| 33 | |
| 34 Completer localCompleter = _completer; | |
| 35 _completer = null; | |
| 36 | |
| 37 // We take the _completer into a local variable so that !isTicking when we | |
| 38 // actually complete the future. | |
| 39 assert(!isTicking); | |
| 40 localCompleter.complete(); | |
| 41 } | |
| 42 | |
| 43 bool get isTicking => _completer != null; | |
| 44 | |
| 45 void _tick(double timeStamp) { | |
| 46 assert(isTicking); | |
| 47 assert(_id != null); | |
| 48 _id = null; | |
| 49 | |
| 50 _onTick(timeStamp); | |
| 51 | |
| 52 if (isTicking) | |
| 53 _scheduleTick(); | |
| 54 } | |
| 55 | |
| 56 void _scheduleTick() { | |
| 57 assert(isTicking); | |
| 58 assert(_id == null); | |
| 59 _id = scheduler.requestAnimationFrame(_tick); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 class Timeline { | |
| 64 | |
| 65 Timeline(Function onTick, { double initialValue: 0.0 }) | |
| 66 : _onTick = onTick, _value = initialValue { | |
| 67 _ticker = new Ticker(_tick); | |
| 68 } | |
| 69 | |
| 70 final Function _onTick; | |
| 71 Ticker _ticker; | |
| 72 | |
| 73 double _value; | |
| 74 double get value => _value; | |
| 75 void set value(double newValue) { | |
| 76 assert(newValue != null); | |
| 77 assert(!_ticker.isTicking); | |
| 78 _value = newValue; | |
| 79 } | |
| 80 | |
| 81 double _initialDelay; | |
| 82 double _duration; | |
| 83 double _begin; | |
| 84 double _end; | |
| 85 Curve _curve; | |
| 86 | |
| 87 double _startTime; | |
| 88 double _time; | |
| 89 | |
| 90 Future start({ | |
| 91 double initialDelay: 0.0, | |
| 92 double duration, | |
| 93 double begin: 0.0, | |
| 94 double end: 1.0, | |
| 95 Curve curve: linear | |
| 96 }) { | |
| 97 assert(initialDelay != null); | |
| 98 assert(duration != null && duration > 0.0); | |
| 99 assert(begin != null); | |
| 100 assert(end != null); | |
| 101 assert(curve != null); | |
| 102 | |
| 103 assert(!_ticker.isTicking); | |
| 104 | |
| 105 _initialDelay = initialDelay; | |
| 106 _duration = duration; | |
| 107 _begin = begin; | |
| 108 _end = end; | |
| 109 _curve = curve; | |
| 110 | |
| 111 _startTime = null; | |
| 112 _time = 0.0; | |
| 113 _value = begin; | |
| 114 | |
| 115 return _ticker.start(); | |
| 116 } | |
| 117 | |
| 118 Future animateTo(double target, { | |
| 119 double initialDelay: 0.0, | |
| 120 double duration, | |
| 121 Curve curve: linear | |
| 122 }) { | |
| 123 return start(initialDelay: initialDelay, | |
| 124 duration: duration, | |
| 125 begin: _value, | |
| 126 end: target, | |
| 127 curve: curve); | |
| 128 } | |
| 129 | |
| 130 void stop() { | |
| 131 _initialDelay = null; | |
| 132 _duration = null; | |
| 133 _begin = null; | |
| 134 _end = null; | |
| 135 _curve = null; | |
| 136 | |
| 137 _startTime = null; | |
| 138 _time = null; | |
| 139 | |
| 140 _ticker.stop(); | |
| 141 } | |
| 142 | |
| 143 double get remainingTime { | |
| 144 if (_duration == null || _time == null) | |
| 145 return 0.0; | |
| 146 return _duration - _time; | |
|
eseidel
2015/07/09 16:55:17
Wrong.
| |
| 147 } | |
| 148 | |
| 149 bool get isAnimating => _time != null; | |
| 150 | |
| 151 void _tick(double timeStamp) { | |
| 152 if (_startTime == null) | |
| 153 _startTime = timeStamp; | |
| 154 | |
| 155 double t = ((timeStamp - (_startTime + _initialDelay)) / _duration).clamp(0. 0, 1.0); | |
| 156 _time = t; | |
| 157 | |
| 158 final bool isLastTick = t >= 1.0; | |
| 159 | |
| 160 _value = isLastTick ? _end : _begin + (_end - _begin) * _curve.transform(t); | |
| 161 _onTick(_value); | |
| 162 | |
| 163 if (isLastTick) | |
| 164 stop(); | |
| 165 } | |
| 166 } | |
| OLD | NEW |