diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index a5cc64b1d..eea0dfdd7 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -3811,6 +3811,9 @@ impl Game<'_> { self.draw_item(batch, &stack, self.cursor.0 - 8.0, self.cursor.1 - 8.0); } self.draw_slot_tooltip(batch); + if self.container.is_none() && self.craft_size == 2 && !self.mount_active() { + self.draw_inventory_effects(batch, (ox, oy), 176); + } } pub(super) fn close_container(&mut self) { self.close_station(); @@ -7364,6 +7367,7 @@ impl Game<'_> { let name = self.res.names.item(&item); self.tooltip(batch, &name, self.cursor.0, self.cursor.1); } + self.draw_inventory_effects(batch, (ox, oy), Creative::WIDTH as i32); } fn creative_trash_hovered(batch: &HudBatch, cursor: (f32, f32)) -> bool { let (ox, oy) = Self::creative_origin(batch); diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index 8dcd2baa9..18b797a17 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -7,7 +7,7 @@ use horizon_client::{ brewing, effects::{EffectAction, EffectInstance, PlayerEffects}, hud::HudBatch, - hud_layout, hud_overlays, + hud_layout, hud_overlays, inventory_effects, items::ItemStack, }; use horizon_core::{geometry::Vec3, random::RandomSource}; @@ -574,22 +574,36 @@ impl Game<'_> { .effects .list .iter() - .map(|e| { - let def = brewing::effect(e.id); - hud_overlays::EffectIcon { - id: e.id.to_owned(), - beneficial: def.is_some_and(|d| d.category == brewing::Category::Beneficial), - color: def.map_or(0, |d| d.color as i32), - ambient: e.ambient, - duration: e.duration, - // Horizon keeps no showIcon flag: every effect shows its icon. - show_icon: true, - } - }) + .map(hud_overlays::EffectIcon::from) .collect(); let draws = hud_overlays::effects(batch.width as i32, false, &icons); hud_layout::paint(batch, &self.res.atlas, &draws); } + /// `EffectsInInventory`: panels to the right of survival/creative inventory. + pub(super) fn draw_inventory_effects( + &self, + batch: &mut HudBatch, + origin: (f32, f32), + image_width: i32, + ) { + let panels = inventory_effects::panels( + batch.width as i32, + [origin.0 as i32, origin.1 as i32], + image_width, + &self.potions.effects.list, + self.clock.rate(), + &|key| self.res.names.get(key).map(str::to_owned), + &|text| self.res.atlas.font.width(text).ceil() as i32, + ); + if let Some(lines) = inventory_effects::paint( + batch, + &self.res.atlas, + &panels, + [self.cursor.0 as i32, self.cursor.1 as i32], + ) { + self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1); + } + } /// Effects saved in level.dat (`Player.active_effects`). pub(super) fn restore_effects(&mut self) { self.potions = PotionState::default(); diff --git a/crates/horizon-client/src/hud.rs b/crates/horizon-client/src/hud.rs index 6161160dd..d308046c8 100644 --- a/crates/horizon-client/src/hud.rs +++ b/crates/horizon-client/src/hud.rs @@ -802,6 +802,74 @@ impl HudRenderer { mod tests { use super::*; + #[test] + fn effect_icons_are_painted_from_the_mob_effect_directory() { + use crate::{effects::EffectInstance, hud_layout, hud_overlays, inventory_effects}; + // A synthetic resource tree exercises loading and painting together. + // No Minecraft installation or GPU is needed for this regression test. + let root = + std::env::temp_dir().join(format!("horizon-effect-icons-{}", std::process::id())); + for (name, size) in [ + ("mob_effect/levitation", 18), + ("gui/sprites/hud/effect_background", 24), + ("gui/sprites/container/inventory/effect_background", 32), + ] { + let path = root.join(format!("assets/minecraft/textures/{name}.png")); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + let mut encoder = png::Encoder::new(fs::File::create(path).unwrap(), size, size); + encoder.set_color(png::ColorType::Rgba); + encoder.set_depth(png::BitDepth::Eight); + encoder + .write_header() + .unwrap() + .write_image_data(&vec![255; (size * size * 4) as usize]) + .unwrap(); + } + let atlas = HudAtlas::build(&root).unwrap(); + let effect = EffectInstance::new("levitation", -1, 0); + let icon = hud_overlays::EffectIcon::from(&effect); + let draws = hud_overlays::effects(320, false, &[icon]); + let mut batch = HudBatch::new([320, 240]); + hud_layout::paint(&mut batch, &atlas, &draws); + // Both the background and the 18x18 effect icon must reach the batch. + assert_eq!(batch.vertices.len(), 12); + let rect = atlas.sprites["mob_effect/levitation"]; + assert_eq!(batch.vertices[6].pos, [298.0, 30.0]); + assert_eq!(batch.vertices[6].uv, [rect.u0, rect.v0]); + assert_eq!(batch.vertices[8].uv, [rect.u1, rect.v1]); + + let panels = inventory_effects::panels( + 320, + [72, 37], + 176, + std::slice::from_ref(&effect), + 20.0, + &|_| None, + &|_| 60, + ); + batch.vertices.clear(); + let tooltip = inventory_effects::paint(&mut batch, &atlas, &panels, [257, 44]); + assert_eq!(tooltip.unwrap()[0].0, "levitation"); + assert_eq!(batch.vertices.len(), 24); + assert_eq!(batch.vertices[18].pos, [257.0, 44.0]); + assert_eq!(batch.vertices[18].uv, [rect.u0, rect.v0]); + assert_eq!(batch.vertices[20].uv, [rect.u1, rect.v1]); + + // Wide panels retain the four-pixel border instead of stretching it. + let panels = + inventory_effects::panels(480, [72, 37], 176, &[effect], 20.0, &|_| None, &|_| 60); + batch.vertices.clear(); + assert!(inventory_effects::paint(&mut batch, &atlas, &panels, [0, 0]).is_none()); + assert_eq!(batch.vertices[0].pos, [250.0, 37.0]); + assert_eq!(batch.vertices[1].pos, [254.0, 37.0]); + let background = atlas.sprites["gui/sprites/container/inventory/effect_background"]; + assert!( + (batch.vertices[1].uv[0] - background.u0 - (background.u1 - background.u0) / 8.0).abs() + < 1e-6 + ); + fs::remove_dir_all(root).unwrap(); + } + #[test] fn container_origins_use_java_integer_division() { // Odd scaled dimensions, odd image sizes and an oversized panel. diff --git a/crates/horizon-client/src/hud_layout.rs b/crates/horizon-client/src/hud_layout.rs index ab28ece3e..0392edb3f 100644 --- a/crates/horizon-client/src/hud_layout.rs +++ b/crates/horizon-client/src/hud_layout.rs @@ -761,8 +761,14 @@ pub fn paint(batch: &mut crate::hud::HudBatch, atlas: &crate::hud::HudAtlas, dra region, color, } => { - let Some(mut r) = atlas.sprites.get(&format!("gui/sprites/{sprite}")).copied() - else { + // Effect sprites belong to the mob_effect atlas directory; + // the other blitSprite ids refer to GUI sprites. + let name = if sprite.starts_with("mob_effect/") { + sprite.to_string() + } else { + format!("gui/sprites/{sprite}") + }; + let Some(mut r) = atlas.sprites.get(&name).copied() else { continue; }; if let Some([sw, sh, u, v]) = *region { diff --git a/crates/horizon-client/src/hud_overlays.rs b/crates/horizon-client/src/hud_overlays.rs index 8468735c3..94eb7a369 100644 --- a/crates/horizon-client/src/hud_overlays.rs +++ b/crates/horizon-client/src/hud_overlays.rs @@ -105,8 +105,23 @@ pub struct EffectIcon { pub show_icon: bool, } +impl From<&crate::effects::EffectInstance> for EffectIcon { + fn from(effect: &crate::effects::EffectInstance) -> Self { + let def = crate::brewing::effect(effect.id); + Self { + id: effect.id.to_owned(), + beneficial: def.is_some_and(|d| d.category == crate::brewing::Category::Beneficial), + color: def.map_or(0, |d| d.color as i32), + ambient: effect.ambient, + duration: effect.duration, + // Horizon keeps no showIcon flag: every effect shows its icon. + show_icon: true, + } + } +} + /// `MobEffectInstance.compareTo`. -fn compare_effects(a: &EffectIcon, o: &EffectIcon) -> Ordering { +pub(crate) fn compare_effects(a: &EffectIcon, o: &EffectIcon) -> Ordering { if (a.duration <= 32147 || o.duration <= 32147) && !(a.ambient && o.ambient) { a.ambient .cmp(&o.ambient) diff --git a/crates/horizon-client/src/inventory_effects.rs b/crates/horizon-client/src/inventory_effects.rs new file mode 100644 index 000000000..3a870a1e6 --- /dev/null +++ b/crates/horizon-client/src/inventory_effects.rs @@ -0,0 +1,255 @@ +//! `EffectsInInventory`: active effect panels beside survival and creative +//! inventories, with compact icons when there is little room on the right. +use crate::{ + effects::EffectInstance, + hud::{HudAtlas, HudBatch}, + hud_overlays::{EffectIcon, compare_effects, java_sort}, +}; + +#[derive(Debug, PartialEq)] +pub struct EffectPanel { + pub icon: EffectIcon, + pub name: String, + pub duration: String, + pub x: i32, + pub y: i32, + pub width: i32, + pub spacing: i32, +} + +/// Lay out panels using the container's actual origin (including recipe book +/// shifts). Vanilla hides them when fewer than 32 GUI pixels remain. +pub fn panels( + screen_width: i32, + origin: [i32; 2], + image_width: i32, + effects: &[EffectInstance], + tick_rate: f32, + translate: &dyn Fn(&str) -> Option, + text_width: &dyn Fn(&str) -> i32, +) -> Vec { + let x = origin[0] + image_width + 2; + let available = screen_width - x; + if effects.is_empty() || available < 32 { + return Vec::new(); + } + let max_width = if available >= 120 { available - 7 } else { 32 }; + let spacing = if effects.len() > 5 { + 132 / (effects.len() as i32 - 1) + } else { + 33 + }; + let mut sorted: Vec<_> = effects.iter().map(|e| (e, EffectIcon::from(e))).collect(); + java_sort(&mut sorted, |a, b| compare_effects(&a.1, &b.1)); + sorted + .into_iter() + .enumerate() + .map(|(i, (effect, icon))| { + let mut name = translate(&format!("effect.minecraft.{}", effect.id)) + .unwrap_or_else(|| effect.id.to_owned()); + if (1..=9).contains(&effect.amplifier) { + let level = effect.amplifier + 1; + let suffix = translate(&format!("enchantment.level.{level}")) + .unwrap_or_else(|| crate::enchant::level_name(level as u32)); + name = format!("{name} {suffix}"); + } + let duration = if effect.duration == -1 { + translate("effect.duration.infinite").unwrap_or_else(|| "∞".to_owned()) + } else { + // StringUtil.formatTickDuration uses the level's current rate. + let seconds = (effect.duration as f32 / tick_rate) as i32; + let (hours, minutes, seconds) = (seconds / 3600, seconds / 60 % 60, seconds % 60); + if hours > 0 { + format!("{hours}:{minutes:02}:{seconds:02}") + } else { + format!("{minutes}:{seconds:02}") + } + }; + let width = max_width.min(39 + text_width(&name).max(text_width(&duration))); + EffectPanel { + icon, + name, + duration, + x, + y: origin[1] + i as i32 * spacing, + width, + spacing, + } + }) + .collect() +} + +/// The inventory effect background has a four-pixel nine-slice border. Since +/// its height stays 32, only horizontal slicing/tiling is needed. +fn background(batch: &mut HudBatch, atlas: &HudAtlas, panel: &EffectPanel) { + let name = if panel.icon.ambient { + "gui/sprites/container/inventory/effect_background_ambient" + } else { + "gui/sprites/container/inventory/effect_background" + }; + let Some(rect) = atlas.sprites.get(name).copied() else { + return; + }; + let mut strip = |dx: i32, sx: i32, width: i32| { + let mut r = rect; + let du = rect.u1 - rect.u0; + r.u0 = rect.u0 + du * sx as f32 / 32.0; + r.u1 = r.u0 + du * width as f32 / 32.0; + batch.rect_uv( + (panel.x + dx) as f32, + panel.y as f32, + width as f32, + 32.0, + r, + u32::MAX, + ); + }; + strip(0, 0, 4); + let mut dx = 4; + while dx < panel.width - 4 { + let width = 24.min(panel.width - 4 - dx); + strip(dx, 4, width); + dx += width; + } + strip(panel.width - 4, 28, 4); +} + +/// Paint panels and return the full name/duration tooltip for a hovered compact +/// or clipped panel. Its hover height follows the spacing of overlapping rows. +pub fn paint( + batch: &mut HudBatch, + atlas: &HudAtlas, + panels: &[EffectPanel], + cursor: [i32; 2], +) -> Option<[(String, u32); 2]> { + let mut tooltip = None; + for panel in panels { + background(batch, atlas, panel); + let text_width = panel.width - 39; + let clipped = atlas.font.width(&panel.name).ceil() as i32 > text_width; + if text_width > 0 { + let shown = if clipped { + let available = text_width as f32 - atlas.font.width("..."); + let count = atlas.font.fit(&panel.name, available); + format!("{}...", panel.name.chars().take(count).collect::()) + } else { + panel.name.clone() + }; + batch.text( + atlas, + &shown, + (panel.x + 32) as f32, + (panel.y + 7) as f32, + u32::MAX, + true, + ); + batch.text( + atlas, + &panel.duration, + (panel.x + 32) as f32, + (panel.y + 16) as f32, + 0xff7f7f7f, + true, + ); + } + batch.sprite_scaled( + atlas, + &format!("mob_effect/{}", panel.icon.id), + (panel.x + 7) as f32, + (panel.y + 7) as f32, + 18.0, + 18.0, + u32::MAX, + ); + if (text_width <= 0 || clipped) + && cursor[0] >= panel.x + && cursor[0] <= panel.x + panel.width + && cursor[1] >= panel.y + && cursor[1] <= panel.y + panel.spacing + { + tooltip = Some([ + (panel.name.clone(), u32::MAX), + (panel.duration.clone(), u32::MAX), + ]); + } + } + tooltip +} + +#[cfg(test)] +mod tests { + use super::*; + + fn layout(width: i32, effects: &[EffectInstance]) -> Vec { + panels( + width, + [72, 37], + 176, + effects, + 20.0, + &|key| match key { + "effect.minecraft.levitation" => Some("Levitation".into()), + "effect.minecraft.speed" => Some("Speed".into()), + _ => None, + }, + &|text| text.chars().count() as i32 * 6, + ) + } + + #[test] + fn inventory_labels_levels_and_durations() { + let effects = [ + EffectInstance::new("levitation", -1, 0), + EffectInstance::new("speed", 450, 1), + ]; + let p = layout(480, &effects); + assert_eq!(p.len(), 2); + // Natural order puts shorter durations before infinite effects. + assert_eq!( + (&p[0].name, &p[0].duration), + (&"Speed II".into(), &"0:22".into()) + ); + assert_eq!( + (&p[1].name, &p[1].duration), + (&"Levitation".into(), &"∞".into()) + ); + assert_eq!((p[0].x, p[0].y, p[0].width), (250, 37, 87)); + assert_eq!((p[1].x, p[1].y, p[1].width), (250, 70, 99)); + } + + #[test] + fn narrow_windows_hide_or_compact_the_panels() { + let effects = [EffectInstance::new("levitation", -1, 0)]; + assert!(layout(281, &effects).is_empty()); + assert_eq!(layout(282, &effects)[0].width, 32); + assert_eq!(layout(369, &effects)[0].width, 32); + assert_eq!(layout(370, &effects)[0].width, 99); + assert!(layout(480, &[]).is_empty()); + } + + #[test] + fn crowded_effects_overlap_and_preserve_ambient_order() { + let mut effects: Vec<_> = (0..7) + .map(|i| EffectInstance::new("speed", (i + 1) * 100, 0)) + .collect(); + effects[0].ambient = true; + let p = layout(480, &effects); + assert_eq!(p[0].icon.duration, 200); + assert!(p.last().unwrap().icon.ambient); + assert_eq!( + p.iter().map(|p| p.y).collect::>(), + [37, 59, 81, 103, 125, 147, 169] + ); + } + + #[test] + fn creative_origin_recipe_book_shift_and_tick_rate() { + let effects = [EffectInstance::new("speed", 72000, 10)]; + let p = panels(640, [222, 43], 195, &effects, 10.0, &|_| None, &|_| 60); + assert_eq!((p[0].x, p[0].y), (419, 43)); + assert_eq!(p[0].name, "speed"); + assert_eq!(p[0].duration, "2:00:00"); + let p = panels(480, [149, 37], 176, &effects, 20.0, &|_| None, &|_| 150); + assert_eq!((p[0].x, p[0].width), (327, 146)); + } +} diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index eae7dc596..b41f03e28 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -68,6 +68,7 @@ pub mod hud_layout; pub mod hud_overlays; pub mod hunger; pub mod input; +pub mod inventory_effects; pub mod item_data; pub mod item_render; pub mod item_tooltip;