Skip to content

Commit c610fb8

Browse files
add shape add and edit dialog
1 parent f860ec8 commit c610fb8

File tree

3 files changed

+480
-118
lines changed

3 files changed

+480
-118
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.smarttoolfactory.cropper.settings.frames.edit
2+
3+
import androidx.compose.material3.AlertDialog
4+
import androidx.compose.material3.Button
5+
import androidx.compose.material3.Text
6+
import androidx.compose.runtime.*
7+
import androidx.compose.ui.graphics.ImageBitmap
8+
import androidx.compose.ui.res.imageResource
9+
import com.smarttoolfactory.cropper.R
10+
import com.smarttoolfactory.cropper.model.*
11+
12+
@Composable
13+
fun CropFrameAddDialog(
14+
aspectRatio: AspectRatio,
15+
cropFrame: CropFrame,
16+
onConfirm: (CropFrame) -> Unit,
17+
onDismiss: () -> Unit
18+
) {
19+
20+
val dstBitmap = ImageBitmap.imageResource(id = R.drawable.landscape2)
21+
22+
val outlineType = cropFrame.outlineType
23+
24+
var outline: CropOutline by remember {
25+
mutableStateOf(cropFrame.copy().outlines[0])
26+
}
27+
28+
AlertDialog(
29+
onDismissRequest = onDismiss,
30+
text = {
31+
when (outlineType) {
32+
OutlineType.RoundedRect -> {
33+
34+
val shape = outline as RoundedCornerCropShape
35+
36+
RoundedCornerCropShapeEdit(
37+
aspectRatio = aspectRatio,
38+
dstBitmap = dstBitmap,
39+
title = outline.title,
40+
roundedCornerCropShape = shape
41+
) {
42+
outline = it
43+
}
44+
}
45+
OutlineType.CutCorner -> {
46+
val shape = outline as CutCornerCropShape
47+
48+
CutCornerCropShapeEdit(
49+
aspectRatio = aspectRatio,
50+
dstBitmap = dstBitmap,
51+
title = outline.title,
52+
cutCornerCropShape = shape
53+
) {
54+
outline = it
55+
}
56+
}
57+
OutlineType.Oval -> {
58+
59+
val shape = outline as OvalCropShape
60+
61+
OvalCropShapeEdit(
62+
aspectRatio = aspectRatio,
63+
dstBitmap = dstBitmap,
64+
title = outline.title,
65+
ovalCropShape = shape
66+
) {
67+
outline = it
68+
}
69+
}
70+
OutlineType.Polygon -> {
71+
72+
val shape = outline as PolygonCropShape
73+
74+
PolygonCropShapeEdit(
75+
aspectRatio = aspectRatio,
76+
dstBitmap = dstBitmap,
77+
title = outline.title,
78+
polygonCropShape = shape
79+
) {
80+
outline = it
81+
}
82+
}
83+
else -> Unit
84+
}
85+
},
86+
confirmButton = {
87+
Button(onClick = {
88+
89+
val newOutlines: List<CropOutline> = cropFrame.outlines
90+
.toMutableList()
91+
.apply {
92+
add(outline)
93+
}
94+
.toList()
95+
96+
val newCropFrame = cropFrame.copy(
97+
cropOutlineContainer = getOutlineContainer(
98+
outlineType = outlineType,
99+
index = newOutlines.size - 1,
100+
outlines = newOutlines
101+
)
102+
)
103+
104+
onConfirm(newCropFrame)
105+
}) {
106+
Text("OK")
107+
}
108+
},
109+
dismissButton = {
110+
Button(onClick = { onDismiss() }) {
111+
Text(text = "Cancel")
112+
}
113+
}
114+
)
115+
}

cropper/src/main/java/com/smarttoolfactory/cropper/settings/frames/edit/CropFrameEditDialog.kt

Lines changed: 5 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,15 @@ import androidx.compose.material3.AlertDialog
44
import androidx.compose.material3.Button
55
import androidx.compose.material3.Text
66
import androidx.compose.runtime.*
7-
import androidx.compose.ui.Modifier
8-
import androidx.compose.ui.draw.drawWithCache
9-
import androidx.compose.ui.geometry.Size
10-
import androidx.compose.ui.graphics.Color
117
import androidx.compose.ui.graphics.ImageBitmap
12-
import androidx.compose.ui.graphics.Shape
13-
import androidx.compose.ui.graphics.drawOutline
14-
import androidx.compose.ui.graphics.drawscope.translate
158
import androidx.compose.ui.res.imageResource
16-
import androidx.compose.ui.unit.Density
179
import com.smarttoolfactory.cropper.R
1810
import com.smarttoolfactory.cropper.model.*
19-
import com.smarttoolfactory.cropper.util.drawBlockWithCheckerAndLayer
2011

