|
@@ -1,10 +1,13 @@
|
|
|
package com.ys.imageProcess.ui.work
|
|
|
|
|
|
+import android.graphics.Point
|
|
|
import android.graphics.PointF
|
|
|
import androidx.compose.foundation.Canvas
|
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
|
import androidx.compose.runtime.Composable
|
|
|
import androidx.compose.ui.Modifier
|
|
|
+import androidx.compose.ui.geometry.Offset
|
|
|
+import androidx.compose.ui.geometry.Rect
|
|
|
import androidx.compose.ui.graphics.Color
|
|
|
import androidx.compose.ui.graphics.Path
|
|
|
import androidx.compose.ui.graphics.drawscope.DrawScope
|
|
@@ -12,6 +15,7 @@ import androidx.compose.ui.graphics.drawscope.Fill
|
|
|
import androidx.compose.ui.graphics.drawscope.Stroke
|
|
|
import androidx.compose.ui.tooling.preview.Preview
|
|
|
import com.ys.imageProcess.ui.theme.ImageProcTheme
|
|
|
+import kotlin.math.abs
|
|
|
|
|
|
@Composable
|
|
|
fun CanvasView(
|
|
@@ -20,47 +24,99 @@ fun CanvasView(
|
|
|
length: List<PointF> = listOf(),
|
|
|
width: List<PointF> = listOf(),
|
|
|
fillColor: Color = Color.Transparent,
|
|
|
- strokeWidth: Float = 2f,
|
|
|
+ scale: Float = 1f,
|
|
|
state: ImageState = ImageState()
|
|
|
) {
|
|
|
Canvas(modifier.fillMaxSize()) {
|
|
|
+ val strokeWidth = 5 / scale
|
|
|
if (state.showContour) {
|
|
|
- drawPolygon(contour, fillColor, Color.Green, strokeWidth)
|
|
|
+ drawPolygon(contour, fillColor, Color.Green, strokeWidth, state.editMode)
|
|
|
}
|
|
|
if (state.showWidth) {
|
|
|
- drawPolygon(width, fillColor, Color.Blue, strokeWidth)
|
|
|
+ drawPolygon(width, fillColor, Color.Blue, strokeWidth, state.editMode)
|
|
|
}
|
|
|
if (state.showLength) {
|
|
|
- drawPolygon(length, fillColor, Color.Red, strokeWidth)
|
|
|
+ drawPolygon(length, fillColor, Color.Red, strokeWidth, state.editMode)
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-fun DrawScope.drawPolygon(
|
|
|
+private fun DrawScope.drawPolygon(
|
|
|
points: List<PointF>,
|
|
|
fillColor: Color,
|
|
|
strokeColor: Color,
|
|
|
strokeWidth: Float,
|
|
|
+ showAnchors: Boolean = false,
|
|
|
closed: Boolean = false
|
|
|
) {
|
|
|
val d = density
|
|
|
val path = Path().apply {
|
|
|
if (points.isNotEmpty()) {
|
|
|
- moveTo(points[0].x * d, points[0].y * d)
|
|
|
- for (i in 1 until points.size) {
|
|
|
- lineTo(points[i].x * d, points[i].y * d)
|
|
|
+ for (i in points.indices) {
|
|
|
+ val x = points[i].x * d
|
|
|
+ val y = points[i].y * d
|
|
|
+ if (i == 0) {
|
|
|
+ moveTo(x, y)
|
|
|
+ } else {
|
|
|
+ lineTo(x, y)
|
|
|
+ }
|
|
|
}
|
|
|
if (closed) {
|
|
|
close()
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ if (showAnchors) {
|
|
|
+ val l = strokeWidth * 5
|
|
|
+
|
|
|
+ for (i in points.indices) {
|
|
|
+ val x = points[i].x * d
|
|
|
+ val y = points[i].y * d
|
|
|
+
|
|
|
+ moveTo(x, y)
|
|
|
+ val rect = Rect(Offset(x - l, y - l), Offset(x + l, y + l))
|
|
|
+ addRect(rect)
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // var showRect = true
|
|
|
+ // var lastRectIndex = -1
|
|
|
+ // val l = strokeWidth * 5
|
|
|
+ //
|
|
|
+ // for (i in points.indices) {
|
|
|
+ // val x = points[i].x * d
|
|
|
+ // val y = points[i].y * d
|
|
|
+ // if (i > 0) {
|
|
|
+ // if (lastRectIndex >= 0) {
|
|
|
+ // val lastRectX = points[lastRectIndex].x * d
|
|
|
+ // val lastRectY = points[lastRectIndex].y * d
|
|
|
+ // showRect =
|
|
|
+ // abs(lastRectX - x) > l * 4 || abs(lastRectY - y) > l * 4
|
|
|
+ // showRect = true
|
|
|
+ // }
|
|
|
+ // if (showRect) {
|
|
|
+ // lastRectIndex = i
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ //
|
|
|
+ // if (showRect) {
|
|
|
+ // moveTo(x, y)
|
|
|
+ // val rect = Rect(Offset(x - l, y - l), Offset(x + l, y + l))
|
|
|
+ // addRect(rect)
|
|
|
+ // }
|
|
|
+
|
|
|
+ // }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- drawPath(
|
|
|
- path = path,
|
|
|
- color = fillColor,
|
|
|
- style = Fill
|
|
|
- )
|
|
|
+// drawPath(
|
|
|
+// path = path,
|
|
|
+// color = fillColor,
|
|
|
+// style = Fill
|
|
|
+// )
|
|
|
|
|
|
drawPath(
|
|
|
path = path,
|