From 6a62ad67cce8fd23254f285ef6ba7881809216c7 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 18:39:46 +0100 Subject: [PATCH 1/2] mods: set-recipes takes a row of tabs, and the tab event says which one the player picked Co-Authored-By: Claude Opus 5.5 --- crates/horizon-mod-sdk/wit/horizon.wit | 10 +++-- crates/horizon-mods/src/runtime.rs | 37 ++++++++++++++----- crates/horizon-mods/tests/sandbox.rs | 10 +++-- crates/horizon-mods/tests/ui_guest/src/lib.rs | 21 ++++++++++- docs/api/mods-reference.md | 3 +- 5 files changed, 64 insertions(+), 17 deletions(-) diff --git a/crates/horizon-mod-sdk/wit/horizon.wit b/crates/horizon-mod-sdk/wit/horizon.wit index 4896c3e21..5f8677df0 100644 --- a/crates/horizon-mod-sdk/wit/horizon.wit +++ b/crates/horizon-mod-sdk/wit/horizon.wit @@ -557,6 +557,8 @@ interface browser { page(u32), /// The back button of `set-recipes`. back, + /// The player picked the tab at this index of `set-recipes`' tabs. + tab(u32), } /// How the game lays out a recipe of `set-recipes`. enum layout { @@ -590,9 +592,11 @@ interface browser { /// The panel's items, at most 8192 (an empty list hides the panel). set-items: func(items: list); /// Shows these recipes in the panel instead of its items, as many as - /// fit a page, under `title` and a back button; an empty list shows the - /// items again. At most 256 recipes. - set-recipes: func(title: string, recipes: list); + /// fit a page, under `title` and a back button, with a row of `tabs` + /// (each an icon; `tab` is the selected one) between them; an empty + /// list of recipes shows the items again. At most 256 recipes and 16 + /// tabs. + set-recipes: func(title: string, tabs: list, tab: u32, recipes: list); /// What the player did on the panel since the last poll, at most 256 /// kept. poll: func() -> list; diff --git a/crates/horizon-mods/src/runtime.rs b/crates/horizon-mods/src/runtime.rs index 43c9e3fac..3b3acf5db 100644 --- a/crates/horizon-mods/src/runtime.rs +++ b/crates/horizon-mods/src/runtime.rs @@ -323,15 +323,25 @@ struct BrowserState { /// across mods and reloads, so the game converts them once. version: u64, /// `set-recipes`' title and recipes, shown instead of the items. - recipes: Option<(String, Vec)>, + recipes: Option, events: std::collections::VecDeque, } +/// What `browser.set-recipes` shows: a title, tabs (`tab` the selected +/// one) and the recipes. +#[derive(Clone, Debug)] +pub struct RecipePage { + pub title: String, + pub tabs: Vec, + pub tab: u32, + pub recipes: Vec, +} static BROWSER_VERSION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1); /// Items a panel may hold, and events kept for `browser.poll`. const BROWSER_ITEMS: usize = 8192; /// Recipes `set-recipes` may show, slots in one, and stacks a slot cycles. const BROWSER_RECIPES: usize = 256; const RECIPE_SLOTS: usize = 9; +const RECIPE_TABS: usize = 16; const SLOT_STACKS: usize = 64; const BROWSER_EVENTS: usize = 256; impl BrowserState { @@ -848,13 +858,11 @@ impl Mods { )) }) } - /// The recipes mod `owner`'s panel shows instead of its items - /// (`browser.set-recipes`): the title and the recipes. - pub fn browser_recipes(&self, owner: usize) -> Option<(&str, &[BrowserRecipe])> { + /// What mod `owner`'s panel shows instead of its items + /// (`browser.set-recipes`). + pub fn browser_recipes(&self, owner: usize) -> Option<&RecipePage> { let b = &self.entries.get(owner)?.run.as_ref()?.store.data().browser; - b.recipes - .as_ref() - .map(|(title, list)| (title.as_str(), list.as_slice())) + b.recipes.as_ref() } /// The player did `event` on mod `owner`'s panel. pub fn browser_event(&mut self, owner: usize, event: BrowserEvent) { @@ -1261,7 +1269,13 @@ impl browser::Host for State { .collect(); self.browser.version = BROWSER_VERSION.fetch_add(1, std::sync::atomic::Ordering::Relaxed); } - fn set_recipes(&mut self, title: String, recipes: Vec) { + fn set_recipes( + &mut self, + title: String, + tabs: Vec, + tab: u32, + recipes: Vec, + ) { let stack = |s: BrowserStack| BrowserStack { item: clip(s.item), components: clip_to(s.components, 4096), @@ -1283,7 +1297,12 @@ impl browser::Host for State { ..r }) .collect(); - (clip(title), recipes) + RecipePage { + title: clip(title), + tabs: tabs.into_iter().take(RECIPE_TABS).map(stack).collect(), + tab, + recipes, + } }); self.browser.version = BROWSER_VERSION.fetch_add(1, std::sync::atomic::Ordering::Relaxed); } diff --git a/crates/horizon-mods/tests/sandbox.rs b/crates/horizon-mods/tests/sandbox.rs index 081884dc2..28fd73ec6 100644 --- a/crates/horizon-mods/tests/sandbox.rs +++ b/crates/horizon-mods/tests/sandbox.rs @@ -1196,9 +1196,13 @@ fn browser_lists_items_and_hears_the_player() { said(&mut mods, "browser_recipes minecraft:oak_planks"), ["recipes shown: 1"] ); - let (title, recipes) = mods.browser_recipes(owner).unwrap(); - assert_eq!(title, "Recipes for minecraft:oak_planks"); - let r = &recipes[0]; + let page = mods.browser_recipes(owner).unwrap(); + assert_eq!(page.title, "Recipes for minecraft:oak_planks"); + assert_eq!( + (page.tabs[0].item.as_str(), page.tab), + ("minecraft:crafting_table", 0) + ); + let r = &page.recipes[0]; assert_eq!( (r.layout, r.width, r.height, r.action), (BrowserLayout::Grid, 1, 1, Some(false)) diff --git a/crates/horizon-mods/tests/ui_guest/src/lib.rs b/crates/horizon-mods/tests/ui_guest/src/lib.rs index 751169565..f879f4302 100644 --- a/crates/horizon-mods/tests/ui_guest/src/lib.rs +++ b/crates/horizon-mods/tests/ui_guest/src/lib.rs @@ -364,7 +364,25 @@ impl Mod for Guest { } }) .collect(); - browser::set_recipes(&format!("Recipes for {args}"), &views); + // A tab per layout shown, the first selected. + let mut tabs: Vec = Vec::new(); + for v in &views { + let icon = match v.layout { + browser::Layout::Grid => "minecraft:crafting_table", + browser::Layout::Cooking => "minecraft:furnace", + browser::Layout::Single => "minecraft:stonecutter", + browser::Layout::Smithing => "minecraft:smithing_table", + browser::Layout::Brewing => "minecraft:brewing_stand", + }; + if !tabs.iter().any(|t| t.item == icon) { + tabs.push(browser::Stack { + item: icon.into(), + count: 1, + components: String::new(), + }); + } + } + browser::set_recipes(&format!("Recipes for {args}"), &tabs, 0, &views); chat::show(&format!("recipes shown: {}", views.len())); } "browser_poll" => { @@ -389,6 +407,7 @@ impl Mod for Guest { browser::Event::Action(i) => format!("action {i}"), browser::Event::Page(first) => format!("page {first}"), browser::Event::Back => "back".into(), + browser::Event::Tab(i) => format!("tab {i}"), }); } } diff --git a/docs/api/mods-reference.md b/docs/api/mods-reference.md index 6af12fa5b..5e9593525 100644 --- a/docs/api/mods-reference.md +++ b/docs/api/mods-reference.md @@ -587,6 +587,7 @@ A click on a slot of a recipe the panel shows (`set-recipes`). | `action` | `u32` | The action button of the recipe at this index in `set-recipes`'s list. | | `page` | `u32` | The player turned the recipes' page (`set-recipes`): the index of the first recipe now shown. | | `back` | | The back button of `set-recipes`. | +| `tab` | `u32` | The player picked the tab at this index of `set-recipes`' tabs. | ### enum `layout` @@ -617,7 +618,7 @@ A recipe to show. Each slot, and the result, cycles through its stacks, one a se | Function | Signature | What it does | | --- | --- | --- | | `set-items` | `func(items: list)` | The panel's items, at most 8192 (an empty list hides the panel). | -| `set-recipes` | `func(title: string, recipes: list)` | Shows these recipes in the panel instead of its items, as many as fit a page, under `title` and a back button; an empty list shows the items again. At most 256 recipes. | +| `set-recipes` | `func(title: string, tabs: list, tab: u32, recipes: list)` | Shows these recipes in the panel instead of its items, as many as fit a page, under `title` and a back button, with a row of `tabs` (each an icon; `tab` is the selected one) between them; an empty list of recipes shows the items again. At most 256 recipes and 16 tabs. | | `poll` | `func() -> list` | What the player did on the panel since the last poll, at most 256 kept. | ## `players` From 5d7097877bccd89bd0e448b875706ec8006ecdbf Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Mon, 26 Oct 2026 19:19:30 +0100 Subject: [PATCH 2/2] game: station tabs on the recipe pages A row of tabs above the recipe pages shows `set-recipes`' stations (their item in a slot, a white frame on the selected one). A click sends `tab(i)` to the mod, R/U over a tab works like over any slot, and horizon-ctl lists them as " tab". Changing tab resets the page. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/mod_browser.rs | 12 +- .../src/game/mod_browser_recipes.rs | 116 ++++++++++++++++-- 2 files changed, 115 insertions(+), 13 deletions(-) diff --git a/crates/horizon-client/src/game/mod_browser.rs b/crates/horizon-client/src/game/mod_browser.rs index 3121eb05b..645415ccb 100644 --- a/crates/horizon-client/src/game/mod_browser.rs +++ b/crates/horizon-client/src/game/mod_browser.rs @@ -127,10 +127,11 @@ impl Game<'_> { self.mods.browser.stacks = stacks; // The same title again (the mod updating its buttons) keeps the page. let old = self.mods.browser.recipes.take(); - self.mods.browser.recipes = - self.mods.mods.browser_recipes(owner).map(|(title, list)| { - super::mod_browser_recipes::Pages::new(items, title, list, old) - }); + self.mods.browser.recipes = self + .mods + .mods + .browser_recipes(owner) + .map(|shown| super::mod_browser_recipes::Pages::new(items, shown, old)); self.mods.browser.of = Some((owner, version)); } Some(owner) @@ -171,6 +172,9 @@ impl Game<'_> { /// as the mod sent it and as a stack. fn browser_pointed_stack(&self, layout: &Layout) -> Option<(BrowserStack, ItemStack)> { if self.mods.browser.recipes.is_some() { + if let Some((_, sent, stack)) = self.recipe_tab_hovered(layout) { + return Some((sent, stack)); + } let (_, _, sent, stack) = self.recipe_slot_hovered(layout)?; return Some((sent, stack)); } diff --git a/crates/horizon-client/src/game/mod_browser_recipes.rs b/crates/horizon-client/src/game/mod_browser_recipes.rs index 14848de90..13b3b78e1 100644 --- a/crates/horizon-client/src/game/mod_browser_recipes.rs +++ b/crates/horizon-client/src/game/mod_browser_recipes.rs @@ -11,12 +11,14 @@ use horizon_client::{ hud::{HudBatch, rgba}, items::ItemStack, }; -use horizon_mods::{BrowserEvent, BrowserLayout, BrowserRecipe, BrowserSlotClick, BrowserStack}; +use horizon_mods::{BrowserEvent, BrowserLayout, BrowserSlotClick, BrowserStack, RecipePage}; use std::time::Instant; const SLOT: f32 = 18.0; /// The title row's height, and the gap under each recipe. const TITLE: f32 = 18.0; +/// A tab's step: an 18 px slot and a gap. +const TAB: f32 = 20.0; const GAP: f32 = 6.0; /// One recipe as the game draws it. @@ -34,6 +36,9 @@ pub(super) struct Shown { /// The recipes a panel shows, and its page. pub(super) struct Pages { title: String, + /// The tabs' icons and the selected one. + tabs: Vec<(BrowserStack, Option)>, + tab: usize, recipes: Vec, page: usize, since: Instant, @@ -42,11 +47,11 @@ pub(super) struct Pages { impl Pages { pub(super) fn new( items: &horizon_client::items::Items, - title: &str, - list: &[BrowserRecipe], + shown: &RecipePage, old: Option, ) -> Self { - let kept = old.filter(|o| o.title == title); + let (title, list, tab) = (&shown.title, &shown.recipes, shown.tab as usize); + let kept = old.filter(|o| o.title == *title && o.tab == tab); let cycle = |stacks: &[BrowserStack]| -> Vec<(BrowserStack, ItemStack)> { stacks .iter() @@ -69,7 +74,13 @@ impl Pages { }) .collect(); Self { - title: title.to_owned(), + title: title.clone(), + tabs: shown + .tabs + .iter() + .map(|t| (t.clone(), browser_stack(items, t))) + .collect(), + tab, recipes, page: kept.as_ref().map_or(0, |o| o.page), since: kept.map_or_else(Instant::now, |o| o.since), @@ -131,11 +142,30 @@ impl Shown { } } +/// The tabs' places, in rows under the title row. +fn tab_places(pages: &Pages, layout: &Layout) -> Vec<(f32, f32)> { + let per_row = ((layout.width() / TAB).floor() as usize).max(1); + (0..pages.tabs.len()) + .map(|i| { + ( + layout.grid.0 + (i % per_row) as f32 * TAB, + layout.grid.1 + TITLE + 3.0 + (i / per_row) as f32 * TAB, + ) + }) + .collect() +} +/// Where the recipes start: under the title row and the tabs. +fn recipes_top(pages: &Pages, layout: &Layout) -> f32 { + let tabs = tab_places(pages, layout); + tabs.last() + .map_or(layout.grid.1 + TITLE, |&(_, y)| y + TAB + 3.0) +} + impl Game<'_> { - /// The first recipe of each page, filling the panel from the title row - /// to the search box. + /// The first recipe of each page, filling the panel from the tabs to + /// the search box. fn recipe_pages(pages: &Pages, layout: &Layout) -> Vec { - let room = layout.search[1] - 4.0 - (layout.grid.1 + TITLE); + let room = layout.search[1] - 4.0 - recipes_top(pages, layout); let mut starts = vec![0]; let mut used = 0.0; for (i, r) in pages.recipes.iter().enumerate() { @@ -156,7 +186,7 @@ impl Game<'_> { let starts = Self::recipe_pages(pages, layout); let page = pages.page.min(starts.len() - 1); let end = starts.get(page + 1).copied().unwrap_or(pages.recipes.len()); - let mut y = layout.grid.1 + TITLE; + let mut y = recipes_top(pages, layout); (starts[page]..end) .map(|i| { let at = (layout.grid.0, y); @@ -177,6 +207,21 @@ impl Game<'_> { let n = pages.since.elapsed().as_secs() as usize; stacks.get(n % stacks.len().max(1)) } + /// The tab under the pointer: its index and icon. + pub(super) fn recipe_tab_hovered( + &self, + layout: &Layout, + ) -> Option<(usize, BrowserStack, ItemStack)> { + let pages = self.mods.browser.recipes.as_ref()?; + let places = tab_places(pages, layout); + places.iter().enumerate().find_map(|(i, &(x, y))| { + if !inside(self.cursor, [x, y, SLOT, SLOT]) { + return None; + } + let (sent, stack) = pages.tabs.get(i)?; + Some((i, sent.clone(), stack.clone()?)) + }) + } /// The recipe slot under the pointer: recipe, slot, and its stack now. pub(super) fn recipe_slot_hovered( &self, @@ -237,6 +282,7 @@ impl Game<'_> { u32::MAX, true, ); + self.draw_recipe_tabs(batch, layout); if pages_n > 1 { let labels = [ self.translate("gui.recipebook.previous_page", "Previous Page"), @@ -351,6 +397,50 @@ impl Game<'_> { } } + /// The tabs, on slots; the selected one framed. + fn draw_recipe_tabs(&mut self, batch: &mut HudBatch, layout: &Layout) { + let Some(pages) = &self.mods.browser.recipes else { + return; + }; + let places = tab_places(pages, layout); + let tabs: Vec> = pages.tabs.iter().map(|t| t.1.clone()).collect(); + let selected = pages.tab; + for (i, (&(x, y), stack)) in places.iter().zip(tabs).enumerate() { + let atlas = &self.gui.atlas; + batch.sprite(atlas, "gui/sprites/container/slot", x, y); + let Some(stack) = stack else { + continue; + }; + self.draw_item(batch, &stack, x + 1.0, y + 1.0); + // The selected tab: a white frame around its slot. + if i == selected { + let white = rgba(255, 255, 255, 255); + let atlas = &self.gui.atlas; + batch.fill(atlas, x - 1.0, y - 1.0, SLOT + 2.0, 1.0, white); + batch.fill(atlas, x - 1.0, y + SLOT, SLOT + 2.0, 1.0, white); + batch.fill(atlas, x - 1.0, y, 1.0, SLOT, white); + batch.fill(atlas, x + SLOT, y, 1.0, SLOT, white); + } + if inside(self.cursor, [x, y, SLOT, SLOT]) { + batch.fill( + &self.gui.atlas, + x + 1.0, + y + 1.0, + 16.0, + 16.0, + rgba(255, 255, 255, 128), + ); + } + let name = self.pack.names.display_item(&stack.item); + horizon_client::widgets::record_as( + &format!("{name} tab"), + [x + 1.0, y + 1.0, 16.0, 16.0], + true, + horizon_client::widgets::Kind::Tab(i == selected), + ); + } + } + /// A press on the recipes: true when it hit one of their parts. pub(super) fn browser_recipes_press( &mut self, @@ -383,6 +473,14 @@ impl Game<'_> { .browser_event(owner, BrowserEvent::Page(starts[to] as u32)); return true; } + if let Some((tab, _, _)) = self.recipe_tab_hovered(layout) { + self.audio + .play("minecraft:ui.button.click", None, 0.25, 1.0); + self.mods + .mods + .browser_event(owner, BrowserEvent::Tab(tab as u32)); + return true; + } if inside(cursor, self.back_button(layout).0) { self.audio .play("minecraft:ui.button.click", None, 0.25, 1.0);