From 1cdaaae87081c6a22aac2c452cd50403b6e479ab Mon Sep 17 00:00:00 2001 From: Matt Sullivan Date: Tue, 17 Nov 2020 11:54:58 -0800 Subject: [PATCH 1/2] Adds animationByName method to Artboard and more tests --- lib/rive.dart | 1 + lib/src/extensions.dart | 21 ++++++++++++++++ lib/src/rive_file.dart | 5 +++- lib/src/runtime_artboard.dart | 3 +++ pubspec.yaml | 4 +-- test/artboard_test.dart | 41 +++++++++++++++++++++++++++++++ test/assets/animations_0_6_2.riv | Bin 0 -> 347 bytes test/rive_file_test.dart | 4 +-- 8 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 lib/src/extensions.dart create mode 100644 test/artboard_test.dart create mode 100644 test/assets/animations_0_6_2.riv diff --git a/lib/rive.dart b/lib/rive.dart index 4660713..ccba7da 100644 --- a/lib/rive.dart +++ b/lib/rive.dart @@ -17,3 +17,4 @@ export 'package:rive/src/rive_core/shapes/paint/linear_gradient.dart'; export 'package:rive/src/rive_core/shapes/paint/radial_gradient.dart'; export 'package:rive/src/rive_core/runtime/runtime_header.dart' show riveVersion; +export 'package:rive/src/extensions.dart'; diff --git a/lib/src/extensions.dart b/lib/src/extensions.dart new file mode 100644 index 0000000..198a430 --- /dev/null +++ b/lib/src/extensions.dart @@ -0,0 +1,21 @@ +/// Extensions to the runtime core classes + +import 'package:rive/src/rive_core/artboard.dart'; +import 'package:rive/src/rive_core/animation/linear_animation.dart'; +import 'package:rive/src/rive_core/animation/linear_animation_instance.dart'; + +/// Artboard extensions +extension ArtboardAdditions on Artboard { + /// Returns an animation with the given name, or null if no animation with + /// that name exists in the artboard + LinearAnimationInstance animationByName(String name) { + final animation = animations.firstWhere( + (animation) => animation is LinearAnimation && animation.name == name, + orElse: () => null, + ); + if (animation != null) { + return LinearAnimationInstance(animation as LinearAnimation); + } + return null; + } +} diff --git a/lib/src/rive_file.dart b/lib/src/rive_file.dart index a2f18a8..0f494f2 100644 --- a/lib/src/rive_file.dart +++ b/lib/src/rive_file.dart @@ -34,14 +34,17 @@ class RiveFile { Artboard artboardByName(String name) => _artboards.firstWhere((a) => a.name == name, orElse: () => null); + /// Imports a Rive file from an array of bytes. Returns true if successfully + /// imported, false otherwise. bool import(ByteData bytes) { assert(_header == null, 'can only import once'); var reader = BinaryReader(bytes); _header = RuntimeHeader.read(reader); - // Property fields toc. + /// Property fields table of contents final propertyToField = HashMap(); + // List of core file types final indexToField = [ RiveCoreContext.uintType, RiveCoreContext.stringType, diff --git a/lib/src/runtime_artboard.dart b/lib/src/runtime_artboard.dart index d5e07cb..9e9b8cd 100644 --- a/lib/src/runtime_artboard.dart +++ b/lib/src/runtime_artboard.dart @@ -4,6 +4,9 @@ import 'package:rive/src/rive_core/component.dart'; import 'package:rive/src/rive_core/event.dart'; import 'package:rive/src/core/core.dart'; +/// This artboard type is purely for use by the runtime system and should not be +/// directly referenced. Use the Artboard type for any direct interactions with +/// an artboard, and use extension methods to add functionality to Artboard. class RuntimeArtboard extends Artboard implements CoreContext { final _redraw = Event(); ChangeNotifier get redraw => _redraw; diff --git a/pubspec.yaml b/pubspec.yaml index 5ab47b5..ad5a89b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,11 +1,11 @@ name: rive description: Rive 2 Flutter Runtime. This package provides runtime functionality for playing back and interacting with animations built with the Rive editor available at https://rive.app. -version: 0.6.2+2 +version: 0.6.2+3 repository: https://github.com/rive-app/rive-flutter homepage: https://rive.app environment: - sdk: ">=2.2.2 <3.0.0" + sdk: ">=2.7.0 <3.0.0" dependencies: flutter: diff --git a/test/artboard_test.dart b/test/artboard_test.dart new file mode 100644 index 0000000..15efb42 --- /dev/null +++ b/test/artboard_test.dart @@ -0,0 +1,41 @@ +import 'dart:io'; +import 'dart:typed_data'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:rive/rive.dart'; + +void main() { + ByteData multipleArtboardsBytes; + + void loadTestAssets() { + multipleArtboardsBytes = ByteData.sublistView( + File('assets/animations_0_6_2.riv').readAsBytesSync()); + } + + setUp(loadTestAssets); + + test('Artboards can be read from files', () { + final riveFile = RiveFile(); + expect(riveFile.import(multipleArtboardsBytes), true); + expect(riveFile.mainArtboard.name, 'My Artboard'); + }); + + test('Animations can be read from artboards', () { + final riveFile = RiveFile(); + expect(riveFile.import(multipleArtboardsBytes), true); + final artboard = riveFile.mainArtboard; + expect(artboard.animations.length, 2); + expect(artboard.animations.first.name, 'First'); + expect(artboard.animations.last.name, 'Second'); + }); + + test('Animations can be read by name from artboards', () { + final riveFile = RiveFile(); + expect(riveFile.import(multipleArtboardsBytes), true); + final artboard = riveFile.mainArtboard; + expect(artboard.animationByName('First'), isNotNull); + expect(artboard.animationByName('Second'), isNotNull); + expect(artboard.animationByName('Does Not Exist'), isNull); + expect(artboard.animationByName('First') is LinearAnimationInstance, true); + }); +} diff --git a/test/assets/animations_0_6_2.riv b/test/assets/animations_0_6_2.riv new file mode 100644 index 0000000000000000000000000000000000000000..926043e9c7d7a692b67d8d173bcbba6b707a4378 GIT binary patch literal 347 zcmWIY40B~;>e|nAij6^>fsuoeh1<7M!Lg_$DL=6&g}vdNybDJ|`8{V&28FFoTnsWE zPTUMYzyl%}m{}Qk8-!Aw`54|ZJ2SAbGV(HrZgt`V5h4ttrp}@uf`OBjkwFBAg;-e_ zL|C}FoD!3>(~I&;^HNwDfP!qQhK7d!8JOhFS=};=ic74S7#Jmu85x+QOnDiY Date: Tue, 17 Nov 2020 11:58:24 -0800 Subject: [PATCH 2/2] Updates changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c00399..686eb81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## [0.6.2+3] - 2020-11-11 12:13:00 + +- Added Artboard tests. +- Added animationByName(String) function to Artboard. + ## [0.6.2+2] - 2020-11-11 12:13:00 - Added RiveFile tests.