Skip to content

Commit

Permalink
Adding artboard option.
Browse files Browse the repository at this point in the history
  • Loading branch information
luigi-rosso committed Jul 24, 2019
1 parent 6f6000f commit 1e5394b
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 45 deletions.
4 changes: 4 additions & 0 deletions flare_dart/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [1.4.4] - 2019-07-24 11:43:51

Adding getArtboard method to Actor class. Allows finding artboards by name.

## [1.4.3] - 2019-07-06 11:08:53

Some cleanup done while fixing issue #104 (using intrinsic artboard size as an option).
Expand Down
4 changes: 4 additions & 0 deletions flare_dart/lib/actor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ abstract class Actor {
Actor();

ActorArtboard get artboard => _artboards.isNotEmpty ? _artboards.first : null;
ActorArtboard getArtboard(String name) => name == null
? artboard
: _artboards.firstWhere((artboard) => artboard?.name == name,
orElse: () => null);

int get version {
return _version;
Expand Down
6 changes: 6 additions & 0 deletions flare_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [1.5.5] - 2019-07-24 11:44:36

- Adding artboard option to FlareActor. Use this to change which artboard gets displayed by the FlareActor widget.
- Fixed incorrect signature of load method. If you were deriving FlareRenderBox, you'll need to update it to match. It's a minor change from void to Future<void>.
- Added some documentation to the FlareActor parameters.

## [1.5.4] - 2019-07-08 21:10:50

- Using Uint16List for vertex indices now that Flutter Stable has been updated.
Expand Down
141 changes: 99 additions & 42 deletions flare_flutter/lib/flare_actor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,67 @@ import 'flare_controller.dart';
typedef void FlareCompletedCallback(String name);

class FlareActor extends LeafRenderObjectWidget {
/// Name of the Flare file to be loaded from the AssetBundle.
final String filename;

/// 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;

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});
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,
});

@override
RenderObject createRenderObject(BuildContext context) {
Expand All @@ -53,7 +89,8 @@ class FlareActor extends LeafRenderObjectWidget {
..color = color
..shouldClip = shouldClip
..boundsNodeName = boundsNode
..useIntrinsicSize = sizeFromArtboard;
..useIntrinsicSize = sizeFromArtboard
..artboardName = artboard;
}

@override
Expand All @@ -70,7 +107,8 @@ class FlareActor extends LeafRenderObjectWidget {
..color = color
..shouldClip = shouldClip
..boundsNodeName = boundsNode
..useIntrinsicSize = sizeFromArtboard;
..useIntrinsicSize = sizeFromArtboard
..artboardName = artboard;
}

@override
Expand All @@ -94,12 +132,24 @@ class FlareAnimationLayer {
class FlareActorRenderObject extends FlareRenderBox {
Mat2D _lastControllerViewTransform;
String _filename;
String _artboardName;
String _animationName;
String _boundsNodeName;
FlareController _controller;
FlareCompletedCallback _completedCallback;
bool snapToEnd = false;
bool _isPaused = false;
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) {
Expand Down Expand Up @@ -213,38 +263,45 @@ class FlareActorRenderObject extends FlareRenderBox {
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.advance(0.0);
updateBounds();

if (_controller != null) {
_controller.initialize(_artboard);
}
_updateAnimation(onlyWhenMissing: true);
markNeedsPaint();
return true;
}

@override
void load() {
Future<void> load() async {
if (_filename == null) {
return;
}
super.load();
loadFlare(_filename).then((FlutterActor actor) {
if (actor == null || actor.artboard == null) {
return;
}
FlutterActorArtboard artboard =
actor.artboard.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.advance(0.0);
updateBounds();

if (_controller != null) {
_controller.initialize(_artboard);
}
_updateAnimation(onlyWhenMissing: true);
markNeedsPaint();
});
_actor = await loadFlare(_filename);
if (_actor == null || _actor.artboard == null) {
return;
}
_instanceArtboard();
}

FlareCompletedCallback get completed => _completedCallback;
Expand Down
4 changes: 2 additions & 2 deletions flare_flutter/lib/flare_render_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ abstract class FlareRenderBox extends RenderBox {
bool _isLoading = false;
bool get isLoading => _isLoading;

void _load() async {
Future<void> _load() async {
if (_isLoading) {
return;
}
Expand All @@ -247,7 +247,7 @@ abstract class FlareRenderBox extends RenderBox {
}

/// Perform any loading logic necessary for this scene.
void load() async {}
Future<void> load() async {}

void _unload() {
for (final FlareCacheAsset asset in _assets) {
Expand Down
3 changes: 2 additions & 1 deletion flare_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ environment:
dependencies:
flutter:
sdk: flutter
flare_dart: ../flare_dart
flare_dart:
path: ../flare_dart
dev_dependencies:
flutter_test:
sdk: flutter

0 comments on commit 1e5394b

Please sign in to comment.