Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(519)

Side by Side Diff: sky/sdk/lib/animation/timeline.dart

Issue 1226263003: Break dependency of AnimationPerformance on AnimatedValue (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: remove unused import Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
9 class Ticker {
10 Ticker(Function onTick) : _onTick = onTick;
11
12 final Function _onTick;
13
14 Completer _completer;
15 int _animationId;
16
17 Future start() {
18 assert(!isTicking);
19 _completer = new Completer();
20 _scheduleTick();
21 return _completer.future;
22 }
23
24 void stop() {
25 if (!isTicking)
26 return;
27
28 if (_animationId != null) {
29 scheduler.cancelAnimationFrame(_animationId);
30 _animationId = null;
31 }
32
33 Completer localCompleter = _completer;
34 _completer = null;
35
36 // We take the _completer into a local variable so that !isTicking when we
37 // actually complete the future.
38 assert(!isTicking);
39 localCompleter.complete();
40 }
41
42 bool get isTicking => _completer != null;
43
44 void _tick(double timeStamp) {
45 assert(isTicking);
46 assert(_animationId != null);
47 _animationId = null;
48
49 _onTick(timeStamp);
50
51 if (isTicking)
52 _scheduleTick();
53 }
54
55 void _scheduleTick() {
56 assert(isTicking);
57 assert(_animationId == null);
58 _animationId = scheduler.requestAnimationFrame(_tick);
59 }
60 }
61
62 class Timeline {
63
64 Timeline(Function onTick) : _onTick = onTick {
65 _ticker = new Ticker(_tick);
66 }
67
68 final Function _onTick;
69 Ticker _ticker;
70
71 double _value;
72 double get value => _value;
73 void set value(double newValue) {
74 assert(newValue != null && newValue >= 0.0 && newValue <= 1.0);
75 assert(!_ticker.isTicking);
76 _value = newValue;
77 _onTick(_value);
78 }
79
80 double _duration;
81 double _begin;
82 double _end;
83
84 double _startTime;
85
86 Future start({
87 double duration,
88 double begin: 0.0,
89 double end: 1.0
90 }) {
91 assert(duration != null && duration > 0.0);
92 assert(begin != null && begin >= 0.0 && begin <= 1.0);
93 assert(end != null && end >= 0.0 && end <= 1.0);
94 assert(begin <= end);
95
96 assert(!_ticker.isTicking);
97
98 _duration = duration;
99 _begin = begin;
100 _end = end;
101
102 _startTime = null;
103 _value = begin;
104
105 return _ticker.start();
106 }
107
108 Future animateTo(double target, { double duration }) {
109 return start(duration: duration, begin: _value, end: target);
110 }
111
112 void stop() {
113 _duration = null;
114 _begin = null;
115 _end = null;
116 _startTime = null;
117 _ticker.stop();
118 }
119
120 bool get isAnimating => _ticker.isTicking;
121
122 void _tick(double timeStamp) {
123 if (_startTime == null)
124 _startTime = timeStamp;
125
126 final double t = ((timeStamp - _startTime) / _duration).clamp(0.0, 1.0);
127 final bool isLastTick = t >= 1.0;
128
129 _value = isLastTick ? _end : _begin + (_end - _begin) * t;
130 _onTick(_value);
131
132 if (isLastTick)
133 stop();
134 }
135 }
OLDNEW
« no previous file with comments | « sky/sdk/lib/animation/animation_performance.dart ('k') | sky/sdk/lib/widgets/animated_component.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698