2112
@Composable
2213
fun CropFrameEditDialog(
2314
aspectRatio: AspectRatio,
24-
index: Int = 0,
15+
index: Int,
2516
cropFrame: CropFrame,
2617
onConfirm: (CropFrame) -> Unit,
2718
onDismiss: () -> Unit
@@ -32,7 +23,7 @@ fun CropFrameEditDialog(
3223
val outlineType = cropFrame.outlineType
3324

3425
var outline: CropOutline by remember {
35-
mutableStateOf(cropFrame.cropOutlineContainer.outlines[index])
26+
mutableStateOf(cropFrame.outlines[index])
3627
}
3728

3829
AlertDialog(
@@ -96,7 +87,7 @@ fun CropFrameEditDialog(
9687
confirmButton = {
9788
Button(onClick = {
9889

99-
val newOutlines: List<CropOutline> = cropFrame.cropOutlineContainer.outlines
90+
val newOutlines: List<CropOutline> = cropFrame.outlines
10091
.toMutableList()
10192
.apply {
10293
set(index, outline)
@@ -109,117 +100,13 @@ fun CropFrameEditDialog(
109100

110101
onConfirm(newCropFrame)
111102
}) {
112-
Text("Confirm")
103+
Text("OK")
113104
}
114105
},
115106
dismissButton = {
116107
Button(onClick = { onDismiss() }) {
117-
Text(text = "Dismiss")
108+
Text(text = "Cancel")
118109
}
119110
}
120111
)
121112
}
122-
123-
internal fun Modifier.drawOutlineWithCache(
124-
aspectRatio: AspectRatio,
125-
shape: Shape,
126-
density: Density,
127-
dstBitmap: ImageBitmap
128-
) = this.then(
129-
Modifier.drawWithCache {
130-
131-
val coefficient = .9f
132-
133-
val width = size.width
134-
val height = size.height
135-
136-
val value = aspectRatio.value
137-
138-
val shapeSize = if (aspectRatio == AspectRatio.Unspecified) {
139-
Size(width * coefficient, height * coefficient)
140-
} else if (value > 1) {
141-
Size(
142-
width = coefficient * width,
143-
height = coefficient * width / value
144-
)
145-
} else {
146-
Size(width = coefficient * height * value, height = coefficient * height)
147-
}
148-
149-
val left = (width - shapeSize.width) / 2
150-
val top = (height - shapeSize.height) / 2
151-
152-
val outline = shape.createOutline(
153-
size = shapeSize,
154-
layoutDirection = layoutDirection,
155-
density = density
156-
)
157-
158-
onDrawWithContent {
159-
drawBlockWithCheckerAndLayer(dstBitmap) {
160-
translate(left = left, top = top) {
161-
drawOutline(
162-
outline = outline,
163-
color = Color.Red,
164-
)
165-
}
166-
}
167-
}
168-
}
169-
)
170-
171-
@Suppress("UNCHECKED_CAST")
172-
private fun getOutlineContainer(
173-
outlineType: OutlineType,
174-
index: Int,
175-
outlines: List<CropOutline>
176-
): CropOutlineContainer<out CropOutline> {
177-
return when (outlineType) {
178-
OutlineType.RoundedRect -> {
179-
RoundedRectOutlineContainer(
180-
selectedIndex = index,
181-
outlines = outlines as List<RoundedCornerCropShape>
182-
)
183-
}
184-
OutlineType.CutCorner -> {
185-
CutCornerRectOutlineContainer(
186-
selectedIndex = index,
187-
outlines = outlines as List<CutCornerCropShape>
188-
)
189-
}
190-
191-
OutlineType.Oval -> {
192-
OvalOutlineContainer(
193-
selectedIndex = index,
194-
outlines = outlines as List<OvalCropShape>
195-
)
196-
}
197-
198-
OutlineType.Polygon -> {
199-
PolygonOutlineContainer(
200-
selectedIndex = index,
201-
outlines = outlines as List<PolygonCropShape>
202-
)
203-
}
204-
205-
OutlineType.Custom -> {
206-
PolygonOutlineContainer(
207-
selectedIndex = index,
208-
outlines = outlines as List<PolygonCropShape>
209-
)
210-
}
211-
212-
OutlineType.ImageMask -> {
213-
ImageMaskOutlineContainer(
214-
selectedIndex = index,
215-
outlines = outlines as List<ImageMaskOutline>
216-
)
217-
}
218-
else -> {
219-
RectOutlineContainer(
220-
selectedIndex = index,
221-
outlines = outlines as List<RectCropShape>
222-
)
223-
}
224-
}
225-
}

0 commit comments

Comments
 (0)