voxels: group the crafting menu into shelves so 76 recipes stay navigable
- Commit
bb7954365260c231f22389f9fef721fbde8c12ab- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-05 15:13 UTC (4 months ago)
- Parents
220125c
Diff
games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/InventoryScreen.kt
diff --git a/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/InventoryScreen.kt b/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/InventoryScreen.ktindex 6408fe6a16..da29d9ba08 100644--- a/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/InventoryScreen.kt+++ b/games/voxels/src/main/java/com/vayunmathur/games/voxels/ui/InventoryScreen.kt@@ -8,6 +8,7 @@ import androidx.compose.foundation.border import androidx.compose.foundation.clickable import androidx.compose.foundation.gestures.detectDragGestures import androidx.compose.foundation.gestures.detectTapGestures+import androidx.compose.foundation.horizontalScroll import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.layout.*@@ -224,26 +225,37 @@ private fun CraftingTable(recipesJson: String) { val arr = org.json.JSONArray(recipesJson) (0 until arr.length()).map { i -> val o = arr.getJSONObject(i)- Recipe(o.getInt("in"), o.getInt("inN"), o.optInt("in2", 0), o.optInt("in2N", 0), o.getInt("out"), o.getInt("outN"))+ Recipe(o.getInt("in"), o.getInt("inN"), o.optInt("in2", 0), o.optInt("in2N", 0), o.getInt("out"), o.getInt("outN"), o.optString("cat", "block")) } } catch (_: Exception) { emptyList() } } var sel by remember { mutableStateOf(0) }+ var cat by remember { mutableStateOf("block") } val r = recipes.getOrNull(sel) Row(Modifier.fillMaxSize(), horizontalArrangement = Arrangement.spacedBy(16.dp)) {- // Left: product picker.- Column(Modifier.width(160.dp).fillMaxHeight().verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(4.dp)) {- Text(stringResource(R.string.products), color = Color.White.copy(0.6f), fontSize = 12.sp)- recipes.forEachIndexed { i, rec ->- val icon = rememberBlockIcon(rec.outId)- Row(- Modifier.fillMaxWidth().clip(RoundedCornerShape(6.dp))- .background(if (i == sel) Color(0xFF3A6B3A) else Color.White.copy(alpha = 0.08f))- .clickable { sel = i }.padding(6.dp),- verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)- ) {- if (icon != null) Image(bitmap = icon, contentDescription = null, modifier = Modifier.size(26.dp), filterQuality = FilterQuality.None)- Text(blockNames[rec.outId] ?: "", color = Color.White.copy(0.95f), fontSize = 12.sp)+ // Left: product picker, filtered to one shelf so the list stays navigable.+ Column(Modifier.width(190.dp).fillMaxHeight(), verticalArrangement = Arrangement.spacedBy(4.dp)) {+ Row(Modifier.fillMaxWidth().horizontalScroll(rememberScrollState()), horizontalArrangement = Arrangement.spacedBy(3.dp)) {+ recipeCategories.forEach { (key, label) ->+ Box(+ Modifier.clip(RoundedCornerShape(6.dp))+ .background(if (cat == key) Color(0xFF3A6B3A) else Color.White.copy(0.08f))+ .clickable { cat = key }.padding(horizontal = 8.dp, vertical = 5.dp)+ ) { Text(label, color = Color.White.copy(0.9f), fontSize = 11.sp) }+ }+ }+ Column(Modifier.fillMaxHeight().verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(4.dp)) {+ recipes.withIndex().filter { it.value.cat == cat }.forEach { (i, rec) ->+ val icon = rememberBlockIcon(rec.outId)+ Row(+ Modifier.fillMaxWidth().clip(RoundedCornerShape(6.dp))+ .background(if (i == sel) Color(0xFF3A6B3A) else Color.White.copy(alpha = 0.08f))+ .clickable { sel = i }.padding(6.dp),+ verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)+ ) {+ if (icon != null) Image(bitmap = icon, contentDescription = null, modifier = Modifier.size(26.dp), filterQuality = FilterQuality.None)+ Text(blockNames[rec.outId] ?: "", color = Color.White.copy(0.95f), fontSize = 12.sp)+ } } } }@@ -278,7 +290,16 @@ private fun CraftingTable(recipesJson: String) { } } -private data class Recipe(val inId: Int, val inN: Int, val in2Id: Int, val in2N: Int, val outId: Int, val outN: Int)+private data class Recipe(+ val inId: Int, val inN: Int, val in2Id: Int, val in2N: Int,+ val outId: Int, val outN: Int, val cat: String,+)++// Crafting shelves, in the order they appear as filter chips.+private val recipeCategories = listOf(+ "block" to "Blocks", "tool" to "Tools", "armor" to "Armor",+ "material" to "Materials", "food" to "Food", "blessing" to "Blessings",+) private data class BlessingInfo(val id: Int, val name: String, val effect: String)
games/voxels/src/main/rust/src/engine.rs
diff --git a/games/voxels/src/main/rust/src/engine.rs b/games/voxels/src/main/rust/src/engine.rsindex 9b931b8c38..1cc7680186 100644--- a/games/voxels/src/main/rust/src/engine.rs+++ b/games/voxels/src/main/rust/src/engine.rs@@ -729,7 +729,10 @@ pub fn get_trades_json() -> String { } pub fn get_recipes_json() -> String { let items: Vec<_> = crate::inventory::RECIPES.iter()- .map(|(i1, c1, i2, c2, oid, oc)| serde_json::json!({"in": i1, "inN": c1, "in2": i2, "in2N": c2, "out": oid, "outN": oc}))+ .map(|(i1, c1, i2, c2, oid, oc)| serde_json::json!({+ "in": i1, "inN": c1, "in2": i2, "in2N": c2, "out": oid, "outN": oc,+ "cat": crate::inventory::recipe_category(*oid),+ })) .collect(); serde_json::json!(items).to_string() }
games/voxels/src/main/rust/src/inventory.rs
diff --git a/games/voxels/src/main/rust/src/inventory.rs b/games/voxels/src/main/rust/src/inventory.rsindex f3194a3a22..d0e4cb2d46 100644--- a/games/voxels/src/main/rust/src/inventory.rs+++ b/games/voxels/src/main/rust/src/inventory.rs@@ -172,6 +172,17 @@ pub const TRADES: [(u8, i32, u8, i32); 16] = [ (222, 8, 156, 1), // 8 Raw Meat -> 1 Emerald (a butcher's price) ]; +// Which shelf of the crafting menu a recipe belongs on. Purely for the UI — the recipe table itself+// stays a flat list so indices remain stable.+pub fn recipe_category(out: u8) -> &'static str {+ if crate::blessing::is_blessing(out) { return "blessing"; }+ if crate::item::food_effects(out).is_some() { return "food"; }+ if crate::item::is_tool(out) { return "tool"; }+ if crate::item::is_armor(out) { return "armor"; }+ if crate::item::is_item(out) { return "material"; }+ "block"+}+ impl Default for Inventory { fn default() -> Self { let mut slots = [InvSlot::default(); SLOTS];@@ -380,6 +391,21 @@ mod tests { use super::*; // Every crafting and smelting output must have a sane recipe: no zero ids, no free lunches.+ // The crafting menu only renders the six shelves it knows about. A recipe landing outside them+ // would be unreachable in the UI even though the engine can still craft it.+ #[test]+ fn every_recipe_lands_on_a_known_shelf() {+ const SHELVES: [&str; 6] = ["block", "tool", "armor", "material", "food", "blessing"];+ for &(_, _, _, _, out, _) in RECIPES.iter() {+ let cat = recipe_category(out);+ assert!(SHELVES.contains(&cat), "recipe for {out} has unknown category {cat}");+ }+ // Each shelf should actually have something on it.+ for shelf in SHELVES {+ assert!(RECIPES.iter().any(|r| recipe_category(r.4) == shelf), "shelf {shelf} is empty");+ }+ }+ #[test] fn recipe_tables_are_well_formed() { for (i, &(in1, n1, in2, n2, out, out_n)) in RECIPES.iter().enumerate() {