|
| 1 | +package com.smarttoolfactory.composecropper.preferences.frames.edit |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.Box |
| 4 | +import androidx.compose.foundation.layout.Column |
| 5 | +import androidx.compose.foundation.layout.aspectRatio |
| 6 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 7 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 8 | +import androidx.compose.material3.TextField |
| 9 | +import androidx.compose.runtime.* |
| 10 | +import androidx.compose.ui.Modifier |
| 11 | +import androidx.compose.ui.draw.clipToBounds |
| 12 | +import androidx.compose.ui.draw.drawWithCache |
| 13 | +import androidx.compose.ui.graphics.Color |
| 14 | +import androidx.compose.ui.graphics.ImageBitmap |
| 15 | +import androidx.compose.ui.graphics.Path |
| 16 | +import com.smarttoolfactory.cropper.model.AspectRatio |
| 17 | +import com.smarttoolfactory.cropper.model.CustomPathOutline |
| 18 | +import com.smarttoolfactory.cropper.util.calculateSizeAndOffsetFromAspectRatio |
| 19 | +import com.smarttoolfactory.cropper.util.drawBlockWithCheckerAndLayer |
| 20 | +import com.smarttoolfactory.cropper.util.scaleAndTranslatePath |
| 21 | + |
| 22 | +@OptIn(ExperimentalMaterial3Api::class) |
| 23 | +@Composable |
| 24 | +internal fun CustomPathEdit( |
| 25 | + aspectRatio: AspectRatio, |
| 26 | + dstBitmap: ImageBitmap, |
| 27 | + customPathOutline: CustomPathOutline, |
| 28 | + onChange: (CustomPathOutline) -> Unit |
| 29 | +) { |
| 30 | + var newTitle by remember { |
| 31 | + mutableStateOf(customPathOutline.title) |
| 32 | + } |
| 33 | + |
| 34 | + Column { |
| 35 | + |
| 36 | + Box( |
| 37 | + modifier = Modifier |
| 38 | + .fillMaxWidth() |
| 39 | + .clipToBounds() |
| 40 | + .aspectRatio(4 / 3f) |
| 41 | + .drawWithCache { |
| 42 | + |
| 43 | + val path = Path().apply { |
| 44 | + addPath(customPathOutline.path) |
| 45 | + |
| 46 | + val (newSize, offset) = calculateSizeAndOffsetFromAspectRatio( |
| 47 | + aspectRatio = aspectRatio, |
| 48 | + coefficient = 1f, |
| 49 | + size = size |
| 50 | + ) |
| 51 | + scaleAndTranslatePath(newSize.width, newSize.height) |
| 52 | + translate(offset) |
| 53 | + } |
| 54 | + |
| 55 | + onDrawWithContent { |
| 56 | + drawBlockWithCheckerAndLayer(dstBitmap) { |
| 57 | + drawPath(path, Color.Red) |
| 58 | + |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + ) |
| 64 | + |
| 65 | + TextField( |
| 66 | + value = newTitle, |
| 67 | + onValueChange = { |
| 68 | + newTitle = it |
| 69 | + onChange( |
| 70 | + customPathOutline.copy( |
| 71 | + title = newTitle |
| 72 | + ) |
| 73 | + ) |
| 74 | + |
| 75 | + } |
| 76 | + ) |
| 77 | + } |
| 78 | +} |
0 commit comments