Expose methods to easily get Rive state machine inputs

Our current API is quite verbose and requires casting to the relevant class types when using the current find API. These methods simplify the process.

Diffs=
1460f5366 Expose methods to easily get Rive state machine inputs (#7085)

Co-authored-by: Gordon <pggordonhayes@gmail.com>
This commit is contained in:
HayesGordon
2024-04-22 10:11:46 +00:00
parent 06265422cc
commit 4e2547d08d
13 changed files with 50 additions and 26 deletions

View File

@ -1 +1 @@
1a8b3c7ccd4bb10f085f2b45bf4685cd23cc0b0f
1460f5366b341ea2535f267c51d589569c870ef7

View File

@ -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

View File

@ -16,8 +16,8 @@ class _ExampleStateMachineState extends State<ExampleStateMachine> {
Artboard? _riveArtboard;
StateMachineController? _controller;
SMIInput<bool>? _hoverInput;
SMIInput<bool>? _pressInput;
SMIBool? _hoverInput;
SMIBool? _pressInput;
@override
void initState() {
@ -37,8 +37,8 @@ class _ExampleStateMachineState extends State<ExampleStateMachine> {
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);
},

View File

@ -17,8 +17,8 @@ class _LiquidDownloadState extends State<LiquidDownload> {
Artboard? _riveArtboard;
StateMachineController? _controller;
SMIInput<bool>? _start;
SMIInput<double>? _progress;
SMITrigger? _start;
SMINumber? _progress;
@override
void initState() {
@ -38,8 +38,8 @@ class _LiquidDownloadState extends State<LiquidDownload> {
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);
},

View File

@ -19,7 +19,7 @@ class _LittleMachineState extends State<LittleMachine> {
Artboard? _riveArtboard;
StateMachineController? _controller;
SMIInput<bool>? _trigger;
SMITrigger? _trigger;
@override
void initState() {
@ -42,7 +42,7 @@ class _LittleMachineState extends State<LittleMachine> {
);
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<LittleMachine> {
_riveArtboard == null
? const SizedBox()
: GestureDetector(
onTapDown: (_) => _trigger?.value = true,
onTapDown: (_) => _trigger?.fire(),
child: Rive(
artboard: _riveArtboard!,
fit: BoxFit.cover,

View File

@ -17,7 +17,7 @@ class _SimpleStateMachineState extends State<SimpleStateMachine> {
void _onRiveInit(Artboard artboard) {
final controller = StateMachineController.fromArtboard(artboard, 'bumpy');
artboard.addController(controller!);
_bump = controller.findInput<bool>('bump') as SMITrigger;
_bump = controller.getTriggerInput('bump');
}
void _hitBump() => _bump?.fire();

View File

@ -31,7 +31,7 @@ class _SkinningDemoState extends State<SkinningDemo> {
);
artboard.addController(controller!);
_skin = controller.findInput<bool>('Skin') as SMITrigger;
_skin = controller.getTriggerInput('Skin');
}
void _onStateChange(String stateMachineName, String stateName) {

View File

@ -16,7 +16,7 @@ class _StateMachineSkillsState extends State<StateMachineSkills> {
Artboard? _riveArtboard;
StateMachineController? _controller;
SMIInput<double>? _levelInput;
SMINumber? _levelInput;
@override
void initState() {
@ -36,7 +36,7 @@ class _StateMachineSkillsState extends State<StateMachineSkills> {
StateMachineController.fromArtboard(artboard, 'Designer\'s Test');
if (controller != null) {
artboard.addController(controller);
_levelInput = controller.findInput('Level');
_levelInput = controller.getNumberInput('Level');
}
setState(() => _riveArtboard = artboard);
},

View File

@ -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<T>? findInput<T>(String name) {
for (final input in _inputs) {
if (input._is<T>() && 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<T>(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<SMIBool>(name);
/// Find a trigger input with a given name.
SMITrigger? getTriggerInput(String name) => findSMI<SMITrigger>(name);
/// Find a number input with a given name.
///
/// See [triggerInput] to directly fire a trigger by its name.
SMINumber? getNumberInput(String name) => findSMI<SMINumber>(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) {

View File

@ -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) {

View File

@ -339,8 +339,8 @@ class RiveAnimationState extends State<RiveAnimation> {
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,

View File

@ -23,7 +23,7 @@ void main() {
)!;
controller.isActive = false;
artboard.addController(controller);
trigger = controller.findInput<bool>('Skin') as SMITrigger;
trigger = controller.getTriggerInput('Skin')!;
},
);

View File

@ -43,7 +43,7 @@ void main() {
)!;
controller.isActive = false;
artboard.addController(controller);
trigger = controller.findInput<bool>('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<bool>('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<bool>('Trigger 1') as SMITrigger;
trigger = controller.getTriggerInput('Trigger 1')!;
},
);
await tester.pumpWidget(widget);