Last active
December 13, 2023 13:23
-
-
Save agarasul/9ee7542cee55aecfd644bd05af345792 to your computer and use it in GitHub Desktop.
Map drawer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum class DrawerMotionEvent { idle, down, up, move } | |
data class MapDrawState( | |
val currentPosition: Offset = Offset.Unspecified, | |
val event: MotionEvent = MotionEvent.idle | |
) | |
@Composable | |
fun MapDrawer() { | |
var state by remember { mutableStateOf(MapDrawState()) } | |
val brush = remember { SolidColor(Color.Black) } | |
val path = remember { Path() } | |
Canvas( | |
modifier = Modifier | |
.fillMaxSize() | |
.pointerInput(Unit) { | |
awaitEachGesture { | |
do { | |
val event: PointerEvent = awaitPointerEvent() | |
event.changes.forEach { changes -> | |
val position = changes.position | |
state = state.copy( | |
currentPosition = position, | |
event = DrawerMotionEvent.move | |
) | |
} | |
} while (event.changes.any { it.pressed }) | |
// Touch Up event | |
state = state.copy( | |
currentPosition = currentEvent.changes.first().position, | |
event = DrawerMotionEvent.up | |
) | |
} | |
}, | |
onDraw = { | |
when (state.event) { | |
DrawerMotionEvent.idle -> Unit | |
DrawerMotionEvent.up, DrawerMotionEvent.move -> path.lineTo( | |
state.currentPosition.x, | |
state.currentPosition.y | |
) | |
DrawerMotionEvent.down -> path.moveTo(state.currentPosition.x, state.currentPosition.y) | |
} | |
drawPath( | |
path = path, | |
brush = brush, | |
style = Stroke(width = 5f) | |
) | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment