mirror of
https://github.com/rive-app/rive-flutter.git
synced 2025-06-26 01:18:13 +08:00
SimpleAnimation exposes mix
This commit is contained in:
@ -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<RuntimeArtboard> {
|
||||
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<RuntimeArtboard> {
|
||||
|
||||
@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;
|
||||
}
|
||||
|
43
test/simple_animation_test.dart
Normal file
43
test/simple_animation_test.dart
Normal file
@ -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);
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user