Merge branch 'feat/mod-recipe-pages-game' into feat/mod-recipe-actions

This commit is contained in:
Ludovic Marie
2026-10-26 19:19:55 +01:00
7 changed files with 179 additions and 30 deletions
@@ -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));
}
@@ -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<ItemStack>)>,
tab: usize,
recipes: Vec<Shown>,
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<Pages>,
) -> 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<usize> {
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<Option<ItemStack>> = 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);
+7 -3
View File
@@ -568,6 +568,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 {
@@ -601,9 +603,11 @@ interface browser {
/// The panel's items, at most 8192 (an empty list hides the panel).
set-items: func(items: list<stack>);
/// 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<recipe-view>);
/// 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<stack>, tab: u32, recipes: list<recipe-view>);
/// What the player did on the panel since the last poll, at most 256
/// kept.
poll: func() -> list<event>;
+28 -9
View File
@@ -336,9 +336,18 @@ 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<BrowserRecipe>)>,
recipes: Option<RecipePage>,
events: std::collections::VecDeque<BrowserEvent>,
}
/// 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<BrowserStack>,
pub tab: u32,
pub recipes: Vec<BrowserRecipe>,
}
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;
@@ -347,6 +356,7 @@ const RECIPE_ACTIONS: usize = 36;
/// 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 {
@@ -863,13 +873,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) {
@@ -1276,7 +1284,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<BrowserRecipe>) {
fn set_recipes(
&mut self,
title: String,
tabs: Vec<BrowserStack>,
tab: u32,
recipes: Vec<BrowserRecipe>,
) {
let stack = |s: BrowserStack| BrowserStack {
item: clip(s.item),
components: clip_to(s.components, 4096),
@@ -1298,7 +1312,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);
}
+7 -3
View File
@@ -1237,9 +1237,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))
+20 -1
View File
@@ -373,7 +373,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<browser::Stack> = 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()));
}
"recipe_place" => {
@@ -436,6 +454,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}"),
});
}
}
+2 -1
View File
@@ -589,6 +589,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`
@@ -619,7 +620,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<stack>)` | The panel's items, at most 8192 (an empty list hides the panel). |
| `set-recipes` | `func(title: string, recipes: list<recipe-view>)` | 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<stack>, tab: u32, recipes: list<recipe-view>)` | 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<event>` | What the player did on the panel since the last poll, at most 256 kept. |
## `players`