-
Notifications
You must be signed in to change notification settings - Fork 470
/
flare_actor.dart
525 lines (463 loc) · 14.2 KB
/
flare_actor.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import 'dart:math';
import 'dart:typed_data';
import 'package:flare_flutter/flare_render_box.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flare_dart/actor_drawable.dart';
import 'package:flare_dart/math/mat2d.dart';
import 'package:flare_dart/math/aabb.dart';
import 'package:flutter/services.dart';
import 'asset_provider.dart';
import 'flare.dart';
import 'flare_controller.dart';
import 'provider/asset_flare.dart';
import 'provider/memory_flare.dart';
typedef void FlareCompletedCallback(String name);
/// A widget that displays a Flare.
///
/// Several constructors are provided for the various ways that a Flare can be
/// specified:
/// * [FlareActor], for obtaining a Flare from an asset [filename].
/// * [FlareActor.asset], for obtaining a Flare from an [AssetProvider].
/// * [FlareActor.bundle], for obtaining a Flare from an [AssetBundle].
/// * [FlareActor.memory], for obtaining a Flare from a [Uint8List].
class FlareActor extends LeafRenderObjectWidget {
/// Name of the Flare file to be loaded from the AssetBundle.
final String filename;
/// The Flare asset to display.
final AssetProvider flareProvider;
/// The name of the artboard to display.
final String artboard;
/// The name of the animation to play.
final String animation;
/// When true, the animation will be applied at the end of its duration.
final bool snapToEnd;
/// The BoxFit strategy used to scale the Flare content into the
/// bounds of this widget.
final BoxFit fit;
/// The alignment that will be applied in conjuction to the [fit] to align
/// the Flare content within the bounds of this widget.
final Alignment alignment;
/// When true, animations do not advance.
final bool isPaused;
/// When true, the Flare content will be clipped against the bounds of this
/// widget.
final bool shouldClip;
/// The [FlareController] used to drive animations/mixing/procedural hierarchy
/// manipulation of the Flare contents.
final FlareController controller;
/// Callback invoked when [animation] has completed. If [animation] is looping
/// this callback is never invoked.
final FlareCompletedCallback callback;
/// The color to override any fills/strokes with.
final Color color;
/// The name of the node to use to determine the bounds of the content.
/// When null it will default to the bounds of the artboard.
final String boundsNode;
/// When true the intrinsic size of the artboard will be used as the
/// dimensions of this widget.
final bool sizeFromArtboard;
/// When false disables antialiasing on drawables.
final bool useAntialias;
const FlareActor(
this.filename, {
this.boundsNode,
this.animation,
this.fit = BoxFit.contain,
this.alignment = Alignment.center,
this.isPaused = false,
this.snapToEnd = false,
this.controller,
this.callback,
this.color,
this.shouldClip = true,
this.sizeFromArtboard = false,
this.artboard,
this.useAntialias = true,
}) : flareProvider = null;
FlareActor.bundle(
String name, {
this.boundsNode,
this.animation,
this.fit = BoxFit.contain,
this.alignment = Alignment.center,
this.isPaused = false,
this.snapToEnd = false,
this.controller,
this.callback,
this.color,
this.shouldClip = true,
this.sizeFromArtboard = false,
this.artboard,
this.useAntialias = true,
AssetBundle bundle,
}) : filename = null,
flareProvider = AssetFlare(bundle: bundle ?? rootBundle, name: name);
FlareActor.memory(
Uint8List bytes, {
this.boundsNode,
this.animation,
this.fit = BoxFit.contain,
this.alignment = Alignment.center,
this.isPaused = false,
this.snapToEnd = false,
this.controller,
this.callback,
this.color,
this.shouldClip = true,
this.sizeFromArtboard = false,
this.artboard,
this.useAntialias = true,
}) : filename = null,
flareProvider = MemoryFlare(bytes: bytes);
const FlareActor.asset(
this.flareProvider, {
this.boundsNode,
this.animation,
this.fit = BoxFit.contain,
this.alignment = Alignment.center,
this.isPaused = false,
this.snapToEnd = false,
this.controller,
this.callback,
this.color,
this.shouldClip = true,
this.sizeFromArtboard = false,
this.artboard,
this.useAntialias = true,
}) : filename = null;
@override
RenderObject createRenderObject(BuildContext context) {
return FlareActorRenderObject()
..assetProvider =
flareProvider ?? AssetFlare(bundle: rootBundle, name: filename)
..fit = fit
..alignment = alignment
..animationName = animation
..snapToEnd = snapToEnd
..isPaused = isPaused
..controller = controller
..completed = callback
..color = color
..shouldClip = shouldClip
..boundsNodeName = boundsNode
..useIntrinsicSize = sizeFromArtboard
..artboardName = artboard
..useAntialias = useAntialias;
}
@override
void updateRenderObject(
BuildContext context, covariant FlareActorRenderObject renderObject) {
renderObject
..assetProvider =
flareProvider ?? AssetFlare(bundle: rootBundle, name: filename)
..fit = fit
..alignment = alignment
..animationName = animation
..snapToEnd = snapToEnd
..isPaused = isPaused
..color = color
..shouldClip = shouldClip
..boundsNodeName = boundsNode
..useIntrinsicSize = sizeFromArtboard
..artboardName = artboard
..useAntialias = useAntialias;
}
@override
void didUnmountRenderObject(covariant FlareActorRenderObject renderObject) {
renderObject.dispose();
}
}
class FlareAnimationLayer {
String name;
ActorAnimation animation;
double time = 0.0, mix = 0.0, mixSeconds = 0.2;
void apply(FlutterActorArtboard artboard) {
animation.apply(time, artboard, mix);
}
double get duration => animation.duration;
bool get isDone => time >= animation.duration;
}
class FlareActorRenderObject extends FlareRenderBox {
Mat2D _lastControllerViewTransform;
AssetProvider _assetProvider;
String _artboardName;
String _animationName;
String _boundsNodeName;
FlareController _controller;
FlareCompletedCallback _completedCallback;
bool snapToEnd = false;
bool _isPaused = false;
bool _useAntialias = true;
FlutterActor _actor;
String get artboardName => _artboardName;
set artboardName(String name) {
if (_artboardName == name) {
return;
}
_artboardName = name;
_instanceArtboard();
}
bool get isPaused => _isPaused;
set isPaused(bool value) {
if (_isPaused == value) {
return;
}
_isPaused = value;
updatePlayState();
}
bool get useAntialias => _useAntialias;
set useAntialias(bool value) {
if (value != _useAntialias) {
_useAntialias = value;
if (_artboard != null) {
_artboard.useAntialias = _useAntialias;
}
markNeedsPaint();
}
}
final List<FlareAnimationLayer> _animationLayers = [];
bool shouldClip;
FlutterActorArtboard _artboard;
AABB _setupAABB;
Color _color;
Color get color => _color;
set color(Color value) {
if (value != _color) {
_color = value;
if (_artboard != null) {
_artboard.overrideColor = value == null
? null
: Float32List.fromList([
value.red / 255.0,
value.green / 255.0,
value.blue / 255.0,
value.opacity
]);
}
markNeedsPaint();
}
}
String get boundsNodeName => _boundsNodeName;
set boundsNodeName(String value) {
if (_boundsNodeName == value) {
return;
}
_boundsNodeName = value;
if (_artboard != null) {
ActorNode node = _artboard.getNode(_boundsNodeName);
if (node is ActorDrawable) {
_setupAABB = node.computeAABB();
}
}
}
void updateBounds() {
if (_artboard != null) {
ActorNode node;
if (_boundsNodeName != null &&
(node = _artboard.getNode(_boundsNodeName)) is ActorDrawable) {
_setupAABB = (node as ActorDrawable).computeAABB();
} else {
_setupAABB = _artboard.artboardAABB();
}
}
}
String get animationName => _animationName;
set animationName(String value) {
if (value != _animationName) {
_animationName = value;
_updateAnimation();
}
}
/// We're playing if we're not paused and our controller is active (or
/// there's no controller) or there are animations running.
@override
bool get isPlaying =>
!_isPaused &&
((_controller?.isActive?.value ?? false) || _animationLayers.isNotEmpty);
void onControllerActiveChange() {
updatePlayState();
}
FlareController get controller => _controller;
set controller(FlareController c) {
if (_controller != c) {
_lastControllerViewTransform = c == null ? null : Mat2D();
_controller?.isActive?.removeListener(onControllerActiveChange);
_controller = c;
_controller?.isActive?.addListener(onControllerActiveChange);
if (_controller != null && _artboard != null) {
_controller.initialize(_artboard);
}
}
}
@override
void onUnload() {
_animationLayers.clear();
}
AssetProvider get assetProvider => _assetProvider;
set assetProvider(AssetProvider value) {
if (value == _assetProvider) {
return;
}
_assetProvider = value;
if (_assetProvider == null) {
markNeedsPaint();
}
// file will change, let's clear out old animations.
_animationLayers.clear();
load();
}
bool _instanceArtboard() {
if (_actor == null || _actor.artboard == null) {
return false;
}
FlutterActorArtboard artboard = _actor
.getArtboard(_artboardName)
.makeInstance() as FlutterActorArtboard;
artboard.initializeGraphics();
_artboard = artboard;
intrinsicSize = Size(artboard.width, artboard.height);
_artboard.overrideColor = _color == null
? null
: Float32List.fromList([
_color.red / 255.0,
_color.green / 255.0,
_color.blue / 255.0,
_color.opacity
]);
_artboard.useAntialias = _useAntialias;
if (_controller != null) {
_controller.initialize(_artboard);
}
_animationLayers.clear();
_updateAnimation(onlyWhenMissing: true);
// Immediately update the newly instanced artboard and compute
// bounds so that the widget can take up the necessary space
advance(0.0);
updateBounds();
markNeedsPaint();
return true;
}
@override
bool get canLoad {
return super.canLoad && _assetProvider != null;
}
/// Attempt a warm load, this is the optimal case when the
/// required asset is already in the cache.
@override
bool warmLoad() {
if (_assetProvider == null) {
return false;
}
_actor = getWarmFlare(_assetProvider);
return _instanceArtboard();
}
/// Load the necessary Flare file specified by [AssetProvider].
/// this occurs when the optimal warmLoad fails to find an asset in cache.
@override
Future<void> coldLoad() async {
if (_assetProvider == null) {
return;
}
_actor = await loadFlare(_assetProvider);
_instanceArtboard();
}
FlareCompletedCallback get completed => _completedCallback;
set completed(FlareCompletedCallback value) {
if (_completedCallback != value) {
_completedCallback = value;
}
}
@override
void advance(double elapsedSeconds) {
if (isPlaying) {
int lastFullyMixed = -1;
double lastMix = 0.0;
List<FlareAnimationLayer> completed = [];
for (int i = 0; i < _animationLayers.length; i++) {
FlareAnimationLayer layer = _animationLayers[i];
if (snapToEnd && !layer.animation.isLooping) {
layer.mix = 1.0;
layer.time = layer.duration;
} else {
layer.mix += elapsedSeconds;
layer.time += elapsedSeconds;
}
lastMix = (layer.mixSeconds == null || layer.mixSeconds == 0.0)
? 1.0
: min(1.0, layer.mix / layer.mixSeconds);
if (layer.animation.isLooping) {
layer.time %= layer.animation.duration;
}
layer.animation.apply(layer.time, _artboard, lastMix);
if (lastMix == 1.0) {
lastFullyMixed = i;
}
if (layer.time > layer.animation.duration) {
completed.add(layer);
}
}
if (lastFullyMixed != -1) {
_animationLayers.removeRange(0, lastFullyMixed);
}
if (animationName == null &&
_animationLayers.length == 1 &&
lastMix == 1.0) {
// Remove remaining animations.
_animationLayers.removeAt(0);
}
for (final FlareAnimationLayer animation in completed) {
_animationLayers.remove(animation);
if (_completedCallback != null) {
_completedCallback(animation.name);
}
}
}
if (_artboard != null &&
_controller != null &&
!_controller.advance(_artboard, elapsedSeconds)) {
_controller?.isActive?.value = false;
}
if (_artboard != null) {
_artboard.advance(elapsedSeconds);
}
}
@override
AABB get aabb => _setupAABB;
@override
void prePaint(Canvas canvas, Offset offset) {
if (shouldClip) {
canvas.clipRect(offset & size);
}
}
@override
void paintFlare(Canvas canvas, Mat2D viewTransform) {
if (_artboard == null) {
return;
}
if (controller != null &&
!Mat2D.areEqual(_lastControllerViewTransform, viewTransform)) {
Mat2D.copy(_lastControllerViewTransform, viewTransform);
controller?.setViewTransform(viewTransform);
}
_artboard.draw(canvas);
}
void _updateAnimation({bool onlyWhenMissing = false}) {
if (onlyWhenMissing && _animationLayers.isNotEmpty) {
return;
}
if (_animationName != null && _artboard != null) {
ActorAnimation animation = _artboard.getAnimation(_animationName);
if (animation != null) {
_animationLayers.add(FlareAnimationLayer()
..name = _animationName
..animation = animation
..mix = 1.0
..mixSeconds = 0.2);
animation.apply(0.0, _artboard, 1.0);
_artboard.advance(0.0);
updatePlayState();
}
}
}
}