mirror of
https://github.com/rive-app/rive-flutter.git
synced 2025-05-17 13:26:03 +08:00
Pointer events working.
This commit is contained in:
BIN
example/assets/cannon.riv
Normal file
BIN
example/assets/cannon.riv
Normal file
Binary file not shown.
22
example/lib/hit_events.dart
Normal file
22
example/lib/hit_events.dart
Normal file
@ -0,0 +1,22 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:rive/rive.dart';
|
||||
|
||||
class HitEventExample extends StatelessWidget {
|
||||
const HitEventExample({Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Hit Events'),
|
||||
),
|
||||
body: const Center(
|
||||
child: RiveAnimation.asset(
|
||||
'assets/cannon.riv',
|
||||
fit: BoxFit.cover,
|
||||
stateMachines: ['State Machine 1'],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:rive_example/custom_controller.dart';
|
||||
import 'package:rive_example/example_state_machine.dart';
|
||||
import 'package:rive_example/hit_events.dart';
|
||||
import 'package:rive_example/liquid_download.dart';
|
||||
import 'package:rive_example/little_machine.dart';
|
||||
import 'package:rive_example/play_one_shot_animation.dart';
|
||||
@ -39,6 +40,20 @@ class Home extends StatelessWidget {
|
||||
const SizedBox(
|
||||
height: 10,
|
||||
),
|
||||
ElevatedButton(
|
||||
child: const Text('Hit Event'),
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute<void>(
|
||||
builder: (context) => const HitEventExample(),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
const SizedBox(
|
||||
height: 10,
|
||||
),
|
||||
ElevatedButton(
|
||||
child: const Text('Play/Pause Animation'),
|
||||
onPressed: () {
|
||||
|
@ -55,9 +55,15 @@ class Mat2D {
|
||||
: _buffer = Float32List.fromList(
|
||||
[1.0, 0.0, 0.0, 1.0, translation.x, translation.y]);
|
||||
|
||||
Mat2D.fromTranslate(double x, double y)
|
||||
: _buffer = Float32List.fromList([1.0, 0.0, 0.0, 1.0, x, y]);
|
||||
|
||||
Mat2D.fromScaling(Vec2D scaling)
|
||||
: _buffer = Float32List.fromList([scaling.x, 0, 0, scaling.y, 0, 0]);
|
||||
|
||||
Mat2D.fromScale(double x, double y)
|
||||
: _buffer = Float32List.fromList([x, 0, 0, y, 0, 0]);
|
||||
|
||||
Mat2D.fromMat4(Float64List mat4)
|
||||
: _buffer = Float32List.fromList(
|
||||
[mat4[0], mat4[1], mat4[4], mat4[5], mat4[12], mat4[13]]);
|
||||
|
@ -262,10 +262,6 @@ class StateMachineController extends RiveAnimationController<CoreContext> {
|
||||
// Initialize all events.
|
||||
HashMap<Shape, _HitShape> hitShapeLookup = HashMap<Shape, _HitShape>();
|
||||
for (final event in stateMachine.events) {
|
||||
// Early out if we know target doesn't exist on the source artboard.
|
||||
if (event.target == null) {
|
||||
continue;
|
||||
}
|
||||
// Resolve target on this artboard instance.
|
||||
var node = core.resolve<Node>(event.targetId);
|
||||
if (node == null) {
|
||||
|
@ -20,6 +20,7 @@ import 'package:rive/src/rive_core/animation/keyed_property.dart';
|
||||
import 'package:rive/src/rive_core/animation/layer_state.dart';
|
||||
import 'package:rive/src/rive_core/animation/linear_animation.dart';
|
||||
import 'package:rive/src/rive_core/animation/state_machine.dart';
|
||||
import 'package:rive/src/rive_core/animation/state_machine_event.dart';
|
||||
import 'package:rive/src/rive_core/animation/state_machine_layer.dart';
|
||||
import 'package:rive/src/rive_core/animation/state_transition.dart';
|
||||
import 'package:rive/src/rive_core/artboard.dart';
|
||||
@ -158,6 +159,9 @@ class RiveFile {
|
||||
case StateMachineLayerBase.typeKey:
|
||||
stackObject = StateMachineLayerImporter(object as StateMachineLayer);
|
||||
break;
|
||||
case StateMachineEventBase.typeKey:
|
||||
stackObject = StateMachineEventImporter(object as StateMachineEvent);
|
||||
break;
|
||||
|
||||
case EntryStateBase.typeKey:
|
||||
case AnyStateBase.typeKey:
|
||||
|
@ -5,7 +5,6 @@ import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
import 'package:rive/src/rive_core/math/aabb.dart';
|
||||
import 'package:rive/src/rive_core/math/mat2d.dart';
|
||||
import 'package:rive/src/rive_core/math/vec2d.dart';
|
||||
|
||||
abstract class RiveRenderBox extends RenderBox {
|
||||
final Stopwatch _stopwatch = Stopwatch();
|
||||
@ -211,81 +210,14 @@ abstract class RiveRenderBox extends RenderBox {
|
||||
|
||||
final Canvas canvas = context.canvas;
|
||||
|
||||
AABB bounds = aabb;
|
||||
|
||||
double contentWidth = bounds[2] - bounds[0];
|
||||
double contentHeight = bounds[3] - bounds[1];
|
||||
|
||||
if (contentWidth == 0 || contentHeight == 0) {
|
||||
AABB contentBounds = aabb;
|
||||
if (contentBounds.width == 0 || contentBounds.height == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
double x = -1 * bounds[0] -
|
||||
contentWidth / 2.0 -
|
||||
(_alignment.x * contentWidth / 2.0);
|
||||
double y = -1 * bounds[1] -
|
||||
contentHeight / 2.0 -
|
||||
(_alignment.y * contentHeight / 2.0);
|
||||
|
||||
double scaleX = 1.0, scaleY = 1.0;
|
||||
|
||||
var transform = computeAlignment(offset);
|
||||
canvas.save();
|
||||
beforeDraw(canvas, offset);
|
||||
|
||||
switch (_fit) {
|
||||
case BoxFit.fill:
|
||||
scaleX = size.width / contentWidth;
|
||||
scaleY = size.height / contentHeight;
|
||||
break;
|
||||
case BoxFit.contain:
|
||||
double minScale =
|
||||
min(size.width / contentWidth, size.height / contentHeight);
|
||||
scaleX = scaleY = minScale;
|
||||
break;
|
||||
case BoxFit.cover:
|
||||
double maxScale =
|
||||
max(size.width / contentWidth, size.height / contentHeight);
|
||||
scaleX = scaleY = maxScale;
|
||||
break;
|
||||
case BoxFit.fitHeight:
|
||||
double minScale = size.height / contentHeight;
|
||||
scaleX = scaleY = minScale;
|
||||
break;
|
||||
case BoxFit.fitWidth:
|
||||
double minScale = size.width / contentWidth;
|
||||
scaleX = scaleY = minScale;
|
||||
break;
|
||||
case BoxFit.none:
|
||||
scaleX = scaleY = 1.0;
|
||||
break;
|
||||
case BoxFit.scaleDown:
|
||||
double minScale =
|
||||
min(size.width / contentWidth, size.height / contentHeight);
|
||||
scaleX = scaleY = minScale < 1.0 ? minScale : 1.0;
|
||||
break;
|
||||
}
|
||||
|
||||
Mat2D transform = Mat2D();
|
||||
|
||||
transform[4] = size.width / 2.0 + (_alignment.x * size.width / 2.0);
|
||||
transform[5] = size.height / 2.0 + (_alignment.y * size.height / 2.0);
|
||||
if (offsetViewTransform) {
|
||||
transform[4] += offset.dx;
|
||||
transform[5] += offset.dy;
|
||||
}
|
||||
Mat2D.scale(transform, transform, Vec2D.fromValues(scaleX, scaleY));
|
||||
Mat2D center = Mat2D();
|
||||
center[4] = x;
|
||||
center[5] = y;
|
||||
Mat2D.multiply(transform, transform, center);
|
||||
|
||||
canvas.translate(
|
||||
offset.dx + size.width / 2.0 + (_alignment.x * size.width / 2.0),
|
||||
offset.dy + size.height / 2.0 + (_alignment.y * size.height / 2.0),
|
||||
);
|
||||
|
||||
canvas.scale(scaleX, scaleY);
|
||||
canvas.translate(x, y);
|
||||
canvas.transform(transform.mat4);
|
||||
|
||||
draw(canvas, transform);
|
||||
|
||||
@ -296,4 +228,78 @@ abstract class RiveRenderBox extends RenderBox {
|
||||
/// Advance animations, physics, etc by elapsedSeconds, returns true if it
|
||||
/// wants to run again.
|
||||
bool advance(double elapsedSeconds);
|
||||
|
||||
Mat2D computeAlignment(Offset offset) {
|
||||
AABB frame = AABB.fromValues(
|
||||
offset.dx, offset.dy, offset.dx + size.width, offset.dy + size.height);
|
||||
AABB content = aabb;
|
||||
double contentWidth = content[2] - content[0];
|
||||
double contentHeight = content[3] - content[1];
|
||||
double x =
|
||||
-content[0] - contentWidth / 2.0 - (alignment.x * contentWidth / 2.0);
|
||||
double y =
|
||||
-content[1] - contentHeight / 2.0 - (alignment.y * contentHeight / 2.0);
|
||||
|
||||
double scaleX = 1.0, scaleY = 1.0;
|
||||
|
||||
switch (fit) {
|
||||
case BoxFit.fill:
|
||||
{
|
||||
scaleX = frame.width / contentWidth;
|
||||
scaleY = frame.height / contentHeight;
|
||||
break;
|
||||
}
|
||||
case BoxFit.contain:
|
||||
{
|
||||
double minScale =
|
||||
min(frame.width / contentWidth, frame.height / contentHeight);
|
||||
scaleX = scaleY = minScale;
|
||||
break;
|
||||
}
|
||||
case BoxFit.cover:
|
||||
{
|
||||
double maxScale =
|
||||
max(frame.width / contentWidth, frame.height / contentHeight);
|
||||
scaleX = scaleY = maxScale;
|
||||
break;
|
||||
}
|
||||
case BoxFit.fitHeight:
|
||||
{
|
||||
double minScale = frame.height / contentHeight;
|
||||
scaleX = scaleY = minScale;
|
||||
break;
|
||||
}
|
||||
case BoxFit.fitWidth:
|
||||
{
|
||||
double minScale = frame.width / contentWidth;
|
||||
scaleX = scaleY = minScale;
|
||||
break;
|
||||
}
|
||||
case BoxFit.none:
|
||||
{
|
||||
scaleX = scaleY = 1.0;
|
||||
break;
|
||||
}
|
||||
case BoxFit.scaleDown:
|
||||
{
|
||||
double minScale =
|
||||
min(frame.width / contentWidth, frame.height / contentHeight);
|
||||
scaleX = scaleY = minScale < 1.0 ? minScale : 1.0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Mat2D translation = Mat2D();
|
||||
translation[4] =
|
||||
frame[0] + frame.width / 2.0 + (alignment.x * frame.width / 2.0);
|
||||
translation[5] =
|
||||
frame[1] + frame.height / 2.0 + (alignment.y * frame.height / 2.0);
|
||||
|
||||
var result = Mat2D();
|
||||
|
||||
Mat2D.multiply(result, translation, Mat2D.fromScale(scaleX, scaleY));
|
||||
Mat2D.multiply(result, result, Mat2D.fromTranslate(x, y));
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:rive/math.dart';
|
||||
import 'package:rive/rive.dart';
|
||||
import 'package:rive/src/rive_render_box.dart';
|
||||
|
||||
/// Specifies whether a source is from an asset bundle or http
|
||||
enum _Source {
|
||||
@ -100,6 +102,8 @@ class _RiveAnimationState extends State<RiveAnimation> {
|
||||
/// Active artboard
|
||||
Artboard? _artboard;
|
||||
|
||||
bool _needsHitDetection = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@ -151,6 +155,9 @@ class _RiveAnimationState extends State<RiveAnimation> {
|
||||
final controller = StateMachineController.fromArtboard(artboard, name);
|
||||
if (controller != null) {
|
||||
artboard.addController((_controllers..add(controller)).last);
|
||||
if (controller.stateMachine.events.isNotEmpty) {
|
||||
_needsHitDetection = true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@ -169,13 +176,92 @@ class _RiveAnimationState extends State<RiveAnimation> {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Vec2D? _transform(Offset offset) {
|
||||
var renderObject = context.findRenderObject();
|
||||
if (renderObject is! RenderBox) {
|
||||
return null;
|
||||
}
|
||||
|
||||
RiveRenderBox? riveRenderBox;
|
||||
renderObject.visitChildren((child) {
|
||||
if (child is RiveRenderBox) {
|
||||
riveRenderBox = child;
|
||||
}
|
||||
});
|
||||
if (riveRenderBox == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var localOffset = riveRenderBox!.globalToLocal(offset);
|
||||
var transform = riveRenderBox!.computeAlignment(Offset.zero);
|
||||
if (Mat2D.invert(transform, transform)) {
|
||||
return transform * Vec2D.fromValues(localOffset.dx, localOffset.dy);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Widget _hitDetect(Widget child) {
|
||||
if (!_needsHitDetection) {
|
||||
return child;
|
||||
}
|
||||
return Listener(
|
||||
behavior: HitTestBehavior.opaque,
|
||||
child: child,
|
||||
onPointerDown: (details) {
|
||||
var position = _transform(details.position);
|
||||
if (position == null) {
|
||||
return;
|
||||
}
|
||||
for (final stateMachine
|
||||
in _controllers.whereType<StateMachineController>()) {
|
||||
stateMachine.pointerDown(position);
|
||||
}
|
||||
},
|
||||
onPointerUp: (details) {
|
||||
var position = _transform(details.position);
|
||||
if (position == null) {
|
||||
return;
|
||||
}
|
||||
for (final stateMachine
|
||||
in _controllers.whereType<StateMachineController>()) {
|
||||
stateMachine.pointerUp(position);
|
||||
}
|
||||
},
|
||||
onPointerHover: (details) {
|
||||
var position = _transform(details.position);
|
||||
if (position == null) {
|
||||
return;
|
||||
}
|
||||
for (final stateMachine
|
||||
in _controllers.whereType<StateMachineController>()) {
|
||||
stateMachine.pointerMove(position);
|
||||
}
|
||||
},
|
||||
onPointerMove: (details) {
|
||||
var position = _transform(details.position);
|
||||
if (position == null) {
|
||||
return;
|
||||
}
|
||||
for (final stateMachine
|
||||
in _controllers.whereType<StateMachineController>()) {
|
||||
stateMachine.pointerMove(position);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => _artboard != null
|
||||
? Rive(
|
||||
artboard: _artboard!,
|
||||
fit: widget.fit,
|
||||
alignment: widget.alignment,
|
||||
antialiasing: widget.antialiasing,
|
||||
)
|
||||
: widget.placeHolder ?? const SizedBox();
|
||||
Widget build(BuildContext context) {
|
||||
if (_artboard == null) {
|
||||
return widget.placeHolder ?? const SizedBox();
|
||||
}
|
||||
return _hitDetect(
|
||||
Rive(
|
||||
artboard: _artboard!,
|
||||
fit: widget.fit,
|
||||
alignment: widget.alignment,
|
||||
antialiasing: widget.antialiasing,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user