From 823d5db5d78e1992d76b9d04bfc9896bfb04772e Mon Sep 17 00:00:00 2001 From: Dasemu Date: Thu, 1 Oct 2026 12:35:12 +0200 Subject: [PATCH] Expand map area on motorcycle display --- .../java/dev/navframe/app/MapFrameComposer.kt | 186 +++++++++++------- .../dev/navframe/app/MapLibreTftRenderer.kt | 2 +- .../dev/navframe/app/MapFrameComposerTest.kt | 15 ++ 3 files changed, 129 insertions(+), 74 deletions(-) diff --git a/app/src/main/java/dev/navframe/app/MapFrameComposer.kt b/app/src/main/java/dev/navframe/app/MapFrameComposer.kt index e39cc0b..3fe941e 100644 --- a/app/src/main/java/dev/navframe/app/MapFrameComposer.kt +++ b/app/src/main/java/dev/navframe/app/MapFrameComposer.kt @@ -5,109 +5,149 @@ import dev.navframe.core.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import java.io.ByteArrayOutputStream -import kotlin.math.roundToInt /** Native JPEG overlay, independent of a map SDK so its layout can be verified without GPU access. */ class MapFrameComposer { suspend fun compose(map: Bitmap?, marker: PointF?, state: NavigationState, mode: NavigationMode, attribution: String, unavailable: Boolean = false, failureCode: String? = null): TftFrame = withContext(Dispatchers.Default) { - require(map == null || (map.width == 480 && map.height == 240)) { "Bitmap dimensions invalid" } - val bitmap = Bitmap.createBitmap(480, 240, Bitmap.Config.ARGB_8888) + require(map == null || (map.width == WIDTH && map.height == HEIGHT)) { "Bitmap dimensions invalid" } + val bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888) try { val canvas = Canvas(bitmap) canvas.drawColor(Color.rgb(13, 19, 25)) - map?.let { canvas.drawBitmap(it, 0f, 0f, null) } // Same dimensions: no phone-resolution render or downscale. + map?.let { canvas.drawBitmap(it, 0f, 0f, null) } val paint = Paint(Paint.ANTI_ALIAS_FLAG) fun text(value: String, x: Float, y: Float, size: Float, color: Int = Color.WHITE) { paint.apply { this.color = color; textSize = size; style = Paint.Style.FILL; typeface = Typeface.create("sans-serif", Typeface.BOLD) } canvas.drawText(value, x, y, paint) } + fun fitted(value: String, size: Float, width: Float): String { + paint.textSize = size + if (paint.measureText(value) <= width) return value + var result = value + while (result.isNotEmpty() && paint.measureText("$result…") > width) result = result.dropLast(1) + return if (result.isEmpty()) "" else "$result…" + } + val lost = state.navigationStatus == NavigationStatus.GPS_LOST val accent = Color.rgb(80, 227, 194) + val guidanceVisible = state.route != null && state.navigationStatus != NavigationStatus.ROUTE_PREVIEW + val preview = state.route != null && !guidanceVisible + val topHeight = when { guidanceVisible -> GUIDANCE_HEIGHT; preview -> PREVIEW_HEIGHT; else -> STATUS_HEIGHT } + + // Keep navigation cues in one shallow band; let the map occupy the whole center. paint.color = Color.rgb(7, 12, 17) - canvas.drawRect(0f, 0f, 480f, 27f, paint) - text(if (mode == NavigationMode.MAP_DEMO) "MAPA · DEMO${if (state.route != null) " · RUTA" else " SIMULADO"}" else if (lost) "MAPA GPS · SIN SEÑAL" else if (state.route != null) "MAPA GPS · RUTA CALCULADA" else "MAPA GPS · SIN RUTA", 9f, 20f, 17f, if (lost) Color.YELLOW else accent) + canvas.drawRect(0f, 0f, WIDTH.toFloat(), topHeight.toFloat(), paint) + val modeText = if (mode == NavigationMode.MAP_DEMO) "DEMO" else "GPS" + val routeText = when { + lost -> "SIN SEÑAL GPS" + unavailable || map == null -> "MAPA NO DISPONIBLE" + state.route != null -> "RUTA" + else -> "MAPA" + } + text("$modeText · $routeText", 8f, 13f, 10f, if (lost || unavailable) Color.YELLOW else accent) + + if (guidanceVisible) { + val warning = when { + unavailable || map == null -> "GUIADO EN ESPERA · SIN MAPA" + lost -> "SIN GPS · esperando posición precisa" + state.navigationStatus == NavigationStatus.REROUTING -> "RECALCULANDO · espera nueva ruta" + state.navigationStatus == NavigationStatus.OFF_ROUTE -> "FUERA DE RUTA · revisa el recorrido" + state.navigationStatus == NavigationStatus.ARRIVED -> "HAS LLEGADO AL DESTINO" + else -> null + } + if (warning != null) { + text(fitted(warning, 16f, 460f), 10f, 39f, 16f, if (state.navigationStatus == NavigationStatus.ARRIVED) accent else Color.YELLOW) + } else { + val maneuver = state.nextManeuver + val direction = maneuverDirection(maneuver?.type).name + canvas.save() + canvas.translate(27f, 33f) + canvas.rotate(if (direction.contains("LEFT")) -90f else if (direction.contains("RIGHT")) 90f else 0f) + paint.apply { style = Paint.Style.STROKE; color = accent; strokeWidth = 6f; strokeCap = Paint.Cap.ROUND; strokeJoin = Paint.Join.ROUND } + val icon = Path().apply { moveTo(0f, 14f); lineTo(0f, -13f); moveTo(-10f, -3f); lineTo(0f, -13f); lineTo(10f, -3f) } + if (direction == "ARRIVAL") { + canvas.drawLine(-8f, 15f, -8f, -15f, paint) + canvas.drawPath(Path().apply { moveTo(-8f, -15f); lineTo(13f, -9f); lineTo(-8f, -2f) }, paint) + } else if (direction.contains("ROUNDABOUT")) canvas.drawCircle(0f, 0f, 13f, paint) + else if (direction.contains("UTURN") || direction.contains("U_TURN")) { + canvas.drawArc(-12f, -14f, 12f, 11f, 180f, 180f, false, paint) + canvas.drawLine(-12f, -2f, -12f, 15f, paint); canvas.drawLine(12f, -2f, 12f, 15f, paint) + } else canvas.drawPath(icon, paint) + canvas.restore() + paint.strokeCap = Paint.Cap.BUTT + text(fitted(maneuver?.instruction ?: "Continúa por la ruta", 15f, 392f), 48f, 30f, 15f) + val next = state.distanceToNextManeuverMeters?.let { if (it >= 1000) "%.1f km".format(java.util.Locale.ROOT, it / 1000.0) else "$it m" } ?: "-- m" + text(next, 48f, 49f, 19f, accent) + } + } else if (preview) { + val route = requireNotNull(state.route) + val summary = "PREVIA · %.1f km · %d min".format(java.util.Locale.ROOT, route.distanceMeters / 1000.0, (route.durationSeconds + 59) / 60) + text(fitted(summary, 13f, 460f), 8f, 31f, 13f) + } + if (map != null && marker != null) { - val x = marker.x - val y = marker.y - val arrow = Path().apply { moveTo(x, y - 19); lineTo(x + 14, y + 16); lineTo(x, y + 8); lineTo(x - 14, y + 16); close() } + val x = marker.x; val y = marker.y + val arrow = Path().apply { moveTo(x, y - 16); lineTo(x + 12, y + 13); lineTo(x, y + 7); lineTo(x - 12, y + 13); close() } paint.apply { color = if (lost) Color.GRAY else Color.WHITE; style = Paint.Style.FILL } canvas.drawPath(arrow, paint) - paint.apply { color = if (lost) Color.YELLOW else accent; style = Paint.Style.STROKE; strokeWidth = 3f } + paint.apply { color = if (lost) Color.YELLOW else accent; style = Paint.Style.STROKE; strokeWidth = 2.5f } canvas.drawPath(arrow, paint) - } - if (unavailable || map == null) { - text(if (unavailable) "MAPA NO DISPONIBLE" else "ESPERANDO GPS", 60f, 109f, 27f) - text(if (unavailable) "TFT: ${failureCode ?: "SDK"} · ${if (lost) "SIN GPS" else "Info"}" else "No se usa una posición ficticia", 52f, 149f, 20f) - } - val guidanceVisible = state.route != null && state.navigationStatus != NavigationStatus.ROUTE_PREVIEW - state.route?.let { route -> paint.style = Paint.Style.FILL + } + + if (unavailable || map == null) { paint.color = Color.rgb(7, 12, 17) - canvas.drawRect(0f, 28f, 480f, if (guidanceVisible) 88f else 51f, paint) - if (!guidanceVisible) text("PREVIA · %.1f km · %d min".format(java.util.Locale.ROOT, route.distanceMeters / 1000.0, (route.durationSeconds + 59) / 60), 9f, 45f, 16f) - else { - val statusText = when { - unavailable || map == null -> "GUIADO EN ESPERA · SIN MAPA" - lost -> "SIN GPS · esperando posición precisa" - state.navigationStatus == NavigationStatus.REROUTING -> "RECALCULANDO · espera nueva ruta" - state.navigationStatus == NavigationStatus.OFF_ROUTE -> "FUERA DE RUTA · revisa el recorrido" - state.navigationStatus == NavigationStatus.ARRIVED -> "HAS LLEGADO AL DESTINO" - else -> null - } - if (statusText != null) text(statusText, 9f, 64f, 20f, if (state.navigationStatus == NavigationStatus.ARRIVED) accent else Color.YELLOW) - else { - val maneuver = state.nextManeuver - val direction = maneuverDirection(maneuver?.type).name - canvas.save() - canvas.translate(40f, 61f) - canvas.rotate(if (direction.contains("LEFT")) -90f else if (direction.contains("RIGHT")) 90f else 0f) - paint.apply { style = Paint.Style.STROKE; color = accent; strokeWidth = 7f; strokeCap = Paint.Cap.ROUND; strokeJoin = Paint.Join.ROUND } - val icon = Path().apply { moveTo(0f, 19f); lineTo(0f, -18f); moveTo(-13f, -5f); lineTo(0f, -18f); lineTo(13f, -5f) } - if (direction == "ARRIVAL") { canvas.drawLine(-10f, 20f, -10f, -20f, paint); val flag = Path().apply { moveTo(-10f, -20f); lineTo(17f, -12f); lineTo(-10f, -3f) }; canvas.drawPath(flag, paint) } - else if (direction.contains("ROUNDABOUT")) canvas.drawCircle(0f, 1f, 16f, paint) - else if (direction.contains("UTURN") || direction.contains("U_TURN")) { canvas.drawArc(-15f, -17f, 15f, 13f, 180f, 180f, false, paint); canvas.drawLine(-15f, -2f, -15f, 19f, paint); canvas.drawLine(15f, -2f, 15f, 19f, paint) } - else canvas.drawPath(icon, paint) - canvas.restore() - paint.strokeCap = Paint.Cap.BUTT - var instruction = maneuver?.instruction ?: "Continúa por la ruta" - paint.textSize = 18f - while (instruction.isNotEmpty() && paint.measureText(instruction) > 384f) instruction = instruction.dropLast(1) - text(instruction, 81f, 49f, 18f) - val next = state.distanceToNextManeuverMeters?.let { if (it >= 1000) "%.1f km".format(java.util.Locale.ROOT, it / 1000.0) else "$it m" } ?: "-- m" - text(next, 81f, 79f, 28f, accent) - } - } + canvas.drawRect(36f, 78f, 444f, 132f, paint) + text(if (unavailable) "MAPA NO DISPONIBLE" else "ESPERANDO GPS", 50f, 101f, 21f) + text(if (unavailable) "TFT: ${failureCode ?: "SDK"} · ${if (lost) "SIN GPS" else "Comprueba fuente"}" else "No se usa una posición ficticia", 50f, 121f, 13f) } - // Wrap rather than clipping a custom provider's mandatory credits. - paint.typeface = Typeface.DEFAULT - paint.textSize = 12f - val lines = mutableListOf() - var line = "" - for (character in attribution.trim().replace(Regex("\\s+"), " ")) { - val next = line + character - if (line.isNotEmpty() && paint.measureText(next) > 464f) { lines += line.trim(); line = character.toString() } - else line = next - } - if (line.isNotEmpty()) lines += line - val footer = (240f - lines.size * 15f - if (guidanceVisible) 55f else 34f).coerceAtLeast(27f) + + // Credits stay visible, but consume only their actual wrapped lines. + val creditPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.WHITE; textSize = CREDIT_SIZE; typeface = Typeface.DEFAULT } + val credits = wrap(attribution, creditPaint, WIDTH - 16f) + val footerDataHeight = if (guidanceVisible) FOOTER_DATA_HEIGHT else 0 + val footerHeight = footerDataHeight + credits.size * CREDIT_LINE_HEIGHT + val footer = (HEIGHT - footerHeight).coerceAtLeast(topHeight + MIN_MAP_HEIGHT) paint.apply { color = Color.rgb(7, 12, 17); style = Paint.Style.FILL } - canvas.drawRect(0f, footer, 480f, 240f, paint) - val speed = if (lost || unavailable || map == null) "-- km/h" else "${(state.speed * 3.6f).roundToInt()} km/h" - text(speed, 9f, footer + 24f, 23f) - val heading = if (lost || unavailable || map == null || !state.bearing.isFinite()) "--" else state.bearing.roundToInt().toString() - text("Rumbo $heading°", 182f, footer + 24f, 20f) - text(if (mode == NavigationMode.MAP_DEMO) "DEMO" else "GPS", 393f, footer + 24f, 19f, accent) + canvas.drawRect(0f, footer.toFloat(), WIDTH.toFloat(), HEIGHT.toFloat(), paint) + if (guidanceVisible) { val valid = !lost && !unavailable && map != null && state.navigationStatus in setOf(NavigationStatus.NAVIGATING, NavigationStatus.ARRIVED) val remaining = if (valid) state.remainingDistanceMeters?.let { "%.1f km restantes".format(java.util.Locale.ROOT, it / 1000.0) } ?: "Restante --" else "Restante --" val eta = if (valid) state.etaEpochMillis?.let { java.text.SimpleDateFormat("HH:mm", java.util.Locale.ROOT).format(java.util.Date(it)) } ?: "--:--" else "--:--" - text(remaining, 9f, footer + 44f, 17f) - text("ETA $eta", 285f, footer + 44f, 17f) + // Speed and heading already occupy the Yamaha instrument panel; keep only trip data here. + text(fitted(remaining, 12f, 340f), 8f, footer + 14f, 12f) + text("ETA $eta", 397f, footer + 14f, 12f) + } + credits.forEachIndexed { index, credit -> + canvas.drawText(credit, 8f, footer + footerDataHeight + (index + 1) * CREDIT_LINE_HEIGHT.toFloat() - 2f, creditPaint) } - paint.apply { textSize = 12f; typeface = Typeface.DEFAULT; color = Color.WHITE } - lines.forEachIndexed { index, credit -> canvas.drawText(credit, 8f, footer + (if (guidanceVisible) 67f else 46f) + index * 15f, paint) } val bytes = ByteArrayOutputStream().use { out -> check(bitmap.compress(Bitmap.CompressFormat.JPEG, 70, out)); out.toByteArray() } TftFrame(encodedBytes = bytes, timestampMs = System.currentTimeMillis()) } finally { bitmap.recycle() } } + + private fun wrap(value: String, paint: Paint, maxWidth: Float): List { + val lines = mutableListOf() + var line = "" + for (character in value.trim().replace(Regex("\\s+"), " ")) { + val next = line + character + if (line.isNotEmpty() && paint.measureText(next) > maxWidth) { lines += line.trim(); line = character.toString() } + else line = next + } + if (line.isNotEmpty()) lines += line + return lines.ifEmpty { listOf("") } + } + + companion object { + const val WIDTH = 480 + const val HEIGHT = 240 + const val GUIDANCE_HEIGHT = 50 + const val PREVIEW_HEIGHT = 36 + const val STATUS_HEIGHT = 20 + const val MIN_MAP_HEIGHT = 88 + private const val FOOTER_DATA_HEIGHT = 18 + private const val CREDIT_SIZE = 8.5f + private const val CREDIT_LINE_HEIGHT = 10 + } } diff --git a/app/src/main/java/dev/navframe/app/MapLibreTftRenderer.kt b/app/src/main/java/dev/navframe/app/MapLibreTftRenderer.kt index b1964d0..34da95c 100644 --- a/app/src/main/java/dev/navframe/app/MapLibreTftRenderer.kt +++ b/app/src/main/java/dev/navframe/app/MapLibreTftRenderer.kt @@ -53,7 +53,7 @@ class MapLibreTftRenderer(context: Context, private val source: MapDataSource, p val position = LatLng(state.position.latitude, state.position.longitude) val camera = TftCameraPolicy().parameters(state) val cameraPosition = CameraPosition.Builder().target(position).zoom(camera.zoom) - .bearing(camera.bearingDegrees.toDouble()).padding(0.0, 70.0, 0.0, 0.0).build() + .bearing(camera.bearingDegrees.toDouble()).padding(0.0, 50.0, 0.0, 0.0).build() val current = snapshotter ?: run { val tile = source.tileSource() val builder = if (tile.uri.startsWith("asset://")) Style.Builder().fromJson(context.assets.open(tile.uri.removePrefix("asset://")).bufferedReader().use { it.readText() }) diff --git a/app/src/test/java/dev/navframe/app/MapFrameComposerTest.kt b/app/src/test/java/dev/navframe/app/MapFrameComposerTest.kt index cc8cff4..335c6b7 100644 --- a/app/src/test/java/dev/navframe/app/MapFrameComposerTest.kt +++ b/app/src/test/java/dev/navframe/app/MapFrameComposerTest.kt @@ -37,6 +37,21 @@ class MapFrameComposerTest { try { assertEquals(480, decoded.width); assertTrue(Color.red(decoded.getPixel(20, 50)) < 25) } finally { decoded.recycle() } } + @Test fun guidedLayoutReservesMoreOfTheTftForTheMap() = runBlocking { + val map = Bitmap.createBitmap(480, 240, Bitmap.Config.ARGB_8888).apply { eraseColor(Color.rgb(44, 60, 76)) } + try { + val route = RouteResult(listOf(GeoPoint(43.36, -5.85), GeoPoint(43.365, -5.845)), emptyList(), 12000, 900) + val state = NavigationState(route = route, nextManeuver = Maneuver("Gira a la derecha", 400, 10, 0, 1, 20), + distanceToNextManeuverMeters = 400, navigationStatus = NavigationStatus.NAVIGATING) + val decoded = decode(MapFrameComposer().compose(map, PointF(240f, 145f), state, NavigationMode.MAP_GPS, ConfiguredMapDataSource.DEFAULT_ATTRIBUTION)) + try { + assertEquals(480, decoded.width); assertEquals(240, decoded.height) + assertTrue("Map should begin immediately below the compact maneuver band", Color.red(decoded.getPixel(10, 56)) > 35) + assertTrue("Map should remain visible almost to the compact footer", Color.red(decoded.getPixel(10, 205)) > 35) + assertTrue("Footer should start below the enlarged map viewport", Color.red(decoded.getPixel(250, 225)) < 25) + } finally { decoded.recycle() } + } finally { map.recycle() } + } @Test fun longAttributionRemainsInsideFrameAndUsesMultipleLines() = runBlocking { val frame = MapFrameComposer().compose(null, null, NavigationState(), NavigationMode.MAP_GPS, "© Example map provider and its contributors · OpenFreeMap · © OpenMapTiles · © OpenStreetMap contributors") val decoded = decode(frame)