/
ball.dart
422 lines (348 loc) · 11.8 KB
/
ball.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import 'dart:math';
import 'dart:ui' as ui;
import 'package:canvas_clock/clock.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
const arrivalDuration = Duration(milliseconds: 920),
departureDuration = Duration(milliseconds: 1242),
arrivalCurve = AccelerationCurve(),
departureCurve = Curves.decelerate,
travelCurve = AccelerationCurve(),
bounceAwayDuration = Duration(milliseconds: 626),
bounceBackDuration = Duration(milliseconds: 547),
bounceAwayCurve = ElasticOutCurve(.67),
bounceBackCurve = Curves.bounceOut;
class Ball extends LeafRenderObjectWidget {
final Animation<double> travelAnimation, arrivalAnimation, departureAnimation;
final BallTrips trips;
final Color primaryColor,
secondaryColor,
dotsIdleColor,
dotsPrimedColor,
dotsDisengagedColor,
shadowColor;
const Ball({
Key key,
@required this.travelAnimation,
@required this.arrivalAnimation,
@required this.departureAnimation,
@required this.trips,
@required this.primaryColor,
@required this.secondaryColor,
@required this.dotsIdleColor,
@required this.dotsPrimedColor,
@required this.dotsDisengagedColor,
@required this.shadowColor,
}) : assert(travelAnimation != null),
assert(arrivalAnimation != null),
assert(departureAnimation != null),
assert(trips != null),
assert(primaryColor != null),
assert(secondaryColor != null),
assert(dotsIdleColor != null),
assert(dotsPrimedColor != null),
assert(dotsDisengagedColor != null),
assert(shadowColor != null),
super(key: key);
@override
RenderBall createRenderObject(BuildContext context) {
return RenderBall(
travelAnimation: travelAnimation,
arrivalAnimation: arrivalAnimation,
departureAnimation: departureAnimation,
trips: trips,
primaryColor: primaryColor,
secondaryColor: secondaryColor,
dotsIdleColor: dotsIdleColor,
dotsPrimedColor: dotsPrimedColor,
dotsDisengagedColor: dotsDisengagedColor,
shadowColor: shadowColor,
);
}
@override
void updateRenderObject(BuildContext context, RenderBall renderObject) {
renderObject
..primaryColor = primaryColor
..secondaryColor = secondaryColor
..dotsIdleColor = dotsIdleColor
..dotsPrimedColor = dotsPrimedColor
..dotsIdleColor = dotsIdleColor
..shadowColor = shadowColor;
}
}
/// For information on what this is see [RenderBall].
enum BallTripStage {
travel,
arrival,
departure,
}
/// A way to count the ball's trips while being able to
/// pass a pointer to the count around.
///
/// This is needed to account for the fact that the ball does not
/// rotate an integer amount of full rotations while rolling
/// along the track.
///
/// A [ValueNotifier] would also do the job, but I do not want
/// to notify. Hence, this is the simpler option.
class BallTrips {
BallTrips([this.count = 0]);
double count;
}
class BallParentData extends ClockChildrenParentData {
Offset startPosition, endPosition, destination;
/// Declares the radius that the circular ball should use.
///
/// See [RenderBall.isRepaintBoundary] for why it is necessary
/// to send this instead of sizing the ball to it.
double radius;
}
/// Renders a ball moving about the scene.
///
/// The movement of the ball is separated into three stages:
///
/// 1. Travel, which brings the ball from the end point to the start point.
/// 1. Arrival, which brings the ball from the start point to its destination.
/// 1. Departure, which brings the ball away from the destination to the end point.
///
/// The ball also rotates to resemble rolling and this rotation is calculated
/// by taking the circumference of the circle, the distance of the current stage, and
/// the progress of the current movement.
class RenderBall
extends RenderCompositionChild<ClockComponent, BallParentData> {
final Animation<double> travelAnimation, arrivalAnimation, departureAnimation;
final BallTrips trips;
RenderBall({
this.travelAnimation,
this.arrivalAnimation,
this.departureAnimation,
this.trips,
Color primaryColor,
Color secondaryColor,
Color dotsIdleColor,
Color dotsPrimedColor,
Color dotsDisengagedColor,
Color shadowColor,
}) : _primaryColor = primaryColor,
_secondaryColor = secondaryColor,
_dotsIdleColor = dotsIdleColor,
_dotsPrimedColor = dotsPrimedColor,
_dotsDisengagedColor = dotsDisengagedColor,
_shadowColor = shadowColor,
super(ClockComponent.ball);
Color _primaryColor,
_secondaryColor,
_dotsIdleColor,
_dotsPrimedColor,
_dotsDisengagedColor,
_shadowColor;
set primaryColor(Color value) {
assert(value != null);
if (_primaryColor == value) {
return;
}
_primaryColor = value;
markNeedsPaint();
}
set secondaryColor(Color value) {
assert(value != null);
if (_secondaryColor == value) {
return;
}
_secondaryColor = value;
markNeedsPaint();
}
set dotsIdleColor(Color value) {
assert(value != null);
if (_dotsIdleColor == value) {
return;
}
_dotsIdleColor = value;
markNeedsPaint();
}
set dotsPrimedColor(Color value) {
assert(value != null);
if (_dotsPrimedColor == value) {
return;
}
_dotsPrimedColor = value;
markNeedsPaint();
}
set dotsDisengagedColor(Color value) {
assert(value != null);
if (_dotsDisengagedColor == value) {
return;
}
_dotsDisengagedColor = value;
markNeedsPaint();
}
set shadowColor(Color value) {
assert(value != null);
if (_shadowColor == value) {
return;
}
_shadowColor = value;
markNeedsPaint();
}
/// Ensures that the constant repaints from the ball
/// do not cause the whole composition to repaint.
///
/// It would be possible layout the ball with a box
/// enclosing just its radius and calling [markNeedsLayout]
/// to update its position instead.
/// I did it like this before, but I do not
/// want the ball causing the whole clock face to
/// repaint - same goes for the analog clock.
/// The reason this would happen is that a repaint
/// boundary is not a relayout boundary and thus
/// [markNeedsLayout] will also mark the parent as dirty.
@override
bool get isRepaintBoundary => true;
@override
void attach(PipelineOwner owner) {
super.attach(owner);
compositionData.hasSemanticsInformation = false;
travelAnimation.addListener(markNeedsPaint);
arrivalAnimation.addListener(markNeedsPaint);
departureAnimation.addListener(markNeedsPaint);
}
@override
void detach() {
travelAnimation.removeListener(markNeedsPaint);
arrivalAnimation.removeListener(markNeedsPaint);
departureAnimation.removeListener(markNeedsPaint);
super.detach();
}
@override
bool get sizedByParent => false;
double _radius, _totalDistance;
Tween<Offset> _travelTween, _arrivalTween, _departureTween;
double _travelDistance, _arrivalDistance, _departureDistance;
BallTripStage stage;
@override
void performLayout() {
size = constraints.biggest;
// The data passed only updates in ClockComposition.performLayout.
_radius = compositionData.radius;
_travelTween = Tween(
begin: compositionData.endPosition,
end: compositionData.startPosition,
);
_arrivalTween = Tween(
begin: compositionData.startPosition,
end: compositionData.destination,
);
_departureTween = Tween(
begin: compositionData.destination,
end: compositionData.endPosition,
);
_travelDistance = _travelTween.distance;
// Negative as the ball rolls backwards along this path.
_arrivalDistance = -_arrivalTween.distance;
_departureDistance = -_departureTween.distance;
_totalDistance = _travelDistance + _arrivalDistance + _departureDistance;
}
List<Color> get shaderColors => [_primaryColor, _secondaryColor];
@override
void paint(PaintingContext context, Offset offset) {
final canvas = context.canvas;
canvas.save();
var translation = offset;
double distanceTraveled;
if (departureAnimation.status == AnimationStatus.forward) {
translation += _departureTween.evaluate(departureAnimation);
distanceTraveled = _travelDistance +
_arrivalDistance +
_departureDistance * departureAnimation.value;
stage = BallTripStage.departure;
} else if (travelAnimation.status == AnimationStatus.forward) {
translation += _travelTween.evaluate(travelAnimation);
distanceTraveled = _travelDistance * travelAnimation.value;
stage = BallTripStage.travel;
} else {
translation += _arrivalTween.evaluate(arrivalAnimation);
distanceTraveled =
_travelDistance + _arrivalDistance * arrivalAnimation.value;
stage = BallTripStage.arrival;
}
// Translate to the center of the ball.
canvas.translate(translation.dx, translation.dy);
// This is the circumference of the ball. Basically,
// it is its length when unwrapping its circle.
final ballLength = _radius * 2 * pi;
final rect = Rect.fromCircle(center: Offset.zero, radius: _radius);
// Rotate the ball as if it rolled along the slides.
// This value can be greater than 1, but I imagine that
// leaving that is cheaper (regarding performance) than
// using modulo.
// It is fine because the Canvas.rotate also takes any multiples
// of the rotation value and accepts it.
final progress = (distanceTraveled +
// After every trip there will probably be some additional
// distance that is not evenly divisible by the circumference,
// which would cause the rotation to visually reset if
// not accounted for.
(_totalDistance % ballLength) * trips.count)
// The ball needs to rotate once for every circumference
// on the track.
/
ballLength,
path = Path()..addOval(rect);
canvas.drawShadow(path, _shadowColor, _radius / 6, false);
canvas.rotate(2 * pi * progress);
canvas.drawPath(
path,
Paint()
..shader = kIsWeb
// The kIsWeb section in here is irrelevant for the submission,
// but I want to be able to host the clock face as a demo using
// Flutter web and Flutter web does not currently support sweep gradients.
// See https://github.com/flutter/flutter/issues/41389.
? ui.Gradient.radial(
rect.center, rect.shortestSide / 2, shaderColors)
: SweepGradient(
startAngle: 0,
endAngle: pi / 2,
colors: shaderColors,
tileMode: TileMode.mirror,
).createShader(rect),
);
_drawDots(canvas);
canvas.restore();
}
/// Draws small dots on two sides
/// of the ball.
///
/// See [_drawDot].
void _drawDots(Canvas canvas) {
_drawDot(canvas, 0);
_drawDot(canvas, pi);
}
Color get dotColor {
switch (stage) {
case BallTripStage.travel:
return _dotsIdleColor;
case BallTripStage.arrival:
return _dotsPrimedColor;
case BallTripStage.departure:
return _dotsDisengagedColor;
}
throw ArgumentError.value(stage);
}
/// Draw small dot onto the ball.
/// The point is to indicate the rotation (rolling)
/// even when the radial shader has to be used because
/// of lacking Flutter web support.
void _drawDot(Canvas canvas, double angle) {
canvas.save();
canvas.rotate(angle);
canvas.drawOval(
Rect.fromCircle(
center: Offset(0, _radius * 5 / 7),
radius: _radius / 9,
),
Paint()..color = dotColor);
canvas.restore();
}
}