Skip to content

Commit

Permalink
added ability to draw arrows
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Sep 28, 2023
1 parent 7a7dc15 commit f0d94ac
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import dev.olshevski.navigation.reimagined.hilt.hiltViewModel
import kotlinx.coroutines.launch
import ru.tech.imageresizershrinker.presentation.draw_screen.components.BlurRadiusSelector
import ru.tech.imageresizershrinker.presentation.draw_screen.components.DrawAlphaSelector
import ru.tech.imageresizershrinker.presentation.draw_screen.components.DrawArrowsSelector
import ru.tech.imageresizershrinker.presentation.draw_screen.components.DrawBackgroundSelector
import ru.tech.imageresizershrinker.presentation.draw_screen.components.DrawBehavior
import ru.tech.imageresizershrinker.presentation.draw_screen.components.DrawColorSelector
Expand Down Expand Up @@ -295,6 +296,9 @@ fun DrawScreen(
var blurRadius by rememberSaveable(viewModel.drawBehavior, drawMode) {
mutableFloatStateOf(if (drawMode is DrawMode.Neon) 35f else 0f)
}
var drawArrowsEnabled by remember(viewModel.drawBehavior) {
mutableStateOf(false)
}

val controls = @Composable {
OpenColorPickerCard(
Expand Down Expand Up @@ -349,6 +353,17 @@ fun DrawScreen(
drawMode = drawMode,
onDrawModeChange = { drawMode = it }
)
AnimatedVisibility(!isEraserOn) {
DrawArrowsSelector(
modifier = Modifier.padding(
start = 16.dp,
end = 16.dp,
bottom = 16.dp
),
checked = drawArrowsEnabled,
onCheckedChange = { drawArrowsEnabled = it }
)
}
SaveExifWidget(
modifier = Modifier.padding(horizontal = 16.dp),
selected = viewModel.saveExif,
Expand Down Expand Up @@ -434,6 +449,7 @@ fun DrawScreen(
onShare = { viewModel.shareBitmap { showConfetti() } },
paths = viewModel.paths,
isEraserOn = isEraserOn,
drawArrowsEnabled = drawArrowsEnabled,
drawMode = drawMode,
backgroundColor = backgroundColor,
drawColor = drawColor.copy(alpha),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import androidx.compose.ui.graphics.ImageShader
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.PaintingStyle
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.PathMeasure
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.StrokeJoin
import androidx.compose.ui.graphics.asAndroidBitmap
Expand All @@ -53,6 +54,8 @@ import ru.tech.imageresizershrinker.presentation.erase_background_screen.compone
import ru.tech.imageresizershrinker.presentation.erase_background_screen.components.transparencyChecker
import ru.tech.imageresizershrinker.presentation.root.theme.outlineVariant
import ru.tech.imageresizershrinker.presentation.root.transformation.filter.StackBlurFilter
import kotlin.math.cos
import kotlin.math.sin


@Composable
Expand All @@ -66,6 +69,7 @@ fun BitmapDrawer(
isEraserOn: Boolean,
drawMode: DrawMode,
modifier: Modifier,
drawArrowsEnabled: Boolean,
onDraw: (Bitmap) -> Unit,
backgroundColor: Color,
zoomEnabled: Boolean,
Expand Down Expand Up @@ -128,7 +132,7 @@ fun BitmapDrawer(
LaunchedEffect(imageBitmap, drawBitmap) {
blurredBitmap = imageManager.transform(
image = drawImageBitmap.overlay(drawBitmap).asAndroidBitmap(),
transformations = listOf(StackBlurFilter(context, 0.3f to 50))
transformations = listOf(StackBlurFilter(context, 0.3f to 20))
)?.asImageBitmap()
}

Expand Down Expand Up @@ -204,6 +208,35 @@ fun BitmapDrawer(

MotionEvent.Up -> {
drawPath.lineTo(currentPosition.x, currentPosition.y)

if (drawArrowsEnabled && !isEraserOn) {
val preLastPoint = PathMeasure().apply {
setPath(drawPath, false)
}.let {
it.getPosition(it.length - strokeWidth * 3f)
}
val lastPoint = currentPosition

val (x, y) = lastPoint - preLastPoint


val angle1 = 150.0
val rotatedX1 =
x * cos(Math.toRadians(angle1)) - y * sin(Math.toRadians(angle1))
val rotatedY1 =
x * sin(Math.toRadians(angle1)) + y * cos(Math.toRadians(angle1))

val angle2 = 210.0
val rotatedX2 =
x * cos(Math.toRadians(angle2)) - y * sin(Math.toRadians(angle2))
val rotatedY2 =
x * sin(Math.toRadians(angle2)) + y * cos(Math.toRadians(angle2))

drawPath.relativeLineTo(rotatedX1.toFloat(), rotatedY1.toFloat())
drawPath.moveTo(lastPoint.x, lastPoint.y)
drawPath.relativeLineTo(rotatedX2.toFloat(), rotatedY2.toFloat())
}

currentPosition = Offset.Unspecified
previousPosition = currentPosition
motionEvent = MotionEvent.Idle
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ru.tech.imageresizershrinker.presentation.draw_screen.components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.ArrowCircleUp
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import ru.tech.imageresizershrinker.R
import ru.tech.imageresizershrinker.presentation.root.utils.modifier.container

@Composable
fun DrawArrowsSelector(
modifier: Modifier,
checked: Boolean,
onCheckedChange: (Boolean) -> Unit
) {
Row(
modifier = modifier
.container(shape = RoundedCornerShape(24.dp), resultPadding = 0.dp)
.clickable { onCheckedChange(!checked) }
.padding(horizontal = 16.dp, vertical = 8.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(Icons.Rounded.ArrowCircleUp, null, modifier = Modifier.defaultMinSize(24.dp, 24.dp))
Column(
modifier = Modifier
.weight(1f)
.padding(start = 16.dp, end = 16.dp, top = 6.dp, bottom = 8.dp)
) {
Text(
text = stringResource(R.string.draw_arrows),
fontWeight = FontWeight.Medium
)
Text(
text = stringResource(R.string.draw_arrows_sub),
fontWeight = FontWeight.Normal,
color = LocalContentColor.current.copy(0.5f),
lineHeight = 12.sp,
fontSize = 12.sp
)
}
Switch(
checked = checked,
onCheckedChange = onCheckedChange
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ fun DrawHost(
drawColor: Color,
drawAlpha: Float,
strokeWidth: Float,
drawArrowsEnabled: Boolean,
bitmap: Bitmap,
blurRadius: Float,
addPath: (PathPaint) -> Unit,
Expand Down Expand Up @@ -160,6 +161,7 @@ fun DrawHost(
zoomEnabled = zoomEnabled,
onDraw = onDraw,
imageManager = imageManager,
drawArrowsEnabled = drawArrowsEnabled,
backgroundColor = backgroundColor
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import ru.tech.imageresizershrinker.domain.image.ImageManager
import ru.tech.imageresizershrinker.presentation.draw_screen.components.BitmapDrawer
import ru.tech.imageresizershrinker.presentation.draw_screen.components.BlurRadiusSelector
import ru.tech.imageresizershrinker.presentation.draw_screen.components.DrawAlphaSelector
import ru.tech.imageresizershrinker.presentation.draw_screen.components.DrawArrowsSelector
import ru.tech.imageresizershrinker.presentation.draw_screen.components.DrawColorSelector
import ru.tech.imageresizershrinker.presentation.draw_screen.components.DrawMode
import ru.tech.imageresizershrinker.presentation.draw_screen.components.DrawModeSelector
Expand Down Expand Up @@ -132,6 +133,9 @@ fun DrawEditOption(
var blurRadius by rememberSaveable(drawMode) {
mutableFloatStateOf(if (drawMode is DrawMode.Neon) 35f else 0f)
}
var drawArrowsEnabled by remember {
mutableStateOf(false)
}

val secondaryControls = @Composable {
val border = BorderStroke(
Expand Down Expand Up @@ -234,6 +238,17 @@ fun DrawEditOption(
drawMode = drawMode,
onDrawModeChange = { drawMode = it }
)
AnimatedVisibility(!isEraserOn) {
DrawArrowsSelector(
modifier = Modifier.padding(
start = 16.dp,
end = 16.dp,
bottom = 16.dp
),
checked = drawArrowsEnabled,
onCheckedChange = { drawArrowsEnabled = it }
)
}
},
fabButtons = null,
actions = {
Expand Down Expand Up @@ -299,6 +314,7 @@ fun DrawEditOption(
onDraw = {
stateBitmap = it
},
drawArrowsEnabled = drawArrowsEnabled,
backgroundColor = Color.Transparent
)
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@
<string name="allow_betas">Allow betas</string>
<string name="allow_betas_sub">Update checking will include beta app versions if enabled</string>
<string name="shadows_setting_sub">Enables drawing shadows behind containers and buttons if borders are disabled</string>
<string name="draw_arrows">Draw Arrows</string>
<string name="draw_arrows_sub">If enabled drawing path will be represented as pointing arrow</string>
<string-array name="fab_alignment_sub">
<item>Start</item>
<item>Center</item>
Expand Down

0 comments on commit f0d94ac

Please sign in to comment.