From e92f74f01dd53a53938e01b778a5b5868d90c538 Mon Sep 17 00:00:00 2001 From: lazy-importer Date: Sun, 18 Oct 2026 00:12:39 +0200 Subject: [PATCH 1/4] fix(minecarts): use per-kind display offset in cart geometry (#741) --- crates/horizon-client/src/game/minecarts.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/game/minecarts.rs b/crates/horizon-client/src/game/minecarts.rs index 21ba79b46..de440433d 100644 --- a/crates/horizon-client/src/game/minecarts.rs +++ b/crates/horizon-client/src/game/minecarts.rs @@ -1324,7 +1324,11 @@ impl Game<'_> { if let Some(state) = self.display_state(c) { let display = base * glam::Mat4::from_scale(glam::Vec3::splat(0.75)) - * glam::Mat4::from_translation(glam::Vec3::new(-0.5, (6.0 - 8.0) / 16.0, 0.5)) + * glam::Mat4::from_translation(glam::Vec3::new( + -0.5, + (display_offset(c.kind) as f32 - 8.0) / 16.0, + 0.5, + )) * glam::Mat4::from_rotation_y(90f32.to_radians()); if c.kind == CartKind::Chest { self.cart_chest(state, display, l, out); @@ -1460,3 +1464,18 @@ impl Minecart { Some(c) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn display_offset_matches_vanilla_for_each_cart_kind() { + assert_eq!(display_offset(CartKind::Hopper), 1.0); + assert_eq!(display_offset(CartKind::Chest), 6.0); + assert_eq!(display_offset(CartKind::Furnace), 6.0); + assert_eq!(display_offset(CartKind::Tnt), 6.0); + assert_eq!(display_offset(CartKind::CommandBlock), 6.0); + assert_eq!(display_offset(CartKind::Rideable), 6.0); + } +} From 9779eb7271e29c08424a6853db5a6bc598af7e8a Mon Sep 17 00:00:00 2001 From: lazy-importer Date: Sun, 18 Oct 2026 00:16:44 +0200 Subject: [PATCH 2/4] fix(game): allow always-edible food when full and apply consume effects (#750) --- crates/horizon-client/src/game.rs | 67 ++++++++++-- crates/horizon-client/src/game/potions.rs | 2 +- crates/horizon-client/src/items.rs | 124 ++++++++++++++++++++++ 3 files changed, 184 insertions(+), 9 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 929c720d8..7c5846e2c 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2764,7 +2764,7 @@ impl Game<'_> { || !self .player .as_ref() - .is_some_and(|p| p.health.food.food < 20 || self.creative) + .is_some_and(|p| info.can_always_eat || p.health.food.food < 20 || self.creative) { return false; } @@ -2773,6 +2773,54 @@ impl Game<'_> { } true } + fn apply_consume_effect( + &mut self, + effect: &horizon_client::items::ConsumeEffect, + eye: [f64; 3], + ) { + use horizon_client::items::ConsumeEffect; + match effect { + ConsumeEffect::ApplyEffects { + effects, + probability, + } => { + if *probability >= 1.0 || self.world.random.next_float() <= *probability { + for eff in effects { + if let Some(def) = horizon_client::brewing::effect(&eff.id) { + if def.instant { + if let Some(a) = + horizon_client::effects::instant(def.id, eff.amplifier, 1.0) + { + self.apply_effect_action(a); + } + } else { + self.potions.effects.add( + horizon_client::effects::EffectInstance::new( + def.id, + eff.duration, + eff.amplifier, + ), + ); + } + } + } + } + } + ConsumeEffect::ClearAllEffects => { + self.potions.effects.clear(); + } + ConsumeEffect::RemoveEffects(to_remove) => { + for target in to_remove { + let id = target.trim_start_matches("minecraft:"); + self.potions.effects.list.retain(|e| e.id != id); + } + } + ConsumeEffect::TeleportRandomly => {} + ConsumeEffect::PlaySound(sound) => { + self.audio.play(sound, Some(eye), 1.0, 1.0); + } + } + } fn consume_held(&mut self) { if self.creative { return; @@ -2875,19 +2923,22 @@ impl Game<'_> { self.eat_particles(&stack, 16); // Consumable.onConsume. self.post_entity_event("minecraft:eat", horizon_client::mobs::Who::Player); - if let Some((nutrition, saturation)) = info.food - && let Some(p) = self.player.as_mut() - { - // FoodData.eat(nutrition, saturation) - let food = &mut p.health.food; - food.food = (food.food + nutrition as i32).clamp(0, 20); - food.saturation = (food.saturation + saturation).clamp(0.0, food.food as f32); + if let Some((nutrition, saturation)) = info.food { + if let Some(p) = self.player.as_mut() { + // FoodData.eat(nutrition, saturation) + let food = &mut p.health.food; + food.food = (food.food + nutrition as i32).clamp(0, 20); + food.saturation = (food.saturation + saturation).clamp(0.0, food.food as f32); + } self.audio.play( "minecraft:entity.player.burp", Some(eye), 0.5, self.world.random.next_float() * 0.1 + 0.9, ); + for effect in &info.consume_effects { + self.apply_consume_effect(effect, eye); + } self.trigger_item("minecraft:consume_item", &stack); self.consume_held(); } diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index ffa63b7c9..be5b24650 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -249,7 +249,7 @@ impl Game<'_> { } true } - fn apply_effect_action(&mut self, action: EffectAction) { + pub(super) fn apply_effect_action(&mut self, action: EffectAction) { let creative = self.creative; let rules = self.world.health_rules; let Some(p) = self.player.as_mut() else { diff --git a/crates/horizon-client/src/items.rs b/crates/horizon-client/src/items.rs index 54a505ab9..2782d254f 100644 --- a/crates/horizon-client/src/items.rs +++ b/crates/horizon-client/src/items.rs @@ -39,6 +39,25 @@ pub struct ToolRule { pub speed: Option, pub correct: Option, } +#[derive(Clone, Debug, PartialEq)] +pub struct ConsumeEffectSpec { + pub id: String, + pub duration: i32, + pub amplifier: i32, +} + +#[derive(Clone, Debug, PartialEq)] +pub enum ConsumeEffect { + ApplyEffects { + effects: Vec, + probability: f32, + }, + ClearAllEffects, + RemoveEffects(Vec), + TeleportRandomly, + PlaySound(String), +} + #[derive(Clone, Debug)] pub struct ItemInfo { pub fire_resistant: bool, @@ -49,7 +68,9 @@ pub struct ItemInfo { pub tool: Vec, pub tool_default_speed: f32, pub food: Option<(u32, f32)>, + pub can_always_eat: bool, pub consumable: bool, + pub consume_effects: Vec, pub model: String, /// Main-hand `attack_damage` / `attack_speed` modifier amounts. pub attack_damage: f32, @@ -421,6 +442,79 @@ impl Items { f["saturation"].as_f64().unwrap_or(0.0) as f32, ) }); + let can_always_eat = c + .get("minecraft:food") + .and_then(|f| f.get("can_always_eat")) + .and_then(|v| v.as_bool()) + .unwrap_or(false); + let mut consume_effects = Vec::new(); + if let Some(effects) = c + .get("minecraft:consumable") + .and_then(|cons| cons.get("on_consume_effects")) + .and_then(|e| e.as_array()) + { + for effect in effects { + match effect.get("type").and_then(|t| t.as_str()) { + Some("minecraft:apply_effects") => { + let probability = effect + .get("probability") + .and_then(|p| p.as_f64()) + .map_or(1.0, |p| p as f32); + let mut specs = Vec::new(); + if let Some(list) = effect.get("effects").and_then(|l| l.as_array()) { + for e in list { + if let Some(id) = e.get("id").and_then(|i| i.as_str()) { + let duration = + e.get("duration").and_then(|d| d.as_i64()).unwrap_or(0) + as i32; + let amplifier = e + .get("amplifier") + .and_then(|a| a.as_i64()) + .unwrap_or(0) + as i32; + specs.push(ConsumeEffectSpec { + id: id.to_string(), + duration, + amplifier, + }); + } + } + } + consume_effects.push(ConsumeEffect::ApplyEffects { + effects: specs, + probability, + }); + } + Some("minecraft:clear_all_effects") => { + consume_effects.push(ConsumeEffect::ClearAllEffects); + } + Some("minecraft:remove_effects") => { + let mut targets = Vec::new(); + if let Some(eff) = effect.get("effects") { + if let Some(s) = eff.as_str() { + targets.push(s.to_string()); + } else if let Some(arr) = eff.as_array() { + for item in arr { + if let Some(s) = item.as_str() { + targets.push(s.to_string()); + } + } + } + } + consume_effects.push(ConsumeEffect::RemoveEffects(targets)); + } + Some("minecraft:teleport_randomly") => { + consume_effects.push(ConsumeEffect::TeleportRandomly); + } + Some("minecraft:play_sound") => { + if let Some(sound) = effect.get("sound").and_then(|s| s.as_str()) { + consume_effects.push(ConsumeEffect::PlaySound(sound.to_string())); + } + } + _ => {} + } + } + } items.insert( id.clone(), ItemInfo { @@ -435,7 +529,9 @@ impl Items { .as_f64() .unwrap_or(1.0) as f32, food, + can_always_eat, consumable: c.get("minecraft:consumable").is_some(), + consume_effects, model: c["minecraft:item_model"] .as_str() .unwrap_or(name) @@ -1496,6 +1592,34 @@ mod tests { .is_empty() ); } + + #[test] + fn always_edible_and_consume_effects_loaded() { + let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client/26.3"); + if !root.join("items.json").exists() { + return; + } + let assets = crate::assets::Assets::load(&root).unwrap(); + let facts = crate::blocks::BlockFacts::load(&root, assets.registry).unwrap(); + let items = Items::load(&root, assets.registry, &facts).unwrap(); + + let apple = items.get("minecraft:golden_apple").expect("golden apple"); + assert!(apple.can_always_eat); + assert!(apple.food.is_some()); + assert!(!apple.consume_effects.is_empty()); + + let enchanted = items + .get("minecraft:enchanted_golden_apple") + .expect("enchanted golden apple"); + assert!(enchanted.can_always_eat); + assert!(enchanted.food.is_some()); + assert!(!enchanted.consume_effects.is_empty()); + + let beef = items.get("minecraft:cooked_beef").expect("cooked beef"); + assert!(!beef.can_always_eat); + assert!(beef.food.is_some()); + } } pub fn take_held_drop(slot: &mut Option, all: bool) -> Option { From ecdff7ad2d94e6cf14d27a0b0bd89a64ce50f12b Mon Sep 17 00:00:00 2001 From: lazy-importer Date: Sun, 18 Oct 2026 00:44:34 +0200 Subject: [PATCH 3/4] fix(game): apply absorption effect attribute modifiers and health (#750) --- crates/horizon-client/src/effects.rs | 31 +++++++++++++++++++ crates/horizon-client/src/game.rs | 4 ++- .../src/game/commands/effects.rs | 4 ++- crates/horizon-client/src/game/potions.rs | 27 ++++++++++++++-- crates/horizon-client/src/game/totem.rs | 4 +-- 5 files changed, 62 insertions(+), 8 deletions(-) diff --git a/crates/horizon-client/src/effects.rs b/crates/horizon-client/src/effects.rs index 2e600333a..a258ccf3a 100644 --- a/crates/horizon-client/src/effects.rs +++ b/crates/horizon-client/src/effects.rs @@ -386,6 +386,12 @@ impl PlayerEffects { 4.0, "add_value", )), + "absorption" => Some(( + "minecraft:max_absorption", + "minecraft:effect.absorption", + 4.0, + "add_value", + )), "luck" => Some(("minecraft:luck", "minecraft:effect.luck", 1.0, "add_value")), "unluck" => Some(( "minecraft:luck", @@ -538,4 +544,29 @@ mod tests { assert!((fx.mining_speed_factor() - 1.4).abs() < 1e-6); assert_eq!(fx.modifiers().len(), 1); } + + #[test] + fn absorption_modifier_scales_with_amplifier() { + let mut fx = PlayerEffects::default(); + fx.add(EffectInstance::new("absorption", 2400, 0)); + assert_eq!( + fx.modifiers(), + vec![( + "minecraft:max_absorption", + "minecraft:effect.absorption", + 4.0, + "add_value", + )] + ); + fx.add(EffectInstance::new("absorption", 2400, 3)); + assert_eq!( + fx.modifiers(), + vec![( + "minecraft:max_absorption", + "minecraft:effect.absorption", + 16.0, + "add_value", + )] + ); + } } diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7c5846e2c..26709516f 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2794,7 +2794,7 @@ impl Game<'_> { self.apply_effect_action(a); } } else { - self.potions.effects.add( + self.add_player_effect( horizon_client::effects::EffectInstance::new( def.id, eff.duration, @@ -2808,12 +2808,14 @@ impl Game<'_> { } ConsumeEffect::ClearAllEffects => { self.potions.effects.clear(); + self.apply_effect_status(); } ConsumeEffect::RemoveEffects(to_remove) => { for target in to_remove { let id = target.trim_start_matches("minecraft:"); self.potions.effects.list.retain(|e| e.id != id); } + self.apply_effect_status(); } ConsumeEffect::TeleportRandomly => {} ConsumeEffect::PlaySound(sound) => { diff --git a/crates/horizon-client/src/game/commands/effects.rs b/crates/horizon-client/src/game/commands/effects.rs index 3e04f7179..86726fed9 100644 --- a/crates/horizon-client/src/game/commands/effects.rs +++ b/crates/horizon-client/src/game/commands/effects.rs @@ -86,7 +86,7 @@ impl Game<'_> { Target::Player => { let mut instance = EffectInstance::new(effect.id, duration, amplifier); instance.visible = particles; - self.potions.effects.add(instance) + self.add_player_effect(instance) } Target::Mob(id) => match self.mobs.iter_mut().find(|m| m.id == id) { Some(m) => { @@ -134,11 +134,13 @@ impl Game<'_> { (Target::Player, None) => { let any = !self.potions.effects.list.is_empty(); self.potions.effects.clear(); + self.apply_effect_status(); any } (Target::Player, Some(e)) => { let before = self.potions.effects.list.len(); self.potions.effects.list.retain(|x| x.id != e.id); + self.apply_effect_status(); self.potions.effects.list.len() != before } (Target::Mob(id), e) => match self.mobs.iter_mut().find(|m| m.id == id) { diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index be5b24650..bc4f3b6bc 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -228,7 +228,7 @@ impl Game<'_> { } let (lasting, now) = PlayerEffects::potion_effects(stack.potion(), 1.0); for e in lasting { - self.potions.effects.add(e); + self.add_player_effect(e); } for a in now { self.apply_effect_action(a); @@ -329,9 +329,24 @@ impl Game<'_> { } self.apply_effect_status(); } + /// Add an active effect to the player and update attributes / absorption. + pub(super) fn add_player_effect(&mut self, effect: EffectInstance) -> bool { + let id = effect.id; + let amp = effect.amplifier; + let changed = self.potions.effects.add(effect); + self.apply_effect_status(); + if id == "absorption" + && let Some(p) = self.player.as_mut() + { + let target = 4.0 * (amp + 1) as f32; + let current = p.health.absorption(); + let _ = p.set_health(current.max(target), true); + } + changed + } /// The effects' attribute modifiers, armor protection and status flags /// on `self.player`. - fn apply_effect_status(&mut self) { + pub(super) fn apply_effect_status(&mut self) { let modifiers = self.potions.effects.modifiers(); if modifiers != self.potions.applied && let Some(p) = self.player.as_mut() @@ -344,6 +359,12 @@ impl Game<'_> { } self.potions.applied = modifiers; } + if let Some(p) = self.player.as_mut() + && self.potions.effects.amplifier("absorption").is_none() + && p.health.absorption() > 0.0 + { + let _ = p.set_health(0.0, true); + } // EnchantmentHelper.getDamageProtection per damage type, recomputed on armor change. let armor: Vec> = self.armor.to_vec(); if self.potions.protection_for.as_ref() != Some(&armor) { @@ -495,7 +516,7 @@ impl Game<'_> { if d > 20 && let Some(e) = brewing::effect(id) { - self.potions.effects.add(EffectInstance::new(e.id, d, *amp)); + self.add_player_effect(EffectInstance::new(e.id, d, *amp)); } } } diff --git a/crates/horizon-client/src/game/totem.rs b/crates/horizon-client/src/game/totem.rs index 805360c8d..47b67d9f8 100644 --- a/crates/horizon-client/src/game/totem.rs +++ b/crates/horizon-client/src/game/totem.rs @@ -53,9 +53,7 @@ impl Game<'_> { ("fire_resistance", 800, 0), ] { if let Some(e) = horizon_client::brewing::effect(id) { - self.potions - .effects - .add(EffectInstance::new(e.id, ticks, amp)); + self.add_player_effect(EffectInstance::new(e.id, ticks, amp)); } } self.audio From d33929c8fab6a73321efc4d713c54e423a355d3d Mon Sep 17 00:00:00 2001 From: LookImEcho-Rick <62575172+LookImEcho-Rick@users.noreply.github.com> Date: Sat, 17 Oct 2026 18:22:40 -0500 Subject: [PATCH 4/4] Fix Horizon options scrollbar dragging --- crates/horizon-client/src/options_horizon.rs | 295 +++++++++++++++++-- crates/horizon-client/src/options_ui.rs | 3 +- 2 files changed, 269 insertions(+), 29 deletions(-) diff --git a/crates/horizon-client/src/options_horizon.rs b/crates/horizon-client/src/options_horizon.rs index 634bb8d75..d3ecab639 100644 --- a/crates/horizon-client/src/options_horizon.rs +++ b/crates/horizon-client/src/options_horizon.rs @@ -263,15 +263,29 @@ enum Target { Style(usize), Level(usize), } +struct HorizonScrollbar { + x: f32, + top: f32, + bottom: f32, + max: f32, + thumb: f32, +} +impl HorizonScrollbar { + fn contains(&self, x: f32, y: f32) -> bool { + x >= self.x && x < self.x + 6.0 && y >= self.top && y < self.bottom + } +} +struct HorizonLayout { + sliders: Vec<(Item, f32, f32)>, + scrollbar: Option, +} +fn horizon_batch(framebuffer: [i32; 2]) -> HudBatch { + HudBatch::new([framebuffer[0].max(1) as u32, framebuffer[1].max(1) as u32]) +} impl OptionsUi { /// Lay out (and draw, on a real frame) the Horizon options shell. - /// Returns the sliders' tracks (option, x, width) for dragging. - fn horizon_layout( - &mut self, - s: &Settings, - ui: &mut Ui, - backdrop: f32, - ) -> Vec<(Item, f32, f32)> { + /// Returns the sliders' tracks and the scroll area for pointer input. + fn horizon_layout(&mut self, s: &Settings, ui: &mut Ui, backdrop: f32) -> HorizonLayout { let (w, h) = (ui.w, ui.h); if self.in_world { ui.atmosphere(horizon_theme::VEIL, 1.0); @@ -499,6 +513,7 @@ impl OptionsUi { .filter(|h| h.rect[1] >= cy0 && h.rect[3] <= cy1) .collect(); ui.hits.extend(kept); + let mut scrollbar = None; if max > 0.0 { // A thin scroll indicator. let track = bottom - top; @@ -512,11 +527,18 @@ impl OptionsUi { 1.5, color::with_alpha(color::MUTED, 0.35), ); + scrollbar = Some(HorizonScrollbar { + x: x + cw + 14.0, + top: top + oy, + bottom: bottom + oy, + max, + thumb: bar, + }); } let _ = ox; ui.opacity = 1.0; ui.offset = (0.0, 0.0); - sliders + HorizonLayout { sliders, scrollbar } } pub(crate) fn draw_horizon( &mut self, @@ -530,25 +552,47 @@ impl OptionsUi { self.horizon_layout(s, &mut ui, backdrop); ui.finish(); } - fn horizon_target(&mut self, s: &Settings, atlas: &HudAtlas, x: f32, y: f32) -> Option { - let mut batch = t::scratch_batch(); + fn horizon_target( + &mut self, + s: &Settings, + atlas: &HudAtlas, + x: f32, + y: f32, + framebuffer: [i32; 2], + ) -> (Option, Option, (f32, f32)) { + let mut batch = horizon_batch(framebuffer); let mut ui = Ui::dry(atlas, &mut batch, (x, y)); - let sliders = self.horizon_layout(s, &mut ui, 1.0); - let hit = ui.hit((x, y))?; + let layout = self.horizon_layout(s, &mut ui, 1.0); + let scrollbar = layout.scrollbar; + // The UI layer transforms raw GUI-pixel input into its own dp. + let cursor = ui.cursor; + let Some(hit) = ui.hit((x, y)) else { + return (None, scrollbar, cursor); + }; let section = self.section.min(SECTIONS.len() - 1); if (NAV..NAV + SECTIONS.len() as u64).contains(&hit) { - return Some(Target::Nav((hit - NAV) as usize)); + return (Some(Target::Nav((hit - NAV) as usize)), scrollbar, cursor); } if hit == DONE { - return Some(Target::Done); + return (Some(Target::Done), scrollbar, cursor); } if hit == STYLE || hit == STYLE + 1 { - return Some(Target::Style((hit - STYLE) as usize)); + return ( + Some(Target::Style((hit - STYLE) as usize)), + scrollbar, + cursor, + ); } if hit == LEVEL || hit == LEVEL + 1 { - return Some(Target::Level((hit - LEVEL) as usize)); + return ( + Some(Target::Level((hit - LEVEL) as usize)), + scrollbar, + cursor, + ); } - let i = hit.checked_sub(ROW)? as usize; + let Some(i) = hit.checked_sub(ROW).map(|i| i as usize) else { + return (None, scrollbar, cursor); + }; let rows = (SECTIONS[section].rows)(s); let item = match rows.get(i) { Some(HRow::Opt(opt)) => Some(Item::Vanilla(*opt)), @@ -556,12 +600,16 @@ impl OptionsUi { _ => None, }; if let Some(item) = item - && let Some(&(_, sx, sw)) = sliders.iter().find(|(o, ..)| *o == item) + && let Some(&(_, sx, sw)) = layout.sliders.iter().find(|(o, ..)| *o == item) { let v = t::slider_value(&ui, sx, sw, x); - return Some(Target::Slider(item, v)); + return (Some(Target::Slider(item, v)), scrollbar, cursor); } - (i < rows.len()).then_some(Target::Row(i)) + ( + (i < rows.len()).then_some(Target::Row(i)), + scrollbar, + cursor, + ) } /// A click (or a drag with the button held) on the Horizon shell. pub(crate) fn click_horizon( @@ -576,14 +624,37 @@ impl OptionsUi { ) -> Vec { let mut events = Vec::new(); if drag { - if let Some(item) = self.hdragging { - let mut batch = t::scratch_batch(); - let mut ui = Ui::dry(atlas, &mut batch, (x, y)); - let sliders = self.horizon_layout(s, &mut ui, 1.0); - if let Some(&(_, sx, sw)) = sliders.iter().find(|(o, ..)| *o == item) { - set_item_slider(item, s, t::slider_value(&ui, sx, sw, x)); - events.push(Event::Changed); + let mut batch = horizon_batch(framebuffer); + let mut ui = Ui::dry(atlas, &mut batch, (x, y)); + let layout = self.horizon_layout(s, &mut ui, 1.0); + // Scrollbar geometry and stored drag positions are in Horizon dp. + let pointer_y = ui.cursor.1; + if let Some(previous_y) = self.scrollbar_drag_y { + if let Some(scrollbar) = layout.scrollbar { + // AbstractScrollArea.mouseDragged (inherited by AbstractSelectionList) + // moves by the pointer delta and sends out-of-range drags to an end. + let track = scrollbar.bottom - scrollbar.top; + self.hscroll = if pointer_y < scrollbar.top { + 0.0 + } else if pointer_y > scrollbar.bottom { + scrollbar.max + } else { + self.hscroll + + (pointer_y - previous_y) * scrollbar.max + / (track - scrollbar.thumb).max(1.0) + }; + self.hscroll = self.hscroll.clamp(0.0, scrollbar.max); + self.scrollbar_drag_y = Some(pointer_y); + } else { + self.scrollbar_drag_y = None; } + return events; + } + if let Some(item) = self.hdragging + && let Some(&(_, sx, sw)) = layout.sliders.iter().find(|(o, ..)| *o == item) + { + set_item_slider(item, s, t::slider_value(&ui, sx, sw, x)); + events.push(Event::Changed); } return events; } @@ -598,7 +669,16 @@ impl OptionsUi { }, ); } - match self.horizon_target(s, atlas, x, y) { + let (target, scrollbar, cursor) = self.horizon_target(s, atlas, x, y, framebuffer); + // AbstractScrollArea.updateScrolling grabs anywhere in the scrollbar column. + if !right + && let Some(scrollbar) = scrollbar + && scrollbar.contains(cursor.0, cursor.1) + { + self.scrollbar_drag_y = Some(cursor.1); + return events; + } + match target { Some(Target::Nav(i)) => { self.section = i; self.hscroll = 0.0; @@ -670,3 +750,162 @@ fn set_item_slider(item: Item, s: &mut Settings, v: f32) { Item::Horizon(_) => item.set_slider(s, v), } } + +#[cfg(test)] +mod tests { + use super::*; + + fn atlas() -> HudAtlas { + HudAtlas::build(std::path::Path::new("/nonexistent/horizon-test-assets")).unwrap() + } + + fn section_ui(name: &str) -> OptionsUi { + let mut ui = OptionsUi::default(); + ui.section = SECTIONS + .iter() + .position(|section| section.name == name) + .unwrap(); + ui + } + + fn extras_ui() -> OptionsUi { + section_ui("Extras") + } + + fn scrollbar( + ui: &mut OptionsUi, + settings: &Settings, + atlas: &HudAtlas, + framebuffer: [i32; 2], + ) -> HorizonScrollbar { + let mut batch = horizon_batch(framebuffer); + let mut draw = Ui::dry(atlas, &mut batch, (0.0, 0.0)); + ui.horizon_layout(settings, &mut draw, 1.0) + .scrollbar + .unwrap() + } + + fn gui_pointer(atlas: &HudAtlas, framebuffer: [i32; 2], point: (f32, f32)) -> (f32, f32) { + let mut batch = horizon_batch(framebuffer); + let ui = Ui::dry(atlas, &mut batch, (0.0, 0.0)); + let gui_per_dp = ui.density / ui.batch.scale; + (point.0 * gui_per_dp, point.1 * gui_per_dp) + } + + #[test] + fn horizon_scrollbar_track_drag_is_bounded_and_stops_on_release() { + let atlas = atlas(); + let framebuffer = [2560, 1440]; + let mut settings = Settings::default(); + let mut ui = section_ui("Controls"); + let bar = scrollbar(&mut ui, &settings, &atlas, framebuffer); + let max = bar.max; + let track = bar.bottom - bar.top; + assert!(max > 0.0); + + // Start on the track below the thumb, then move horizontally away; + // the held scrollbar keeps following the pointer. + let x = bar.x + 2.0; + let y = bar.top + bar.thumb + (track - bar.thumb) / 2.0; + assert!(y < bar.bottom); + let (click_x, click_y) = gui_pointer(&atlas, framebuffer, (x, y)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + click_x, + click_y, + framebuffer, + false, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, 0.0); + let delta = 5.0; + let (drag_x, drag_y) = gui_pointer(&atlas, framebuffer, (x - 100.0, y + delta)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + drag_x, + drag_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert!((ui.hscroll - delta * max / (track - bar.thumb)).abs() < 0.001); + + let (below_x, below_y) = gui_pointer(&atlas, framebuffer, (x, bar.bottom + 100.0)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + below_x, + below_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, max); + let (above_x, above_y) = gui_pointer(&atlas, framebuffer, (x, bar.top - 100.0)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + above_x, + above_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, 0.0); + + assert!(ui.release().is_empty()); + let (below_x, below_y) = gui_pointer(&atlas, framebuffer, (x, bar.bottom + 100.0)); + assert!( + ui.click_horizon( + &mut settings, + &atlas, + below_x, + below_y, + framebuffer, + true, + false, + ) + .is_empty() + ); + assert_eq!(ui.hscroll, 0.0); + assert_eq!(settings.keys, Settings::default().keys); + } + + #[test] + fn horizon_key_rebinding_takes_precedence_over_scrollbar_press() { + let atlas = atlas(); + let framebuffer = [2560, 1440]; + let mut settings = Settings::default(); + let mut ui = extras_ui(); + let bar = scrollbar(&mut ui, &settings, &atlas, framebuffer); + let dp_y = bar.top + bar.thumb + (bar.bottom - bar.top - bar.thumb) / 2.0; + ui.selected_key = Some(extras::SORT_KEY); + let (x, y) = gui_pointer(&atlas, framebuffer, (bar.x + 2.0, dp_y)); + + assert_eq!( + ui.click_horizon(&mut settings, &atlas, x, y, framebuffer, false, false,), + vec![Event::Changed] + ); + assert_eq!(settings.extras.sort_key, "key.mouse.left"); + assert_eq!(ui.selected_key, None); + assert_eq!(ui.hscroll, 0.0); + + let (x, drag_y) = gui_pointer(&atlas, framebuffer, (bar.x + 2.0, dp_y + 10.0)); + ui.click_horizon(&mut settings, &atlas, x, drag_y, framebuffer, true, false); + assert_eq!(ui.hscroll, 0.0); + } +} diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index dca180e15..fc80530d9 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -806,7 +806,8 @@ pub struct OptionsUi { /// when it last moved: the handle follows the pointer while the option /// keeps its stepped value, then snaps to it (`resetValue`). slider_value: Option<(Opt, f32, Instant)>, - scrollbar_drag_y: Option, + /// The pointer's last y while a Classic or Horizon scrollbar is held. + pub(crate) scrollbar_drag_y: Option, /// Horizon style: the selected sidebar section, the one last drawn and /// the rows' scroll in dp (`options_horizon`). pub section: usize,