Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose speedMultiplier property #423

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions lib/src/rive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ class Rive extends LeafRenderObjectWidget {
/// {@endtemplate}
final Rect? clipRect;

/// A multiplier for controlling the speed of the Rive animation playback.
///
/// Default `1.0`.
final double speedMultiplier;

const Rive({
required this.artboard,
super.key,
Expand All @@ -105,6 +110,7 @@ class Rive extends LeafRenderObjectWidget {
this.fit = BoxFit.contain,
this.alignment = Alignment.center,
this.clipRect,
this.speedMultiplier = 1.0,
});

@override
Expand All @@ -121,7 +127,8 @@ class Rive extends LeafRenderObjectWidget {
..tickerModeEnabled = tickerModeValue
..enableHitTests = enablePointerEvents
..cursor = cursor
..behavior = behavior;
..behavior = behavior
..speedMultiplier = speedMultiplier;
}

@override
Expand All @@ -139,12 +146,14 @@ class Rive extends LeafRenderObjectWidget {
..tickerModeEnabled = tickerModeValue
..enableHitTests = enablePointerEvents
..cursor = cursor
..behavior = behavior;
..behavior = behavior
..speedMultiplier = speedMultiplier;
}
}

class RiveRenderObject extends RiveRenderBox implements MouseTrackerAnnotation {
RuntimeArtboard _artboard;
double _speedMultiplier = 1;
RiveRenderObject(
this._artboard, {
this.behavior = RiveHitTestBehavior.opaque,
Expand All @@ -167,6 +176,14 @@ class RiveRenderObject extends RiveRenderBox implements MouseTrackerAnnotation {
markNeedsLayout();
}

double get speedMultiplier => _speedMultiplier;

set speedMultiplier(double value) {
if (value != _speedMultiplier) {
_speedMultiplier = value;
}
}

/// Local offset to global artboard position
Vec2D _toArtboard(Offset local) {
final globalCoordinates = localToGlobal(local);
Expand Down Expand Up @@ -333,7 +350,8 @@ class RiveRenderObject extends RiveRenderBox implements MouseTrackerAnnotation {

@override
bool advance(double elapsedSeconds) =>
_artboard.isPlaying && _artboard.advance(elapsedSeconds, nested: true);
_artboard.isPlaying &&
_artboard.advance(elapsedSeconds * _speedMultiplier, nested: true);

@override
void beforeDraw(Canvas canvas, Offset offset) {
Expand Down
10 changes: 10 additions & 0 deletions lib/src/widgets/rive_animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ class RiveAnimation extends StatefulWidget {
/// rendering.
final ObjectGenerator? objectGenerator;

/// A multiplier for controlling the speed of the Rive animation playback.
///
/// Default `1.0`.
final double speedMultiplier;

/// Creates a new [RiveAnimation] from an asset bundle.
///
/// *Example:*
Expand All @@ -92,6 +97,7 @@ class RiveAnimation extends StatefulWidget {
this.onInit,
this.behavior = RiveHitTestBehavior.opaque,
this.objectGenerator,
this.speedMultiplier = 1,
Key? key,
}) : name = asset,
file = null,
Expand Down Expand Up @@ -121,6 +127,7 @@ class RiveAnimation extends StatefulWidget {
this.headers,
this.behavior = RiveHitTestBehavior.opaque,
this.objectGenerator,
this.speedMultiplier = 1,
Key? key,
}) : name = url,
file = null,
Expand Down Expand Up @@ -148,6 +155,7 @@ class RiveAnimation extends StatefulWidget {
this.onInit,
this.behavior = RiveHitTestBehavior.opaque,
this.objectGenerator,
this.speedMultiplier = 1,
Key? key,
}) : name = path,
file = null,
Expand Down Expand Up @@ -176,6 +184,7 @@ class RiveAnimation extends StatefulWidget {
this.clipRect,
this.controllers = const [],
this.onInit,
this.speedMultiplier = 1,
Key? key,
this.behavior = RiveHitTestBehavior.opaque,
}) : name = null,
Expand Down Expand Up @@ -346,6 +355,7 @@ class RiveAnimationState extends State<RiveAnimation> {
clipRect: widget.clipRect,
enablePointerEvents: _shouldAddHitTesting,
behavior: widget.behavior,
speedMultiplier: widget.speedMultiplier,
)
: widget.placeHolder ?? const SizedBox();
}