@@ -32,11 +32,12 @@ import androidx.compose.ui.Modifier
32
32
import androidx.compose.ui.geometry.Offset
33
33
import androidx.compose.ui.geometry.Size
34
34
import androidx.compose.ui.graphics.Matrix
35
- import androidx.compose.ui.graphics.drawscope.withTransform
36
35
import androidx.compose.ui.graphics.graphicsLayer
37
36
import androidx.compose.ui.input.pointer.pointerInput
37
+ import androidx.compose.ui.layout.onSizeChanged
38
38
import androidx.compose.ui.res.stringResource
39
39
import androidx.compose.ui.tooling.preview.Preview
40
+ import androidx.compose.ui.unit.toSize
40
41
import com.dede.android_eggs.cat_editor.CaptureControllerDelegate.Companion.rememberCaptureControllerDelegate
41
42
import com.dede.android_eggs.cat_editor.CatParts.VIEW_PORT_SIZE
42
43
import com.dede.android_eggs.cat_editor.Utilities.asAndroidMatrix
@@ -84,8 +85,7 @@ internal fun CatEditor(
84
85
captureController.onPerCapture = {
85
86
// set normal state
86
87
selectedPart = - 1
87
- scale = 1f
88
- offset = Offset .Zero
88
+ controllerImpl.resetGraphicsLayer()
89
89
}
90
90
}
91
91
@@ -102,10 +102,15 @@ internal fun CatEditor(
102
102
scale = nextScaleLevel(scale, S_MAX , S_MIN )
103
103
}
104
104
105
+ var editorSize by remember { mutableStateOf(Size .Zero ) }
106
+
105
107
Box (
106
108
contentAlignment = Alignment .Center ,
107
109
modifier = Modifier
108
110
.fillMaxSize()
111
+ .onSizeChanged {
112
+ editorSize = it.toSize()
113
+ }
109
114
.pointerInput(controllerImpl.isGesturesEnabled) {
110
115
if (! controllerImpl.isGesturesEnabled) {
111
116
return @pointerInput
@@ -146,8 +151,15 @@ internal fun CatEditor(
146
151
var canvasSize by remember { mutableStateOf(Size .Zero ) }
147
152
val canvasMatrix = remember(canvasSize) { createCanvasMatrix(canvasSize) }
148
153
154
+ LaunchedEffect (canvasSize, editorSize) {
155
+ if (canvasSize != Size .Zero && editorSize != Size .Zero ) {
156
+ controllerImpl.defaultGraphicsLayerScale =
157
+ editorSize.minDimension / canvasSize.maxDimension
158
+ }
159
+ }
160
+
149
161
val bitmap: Bitmap ? = remember(canvasSize) {
150
- if (needAndroidCanvasDraw ) createAndroidBitmap(canvasSize) else null
162
+ if (useAndroidCanvasDraw ) createAndroidBitmap(canvasSize) else null
151
163
}
152
164
Canvas (
153
165
contentDescription = stringResource(StringR .string.cat_editor),
@@ -188,28 +200,23 @@ internal fun CatEditor(
188
200
return @Canvas
189
201
}
190
202
191
- // native canvas draw
192
- if (needAndroidCanvasDraw && bitmap != null ) {
203
+ if (useAndroidCanvasDraw && bitmap != null ) {
204
+ // android canvas draw
193
205
androidCanvasDraw(
194
206
canvasMatrix.asAndroidMatrix(),
195
207
bitmap,
196
208
controllerImpl.colorList,
197
209
selectedPart,
198
210
blendRatio
199
211
)
200
- return @Canvas
201
- }
202
-
203
- // compose canvas draw
204
- withTransform({ transform(canvasMatrix) }) {
205
- CatParts .drawOrders.forEachIndexed { index, pathDraw ->
206
- var color = controllerImpl.colorList[index]
207
- if (selectedPart == index) {
208
- val blend = Utilities .getHighlightColor(color)
209
- color = Utilities .blendColor(color, blend, blendRatio)
210
- }
211
- pathDraw.drawLambda.invoke(this , color)
212
- }
212
+ } else {
213
+ // compose canvas draw
214
+ composeCanvasDraw(
215
+ canvasMatrix,
216
+ controllerImpl.colorList,
217
+ selectedPart,
218
+ blendRatio
219
+ )
213
220
}
214
221
}
215
222
)
0 commit comments