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 _animationId; | |
| 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 (_animationId != null) { | |
| 30 scheduler.cancelAnimationFrame(_animationId); | |
| 31 _animationId = 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(_animationId != null); | |
| 48 _animationId = null; | |
| 49 | |
| 50 _onTick(timeStamp); | |
| 51 | |
| 52 if (isTicking) | |
| 53 _scheduleTick(); | |
| 54 } | |
| 55 | |
| 56 void _scheduleTick() { | |
| 57 assert(isTicking); | |
| 58 assert(_animationId == null); | |
| 59 _animationId = 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 _onTick(_value); | |
| 80 } | |
| 81 | |
| 82 double _initialDelay; | |
| 83 double _duration; | |
| 84 double _begin; | |
| 85 double _end; | |
| 86 Curve _curve; | |
|
Matt Perry
2015/07/09 17:38:08
This class seems overly complicated to me. If it's
abarth-chromium
2015/07/09 17:48:30
Ok. I'll remove that functionality.
| |
| 87 | |
| 88 double _startTime; | |
| 89 double _t; | |
| 90 | |
| 91 Future start({ | |
| 92 double initialDelay: 0.0, | |
| 93 double duration, | |
| 94 double begin: 0.0, | |
| 95 double end: 1.0, | |
| 96 Curve curve: linear | |
| 97 }) { | |
| 98 assert(initialDelay != null); | |
| 99 assert(duration != null && duration > 0.0); | |
| 100 assert(begin != null); | |
| 101 assert(end != null); | |
| 102 assert(curve != null); | |
| 103 | |
| 104 assert(!_ticker.isTicking); | |
| 105 | |
| 106 _initialDelay = initialDelay; | |
| 107 _duration = duration; | |
| 108 _begin = begin; | |
| 109 _end = end; | |
| 110 _curve = curve; | |
| 111 | |
| 112 _startTime = null; | |
| 113 _t = 0.0; | |
| 114 _value = begin; | |
| 115 | |
| 116 return _ticker.start(); | |
| 117 } | |
| 118 | |
| 119 Future animateTo(double target, { | |
| 120 double initialDelay: 0.0, | |
| 121 double duration, | |
| 122 Curve curve: linear | |
| 123 }) { | |
| 124 return start(initialDelay: initialDelay, | |
| 125 duration: duration, | |
| 126 begin: _value, | |
| 127 end: target, | |
| 128 curve: curve); | |
| 129 } | |
| 130 | |
| 131 void stop() { | |
| 132 _initialDelay = null; | |
| 133 _duration = null; | |
| 134 _begin = null; | |
| 135 _end = null; | |
| 136 _curve = null; | |
| 137 | |
| 138 _startTime = null; | |
| 139 _t = null; | |
| 140 | |
| 141 _ticker.stop(); | |
| 142 } | |
| 143 | |
| 144 bool get isAnimating => _t != null; | |
| 145 | |
| 146 void _tick(double timeStamp) { | |
| 147 if (_startTime == null) | |
| 148 _startTime = timeStamp; | |
| 149 | |
| 150 _t = ((timeStamp - (_startTime + _initialDelay)) / _duration).clamp(0.0, 1.0 ); | |
| 151 | |
| 152 final bool isLastTick = _t >= 1.0; | |
| 153 | |
| 154 _value = isLastTick ? _end : _begin + (_end - _begin) * _curve.transform(_t) ; | |
| 155 _onTick(_value); | |
| 156 | |
| 157 if (isLastTick) | |
| 158 stop(); | |
| 159 } | |
| 160 } | |
| OLD | NEW |