2d-inc / Flare-Flutter

Load and get full control of your Rive files in a Flutter project using this library.
https://rive.app/
MIT License
2.55k stars 469 forks source link

Measuring the distance of a click of the user and a subtree of a flare document #193

Open ChristianKleineidam opened 4 years ago

ChristianKleineidam commented 4 years ago

My flutter app requires the user to click on the correct part of an image. If the user clicked the correct part of image or within X dp of the part I want to start animation_correct, otherwise I want to start animation_wrong.

Does flare provide me inbuild methods to help me? Otherwise, what's the best strategy to implement such a feature?

luigi-rosso commented 4 years ago

We don't have anything that easily abstracts this, but Flutter provides everything you need to do this. I can think of two ways to do this.

One would require writing a custom LeafRenderWidget (this is what FlareActor uses). If you haven't seen how these work, basically they create a custom (ideally longer lived) RenderObject which does the actual rendering and loading of visual assets. Our FlareActor (LeafRenderWidget) uses a custom FlareRenderBox as it's RenderObject. This FlareRenderBox has a hitTestSelf method which you can override. It gets passed in the screenOffset as an argument. You could then compare that coordinate to the image/shape you're interested in and perhaps invoke some callback that you passed into your custom widget. We currently use this just to let the Flutter widget tree know that the Flare Object was tapped on as a whole, but you can use this functionality to inject a custom hit detector.

Another, I think more tedious way, would be to wrap the FlareActor in a GestureDetector and then propagate tap events to a custom controller on the FlareActor that can do hit detection. I dislike this because it happens higher in the widget hierarchy and I think it'll be more difficult to transform coordinate spaces here, but maybe not...

If you have a specific example file, we could try to hook up an example for you!

ChristianKleineidam commented 4 years ago

It's great for me to hear that this sounds doable. I'm not very far yet with creating my assets but I created one of a map of Europe. The user is supposed to click on Italy. I ideally get back how far the distance happen to be so that I can either start the "Correct" or the "Wrong" animation (it's nice to have the distance information to store). While preparing this example I noted that there currently seem to be no obvious way to crop the image in 2Dimensions.

europe-map.zip (The map is based on the public domain https://commons.wikimedia.org/wiki/File:CIA_WorldFactBook-Political_world.svg and free to use) https://www.2dimensions.com/a/christiankl/files/flare/map-of-europe

ChristianKleineidam commented 4 years ago

@luigi-rosso I'd love to implement this future soon. Can you hook up an example as you said above or give me pointers about how I can best do it myself?