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

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: less curves 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 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) : _onTick = onTick {
66 _ticker = new Ticker(_tick);
67 }
68
69 final Function _onTick;
70 Ticker _ticker;
71
72 double _value;
73 double get value => _value;
74 void set value(double newValue) {
75 assert(newValue != null && newValue >= 0.0 && newValue <= 1.0);
76 assert(!_ticker.isTicking);
77 _value = newValue;
78 _onTick(_value);
79 }
80
81 double _duration;
82 double _begin;
83 double _end;
84
85 double _startTime;
86
87 Future start({
88 double duration,
89 double begin: 0.0,
Matt Perry 2015/07/09 17:52:57 I don't even think we need a begin/end value. Alwa
abarth-chromium 2015/07/09 17:55:37 AnimationPerformance uses different begin and end
Matt Perry 2015/07/09 18:10:39 Oh right. Makes sense.
90 double end: 1.0
91 }) {
92 assert(duration != null && duration > 0.0);
93 assert(begin != null && begin >= 0.0 && begin <= 1.0);
94 assert(end != null && end >= 0.0 && end <= 1.0);
95 assert(begin <= end);
96
97 assert(!_ticker.isTicking);
98
99 _duration = duration;
100 _begin = begin;
101 _end = end;
102
103 _startTime = null;
104 _value = begin;
105
106 return _ticker.start();
107 }
108
109 Future animateTo(double target, { double duration }) {
110 return start(duration: duration, begin: _value, end: target);
111 }
112
113 void stop() {
114 _duration = null;
115 _begin = null;
116 _end = null;
117 _startTime = null;
118 _ticker.stop();
119 }
120
121 bool get isAnimating => _ticker.isTicking;
122
123 void _tick(double timeStamp) {
124 if (_startTime == null)
125 _startTime = timeStamp;
126
127 final double t = ((timeStamp - _startTime) / _duration).clamp(0.0, 1.0);
128 final bool isLastTick = t >= 1.0;
129
130 _value = isLastTick ? _end : _begin + (_end - _begin) * t;
131 _onTick(_value);
132
133 if (isLastTick)
134 stop();
135 }
136 }
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