Skip to content

Commit

Permalink
feat: Lottie bridge package (#2157)
Browse files Browse the repository at this point in the history
This PR adds support for Lottie animations to flame
  • Loading branch information
tozu committed Nov 22, 2022
1 parent 98e295c commit 3a73d14
Show file tree
Hide file tree
Showing 22 changed files with 489 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .github/.cspell/flame_dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ spydon
tavian
trex
wolfenrain
Wyrmsun
Wyrmsun
Bodymovin
xaha
51 changes: 51 additions & 0 deletions doc/bridge_packages/flame_lottie/flame_lottie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# flame_lottie

This package allows you to load and add Lottie animations to your Flame game.


The native Lottie libraries (such as [lottie-android](https://github.com/airbnb/lottie-android))
are maintained by **Airbnb**.

The Flutter package ``lottie``, on which this wrapper is based on, is by developed **xaha.dev** and
can be found on [pub.dev](https://pub.dev/packages/lottie).


## Usage

To use it in your game you just need to add `flame_lottie` to your pubspec.yaml.

Simply load the Lottie animation using the **loadLottie** method and
the [LottieBuilder](https://pub.dev/documentation/lottie/latest/lottie/LottieBuilder-class.html).
It allows all the various ways of loading a Lottie file:

- [Lottie.asset](https://pub.dev/documentation/lottie/latest/lottie/Lottie/asset.html), for
obtaining a Lottie file from an AssetBundle using a key.
- [Lottie.network](https://pub.dev/documentation/lottie/latest/lottie/Lottie/network.html), for
obtaining a lottie file from a URL.
- [Lottie.file](https://pub.dev/documentation/lottie/latest/lottie/Lottie/file.html), for obtaining
a lottie file from a File.
- [Lottie.memory](https://pub.dev/documentation/lottie/latest/lottie/Lottie/memory.html), for
obtaining a lottie file from a Uint8List.

... and add it as `LottieComponent` to your Flame 🔥 game.

Example:

```dart
class MyGame extends FlameGame {
...
@override
Future<void> onLoad() async {
final asset = Lottie.asset('assets/LottieLogo1.json');
final animation = await loadLottie(asset);
add(
LottieComponent(
animation,
repeating: true, // Continuously loop the animation.
size: Vector2.all(400),
),
);
}
...
}
```
1 change: 1 addition & 0 deletions examples/assets/images/animations/lottieLogo.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions examples/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:dashbook/dashbook.dart';
import 'package:examples/stories/animations/animations.dart';
import 'package:examples/stories/bridge_libraries/audio/audio.dart';
import 'package:examples/stories/bridge_libraries/flame_isolate/isolate.dart';
import 'package:examples/stories/bridge_libraries/flame_lottie/lottie.dart';
import 'package:examples/stories/bridge_libraries/forge2d/flame_forge2d.dart';
import 'package:examples/stories/camera_and_viewport/camera_and_viewport.dart';
import 'package:examples/stories/collision_detection/collision_detection.dart';
Expand Down Expand Up @@ -50,6 +51,7 @@ void main() {
// Bridge package examples
addForge2DStories(dashbook);
addFlameIsolateExample(dashbook);
addFlameLottieExample(dashbook);

runApp(dashbook);
}
15 changes: 15 additions & 0 deletions examples/lib/stories/bridge_libraries/flame_lottie/lottie.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:dashbook/dashbook.dart';
import 'package:examples/commons/commons.dart';
import 'package:examples/stories/bridge_libraries/flame_lottie/lottie_animation_example.dart';
import 'package:flame/game.dart';

void addFlameLottieExample(Dashbook dashbook) {
dashbook.storiesOf('FlameLottie').add(
'Lottie Animation example',
(_) => GameWidget(
game: LottieAnimationExample(),
),
codeLink: baseLink('flame_lottie/lottie_animation_example.dart'),
info: LottieAnimationExample.description,
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:flame/game.dart';
import 'package:flame_lottie/flame_lottie.dart';

class LottieAnimationExample extends FlameGame {
static const String description = '''
This example shows how to load a Lottie animation. It is configured to
continuously loop the animation and restart once its done.
''';

@override
Future<void> onLoad() async {
final asset = await loadLottie(
Lottie.asset('assets/images/animations/lottieLogo.json'),
);

add(LottieComponent(asset, size: Vector2.all(400), repeating: true));
}
}
1 change: 1 addition & 0 deletions examples/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:
flame_audio: ^1.3.2
flame_forge2d: ^0.12.2
flame_isolate: ^0.1.0
flame_lottie: ^0.1.0
flame_svg: ^1.6.0
flame_tiled: ^1.8.0
flutter:
Expand Down
10 changes: 10 additions & 0 deletions packages/flame_lottie/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled.

version:
revision: e3c29ec00c9c825c891d75054c63fcc46454dca1
channel: stable

project_type: package
22 changes: 22 additions & 0 deletions packages/flame_lottie/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2022 Blue Fire

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

55 changes: 55 additions & 0 deletions packages/flame_lottie/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# flame_lottie

This package allows you to load and add Lottie animations to your Flame game.

> Lottie is a mobile library for Android and iOS that parses Adobe After Effects animations
exported as json with Bodymovin and renders them natively on mobile!

Source: [lottie-android](https://github.com/airbnb/lottie-android) on Github


The native Lottie libraries (such as [lottie-android](https://github.com/airbnb/lottie-android))
are maintained by **Airbnb**.

The Flutter package ``lottie``, on which this wrapper is based on, is by **xaha.dev** and can be
found on [pub dev](https://pub.dev/packages/lottie).


## Usage

To use it in your game you just need to add `flame_lottie` to your pubspec.yaml.

Simply load the Lottie animation using the **loadLottie** method and the
[LottieBuilder](https://pub.dev/documentation/lottie/latest/lottie/LottieBuilder-class.html). It
allows all the various ways of loading a Lottie file:

- [Lottie.asset](https://pub.dev/documentation/lottie/latest/lottie/Lottie/asset.html), for
obtaining a Lottie file from an AssetBundle using a key.
- [Lottie.network](https://pub.dev/documentation/lottie/latest/lottie/Lottie/network.html), for
obtaining a lottie file from a URL.
- [Lottie.file](https://pub.dev/documentation/lottie/latest/lottie/Lottie/file.html), for obtaining
a lottie file from a File.
- [Lottie.memory](https://pub.dev/documentation/lottie/latest/lottie/Lottie/memory.html), for
obtaining a lottie file from a Uint8List.

... and add it as `LottieComponent` to your flame 🔥 game.

Example:

```dart
class MyGame extends FlameGame {
...
@override
Future<void> onLoad() async {
final asset = Lottie.asset('assets/LottieLogo1.json');
final animation = await loadLottie(asset);
add(
LottieComponent(
composition: animation,
repeating: true, // continuously loop the animation
),
);
}
...
}
```
1 change: 1 addition & 0 deletions packages/flame_lottie/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:flame_lint/analysis_options.yaml
30 changes: 30 additions & 0 deletions packages/flame_lottie/example/.metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled.

version:
revision: d9111f64021372856901a1fd5bfbc386cade3318
channel: stable

project_type: app

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: d9111f64021372856901a1fd5bfbc386cade3318
base_revision: d9111f64021372856901a1fd5bfbc386cade3318
- platform: macos
create_revision: d9111f64021372856901a1fd5bfbc386cade3318
base_revision: d9111f64021372856901a1fd5bfbc386cade3318

# User provided section

# List of Local paths (relative to this file) that should be
# ignored by the migrate tool.
#
# Files that are not part of the templates will be ignored by default.
unmanaged_files:
- 'lib/main.dart'
- 'ios/Runner.xcodeproj/project.pbxproj'
8 changes: 8 additions & 0 deletions packages/flame_lottie/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# flame_lottie example

An example for using the `flame_lottie` package, a bridge between
[Flame](https://flame-engine.org/) 🔥 and *Lottie*.

The original, native **lottie** library is developed and maintained by [Airbnb](https://github.com/airbnb/lottie-android).

The Flutter package `lottie` is maintained by **xaha.dev** and is available on pub dev ([link](https://pub.dev/packages/lottie))
1 change: 1 addition & 0 deletions packages/flame_lottie/example/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:flame_lint/analysis_options.yaml
1 change: 1 addition & 0 deletions packages/flame_lottie/example/assets/LottieLogo1.json

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions packages/flame_lottie/example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:flame/game.dart';
import 'package:flame_lottie/flame_lottie.dart';
import 'package:flutter/material.dart';

void main() async {
runApp(GameWidget(game: LottieExampleGame()));
}

class LottieExampleGame extends FlameGame {
@override
Future<void> onLoad() async {
final asset = await loadLottie(Lottie.asset('assets/LottieLogo1.json'));
add(
LottieComponent(
asset,
size: Vector2.all(400),
repeating: true,
),
);
}
}
23 changes: 23 additions & 0 deletions packages/flame_lottie/example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: flame_lottie_example
description: An example for flame_lottie. The example shows how load a Lottie animation and add it to a flame game.

publish_to: 'none'
version: 0.0.1+1

environment:
sdk: '>=2.18.2 <3.0.0'

dependencies:
flame: ^1.4.0
flame_lottie: ^0.1.0
flutter:
sdk: flutter
lottie:

dev_dependencies:
flame_lint: ^0.1.3

flutter:
uses-material-design: true
assets:
- assets/
5 changes: 5 additions & 0 deletions packages/flame_lottie/lib/flame_lottie.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library flame_lottie;

export 'package:lottie/lottie.dart';
export 'src/lottie_component.dart';
export 'src/lottie_renderer.dart';
71 changes: 71 additions & 0 deletions packages/flame_lottie/lib/src/lottie_component.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import 'dart:async';

import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame_lottie/src/lottie_renderer.dart';
import 'package:flutter/widgets.dart';
import 'package:lottie/lottie.dart';

/// A Flame [Component] which renders a [Lottie] animation using the already
/// existing Flutter library [lottie](https://pub.dev/packages/lottie).
class LottieComponent extends PositionComponent with HasPaint {
late final LottieRenderer _renderer;

/// The [controller] drives the [Lottie] animation. In case none is specified
/// it will be created implicitly in the [LottieRenderer].
LottieComponent(
LottieComposition composition, {
EffectController? controller,
double? progress,
LottieDelegates? delegates,
bool? enableMergePaths,
FrameRate? frameRate,
double? duration,
bool? repeating,
Alignment alignment = Alignment.center,
BoxFit? fit = BoxFit.contain,
super.position,
super.size,
super.scale,
super.angle,
super.anchor,
super.children,
super.priority,
}) {
_renderer = LottieRenderer(
composition: composition,
progress: progress ?? 0.0,
size: size,
controller: controller,
duration: duration,
repeating: repeating,
alignment: alignment,
fit: fit,
delegates: delegates,
enableMergePaths: enableMergePaths,
frameRate: frameRate,
);
}

@mustCallSuper
@override
void render(Canvas canvas) {
_renderer.render(canvas);
}

@mustCallSuper
@override
void update(double dt) {
_renderer.update(dt);
}
}

/// Loads the Lottie animation from the specified Lottie file.
Future<LottieComposition> loadLottie(
FutureOr<LottieBuilder> file,
) async {
final loaded = await file;
final composition = await loaded.lottie.load();

return composition;
}

0 comments on commit 3a73d14

Please sign in to comment.