diff --git a/.rive_head b/.rive_head index 9fc8b3c..900dd7f 100644 --- a/.rive_head +++ b/.rive_head @@ -1 +1 @@ -1a8b3c7ccd4bb10f085f2b45bf4685cd23cc0b0f +1460f5366b341ea2535f267c51d589569c870ef7 diff --git a/CHANGELOG.md b/CHANGELOG.md index 46deca1..d177256 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Support for asset audio volume. - Fixed an issue with audio decoder in web build. - Adds `play()`/`pause()` and `isPlaying` to an `Artboard`. This completely stops the artboard from playing (including nested artboards) and stops/starts the animation ticker. Pausing an artboard can be used to reduce resources used for Rive graphics that aren't visible on screen. +- Adds `getBoolInput`, `getTriggerInput`, `getNumberInput`, and `triggerInput` on `StateMachineController` to easily retrieve state machine inputs and fire triggers. This can be used instead of `findInput` and `findSMI`, to easily retrieve an `SMIBool`, `SMINumber`, and `SMITrigger` to manipulate a Rive state machine. ## 0.13.1 diff --git a/example/lib/example_state_machine.dart b/example/lib/example_state_machine.dart index 47e527f..a1eda41 100644 --- a/example/lib/example_state_machine.dart +++ b/example/lib/example_state_machine.dart @@ -16,8 +16,8 @@ class _ExampleStateMachineState extends State { Artboard? _riveArtboard; StateMachineController? _controller; - SMIInput? _hoverInput; - SMIInput? _pressInput; + SMIBool? _hoverInput; + SMIBool? _pressInput; @override void initState() { @@ -37,8 +37,8 @@ class _ExampleStateMachineState extends State { StateMachineController.fromArtboard(artboard, 'Button'); if (controller != null) { artboard.addController(controller); - _hoverInput = controller.findInput('Hover'); - _pressInput = controller.findInput('Press'); + _hoverInput = controller.getBoolInput('Hover'); + _pressInput = controller.getBoolInput('Press'); } setState(() => _riveArtboard = artboard); }, diff --git a/example/lib/liquid_download.dart b/example/lib/liquid_download.dart index 9e892f7..5ec3c3c 100644 --- a/example/lib/liquid_download.dart +++ b/example/lib/liquid_download.dart @@ -17,8 +17,8 @@ class _LiquidDownloadState extends State { Artboard? _riveArtboard; StateMachineController? _controller; - SMIInput? _start; - SMIInput? _progress; + SMITrigger? _start; + SMINumber? _progress; @override void initState() { @@ -38,8 +38,8 @@ class _LiquidDownloadState extends State { StateMachineController.fromArtboard(artboard, 'Download'); if (controller != null) { artboard.addController(controller); - _start = controller.findInput('Download'); - _progress = controller.findInput('Progress'); + _start = controller.getTriggerInput('Download'); + _progress = controller.getNumberInput('Progress'); } setState(() => _riveArtboard = artboard); }, diff --git a/example/lib/little_machine.dart b/example/lib/little_machine.dart index 905a1d4..5c857b6 100644 --- a/example/lib/little_machine.dart +++ b/example/lib/little_machine.dart @@ -19,7 +19,7 @@ class _LittleMachineState extends State { Artboard? _riveArtboard; StateMachineController? _controller; - SMIInput? _trigger; + SMITrigger? _trigger; @override void initState() { @@ -42,7 +42,7 @@ class _LittleMachineState extends State { ); if (controller != null) { artboard.addController(controller); - _trigger = controller.findInput('Trigger 1'); + _trigger = controller.getTriggerInput('Trigger 1'); } setState(() => _riveArtboard = artboard); }, @@ -64,7 +64,7 @@ class _LittleMachineState extends State { _riveArtboard == null ? const SizedBox() : GestureDetector( - onTapDown: (_) => _trigger?.value = true, + onTapDown: (_) => _trigger?.fire(), child: Rive( artboard: _riveArtboard!, fit: BoxFit.cover, diff --git a/example/lib/simple_state_machine.dart b/example/lib/simple_state_machine.dart index 3a54614..87b8dd6 100644 --- a/example/lib/simple_state_machine.dart +++ b/example/lib/simple_state_machine.dart @@ -17,7 +17,7 @@ class _SimpleStateMachineState extends State { void _onRiveInit(Artboard artboard) { final controller = StateMachineController.fromArtboard(artboard, 'bumpy'); artboard.addController(controller!); - _bump = controller.findInput('bump') as SMITrigger; + _bump = controller.getTriggerInput('bump'); } void _hitBump() => _bump?.fire(); diff --git a/example/lib/skinning_demo.dart b/example/lib/skinning_demo.dart index 24fcc84..a991e86 100644 --- a/example/lib/skinning_demo.dart +++ b/example/lib/skinning_demo.dart @@ -31,7 +31,7 @@ class _SkinningDemoState extends State { ); artboard.addController(controller!); - _skin = controller.findInput('Skin') as SMITrigger; + _skin = controller.getTriggerInput('Skin'); } void _onStateChange(String stateMachineName, String stateName) { diff --git a/example/lib/state_machine_skills.dart b/example/lib/state_machine_skills.dart index 14bef43..e815c3a 100644 --- a/example/lib/state_machine_skills.dart +++ b/example/lib/state_machine_skills.dart @@ -16,7 +16,7 @@ class _StateMachineSkillsState extends State { Artboard? _riveArtboard; StateMachineController? _controller; - SMIInput? _levelInput; + SMINumber? _levelInput; @override void initState() { @@ -36,7 +36,7 @@ class _StateMachineSkillsState extends State { StateMachineController.fromArtboard(artboard, 'Designer\'s Test'); if (controller != null) { artboard.addController(controller); - _levelInput = controller.findInput('Level'); + _levelInput = controller.getNumberInput('Level'); } setState(() => _riveArtboard = artboard); }, diff --git a/lib/src/controllers/state_machine_controller.dart b/lib/src/controllers/state_machine_controller.dart index f74ca11..fbb792e 100644 --- a/lib/src/controllers/state_machine_controller.dart +++ b/lib/src/controllers/state_machine_controller.dart @@ -165,6 +165,9 @@ class StateMachineController extends core.StateMachineController { } /// Find an input with a specific backing type and a given name. + /// + /// For easier to use methods, see [getBoolInput], [getTriggerInput], + /// [getNumberInput]. SMIInput? findInput(String name) { for (final input in _inputs) { if (input._is() && input.name == name) { @@ -175,6 +178,9 @@ class StateMachineController extends core.StateMachineController { } /// Find an input of specific concrete input type, with a given name. + /// + /// For easier to use methods, see [getBoolInput], [getTriggerInput], + /// [getNumberInput]. T? findSMI(String name) { for (final input in _inputs) { if (input is T && input.name == name) { @@ -184,6 +190,24 @@ class StateMachineController extends core.StateMachineController { return null; } + /// Find a boolean input with a given name. + SMIBool? getBoolInput(String name) => findSMI(name); + + /// Find a trigger input with a given name. + SMITrigger? getTriggerInput(String name) => findSMI(name); + + /// Find a number input with a given name. + /// + /// See [triggerInput] to directly fire a trigger by its name. + SMINumber? getNumberInput(String name) => findSMI(name); + + /// Convenience method for firing a trigger input with a given name. + /// + /// Also see [getTriggerInput] to get a reference to the trigger input. If the + /// trigger happens frequently, it's more efficient to get a reference to the + /// trigger input and call `trigger.fire()` directly. + void triggerInput(String name) => getTriggerInput(name)?.fire(); + @override void advanceInputs() { for (final input in _inputs) { diff --git a/lib/src/rive.dart b/lib/src/rive.dart index 65f36f9..e6e007e 100644 --- a/lib/src/rive.dart +++ b/lib/src/rive.dart @@ -101,11 +101,10 @@ class Rive extends LeafRenderObjectWidget { this.enablePointerEvents = false, this.cursor = MouseCursor.defer, this.behavior = RiveHitTestBehavior.opaque, - BoxFit? fit, - Alignment? alignment, + this.fit = BoxFit.contain, + this.alignment = Alignment.center, this.clipRect, - }) : fit = fit ?? BoxFit.contain, - alignment = alignment ?? Alignment.center; + }); @override RenderObject createRenderObject(BuildContext context) { diff --git a/lib/src/widgets/rive_animation.dart b/lib/src/widgets/rive_animation.dart index 40c27d2..d80f2da 100644 --- a/lib/src/widgets/rive_animation.dart +++ b/lib/src/widgets/rive_animation.dart @@ -339,8 +339,8 @@ class RiveAnimationState extends State { Widget build(BuildContext context) => _artboard != null ? Rive( artboard: _artboard!, - fit: widget.fit, - alignment: widget.alignment, + fit: widget.fit ?? BoxFit.contain, + alignment: widget.alignment ?? Alignment.center, antialiasing: widget.antialiasing, useArtboardSize: widget.useArtboardSize, clipRect: widget.clipRect, diff --git a/test/goldens/skins_opacity/golden_skins_opacity_test.dart b/test/goldens/skins_opacity/golden_skins_opacity_test.dart index f68671d..dd6c7b3 100644 --- a/test/goldens/skins_opacity/golden_skins_opacity_test.dart +++ b/test/goldens/skins_opacity/golden_skins_opacity_test.dart @@ -23,7 +23,7 @@ void main() { )!; controller.isActive = false; artboard.addController(controller); - trigger = controller.findInput('Skin') as SMITrigger; + trigger = controller.getTriggerInput('Skin')!; }, ); diff --git a/test/goldens/spilled_time/spilled_time_test.dart b/test/goldens/spilled_time/spilled_time_test.dart index befd499..9328f67 100644 --- a/test/goldens/spilled_time/spilled_time_test.dart +++ b/test/goldens/spilled_time/spilled_time_test.dart @@ -43,7 +43,7 @@ void main() { )!; controller.isActive = false; artboard.addController(controller); - trigger = controller.findInput('Trigger 1') as SMITrigger; + trigger = controller.getTriggerInput('Trigger 1')!; }, ); await tester.pumpWidget(widget); @@ -96,7 +96,7 @@ void main() { )!; controller.isActive = false; artboard.addController(controller); - trigger = controller.findInput('Trigger 1') as SMITrigger; + trigger = controller.getTriggerInput('Trigger 1')!; }, ); await tester.pumpWidget(widget); @@ -131,7 +131,7 @@ void main() { )!; controller.isActive = false; artboard.addController(controller); - trigger = controller.findInput('Trigger 1') as SMITrigger; + trigger = controller.getTriggerInput('Trigger 1')!; }, ); await tester.pumpWidget(widget);