| 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 'package:newton/newton.dart'; |
| 8 |
| 9 import 'timeline.dart'; |
| 10 |
| 11 const double _kSecondsPerMillisecond = 1000.0; |
| 12 |
| 13 class AnimatedSimulation { |
| 14 |
| 15 AnimatedSimulation(Function onTick) : _onTick = onTick { |
| 16 _ticker = new Ticker(_tick); |
| 17 } |
| 18 |
| 19 final Function _onTick; |
| 20 Ticker _ticker; |
| 21 |
| 22 Simulation _simulation; |
| 23 double _startTime; |
| 24 |
| 25 double _value = 0.0; |
| 26 double get value => _value; |
| 27 |
| 28 Future start(Simulation simulation) { |
| 29 assert(simulation != null); |
| 30 assert(!_ticker.isTicking); |
| 31 _simulation = simulation; |
| 32 _startTime = null; |
| 33 _value = simulation.x(0.0); |
| 34 return _ticker.start(); |
| 35 } |
| 36 |
| 37 void stop() { |
| 38 _simulation = null; |
| 39 _startTime = null; |
| 40 _value = 0.0; |
| 41 _ticker.stop(); |
| 42 } |
| 43 |
| 44 bool get isAnimating => _ticker.isTicking; |
| 45 |
| 46 void _tick(double timeStamp) { |
| 47 if (_startTime == null) |
| 48 _startTime = timeStamp; |
| 49 |
| 50 double timeInSeconds = (timeStamp - _startTime) / _kSecondsPerMillisecond; |
| 51 _value = _simulation.x(timeInSeconds); |
| 52 final bool isLastTick = _simulation.isDone(timeInSeconds); |
| 53 |
| 54 _onTick(_value); |
| 55 |
| 56 if (isLastTick) |
| 57 stop(); |
| 58 } |
| 59 |
| 60 } |
| OLD | NEW |