From 049311ec9b9abff15503a06506d529b32cde2673 Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Tue, 30 Sep 2025 13:27:56 +0300 Subject: [PATCH] distance measurer --- .../com/mirenkov/ktheightmap/MapCanvas.kt | 65 +++++++++++++++---- 1 file changed, 54 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/mirenkov/ktheightmap/MapCanvas.kt b/app/src/main/java/com/mirenkov/ktheightmap/MapCanvas.kt index 10378c6..1a3f31f 100644 --- a/app/src/main/java/com/mirenkov/ktheightmap/MapCanvas.kt +++ b/app/src/main/java/com/mirenkov/ktheightmap/MapCanvas.kt @@ -49,6 +49,7 @@ fun MapCanvas( var pointLat by rememberSaveable { viewModel.rememberedPointLat } var pointLon by rememberSaveable { viewModel.rememberedPointLon } var invokeHeightCalc by remember { mutableStateOf(false) } + val startTargetMeters = 64 * 1000 * 1000 Canvas( modifier = modifier.fillMaxSize() .pointerInput(Unit) { @@ -93,6 +94,10 @@ fun MapCanvas( } } + val targetMeters = startTargetMeters shr level + val targetPixels = (targetMeters).toFloat() / SphereMercator.scaledMetersPerPixel(level) + val measurerHeight = 24F + val centerTileX = (1 + (offsetX + halvedX) / TILE_SIZE).toDouble() val centerTileY = (1 + (offsetY + halvedY) / TILE_SIZE).toDouble() @@ -239,19 +244,31 @@ fun MapCanvas( } // Cursor path - val path = Path() - path.moveTo(halvedX - crossRadius, halvedY) - path.lineTo(halvedX + crossRadius, halvedY) - path.moveTo(halvedX, halvedY - crossRadius) - path.lineTo(halvedX, halvedY + crossRadius) - path.close() + val cursorPath = Path() + with(cursorPath) { + moveTo(halvedX - crossRadius, halvedY) + lineTo(halvedX + crossRadius, halvedY) + moveTo(halvedX, halvedY - crossRadius) + lineTo(halvedX, halvedY + crossRadius) + close() + } // Cursor drawPath( - path, + cursorPath, Color.White, style = Stroke(width = 6F) ) + // Height under cursor + drawText( + textMeasurer = textMeasurer, + text = buildAnnotatedString { + withStyle(SpanStyle(color = Color.White)) { + append("${KhmParser.getHeight(lon, lat, ctx)}m") + } + }, + topLeft = Offset(halvedX + crossRadius, halvedY + crossRadius) + ) // Info box drawRect( @@ -277,15 +294,41 @@ fun MapCanvas( topLeft = latLonOffset ) - // Height under cursor + // Distance measurer path + val measurerPath = Path() + with(measurerPath) { + moveTo((halvedX - targetPixels).toFloat(), 8F) + relativeLineTo(0F, measurerHeight) + relativeMoveTo(targetPixels.toFloat() * 2F, 0F) + relativeLineTo(0F, -measurerHeight) + relativeMoveTo(0F, measurerHeight / 2F) + relativeLineTo(-2F * targetPixels.toFloat(), 0F) + close() + } + + // Distance measurer + drawPath( + measurerPath, + Color.White, + style = Stroke(width = 6F) + ) + + // Distance measurer text drawText( textMeasurer = textMeasurer, text = buildAnnotatedString { - withStyle(SpanStyle(color = Color.White)) { - append("${KhmParser.getHeight(lon, lat, ctx)}m") + withStyle(ParagraphStyle(textAlign = TextAlign.Center)) { + withStyle(SpanStyle(color = Color.White)) { + val text = if (targetMeters >= 100000) + "${targetMeters / 1000}km" + else + "${targetMeters}m" + append(text) + } } }, - topLeft = Offset(halvedX + crossRadius, halvedY + crossRadius) + topLeft = Offset(targetPixels.toFloat() * 2F, measurerHeight), + size = Size(targetPixels.toFloat() * 2F, 48F) ) } } \ No newline at end of file