diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index 74b071efb..da0cf34a0 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -112,6 +112,12 @@ object SessionPrefs { prefs(context).edit().putString("statsStrip", id).apply() } + fun statsSpeedUnit(context: Context): String? = prefs(context).getString("statsSpeedUnit", null) + + fun setStatsSpeedUnit(context: Context, id: String) { + prefs(context).edit().putString("statsSpeedUnit", id).apply() + } + fun statsDash(context: Context): String? = prefs(context).getString("statsDash", null) fun setStatsDash(context: Context, id: String) { diff --git a/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt b/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt index 1756c631a..bce07bba5 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SettingsReset.kt @@ -26,7 +26,7 @@ object SettingsReset { "upscaler", "upscaleSharpness", "screenEffects", "textureAnisotropy", "textureLodBias", "steamDownloadsInBackground", "steam_offline", "bootMovie", "bootMovieSound", "bootMovieVolume", "bootMovieSeconds", "bootMovieApplyNext", "bootMovieSteamSeen", - "questDefaultsV1", "telemetryAuto", "telemetryOff", "statsStrip", "statsDash", + "questDefaultsV1", "telemetryAuto", "telemetryOff", "statsStrip", "statsDash", "statsSpeedUnit", ) internal val SESSION_PREFIXES = listOf( diff --git a/app/src/main/java/com/droiddeck/launcher/session/StatsLayout.kt b/app/src/main/java/com/droiddeck/launcher/session/StatsLayout.kt index d022cc139..d0ff1486a 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/StatsLayout.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/StatsLayout.kt @@ -11,6 +11,23 @@ enum class StatsStrip(val id: String) { } } +/** Speeds on the racing dashes; telemetry arrives in km/h. */ +enum class SpeedUnit(val id: String, val label: String, val perKmh: Float) { + AUTO("auto", "", 1f), KMH("kmh", "km/h", 1f), MPH("mph", "mph", 0.621371f); + + /** Auto is mph where road speed limits are signed in mph, by the device's region, else km/h. */ + fun resolve(country: String): SpeedUnit = if (this != AUTO) this else if (country.uppercase() in MPH_REGIONS) MPH else KMH + + fun next(): SpeedUnit = entries[(ordinal + 1) % entries.size] + + companion object { + /** The United States and its territories, the United Kingdom and its Crown Dependencies, Liberia, the Bahamas, Belize. */ + private val MPH_REGIONS = setOf("US", "PR", "GU", "VI", "AS", "MP", "GB", "IM", "JE", "GG", "LR", "BS", "BZ") + + fun from(id: String?): SpeedUnit = entries.firstOrNull { it.id == id } ?: AUTO + } +} + /** The racing dash under the strip; Auto picks one by the game's data format. */ enum class StatsDash(val id: String) { AUTO("auto"), SPEEDOMETER("speedometer"), CIRCUIT("circuit"), TRACE("trace"), DIALS("dials"), GLANCE("glance"); diff --git a/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt index 19e857162..4d179d07e 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/BottomTelemetryPage.kt @@ -1,5 +1,6 @@ package com.droiddeck.launcher.ui +import android.content.res.Resources import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box @@ -12,6 +13,7 @@ import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.width import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.clickable import androidx.compose.foundation.selection.toggleable import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.verticalScroll @@ -47,6 +49,7 @@ import com.droiddeck.launcher.session.GameStatusFiles import com.droiddeck.launcher.session.GameTelemetry import com.droiddeck.launcher.session.SessionPrefs import com.droiddeck.launcher.session.SessionState +import com.droiddeck.launcher.session.SpeedUnit import com.droiddeck.launcher.session.StatsDash import com.droiddeck.launcher.session.StatsStrip import com.droiddeck.launcher.session.Telemetry @@ -107,6 +110,8 @@ fun BottomTelemetryPage(fps: () -> Float?, runningGame: () -> String? = { null } var unknown by remember { mutableStateOf>(emptyList()) } var strip by remember { mutableStateOf(StatsStrip.from(SessionPrefs.statsStrip(context))) } var dash by remember { mutableStateOf(StatsDash.from(SessionPrefs.statsDash(context))) } + var speedUnit by remember { mutableStateOf(SpeedUnit.from(SessionPrefs.statsSpeedUnit(context))) } + val shownUnit = speedUnit.resolve(Resources.getSystem().configuration.locales[0].country) val fpsHistory = remember { History(PERF_N) } val cpuHistory = remember { History(PERF_N) } val gpuHistory = remember { History(PERF_N) } @@ -229,7 +234,7 @@ fun BottomTelemetryPage(fps: () -> Float?, runningGame: () -> String? = { null } race?.let { r -> val shown = dash.resolve(r.source) traceTick.let { - RaceDashView(shown, r, TraceData(throttleTrace.snapshot(), brakeTrace.snapshot(), speedTrace.snapshot())) { + RaceDashView(shown, r, TraceData(throttleTrace.snapshot(), brakeTrace.snapshot(), speedTrace.snapshot()), shownUnit) { dash = StatsDash.tapped(shown) SessionPrefs.setStatsDash(context, dash.id) } @@ -299,6 +304,11 @@ fun BottomTelemetryPage(fps: () -> Float?, runningGame: () -> String? = { null } Text(stringResource(R.string.bottom_telemetry_port_busy, busyPorts.sorted().joinToString()), fontSize = 13.sp, color = colors.error, modifier = Modifier.padding(horizontal = 4.dp)) } + ChoiceRow(stringResource(R.string.stats_speed_units), + if (speedUnit == SpeedUnit.AUTO) stringResource(R.string.stats_speed_units_auto, shownUnit.label) else speedUnit.label) { + speedUnit = speedUnit.next() + SessionPrefs.setStatsSpeedUnit(context, speedUnit.id) + } Text(stringResource(R.string.stats_tap_hint), fontSize = 12.sp, color = colors.onSurfaceVariant, modifier = Modifier.padding(horizontal = 4.dp)) } } @@ -315,6 +325,16 @@ private fun httpGet(url: String): String { } +@Composable +private fun ChoiceRow(title: String, value: String, onClick: () -> Unit) { + val colors = MaterialTheme.colorScheme + Row(Modifier.fillMaxWidth().clickable(role = Role.Button, onClick = onClick).padding(horizontal = 4.dp, vertical = 6.dp), + verticalAlignment = Alignment.CenterVertically) { + Text(title, fontSize = 15.sp, color = colors.onBackground, modifier = Modifier.weight(1f)) + Text(value, fontSize = 15.sp, fontWeight = FontWeight.Bold, color = LocalPalette.current.primary) + } +} + @Composable private fun SwitchRow(title: String, hint: String?, checked: Boolean, enabled: Boolean, onChange: (Boolean) -> Unit) { val colors = MaterialTheme.colorScheme diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StatsDashes.kt b/app/src/main/java/com/droiddeck/launcher/ui/StatsDashes.kt index 651ae759b..9f185b824 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/StatsDashes.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/StatsDashes.kt @@ -46,6 +46,7 @@ import androidx.compose.ui.unit.sp import com.droiddeck.launcher.R import com.droiddeck.launcher.session.GameStatusFiles import com.droiddeck.launcher.session.GameTelemetry +import com.droiddeck.launcher.session.SpeedUnit import com.droiddeck.launcher.session.StatsDash import com.droiddeck.launcher.session.Telemetry import kotlin.math.abs @@ -62,16 +63,16 @@ internal const val TRACE_N = 150 internal class TraceData(val throttle: List, val brake: List, val speed: List) -/** The racing dash in [style]; a tap moves to the next one. */ +/** The racing dash in [style] with speeds in [unit] (km/h or mph); a tap moves to the next one. */ @Composable -internal fun RaceDashView(style: StatsDash, t: Telemetry, trace: TraceData, onTap: () -> Unit) { +internal fun RaceDashView(style: StatsDash, t: Telemetry, trace: TraceData, unit: SpeedUnit, onTap: () -> Unit) { Box(Modifier.fillMaxWidth().clickable(onClick = onTap)) { when (style) { - StatsDash.AUTO, StatsDash.SPEEDOMETER -> SpeedometerDash(t) - StatsDash.CIRCUIT -> CircuitDash(t) - StatsDash.TRACE -> TraceDash(t, trace) - StatsDash.DIALS -> DialsDash(t) - StatsDash.GLANCE -> GlanceDash(t) + StatsDash.AUTO, StatsDash.SPEEDOMETER -> SpeedometerDash(t, unit) + StatsDash.CIRCUIT -> CircuitDash(t, unit) + StatsDash.TRACE -> TraceDash(t, trace, unit) + StatsDash.DIALS -> DialsDash(t, unit) + StatsDash.GLANCE -> GlanceDash(t, unit) } } } @@ -117,23 +118,29 @@ private fun DrawScope.needle(cx: Float, cy: Float, len: Float, deg: Float, color drawCircle(hub, radius = 7.dp.toPx(), center = Offset(cx, cy)) } -private fun speedScale(speed: Float?): Float = if ((speed ?: 0f) > 210f) 320f else 220f +private fun speedValue(t: Telemetry, unit: SpeedUnit): Float = (t.speedKmh ?: 0f) * unit.perKmh + +private fun speedScale(speed: Float, unit: SpeedUnit): Float = + if (unit == SpeedUnit.MPH) (if (speed > 130f) 200f else 140f) else (if (speed > 210f) 320f else 220f) private fun rpmScale(t: Telemetry): Float = max(t.maxRpm ?: 9000f, t.rpm ?: 0f) -private fun kmh(t: Telemetry) = t.speedKmh?.let { "%.0f".format(it) } ?: "–" +private fun speedText(t: Telemetry, unit: SpeedUnit) = t.speedKmh?.let { "%.0f".format(it * unit.perKmh) } ?: "–" @Composable -private fun SpeedometerDash(t: Telemetry) { +private fun unitAndGear(t: Telemetry, unit: SpeedUnit) = "${unit.label} · ${stringResource(R.string.stats_gear)} ${t.gear ?: "–"}" + +@Composable +private fun SpeedometerDash(t: Telemetry, unit: SpeedUnit) { val colors = MaterialTheme.colorScheme val accent = LocalPalette.current.primary Row(Modifier.fillMaxWidth().height(DASH_H), horizontalArrangement = Arrangement.spacedBy(8.dp)) { StatsCard(stringResource(R.string.stats_speed), Modifier.weight(1.55f).fillMaxHeight()) { Box(Modifier.fillMaxWidth().weight(1f)) { - SpeedDial(t, Modifier.fillMaxSize()) + SpeedDial(t, unit, Modifier.fillMaxSize()) Column(Modifier.align(Alignment.BottomCenter), horizontalAlignment = Alignment.CenterHorizontally) { - Text(t.gear ?: "–", fontSize = 44.sp, fontWeight = FontWeight.Bold, fontFamily = FontFamily.Monospace, color = accent) - Text("${kmh(t)} km/h", fontSize = 16.sp, fontWeight = FontWeight.Bold, fontFamily = FontFamily.Monospace, color = colors.onBackground) + Text(speedText(t, unit), fontSize = 44.sp, fontWeight = FontWeight.Bold, fontFamily = FontFamily.Monospace, color = accent) + Text(unitAndGear(t, unit), fontSize = 16.sp, fontWeight = FontWeight.Bold, fontFamily = FontFamily.Monospace, color = colors.onBackground) Text(t.rpm?.let { "%,.0f rpm".format(it) } ?: "", fontSize = 11.sp, fontFamily = FontFamily.Monospace, color = colors.onSurfaceVariant) } } @@ -148,12 +155,13 @@ private fun SpeedometerDash(t: Telemetry) { } @Composable -private fun SpeedDial(t: Telemetry, modifier: Modifier) { +private fun SpeedDial(t: Telemetry, unit: SpeedUnit, modifier: Modifier) { val pal = LocalPalette.current val colors = MaterialTheme.colorScheme val measurer = rememberTextMeasurer() val labelStyle = TextStyle(color = colors.onSurfaceVariant, fontSize = 11.sp, fontFamily = FontFamily.Monospace) - val vmax = speedScale(t.speedKmh) + val speed = speedValue(t, unit) + val vmax = speedScale(speed, unit) val rpmFraction = ((t.rpm ?: 0f) / rpmScale(t)).coerceIn(0f, 1f) Canvas(modifier) { val r = min(size.width * 0.42f, size.height * 0.47f) @@ -166,7 +174,7 @@ private fun SpeedDial(t: Telemetry, modifier: Modifier) { drawArc(pal.line, DIAL_START - 90f, DIAL_SWEEP, false, topLeft = tl, size = box, style = Stroke(5.dp.toPx(), cap = StrokeCap.Round)) if (rpmFraction > 0f) drawArc(if (rpmFraction > 0.9f) StatsColors.brake else pal.primary, DIAL_START - 90f, DIAL_SWEEP * rpmFraction, false, topLeft = tl, size = box, style = Stroke(5.dp.toPx(), cap = StrokeCap.Round)) - needle(cx, cy, r - 16.dp.toPx(), dialAngle(t.speedKmh ?: 0f, vmax), colors.onBackground, pal.primary) + needle(cx, cy, r - 16.dp.toPx(), dialAngle(speed, vmax), colors.onBackground, pal.primary) } } @@ -219,7 +227,7 @@ private fun GForce(t: Telemetry, modifier: Modifier) { } @Composable -private fun CircuitDash(t: Telemetry) { +private fun CircuitDash(t: Telemetry, unit: SpeedUnit) { val colors = MaterialTheme.colorScheme val accent = LocalPalette.current.primary Column(Modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(8.dp)) { @@ -234,8 +242,8 @@ private fun CircuitDash(t: Telemetry) { } StatsCard("", Modifier.weight(1.2f).fillMaxHeight()) { Column(Modifier.fillMaxWidth().weight(1f), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) { - Text(t.gear ?: "–", fontSize = 72.sp, fontWeight = FontWeight.Bold, fontFamily = FontFamily.Monospace, color = accent) - Text("${kmh(t)} km/h", fontSize = 24.sp, fontWeight = FontWeight.Bold, fontFamily = FontFamily.Monospace, color = colors.onBackground) + Text(speedText(t, unit), fontSize = 72.sp, fontWeight = FontWeight.Bold, fontFamily = FontFamily.Monospace, color = accent) + Text(unitAndGear(t, unit), fontSize = 24.sp, fontWeight = FontWeight.Bold, fontFamily = FontFamily.Monospace, color = colors.onBackground) Text(t.rpm?.let { rpm -> t.maxRpm?.let { "%,.0f / %,.0f rpm".format(rpm, it) } ?: "%,.0f rpm".format(rpm) } ?: "", fontSize = 12.sp, fontFamily = FontFamily.Monospace, color = colors.onSurfaceVariant) } @@ -318,7 +326,7 @@ private fun TireBox(c: Float, modifier: Modifier) { } @Composable -private fun TraceDash(t: Telemetry, trace: TraceData) { +private fun TraceDash(t: Telemetry, trace: TraceData, unit: SpeedUnit) { val colors = MaterialTheme.colorScheme Column(Modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(8.dp)) { StatsCard(stringResource(R.string.stats_trace), Modifier.fillMaxWidth().height(168.dp)) { @@ -328,13 +336,14 @@ private fun TraceDash(t: Telemetry, trace: TraceData) { } TraceChart(listOf(trace.throttle to StatsColors.throttle, trace.brake to StatsColors.brake), 1f, Modifier.fillMaxWidth().weight(1f)) } - val top = max(200f, (trace.speed.maxOrNull() ?: 0f) * 1.1f) - StatsCard(stringResource(R.string.stats_speed_chart), Modifier.fillMaxWidth().height(114.dp)) { + val speeds = trace.speed.map { it * unit.perKmh } + val top = max(if (unit == SpeedUnit.MPH) 125f else 200f, (speeds.maxOrNull() ?: 0f) * 1.1f) + StatsCard(stringResource(R.string.stats_speed_chart, unit.label), Modifier.fillMaxWidth().height(114.dp)) { Row(horizontalArrangement = Arrangement.spacedBy(14.dp)) { - Legend(StatsColors.speed, "${stringResource(R.string.stats_speed)} ${kmh(t)}") + Legend(StatsColors.speed, "${stringResource(R.string.stats_speed)} ${speedText(t, unit)}") Text("0 – %.0f".format(top), fontSize = 11.sp, fontFamily = FontFamily.Monospace, color = colors.onSurfaceVariant) } - TraceChart(listOf(trace.speed to StatsColors.speed), top, Modifier.fillMaxWidth().weight(1f)) + TraceChart(listOf(speeds to StatsColors.speed), top, Modifier.fillMaxWidth().weight(1f)) } } } @@ -370,14 +379,16 @@ private fun TraceChart(series: List, Color>>, max: Float, modif } @Composable -private fun DialsDash(t: Telemetry) { +private fun DialsDash(t: Telemetry, unit: SpeedUnit) { val colors = MaterialTheme.colorScheme val rpmMax = (rpmScale(t) / 1000f).let { kotlin.math.ceil(it) * 1000f } val hasExtras = t.lights != null || t.turboBar != null || t.engineTempC != null || t.oilTempC != null Column(Modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(8.dp)) { Row(Modifier.fillMaxWidth().height(if (hasExtras) DASH_H - 56.dp else DASH_H), horizontalArrangement = Arrangement.spacedBy(8.dp)) { StatsCard("", Modifier.weight(1f).fillMaxHeight()) { - ArcDial(t.speedKmh ?: 0f, speedScale(t.speedKmh), 40f, { "%.0f".format(it) }, null, kmh(t), "km/h", Modifier.fillMaxWidth().weight(1f)) + val speed = speedValue(t, unit) + ArcDial(speed, speedScale(speed, unit), if (unit == SpeedUnit.MPH) 20f else 40f, { "%.0f".format(it) }, null, + speedText(t, unit), unit.label, Modifier.fillMaxWidth().weight(1f)) } StatsCard("", Modifier.weight(1f).fillMaxHeight()) { ArcDial(t.rpm ?: 0f, rpmMax, 1000f, { "%.0f".format(it / 1000f) }, t.maxRpm?.let { it * 0.9f }, @@ -433,7 +444,7 @@ private fun Lamp(label: String, on: Boolean, color: Color) { } @Composable -private fun GlanceDash(t: Telemetry) { +private fun GlanceDash(t: Telemetry, unit: SpeedUnit) { val colors = MaterialTheme.colorScheme val accent = LocalPalette.current.primary val ratio = ((t.rpm ?: 0f) / rpmScale(t)).coerceIn(0f, 1f) @@ -442,8 +453,8 @@ private fun GlanceDash(t: Telemetry) { StatsCard("", Modifier.weight(1f).fillMaxHeight()) { Bar(ratio, if (ratio > 0.9f) StatsColors.brake else accent, Modifier.fillMaxWidth().height(14.dp)) Column(Modifier.fillMaxWidth().weight(1f), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) { - Text(t.gear ?: "–", fontSize = 110.sp, fontWeight = FontWeight.Bold, fontFamily = FontFamily.Monospace, color = accent, lineHeight = 112.sp) - Text("${kmh(t)} km/h", fontSize = 34.sp, fontWeight = FontWeight.Bold, fontFamily = FontFamily.Monospace, color = colors.onBackground) + Text(speedText(t, unit), fontSize = 110.sp, fontWeight = FontWeight.Bold, fontFamily = FontFamily.Monospace, color = accent, lineHeight = 112.sp) + Text(unitAndGear(t, unit), fontSize = 34.sp, fontWeight = FontWeight.Bold, fontFamily = FontFamily.Monospace, color = colors.onBackground) } } EdgeBar(t.brake, StatsColors.brake) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a38edb81d..9fcf9d129 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1899,7 +1899,10 @@ Best lap Tires °C Throttle and brake, last 10 s - Speed, km/h + Speed, %1$s + Gear + Speed units + Automatic (%1$s) Throttle Brake Dashboard diff --git a/app/src/test/java/com/droiddeck/launcher/session/StatsLayoutTest.kt b/app/src/test/java/com/droiddeck/launcher/session/StatsLayoutTest.kt index fa50f1472..636e7e439 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/StatsLayoutTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/StatsLayoutTest.kt @@ -29,4 +29,23 @@ class StatsLayoutTest { assertEquals(StatsDash.SPEEDOMETER, StatsDash.tapped(StatsDash.GLANCE)) assertEquals(StatsDash.SPEEDOMETER, StatsDash.tapped(StatsDash.AUTO)) } + + @Test fun automaticSpeedUnitsFollowTheRegion() { + assertEquals(SpeedUnit.MPH, SpeedUnit.AUTO.resolve("US")) + assertEquals(SpeedUnit.MPH, SpeedUnit.AUTO.resolve("gb")) + assertEquals(SpeedUnit.KMH, SpeedUnit.AUTO.resolve("DE")) + assertEquals(SpeedUnit.KMH, SpeedUnit.AUTO.resolve("")) + assertEquals(SpeedUnit.KMH, SpeedUnit.KMH.resolve("US")) + assertEquals(SpeedUnit.MPH, SpeedUnit.MPH.resolve("DE")) + } + + @Test fun speedUnitsCycleAndConvert() { + assertEquals(SpeedUnit.AUTO, SpeedUnit.from(null)) + assertEquals(SpeedUnit.MPH, SpeedUnit.from("mph")) + assertEquals(SpeedUnit.KMH, SpeedUnit.AUTO.next()) + assertEquals(SpeedUnit.MPH, SpeedUnit.KMH.next()) + assertEquals(SpeedUnit.AUTO, SpeedUnit.MPH.next()) + assertEquals(62.1f, 100f * SpeedUnit.MPH.perKmh, 0.05f) + assertEquals(100f, 100f * SpeedUnit.KMH.perKmh, 0f) + } }