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

Side by Side Diff: sky/framework/animation/generators.dart

Issue 1132063007: Rationalize Dart mojo and sky package structure (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 7 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
« no previous file with comments | « sky/framework/animation/fling_curve.dart ('k') | sky/framework/animation/mechanics.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 'curves.dart';
6 import 'dart:async';
7 import 'dart:math' as math;
8 import 'dart:sky' as sky;
9 import 'mechanics.dart';
10
11 abstract class Generator {
12 Stream<double> get onTick;
13 void cancel();
14 }
15
16 class FrameGenerator extends Generator {
17 Function onDone;
18 StreamController _controller;
19
20 Stream<double> get onTick => _controller.stream;
21
22 int _animationId = 0;
23 bool _cancelled = false;
24
25 FrameGenerator({this.onDone}) {
26 _controller = new StreamController(
27 sync: true,
28 onListen: _scheduleTick,
29 onCancel: cancel);
30 }
31
32 void cancel() {
33 if (_cancelled) {
34 return;
35 }
36 if (_animationId != 0) {
37 sky.window.cancelAnimationFrame(_animationId);
38 }
39 _animationId = 0;
40 _cancelled = true;
41 if (onDone != null) {
42 onDone();
43 }
44 }
45
46 void _scheduleTick() {
47 assert(_animationId == 0);
48 _animationId = sky.window.requestAnimationFrame(_tick);
49 }
50
51 void _tick(double timeStamp) {
52 _animationId = 0;
53 _controller.add(timeStamp);
54 if (!_cancelled) {
55 _scheduleTick();
56 }
57 }
58 }
59
60 class AnimationGenerator extends Generator {
61 Stream<double> get onTick => _stream;
62 final double initialDelay;
63 final double duration;
64 final double begin;
65 final double end;
66 final Curve curve;
67
68 FrameGenerator _generator;
69 Stream<double> _stream;
70 bool _done = false;
71 double _lastTime;
72
73 AnimationGenerator({
74 this.initialDelay: 0.0,
75 this.duration,
76 this.begin: 0.0,
77 this.end: 1.0,
78 this.curve: linear,
79 Function onDone
80 }) {
81 assert(curve != null);
82 assert(duration != null && duration > 0.0);
83 _generator = new FrameGenerator(onDone: onDone);
84
85 double startTime = 0.0;
86 _stream = _generator.onTick.map((timeStamp) {
87 if (startTime == 0.0)
88 startTime = timeStamp;
89
90 double t = (timeStamp - (startTime + initialDelay)) / duration;
91 _lastTime = math.max(0.0, math.min(t, 1.0));
92 return _lastTime;
93 })
94 .takeWhile(_checkForCompletion)
95 .where((t) => t >= 0.0)
96 .map(_transform);
97 }
98
99 double get remainingTime {
100 if (_lastTime == null)
101 return duration;
102 return duration - _lastTime;
103 }
104
105 void cancel() {
106 _generator.cancel();
107 }
108
109 double _transform(double t) {
110 if (_done)
111 return end;
112 return begin + (end - begin) * curve.transform(t);
113 }
114
115 // This is required because Dart Streams don't have takeUntil (inclusive).
116 bool _checkForCompletion(double t) {
117 if (_done)
118 return false;
119
120 _done = t >= 1;
121 return true;
122 }
123 }
124
125 class Simulation extends Generator {
126 Stream<double> get onTick => _stream;
127 final System system;
128
129 FrameGenerator _generator;
130 Stream<double> _stream;
131 double _previousTime = 0.0;
132
133 Simulation(this.system, {Function terminationCondition, Function onDone}) {
134 _generator = new FrameGenerator(onDone: onDone);
135 _stream = _generator.onTick.map(_update);
136
137 if (terminationCondition != null) {
138 bool done = false;
139 _stream = _stream.takeWhile((_) {
140 if (done)
141 return false;
142 done = terminationCondition();
143 return true;
144 });
145 }
146 }
147
148 void cancel() {
149 _generator.cancel();
150 }
151
152 double _update(double timeStamp) {
153 double previousTime = _previousTime;
154 _previousTime = timeStamp;
155 if (previousTime == 0.0)
156 return timeStamp;
157 double deltaT = timeStamp - previousTime;
158 system.update(deltaT);
159 return timeStamp;
160 }
161 }
OLDNEW
« no previous file with comments | « sky/framework/animation/fling_curve.dart ('k') | sky/framework/animation/mechanics.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698