Fixing origin

This commit is contained in:
Luigi Rosso
2022-05-26 15:23:01 -07:00
committed by Luigi Rosso
parent 5b752605f6
commit 2ac9a3c5a0
3 changed files with 29 additions and 21 deletions

View File

@ -250,8 +250,10 @@ class StateMachineController extends RiveAnimationController<CoreContext> {
late List<_HitShape> hitShapes; late List<_HitShape> hitShapes;
late List<NestedArtboard> hitNestedArtboards; late List<NestedArtboard> hitNestedArtboards;
Artboard? _artboard;
/// The artboard that this state machine controller is manipulating. /// The artboard that this state machine controller is manipulating.
Artboard? get artboard => stateMachine.artboard; Artboard? get artboard => _artboard;
@override @override
bool init(CoreContext core) { bool init(CoreContext core) {
@ -292,14 +294,16 @@ class StateMachineController extends RiveAnimationController<CoreContext> {
} }
hitShapes = hitShapeLookup.values.toList(); hitShapes = hitShapeLookup.values.toList();
var artboard = core as RuntimeArtboard; _artboard = core as RuntimeArtboard;
List<NestedArtboard> nestedArtboards = []; List<NestedArtboard> nestedArtboards = [];
for (final nestedArtboard in artboard.activeNestedArtboards) { if (_artboard != null) {
for (final nestedArtboard in _artboard!.activeNestedArtboards) {
if (nestedArtboard.hasNestedStateMachine) { if (nestedArtboard.hasNestedStateMachine) {
nestedArtboards.add(nestedArtboard); nestedArtboards.add(nestedArtboard);
} }
} }
}
hitNestedArtboards = nestedArtboards; hitNestedArtboards = nestedArtboards;
return super.init(core); return super.init(core);
} }
@ -338,6 +342,18 @@ class StateMachineController extends RiveAnimationController<CoreContext> {
} }
void _processEvent(Vec2D position, {EventType? hitEvent}) { void _processEvent(Vec2D position, {EventType? hitEvent}) {
var artboard = this.artboard;
if (artboard == null) {
return;
}
if (artboard.frameOrigin) {
// ignore: parameter_assignments
position = position -
Vec2D.fromValues(
artboard.width * artboard.originX,
artboard.height * artboard.originY,
);
}
const hitRadius = 2; const hitRadius = 2;
var hitArea = IAABB( var hitArea = IAABB(
(position.x - hitRadius).round(), (position.x - hitRadius).round(),

View File

@ -129,6 +129,7 @@ class RuntimeNestedStateMachineInstance extends NestedStateMachineInstance {
class RuntimeMountedArtboard extends MountedArtboard { class RuntimeMountedArtboard extends MountedArtboard {
final RuntimeArtboard artboardInstance; final RuntimeArtboard artboardInstance;
RuntimeMountedArtboard(this.artboardInstance) { RuntimeMountedArtboard(this.artboardInstance) {
artboardInstance.frameOrigin = false;
artboardInstance.advance(0, nested: true); artboardInstance.advance(0, nested: true);
} }
@ -142,8 +143,6 @@ class RuntimeMountedArtboard extends MountedArtboard {
void draw(Canvas canvas) { void draw(Canvas canvas) {
canvas.save(); canvas.save();
canvas.transform(worldTransform.mat4); canvas.transform(worldTransform.mat4);
canvas.translate(-artboardInstance.originX * artboardInstance.width,
-artboardInstance.originY * artboardInstance.height);
artboardInstance.draw(canvas); artboardInstance.draw(canvas);
canvas.restore(); canvas.restore();
} }
@ -169,10 +168,4 @@ class RuntimeMountedArtboard extends MountedArtboard {
@override @override
bool advance(double seconds) => bool advance(double seconds) =>
artboardInstance.advance(seconds, nested: true); artboardInstance.advance(seconds, nested: true);
@override
Mat2D get originTransform => Mat2D.fromTranslate(
artboardInstance.width * -artboardInstance.originX,
artboardInstance.height * -artboardInstance.originY,
);
} }

View File

@ -189,18 +189,17 @@ class _RiveAnimationState extends State<RiveAnimation> {
} }
var globalCoordinates = renderObject.localToGlobal(local); var globalCoordinates = renderObject.localToGlobal(local);
var artboardCoord = riveRenderer!.globalToArtboard(globalCoordinates); return riveRenderer!.globalToArtboard(globalCoordinates);
return artboardCoord -
Vec2D.fromValues(_artboard!.originX * _artboard!.width,
_artboard!.originY * _artboard!.height);
} }
Widget _optionalHitTester(BuildContext context, Widget child) { Widget _optionalHitTester(BuildContext context, Widget child) {
assert(_artboard != null); assert(_artboard != null);
var hasHitTesting = _artboard!.animationControllers.any((controller) => var hasHitTesting = _artboard!.animationControllers.any(
(controller) =>
controller is StateMachineController && controller is StateMachineController &&
controller.hitShapes.isNotEmpty); (controller.hitShapes.isNotEmpty ||
controller.hitNestedArtboards.isNotEmpty),
);
if (hasHitTesting) { if (hasHitTesting) {
void hitHelper(PointerEvent event, void hitHelper(PointerEvent event,