Merge pull request #25 from rive-app/animation_by_name_mk2

Adds animationByName method to Artboard
This commit is contained in:
Matt Sullivan
2020-11-17 15:00:17 -05:00
committed by GitHub
9 changed files with 78 additions and 6 deletions

View File

@ -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.

View File

@ -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';

21
lib/src/extensions.dart Normal file
View File

@ -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;
}
}

View File

@ -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<int, CoreFieldType>();
// List of core file types
final indexToField = <CoreFieldType>[
RiveCoreContext.uintType,
RiveCoreContext.stringType,

View File

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

View File

@ -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:

41
test/artboard_test.dart Normal file
View File

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

Binary file not shown.

View File

@ -12,9 +12,7 @@ void main() {
File('assets/multiple_artboards_0_6_2.riv').readAsBytesSync());
}
setUp(() {
loadTestAssets();
});
setUp(loadTestAssets);
test('Rive files load', () {
// Create a dummy RiveFile