From a8e27e3ef821b257f8a20de00489af79f753f46d Mon Sep 17 00:00:00 2001 From: matt Sullivan Date: Mon, 14 Jun 2021 17:18:29 -0700 Subject: [PATCH] Simplifies simple controller --- example/lib/custom_controller.dart | 21 ++++---------- example/lib/play_one_shot_animation.dart | 2 +- lib/src/controllers/simple_controller.dart | 33 ++++++---------------- 3 files changed, 15 insertions(+), 41 deletions(-) diff --git a/example/lib/custom_controller.dart b/example/lib/custom_controller.dart index 800261c..e2bafcc 100644 --- a/example/lib/custom_controller.dart +++ b/example/lib/custom_controller.dart @@ -28,30 +28,19 @@ class SpeedyAnimation extends StatelessWidget { class SpeedController extends SimpleAnimation { final double speedMultiplier; - /// Stops the animation on the next apply - bool _stopOnNextApply = false; - SpeedController( String animationName, { double mix = 1, this.speedMultiplier = 1, - }) : super( - animationName, - mix: mix, - ); + }) : super(animationName, mix: mix); @override void apply(RuntimeArtboard artboard, double elapsedSeconds) { - if (_stopOnNextApply || instance == null) { + if (instance == null || !instance!.keepGoing) { isActive = false; } - - instance!.animation.apply(instance!.time, coreContext: artboard, mix: mix); - if (!instance!.advance(elapsedSeconds * speedMultiplier)) { - _stopOnNextApply = true; - } + instance! + ..animation.apply(instance!.time, coreContext: artboard, mix: mix) + ..advance(elapsedSeconds * speedMultiplier); } - - @override - void onActivate() => _stopOnNextApply = false; } diff --git a/example/lib/play_one_shot_animation.dart b/example/lib/play_one_shot_animation.dart index 2f746d3..7fcb7d4 100644 --- a/example/lib/play_one_shot_animation.dart +++ b/example/lib/play_one_shot_animation.dart @@ -44,7 +44,7 @@ class _PlayOneShotAnimationState extends State { floatingActionButton: FloatingActionButton( // disable the button while playing the animation onPressed: () => _isPlaying ? null : _controller.isActive = true, - tooltip: 'Play', + tooltip: 'Bounce', child: const Icon(Icons.arrow_upward), ), ); diff --git a/lib/src/controllers/simple_controller.dart b/lib/src/controllers/simple_controller.dart index fd9c958..e7fcc48 100644 --- a/lib/src/controllers/simple_controller.dart +++ b/lib/src/controllers/simple_controller.dart @@ -20,9 +20,6 @@ class SimpleAnimation extends RiveAnimationController { /// Animation name final String animationName; - /// Stops the animation on the next apply - bool _stopOnNextApply = false; - /// Pauses the animation when it's created final bool autoplay; @@ -39,21 +36,19 @@ class SimpleAnimation extends RiveAnimationController { @override void apply(RuntimeArtboard artboard, double elapsedSeconds) { - if (_stopOnNextApply || _instance == null) { + if (_instance == null || !_instance!.keepGoing) { isActive = false; } // We apply before advancing. So we want to stop rendering only once the - // last advanced frame has been applied. This means tracking when the last + // last advanced frame has been applied. This means knowing when the last // frame is advanced, ensuring the next apply happens, and then finally - // stopping playback. We do this by tracking _stopOnNextApply making sure to - // reset it when the controller is re-activated. Fixes #28 and should help - // with issues #51 and #56. - _instance!.animation - .apply(_instance!.time, coreContext: artboard, mix: mix); - if (!_instance!.advance(elapsedSeconds)) { - _stopOnNextApply = true; - } + // stopping playback. We do this with keepGoing as this will be true of a + // one-shot has passed its stop time. Fixes #28 and should help with issues + // #51 and #56. + _instance! + ..animation.apply(_instance!.time, coreContext: artboard, mix: mix) + ..advance(elapsedSeconds); } @override @@ -63,16 +58,6 @@ class SimpleAnimation extends RiveAnimationController { return _instance != null; } - @override - void onActivate() { - // We override onActivate to reset stopOnNextApply. This ensures that when - // the controller is re-activated after stopping, it doesn't prematurely - // stop itself. - _stopOnNextApply = false; - } - /// Resets the animation back to it's starting time position - void reset() { - _instance?.reset(); - } + void reset() => _instance?.reset(); }