1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.camera.viewfinder.compose
18 
19 import androidx.compose.runtime.getValue
20 import androidx.compose.runtime.mutableStateOf
21 import androidx.compose.runtime.setValue
22 import androidx.compose.ui.geometry.Offset
23 import androidx.compose.ui.graphics.Matrix
24 
25 /**
26  * Coordinate transformer that's used to convert coordinates from one space to another.
27  *
28  * [transformMatrix] must be set by whoever manipulates the coordinate space, otherwise an identity
29  * will be used for the coordinate transformations. When used in a [Viewfinder], the viewfinder will
30  * set this transform matrix.
31  */
32 interface CoordinateTransformer {
33     /** Matrix that's used for coordinate transformations. */
34     val transformMatrix: Matrix
35 
36     /** Returns the [Offset] in the transformed space. */
transformnull37     fun Offset.transform() = transformMatrix.map(this)
38 }
39 
40 /** CoordinateTransformer where the transformMatrix is mutable. */
41 interface MutableCoordinateTransformer : CoordinateTransformer {
42     override var transformMatrix: Matrix
43 }
44 
45 /** Creates a [MutableCoordinateTransformer] with the given matrix as the transformMatrix. */
MutableCoordinateTransformernull46 fun MutableCoordinateTransformer(matrix: Matrix = Matrix()): MutableCoordinateTransformer =
47     MutableCoordinateTransformerImpl(matrix)
48 
49 private class MutableCoordinateTransformerImpl(initialMatrix: Matrix) :
50     MutableCoordinateTransformer {
51     override var transformMatrix: Matrix by mutableStateOf(initialMatrix)
52 }
53 
54 /** [CoordinateTransformer] where the transformMatrix is the identity matrix. */
55 object IdentityCoordinateTransformer : CoordinateTransformer {
56     override val transformMatrix = Matrix()
57 
transformnull58     override fun Offset.transform() = this
59 }
60