voxels: push in-game overlays as routes over the game (#nav)
- Commit
dc3a7f88f375a23743c95e3edee831f7e0438049- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-16 20:07 UTC (19 days ago)
- Parents
4469852
The game swapped its full-screen screens over the Vulkan surface with six booleans (inventoryOpen, furnaceOpen, chestOpen, stonecutterOpen, tradeOpen, settingsOpen, plus invStartTab and furnaceIsBlast), and domain/BackTarget hand-encoded which one Back closed first. Route gains Game(worldId) as the root of an in-game stack and the overlays as destinations over it: Inventory(startTab), Furnace (isBlast), Chest, Stonecutter, Trade and Settings. MainActivity holds a rememberNavBackStack<Route>(Route.Game(worldId)); the engine action-result codes and the HUD buttons push routes instead of setting booleans (re-pushing a screen already up refreshes its arguments instead of duplicating it), and each overlay's onClose removes its own route — closing a chest still tells the engine on every path, via one closeChest() helper. The stack is deliberately state only, not a NavDisplay: the surface and HUD are composed unconditionally and the overlays are drawn in stack order exactly where the old if-blocks sat, so pushing or popping never uncomposes the game entry, never recreates the Vulkan surface and never touches the render loop. Back is one handler: pop the top overlay (chest first closes the container), else toggle the pause menu. domain/BackTarget.kt and its test are deleted — the priority table is the stack order now. Kept as plain screen state, per the audit's C class: the paused pause overlay (Back with nothing up still pauses instead of quitting, and an overlay up while paused still closes first), LoadPhase/LoadingOverlay, and all HUD state. The engine polls now derive overlay presence from the stack (furnace/chest/inventory), and the settings-pauses-the-world effect keys off Route.Settings being on the stack, unchanged in effect. Tests: BackTargetTest is replaced by RouteTest, which covers the game root carrying its worldId, Back popping overlays in stack order, the Inventory/Furnace arguments, and the menu Worlds -> CreateWorld push/pop. The existing Robolectric menu and in-game UI suites (creator flow, back-opens-pause, HUD buttons) pass unchanged. The module AGENTS entry line names the game routes. Verified on devvm21958 (no push): - ./gradlew :games:voxels:compileDevKotlin — green - ./gradlew :games:voxels:testDevUnitTest — green (204 tests) - ./gradlew :games:voxels:lintDev — green
Diff
games/voxels/AGENTS.md
diff --git a/games/voxels/AGENTS.md b/games/voxels/AGENTS.mdindex 42ad1c8849..2ad4b14498 100644--- a/games/voxels/AGENTS.md+++ b/games/voxels/AGENTS.md@@ -6,7 +6,7 @@ _Install: ./install games/voxels (dev by default)._ <!-- last-reviewed: 2026-10-16 --> - Gradle: :games:voxels / dir: games/voxels/ - Package roots present: ui, data, platform, network-- Entry files: MainActivity.kt + platform/MenuActivity.kt (menu/world select) + Route.kt (menu hosts Route.Worlds / Route.CreateWorld)+- Entry files: MainActivity.kt + platform/MenuActivity.kt (menu/world select) + Route.kt (menu: Route.Worlds / CreateWorld; game: Route.Game(worldId) + Inventory/Furnace/Chest/Stonecutter/Trade/Settings overlays) - Deps: :sdk:games, :library:network, :library:e2ee-p2p - Metadata: metadata_data/games-voxels.md (present) - Rust: yes / screenshotTest: hand-committed
games/voxels/src/main/java/com/vayunmathur/games/voxels/MainActivity.kt
diff --git a/games/voxels/src/main/java/com/vayunmathur/games/voxels/MainActivity.kt b/games/voxels/src/main/java/com/vayunmathur/games/voxels/MainActivity.ktindex d34563871b..0a60d89f25 100644--- a/games/voxels/src/main/java/com/vayunmathur/games/voxels/MainActivity.kt+++ b/games/voxels/src/main/java/com/vayunmathur/games/voxels/MainActivity.kt@@ -57,13 +57,11 @@ import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsControllerCompat import com.vayunmathur.games.voxels.data.GameSettingsStore import com.vayunmathur.games.voxels.data.WorldMode-import com.vayunmathur.games.voxels.domain.BackTarget import com.vayunmathur.games.voxels.domain.NetStatus import com.vayunmathur.games.voxels.network.JoinRequests import com.vayunmathur.games.voxels.network.MultiplayerPump import com.vayunmathur.games.voxels.network.VoxelsSync-import com.vayunmathur.games.voxels.domain.OverlayFlags-import com.vayunmathur.games.voxels.domain.backTarget+import com.vayunmathur.library.util.rememberNavBackStack import com.vayunmathur.games.voxels.ui.BossBar import com.vayunmathur.games.voxels.ui.ChestOverlay import com.vayunmathur.games.voxels.ui.DebugOverlay@@ -158,7 +156,6 @@ class MainActivity : ComponentActivity() { var healthJson by remember { mutableStateOf("{}") } // Player settings (gear button). The debug readout is now one of them, off by default. var settings by remember { mutableStateOf(GameSettingsStore.load(this@MainActivity)) }- var settingsOpen by remember { mutableStateOf(false) } LaunchedEffect(settings) { com.vayunmathur.games.voxels.platform.MusicFx.setVolume(settings.musicVolume) com.vayunmathur.games.voxels.platform.SoundFx.volume = settings.sfxVolume@@ -167,22 +164,44 @@ class MainActivity : ComponentActivity() { } var flying by remember { mutableStateOf(false) } var sneaking by remember { mutableStateOf(false) }- var inventoryOpen by remember { mutableStateOf(false) } val activity = LocalActivity.current- var invStartTab by remember { mutableStateOf(0) }+ // The full-screen screens over the world are destinations pushed over the+ // game, not open/closed booleans: Back pops the top one, so the closing order+ // is the stack order. This stack is state only — the surface and HUD below are+ // composed unconditionally and the overlays are drawn over them in stack+ // order, exactly where the old `if (xOpen)` blocks sat, so pushing or popping+ // an overlay never disturbs the Vulkan surface or the render loop.+ val backStack = rememberNavBackStack<Route>(Route.Game(worldId))+ // Overlay presence, derived from the stack for the engine polls below.+ val inventoryOpen = backStack.backStack.any { it is Route.Inventory }+ val furnaceOpen = backStack.backStack.any { it is Route.Furnace }+ val chestOpen = backStack.backStack.any { it is Route.Chest }+ val settingsOpen = backStack.backStack.any { it is Route.Settings }+ // Opening a screen that is already on the stack refreshes its arguments+ // (a second furnace tap re-reads blast-ness) instead of stacking a duplicate.+ fun openOverlay(route: Route) {+ val idx = backStack.backStack.indexOfLast { it::class == route::class }+ if (idx >= 0) backStack.set(idx, route) else backStack.add(route)+ }+ fun closeOverlay(route: Route) {+ if (backStack.backStack.lastOrNull() == route) backStack.pop()+ else if (backStack.backStack.contains(route)) {+ backStack.reset(*backStack.backStack.filterNot { it == route }.toTypedArray())+ }+ }+ // Closing a chest also tells the engine it is closed, on every close path.+ fun closeChest() {+ closeOverlay(Route.Chest)+ try { VoxelsNative.closeContainer() } catch (_: Exception) {}+ } var recipesJson by remember { mutableStateOf("[]") } var blessingsJson by remember { mutableStateOf("""{"slots":[]}""") } var blessingCatalogJson by remember { mutableStateOf("[]") } var smeltingJson by remember { mutableStateOf("[]") } var smeltJson by remember { mutableStateOf("{}") } var containerJson by remember { mutableStateOf("""{"slots":[]}""") }- var furnaceOpen by remember { mutableStateOf(false) }- var furnaceIsBlast by remember { mutableStateOf(false) }- var chestOpen by remember { mutableStateOf(false) } var cutsJson by remember { mutableStateOf("[]") }- var stonecutterOpen by remember { mutableStateOf(false) } var tradesJson by remember { mutableStateOf("{}") }- var tradeOpen by remember { mutableStateOf(false) } // Online multiplayer HUD state. var netStatus by remember { mutableStateOf<NetStatus>(NetStatus.Connecting) } var peersJson by remember { mutableStateOf("[]") }@@ -215,27 +234,26 @@ class MainActivity : ComponentActivity() { if (code == 1) com.vayunmathur.games.voxels.platform.SoundFx.playBreak() } else when (code) { 1 -> com.vayunmathur.games.voxels.platform.SoundFx.playPlace()- 11 -> { invStartTab = 2; inventoryOpen = true } // crafting table+ 11 -> openOverlay(Route.Inventory(startTab = 2)) // crafting table 12, 14 -> { // furnace / blast furnace- furnaceIsBlast = code == 14 smeltJson = try { VoxelsNative.getSmeltJson() } catch (_: Exception) { "{}" }- furnaceOpen = true+ openOverlay(Route.Furnace(isBlast = code == 14)) } 13 -> { // jukebox: play the held disc (or stop) val inv = try { com.vayunmathur.games.voxels.ui.voxelsJson.decodeFromString<com.vayunmathur.games.voxels.ui.InventoryState>(inventoryJson) } catch (_: Exception) { null } val held = inv?.slots?.getOrNull(inv.selected)?.id ?: 0 com.vayunmathur.games.voxels.platform.MusicFx.toggle(this@MainActivity, com.vayunmathur.games.voxels.ui.discTrack[held]) }- 15 -> stonecutterOpen = true // stonecutter+ 15 -> openOverlay(Route.Stonecutter) // stonecutter // Every villager has its own profession and level, so the stall is // read fresh from the one that was tapped. 20 -> { tradesJson = try { VoxelsNative.getTradesJson() } catch (_: Exception) { "{}" }- tradeOpen = true+ openOverlay(Route.Trade) } 30 -> { // chest containerJson = try { VoxelsNative.getContainerJson() } catch (_: Exception) { """{"slots":[]}""" }- chestOpen = true+ openOverlay(Route.Chest) // Online clients mirror host state: ask for the real contents. if (online) try { VoxelsNative.netRequestContainer() } catch (_: Exception) {} com.vayunmathur.games.voxels.platform.SoundFx.playPlace()@@ -385,24 +403,15 @@ class MainActivity : ComponentActivity() { } catch (_: Exception) {} } - // Back closes the top-most overlay or opens the pause menu; it never quits the world.+ // Back pops the top-most overlay, or opens the pause menu; it never quits the+ // world. The pause menu is not a destination (it is this screen's own state),+ // so with no overlay up Back still toggles it: open -> resume, closed -> pause.+ // An overlay up while paused (e.g. after onStop) still closes first, as before. BackHandler {- val open = OverlayFlags(- chest = chestOpen, trade = tradeOpen, furnace = furnaceOpen,- stonecutter = stonecutterOpen, inventory = inventoryOpen, settings = settingsOpen, paused = paused,- )- when (backTarget(open)) {- BackTarget.CHEST -> {- chestOpen = false- try { VoxelsNative.closeContainer() } catch (_: Exception) {}- }- BackTarget.TRADE -> tradeOpen = false- BackTarget.FURNACE -> furnaceOpen = false- BackTarget.STONECUTTER -> stonecutterOpen = false- BackTarget.INVENTORY -> inventoryOpen = false- BackTarget.SETTINGS -> settingsOpen = false- BackTarget.RESUME -> paused = false- BackTarget.PAUSE -> paused = true+ when (val top = backStack.backStack.lastOrNull()) {+ is Route.Chest -> closeChest()+ is Route.Game, null -> if (paused) paused = false else paused = true+ else -> closeOverlay(top) } } @@ -545,7 +554,7 @@ class MainActivity : ComponentActivity() { Box(Modifier.align(Alignment.BottomCenter).padding(bottom = 12.dp)) { Hotbar(inventoryJson = inventoryJson, onSelect = { slot -> if (VoxelsNative.isAvailable) try { VoxelsNative.selectSlot(slot) } catch (_: Exception) {} },- onOpenInventory = { invStartTab = 0; inventoryOpen = true })+ onOpenInventory = { openOverlay(Route.Inventory(startTab = 0)) }) } Row(Modifier.align(Alignment.TopCenter).fillMaxWidth().padding(top = 36.dp, start = 12.dp, end = 12.dp),@@ -555,7 +564,7 @@ class MainActivity : ComponentActivity() { // Dark disc + white glyph so the buttons read over bright snow or sky too. val hudButton = Modifier.background(Color.Black.copy(alpha = 0.38f), CircleShape) IconButton(onClick = { paused = true }, modifier = hudButton) { IconMenu(tint = Color.White) }- IconButton(onClick = { settingsOpen = true }, modifier = hudButton) { IconSettings(tint = Color.White) }+ IconButton(onClick = { openOverlay(Route.Settings) }, modifier = hudButton) { IconSettings(tint = Color.White) } } // Debug readout under the buttons (left), clear of the online chips (right). if (settings.showDebug) { DebugOverlay(debugJson = debugJson, modifier = Modifier.widthIn(max = 280.dp)) }@@ -600,40 +609,40 @@ class MainActivity : ComponentActivity() { } } - if (inventoryOpen && VoxelsNative.isAvailable) {- InventoryOverlay(- inventoryJson = inventoryJson, recipesJson = recipesJson,- blessingsJson = blessingsJson, blessingCatalogJson = blessingCatalogJson,- onClose = { inventoryOpen = false }, startTab = invStartTab, creative = creative,- worldAchievements = achievements?.world,- )- }-- if (tradeOpen && VoxelsNative.isAvailable) {- TradeOverlay(tradesJson = tradesJson, onClose = { tradeOpen = false })- }-- if (furnaceOpen && VoxelsNative.isAvailable) {- FurnaceOverlay(- smeltingJson = smeltingJson, smeltJson = smeltJson,- isBlast = furnaceIsBlast, onClose = { furnaceOpen = false }- )- }-- if (chestOpen && VoxelsNative.isAvailable) {- ChestOverlay(containerJson = containerJson, inventoryJson = inventoryJson, onClose = {- chestOpen = false- try { VoxelsNative.closeContainer() } catch (_: Exception) {}- })- }-- if (stonecutterOpen && VoxelsNative.isAvailable) {- StonecutterOverlay(cutsJson = cutsJson, inventoryJson = inventoryJson,- onClose = { stonecutterOpen = false })- }-- if (settingsOpen) {- SettingsSheet(settings = settings, onChange = { settings = it.sanitized() }, onClose = { settingsOpen = false })+ // Overlays in stack order over the surface: the last entry draws on top,+ // as the stacked `if (xOpen)` blocks did. Settings has no engine gate.+ for (route in backStack.backStack.drop(1)) {+ when (route) {+ is Route.Inventory -> if (VoxelsNative.isAvailable) {+ InventoryOverlay(+ inventoryJson = inventoryJson, recipesJson = recipesJson,+ blessingsJson = blessingsJson, blessingCatalogJson = blessingCatalogJson,+ onClose = { closeOverlay(route) }, startTab = route.startTab, creative = creative,+ worldAchievements = achievements?.world,+ )+ }+ is Route.Trade -> if (VoxelsNative.isAvailable) {+ TradeOverlay(tradesJson = tradesJson, onClose = { closeOverlay(route) })+ }+ is Route.Furnace -> if (VoxelsNative.isAvailable) {+ FurnaceOverlay(+ smeltingJson = smeltingJson, smeltJson = smeltJson,+ isBlast = route.isBlast, onClose = { closeOverlay(route) }+ )+ }+ is Route.Chest -> if (VoxelsNative.isAvailable) {+ ChestOverlay(containerJson = containerJson, inventoryJson = inventoryJson,+ onClose = { closeChest() })+ }+ is Route.Stonecutter -> if (VoxelsNative.isAvailable) {+ StonecutterOverlay(cutsJson = cutsJson, inventoryJson = inventoryJson,+ onClose = { closeOverlay(route) })+ }+ is Route.Settings -> SettingsSheet(+ settings = settings, onChange = { settings = it.sanitized() },+ onClose = { closeOverlay(route) })+ else -> Unit+ } } if (loadPhase != LoadPhase.READY && VoxelsNative.isAvailable) {
games/voxels/src/main/java/com/vayunmathur/games/voxels/Route.kt
diff --git a/games/voxels/src/main/java/com/vayunmathur/games/voxels/Route.kt b/games/voxels/src/main/java/com/vayunmathur/games/voxels/Route.ktindex 53d98940d8..46d38f82d5 100644--- a/games/voxels/src/main/java/com/vayunmathur/games/voxels/Route.kt+++ b/games/voxels/src/main/java/com/vayunmathur/games/voxels/Route.kt@@ -8,9 +8,16 @@ import kotlinx.serialization.Serializable * * The menu ([com.vayunmathur.games.voxels.platform.MenuActivity]) hosts * [Worlds] and [CreateWorld] on a back stack; playing a world still- * starts the game Activity by Intent, so no game destination lives here- * yet — the in-game overlays join this type with the game commit.- * Routes carry ids and small arguments, never whole objects.+ * starts the game Activity by Intent.+ *+ * Inside the game, [Game] is the root of a second stack and the+ * full-screen screens that used to be six `*Open` booleans are overlay+ * destinations pushed over it: [Inventory], [Furnace], [Chest],+ * [Stonecutter], [Trade] and [Settings]. Back pops the top overlay, so+ * the old hand-encoded back priority is now just stack order. The+ * pause menu, the loading phase and the HUD are not destinations —+ * they stay plain screen state inside the game. Routes carry ids and+ * small arguments, never whole objects. */ @Serializable sealed interface Route : NavKey {@@ -21,4 +28,32 @@ sealed interface Route : NavKey { /** The world creator, pushed over [Worlds]; Back returns to the list. */ @Serializable data object CreateWorld : Route++ /** The live world (Vulkan surface + HUD): the game's root destination. */+ @Serializable+ data class Game(val worldId: String) : Route++ /** Inventory / crafting overlay; [startTab] selects its opening tab. */+ @Serializable+ data class Inventory(val startTab: Int = 0) : Route++ /** Furnace overlay; [isBlast] unlocks the blast-furnace alloy recipes. */+ @Serializable+ data class Furnace(val isBlast: Boolean) : Route++ /** Chest / container overlay over the world. */+ @Serializable+ data object Chest : Route++ /** Stonecutter overlay over the world. */+ @Serializable+ data object Stonecutter : Route++ /** Villager trade overlay over the world. */+ @Serializable+ data object Trade : Route++ /** In-game settings sheet over the world; the world holds still while it is up. */+ @Serializable+ data object Settings : Route }
games/voxels/src/main/java/com/vayunmathur/games/voxels/domain/BackTarget.kt
diff --git a/games/voxels/src/main/java/com/vayunmathur/games/voxels/domain/BackTarget.kt b/games/voxels/src/main/java/com/vayunmathur/games/voxels/domain/BackTarget.ktdeleted file mode 100644index f3622b8643..0000000000--- a/games/voxels/src/main/java/com/vayunmathur/games/voxels/domain/BackTarget.kt+++ /dev/null@@ -1,48 +0,0 @@-package com.vayunmathur.games.voxels.domain--/** The in-game layer the system Back gesture acts on. */-enum class BackTarget {- /** Close the open chest (and tell the engine it is closed). */- CHEST,- /** Close the villager trade screen. */- TRADE,- /** Close the furnace screen. */- FURNACE,- /** Close the stonecutter screen. */- STONECUTTER,- /** Close the inventory / crafting screen. */- INVENTORY,- /** Close the settings sheet. */- SETTINGS,- /** Leave the pause menu and resume. */- RESUME,- /** Nothing is open: open the pause menu instead of quitting the world. */- PAUSE,-}--/** Which overlays are currently showing over the world. */-data class OverlayFlags(- val chest: Boolean = false,- val trade: Boolean = false,- val furnace: Boolean = false,- val stonecutter: Boolean = false,- val inventory: Boolean = false,- val settings: Boolean = false,- val paused: Boolean = false,-)--/**- * Back closes the top-most overlay; with none open it pauses. It never finishes the activity:- * leaving the world is the pause menu's explicit "Save & Quit", so a stray edge swipe while- * looking around can no longer drop the player out of their world.- */-fun backTarget(open: OverlayFlags): BackTarget = when {- open.settings -> BackTarget.SETTINGS- open.chest -> BackTarget.CHEST- open.trade -> BackTarget.TRADE- open.furnace -> BackTarget.FURNACE- open.stonecutter -> BackTarget.STONECUTTER- open.inventory -> BackTarget.INVENTORY- open.paused -> BackTarget.RESUME- else -> BackTarget.PAUSE-}
games/voxels/src/test/java/com/vayunmathur/games/voxels/RouteTest.kt
diff --git a/games/voxels/src/test/java/com/vayunmathur/games/voxels/RouteTest.kt b/games/voxels/src/test/java/com/vayunmathur/games/voxels/RouteTest.ktnew file mode 100644index 0000000000..7edc70672b--- /dev/null+++ b/games/voxels/src/test/java/com/vayunmathur/games/voxels/RouteTest.kt@@ -0,0 +1,47 @@+package com.vayunmathur.games.voxels++import com.vayunmathur.library.util.NavBackStack+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertIs+import kotlin.test.assertTrue++class RouteTest {+ @Test fun `game stack starts on the world it was opened for`() {+ val stack = NavBackStack<Route>(arrayOf(Route.Game("world-7")))+ assertEquals(Route.Game("world-7"), stack.last())+ assertEquals("world-7", (stack.last() as Route.Game).worldId)+ }++ @Test fun `back pops overlays in stack order, top first`() {+ // The old hand-encoded priority (settings over chest over inventory) is now+ // simply the order the screens were pushed: Back takes the top one first.+ val stack = NavBackStack<Route>(arrayOf(Route.Game("w")))+ stack.add(Route.Inventory(startTab = 2))+ stack.add(Route.Chest)+ stack.add(Route.Settings)+ assertEquals(Route.Settings, stack.last())+ stack.pop()+ assertEquals(Route.Chest, stack.last())+ stack.pop()+ val inventory = stack.last()+ assertIs<Route.Inventory>(inventory)+ assertEquals(2, inventory.startTab)+ stack.pop()+ assertEquals(Route.Game("w"), stack.last())+ }++ @Test fun `overlay routes carry their opening arguments`() {+ assertTrue(Route.Furnace(isBlast = true).isBlast)+ assertEquals(0, Route.Inventory().startTab)+ assertEquals(3, Route.Inventory(startTab = 3).startTab)+ }++ @Test fun `menu creator pushes over the world list and pops back`() {+ val stack = NavBackStack<Route>(arrayOf(Route.Worlds))+ stack.add(Route.CreateWorld)+ assertEquals(Route.CreateWorld, stack.last())+ stack.pop()+ assertEquals(Route.Worlds, stack.last())+ }+}
games/voxels/src/test/java/com/vayunmathur/games/voxels/domain/BackTargetTest.kt
diff --git a/games/voxels/src/test/java/com/vayunmathur/games/voxels/domain/BackTargetTest.kt b/games/voxels/src/test/java/com/vayunmathur/games/voxels/domain/BackTargetTest.ktdeleted file mode 100644index f0096ae66f..0000000000--- a/games/voxels/src/test/java/com/vayunmathur/games/voxels/domain/BackTargetTest.kt+++ /dev/null@@ -1,23 +0,0 @@-package com.vayunmathur.games.voxels.domain--import kotlin.test.Test-import kotlin.test.assertEquals--class BackTargetTest {- @Test fun `back with nothing open pauses instead of quitting`() {- assertEquals(BackTarget.PAUSE, backTarget(OverlayFlags()))- }-- @Test fun `back from the pause menu resumes`() {- assertEquals(BackTarget.RESUME, backTarget(OverlayFlags(paused = true)))- }-- @Test fun `back closes the top-most overlay first`() {- assertEquals(BackTarget.CHEST, backTarget(OverlayFlags(chest = true, inventory = true, paused = true)))- assertEquals(BackTarget.INVENTORY, backTarget(OverlayFlags(inventory = true, paused = true)))- assertEquals(BackTarget.SETTINGS, backTarget(OverlayFlags(settings = true, chest = true)))- assertEquals(BackTarget.TRADE, backTarget(OverlayFlags(trade = true)))- assertEquals(BackTarget.FURNACE, backTarget(OverlayFlags(furnace = true)))- assertEquals(BackTarget.STONECUTTER, backTarget(OverlayFlags(stonecutter = true)))- }-}