From 66305cb511f4787c3b6cecb09c15b18bd9c5149d Mon Sep 17 00:00:00 2001 From: Matt Sullivan Date: Fri, 11 Dec 2020 13:10:21 -0800 Subject: [PATCH] SimpleAnimation exposes mix --- lib/src/controllers/simple_controller.dart | 11 +++++- test/simple_animation_test.dart | 43 ++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 test/simple_animation_test.dart diff --git a/lib/src/controllers/simple_controller.dart b/lib/src/controllers/simple_controller.dart index 873046c..e146dd4 100644 --- a/lib/src/controllers/simple_controller.dart +++ b/lib/src/controllers/simple_controller.dart @@ -7,9 +7,16 @@ import 'package:rive/src/runtime_artboard.dart'; /// by an artist. All playback parameters (looping, speed, keyframes) are artist /// defined in the Rive editor. class SimpleAnimation extends RiveAnimationController { + SimpleAnimation(this.animationName, {double mix}) + : _mix = mix?.clamp(0, 1)?.toDouble() ?? 1.0; + LinearAnimationInstance _instance; final String animationName; - SimpleAnimation(this.animationName); + + // Controls the level of mix for the animation, clamped between 0 and 1 + double _mix; + double get mix => _mix; + set mix(double value) => _mix = value?.clamp(0, 1)?.toDouble() ?? 1; LinearAnimationInstance get instance => _instance; @@ -29,7 +36,7 @@ class SimpleAnimation extends RiveAnimationController { @override void apply(RuntimeArtboard artboard, double elapsedSeconds) { - _instance.animation.apply(_instance.time, coreContext: artboard); + _instance.animation.apply(_instance.time, coreContext: artboard, mix: mix); if (!_instance.advance(elapsedSeconds)) { isActive = false; } diff --git a/test/simple_animation_test.dart b/test/simple_animation_test.dart new file mode 100644 index 0000000..b65edfb --- /dev/null +++ b/test/simple_animation_test.dart @@ -0,0 +1,43 @@ +import 'dart:io'; +import 'dart:typed_data'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:rive/rive.dart'; + +void main() { + ByteData riveData; + + void loadTestAssets() { + riveData = ByteData.sublistView( + File('assets/animations_0_6_2.riv').readAsBytesSync(), + ); + } + + setUp(loadTestAssets); + + test('SimpleAnimation exposes mix', () { + // Load a Rive file + final riveFile = RiveFile(); + expect(riveFile.import(riveData), true); + expect(riveFile.mainArtboard.name, 'My Artboard'); + + final firstController = + SimpleAnimation(riveFile.mainArtboard.animations.first.name); + expect(firstController.animationName, 'First'); + expect(firstController.mix, 1.0); + + firstController.mix = 0.5; + expect(firstController.mix, 0.5); + + firstController.mix = 2.5; + expect(firstController.mix, 1.0); + + firstController.mix = -1; + expect(firstController.mix, 0.0); + + final secondController = + SimpleAnimation(riveFile.mainArtboard.animations.last.name, mix: 0.8); + expect(secondController.animationName, 'Second'); + expect(secondController.mix, 0.8); + }); +}