Merge pull request #126 from rive-app/simplify_controllers

Simplifies simple controller
This commit is contained in:
Matt Sullivan
2021-06-14 17:46:25 -07:00
committed by GitHub
3 changed files with 15 additions and 41 deletions

View File

@ -28,30 +28,19 @@ class SpeedyAnimation extends StatelessWidget {
class SpeedController extends SimpleAnimation { class SpeedController extends SimpleAnimation {
final double speedMultiplier; final double speedMultiplier;
/// Stops the animation on the next apply
bool _stopOnNextApply = false;
SpeedController( SpeedController(
String animationName, { String animationName, {
double mix = 1, double mix = 1,
this.speedMultiplier = 1, this.speedMultiplier = 1,
}) : super( }) : super(animationName, mix: mix);
animationName,
mix: mix,
);
@override @override
void apply(RuntimeArtboard artboard, double elapsedSeconds) { void apply(RuntimeArtboard artboard, double elapsedSeconds) {
if (_stopOnNextApply || instance == null) { if (instance == null || !instance!.keepGoing) {
isActive = false; isActive = false;
} }
instance!
instance!.animation.apply(instance!.time, coreContext: artboard, mix: mix); ..animation.apply(instance!.time, coreContext: artboard, mix: mix)
if (!instance!.advance(elapsedSeconds * speedMultiplier)) { ..advance(elapsedSeconds * speedMultiplier);
_stopOnNextApply = true;
}
} }
@override
void onActivate() => _stopOnNextApply = false;
} }

View File

@ -44,7 +44,7 @@ class _PlayOneShotAnimationState extends State<PlayOneShotAnimation> {
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
// disable the button while playing the animation // disable the button while playing the animation
onPressed: () => _isPlaying ? null : _controller.isActive = true, onPressed: () => _isPlaying ? null : _controller.isActive = true,
tooltip: 'Play', tooltip: 'Bounce',
child: const Icon(Icons.arrow_upward), child: const Icon(Icons.arrow_upward),
), ),
); );

View File

@ -20,9 +20,6 @@ class SimpleAnimation extends RiveAnimationController<RuntimeArtboard> {
/// Animation name /// Animation name
final String animationName; final String animationName;
/// Stops the animation on the next apply
bool _stopOnNextApply = false;
/// Pauses the animation when it's created /// Pauses the animation when it's created
final bool autoplay; final bool autoplay;
@ -39,21 +36,19 @@ class SimpleAnimation extends RiveAnimationController<RuntimeArtboard> {
@override @override
void apply(RuntimeArtboard artboard, double elapsedSeconds) { void apply(RuntimeArtboard artboard, double elapsedSeconds) {
if (_stopOnNextApply || _instance == null) { if (_instance == null || !_instance!.keepGoing) {
isActive = false; isActive = false;
} }
// We apply before advancing. So we want to stop rendering only once the // 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 // frame is advanced, ensuring the next apply happens, and then finally
// stopping playback. We do this by tracking _stopOnNextApply making sure to // stopping playback. We do this with keepGoing as this will be true of a
// reset it when the controller is re-activated. Fixes #28 and should help // one-shot has passed its stop time. Fixes #28 and should help with issues
// with issues #51 and #56. // #51 and #56.
_instance!.animation _instance!
.apply(_instance!.time, coreContext: artboard, mix: mix); ..animation.apply(_instance!.time, coreContext: artboard, mix: mix)
if (!_instance!.advance(elapsedSeconds)) { ..advance(elapsedSeconds);
_stopOnNextApply = true;
}
} }
@override @override
@ -63,16 +58,6 @@ class SimpleAnimation extends RiveAnimationController<RuntimeArtboard> {
return _instance != null; 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 /// Resets the animation back to it's starting time position
void reset() { void reset() => _instance?.reset();
_instance?.reset();
}
} }