pskink / matrix_gesture_detector

A gesture detector mapping translation/rotation/scale gestures to a Matrix4 object.
Other
134 stars 102 forks source link

How to reset the matrix #15

Open meDerekD17 opened 4 years ago

meDerekD17 commented 4 years ago

Using info from Resetting the Matrix#8, I got the matrix to reset correcting when using CustomPaint (basically the same code as the example with CustomPaint). But I have no idea why it is working. I seem to have replaced notifier.value = m, with notifier.value = matrix; when normal zoom, offset and rotation.

It seems to work, but I do not understand. Is it possible my code will give memory leaks or anything? Is it the best way for CustomPaint?

My Code: class _CustomPainterDemoState extends State { Matrix4 matrix = Matrix4.identity(); ValueNotifier notifier; bool resetMatrix = false;

@override void initState() { super.initState(); notifier = ValueNotifier(matrix); ..... in the build body: MatrixGestureDetector( onMatrixUpdate: (m, tm, sm, rm) { if(resetMatrix) { matrix = MatrixGestureDetector.compose(matrix, tm, sm, rm); //above appears to reset all matrices } notifier.value = matrix; //original code had notifier.value = m },

I then have child: CustomPaint, exactly like the example At the bottom, I have a floating Action Button to reset the screen floatingActionButton: FloatingActionButton( onPressed:(){ setState(() { matrix = Matrix4.identity(); notifier = ValueNotifier(matrix); resetMatrix = true; }); } ),

JoseGeorges8 commented 4 years ago

This works for me:

onMatrixUpdate: (Matrix4 m, Matrix4 tm, Matrix4 sm, Matrix4 rm) {
    setState(() {
        matrix = MatrixGestureDetector.compose(matrix, tm, sm, rm);
    });
},

And arround MatrixGestureDetector I've got a gesture detector like so:

GestureDetector(
    onDoubleTap: (){
        setState(() {
            matrix = Matrix4.identity();
         });
     },
     child: MatrixGestureDetector(...),
);