From 9f0df29d6c2efdb295c518e84f35c118ceef81e0 Mon Sep 17 00:00:00 2001 From: Cosmic <84875247+Online5277@users.noreply.github.com> Date: Tue, 13 Oct 2026 19:15:54 -0400 Subject: [PATCH 1/3] Keep the inventory tab in the creative screen --- crates/horizon-client/src/creative.rs | 52 ++++ crates/horizon-client/src/game.rs | 259 ++++++++++++++------ tests/test_creative_inventory_transition.py | 58 +++++ tests/test_hand_frame_policy.py | 62 +++++ 4 files changed, 351 insertions(+), 80 deletions(-) create mode 100644 tests/test_creative_inventory_transition.py create mode 100644 tests/test_hand_frame_policy.py diff --git a/crates/horizon-client/src/creative.rs b/crates/horizon-client/src/creative.rs index b696348f2..6ea7e6b14 100644 --- a/crates/horizon-client/src/creative.rs +++ b/crates/horizon-client/src/creative.rs @@ -67,6 +67,32 @@ impl Names { impl Creative { pub const WIDTH: f32 = 195.0; pub const HEIGHT: f32 = 136.0; + pub const TRASH_POSITION: (f32, f32) = (173.0, 112.0); + + /// Visible player slots in the creative inventory tab (game slot ids). + pub fn inventory_slots() -> impl Iterator { + (0..36).chain(500..505) + } + + /// CreativeModeInventoryScreen.selectTab positions its InventoryMenu wrappers. + pub fn inventory_slot_position(slot: usize) -> Option<(f32, f32)> { + match slot { + 0..9 => Some((9.0 + slot as f32 * 18.0, 112.0)), + 9..36 => Some(( + 9.0 + ((slot - 9) % 9) as f32 * 18.0, + 54.0 + ((slot - 9) / 9) as f32 * 18.0, + )), + 500..504 => { + let position = slot - 500; + Some(( + 54.0 + (position / 2) as f32 * 54.0, + 6.0 + (position % 2) as f32 * 27.0, + )) + } + 504 => Some((35.0, 20.0)), + _ => None, + } + } /// The scrollbar track (insideScrollbar), relative to the screen origin. pub const SCROLL_X: f32 = 175.0; pub const SCROLL_Y: f32 = 18.0; @@ -391,6 +417,32 @@ mod tests { Creative::from_json(&raw).unwrap() } + #[test] + fn inventory_tab_slot_layout_matches_vanilla() { + assert_eq!(Creative::inventory_slots().count(), 41); + for (slot, expected) in [ + (0, (9.0, 112.0)), + (8, (153.0, 112.0)), + (9, (9.0, 54.0)), + (35, (153.0, 90.0)), + (500, (54.0, 6.0)), + (501, (54.0, 33.0)), + (502, (108.0, 6.0)), + (503, (108.0, 33.0)), + (504, (35.0, 20.0)), + ] { + assert_eq!(Creative::inventory_slot_position(slot), Some(expected)); + } + assert_eq!(Creative::inventory_slot_position(100), None); + assert_eq!(Creative::TRASH_POSITION, (173.0, 112.0)); + let mut c = creative(90); + c.select(2); + assert!(c.tab().inventory); + assert!(!c.can_scroll(&Names { + map: HashMap::new() + })); + } + #[test] fn hidden_tabs_are_skipped() { let c = creative(0); diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index a0d350a6b..594eb9325 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -3392,7 +3392,13 @@ impl Game<'_> { self.draw_book(&mut batch); batch } + fn creative_inventory_open(&self) -> bool { + self.screen == Screen::Creative && self.creative_screen.tab().inventory + } fn inventory_origin(&self, batch: &HudBatch) -> (f32, f32) { + if self.creative_inventory_open() { + return Self::creative_origin(batch); + } let (x, y) = batch.centered_origin(176.0, self.screen_height()); (x + self.recipe_book_shift(batch.width), y) } @@ -3403,6 +3409,9 @@ impl Game<'_> { } /// 300.. are the open container's slots. fn slots(&self) -> Vec { + if self.creative_inventory_open() { + return Creative::inventory_slots().collect(); + } if let Some(v) = self.station_slots().or_else(|| self.mount_slots()) { return v; } @@ -3433,6 +3442,11 @@ impl Game<'_> { } /// InventoryMenu / CraftingMenu slot layouts. fn slot_position(&self, i: usize) -> (f32, f32) { + if self.creative_inventory_open() + && let Some(position) = Creative::inventory_slot_position(i) + { + return position; + } if let Some(p) = self .station_slot_position(i) .or_else(|| self.machine_slot_position(i)) @@ -3542,6 +3556,32 @@ impl Game<'_> { .find_with(&grid, n, &scale) .map(|(name, result)| (name.to_owned(), result)) } + fn draw_inventory_slots(&mut self, batch: &mut HudBatch, ox: f32, oy: f32) { + for i in self.slots() { + let (sx, sy) = self.slot_position(i); + if i == 504 && self.offhand.is_none() { + batch.sprite( + &self.res.atlas, + "gui/sprites/container/slot/shield", + ox + sx, + oy + sy, + ); + } + if (500..504).contains(&i) && self.armor[i - 500].is_none() { + let icon = ["helmet", "chestplate", "leggings", "boots"][i - 500]; + batch.sprite( + &self.res.atlas, + &format!("gui/sprites/container/slot/{icon}"), + ox + sx, + oy + sy, + ); + } + let stack = self.slot_view(i); + if let Some(stack) = stack { + self.draw_item(batch, &stack, ox + sx, oy + sy); + } + } + } fn draw_inventory(&mut self, batch: &mut HudBatch) { if self.draw_station(batch) { return; @@ -3668,30 +3708,7 @@ impl Game<'_> { if self.container.is_none() && self.craft_size != 3 && !self.mount_active() { self.draw_inventory_player(batch, ox, oy); } - for i in self.slots() { - let (sx, sy) = self.slot_position(i); - if i == 504 && self.offhand.is_none() { - batch.sprite( - &self.res.atlas, - "gui/sprites/container/slot/shield", - ox + sx, - oy + sy, - ); - } - if (500..504).contains(&i) && self.armor[i - 500].is_none() { - let icon = ["helmet", "chestplate", "leggings", "boots"][i - 500]; - batch.sprite( - &self.res.atlas, - &format!("gui/sprites/container/slot/{icon}"), - ox + sx, - oy + sy, - ); - } - let stack = self.slot_view(i); - if let Some(stack) = stack { - self.draw_item(batch, &stack, ox + sx, oy + sy); - } - } + self.draw_inventory_slots(batch, ox, oy); self.draw_recipe_book(batch); if let Some(slot) = self.slot_at(batch, self.cursor.0, self.cursor.1) { let (sx, sy) = self.slot_position(slot); @@ -7260,63 +7277,100 @@ impl Game<'_> { ); } let atlas = &self.res.atlas; - batch.text( - atlas, - &tab.title, - ox + 8.0, - oy + 6.0, - rgba(64, 64, 64, 255), - false, - ); + if !tab.inventory { + batch.text( + atlas, + &tab.title, + ox + 8.0, + oy + 6.0, + rgba(64, 64, 64, 255), + false, + ); + } if tab.search { let shown = format!("{}_", self.creative_screen.search); batch.text(atlas, &shown, ox + 82.0, oy + 6.0, u32::MAX, true); } - let items = self.creative_screen.items(&self.res.names); - let first = self.creative_screen.first_row(&self.res.names); let mut hovered: Option = None; - for r in 0..5 { - for c in 0..9 { - let Some(stack) = items.get((first + r) * 9 + c) else { - continue; - }; - let (x, y) = (ox + 9.0 + c as f32 * 18.0, oy + 18.0 + r as f32 * 18.0); - self.draw_item(batch, stack, x, y); - if self.cursor.0 >= x - && self.cursor.0 < x + 16.0 - && self.cursor.1 >= y - && self.cursor.1 < y + 16.0 - { - batch.fill(&self.res.atlas, x, y, 16.0, 16.0, rgba(255, 255, 255, 128)); - hovered = Some(stack.item.to_string()); - } + if tab.inventory { + self.draw_player_preview( + batch, + ox + 73.0, + oy + 6.0, + ox + 105.0, + oy + 49.0, + 20.0, + 0.0625, + ); + self.draw_inventory_slots(batch, ox, oy); + if Self::creative_trash_hovered(batch, self.cursor) { + let (x, y) = Creative::TRASH_POSITION; + batch.fill( + &self.res.atlas, + ox + x, + oy + y, + 16.0, + 16.0, + rgba(255, 255, 255, 128), + ); } - } - for c in 0..9 { - let (x, y) = (ox + 9.0 + c as f32 * 18.0, oy + 112.0); - if let Some(stack) = self.inventory[c].clone() { - self.draw_item(batch, &stack, x, y); - if self.cursor.0 >= x - && self.cursor.0 < x + 16.0 - && self.cursor.1 >= y - && self.cursor.1 < y + 16.0 - { - hovered = Some(stack.item.to_string()); - } + if let Some(slot) = self.slot_at(batch, self.cursor.0, self.cursor.1) { + let (x, y) = self.slot_position(slot); + batch.fill( + &self.res.atlas, + ox + x, + oy + y, + 16.0, + 16.0, + rgba(255, 255, 255, 128), + ); } - } - let scrollable = self.creative_screen.rows(&self.res.names) > 5; - let scroller = if scrollable { - "gui/sprites/container/creative_inventory/scroller" } else { - "gui/sprites/container/creative_inventory/scroller_disabled" - }; - batch.sprite( - &self.res.atlas, - scroller, - ox + Creative::SCROLL_X, - oy + Creative::SCROLL_Y + self.creative_screen.scroller_offset(), - ); + let items = self.creative_screen.items(&self.res.names); + let first = self.creative_screen.first_row(&self.res.names); + for r in 0..5 { + for c in 0..9 { + let Some(stack) = items.get((first + r) * 9 + c) else { + continue; + }; + let (x, y) = (ox + 9.0 + c as f32 * 18.0, oy + 18.0 + r as f32 * 18.0); + self.draw_item(batch, stack, x, y); + if self.cursor.0 >= x + && self.cursor.0 < x + 16.0 + && self.cursor.1 >= y + && self.cursor.1 < y + 16.0 + { + batch.fill(&self.res.atlas, x, y, 16.0, 16.0, rgba(255, 255, 255, 128)); + hovered = Some(stack.item.to_string()); + } + } + } + for c in 0..9 { + let (x, y) = (ox + 9.0 + c as f32 * 18.0, oy + 112.0); + if let Some(stack) = self.inventory[c].clone() { + self.draw_item(batch, &stack, x, y); + if self.cursor.0 >= x + && self.cursor.0 < x + 16.0 + && self.cursor.1 >= y + && self.cursor.1 < y + 16.0 + { + hovered = Some(stack.item.to_string()); + } + } + } + let scrollable = self.creative_screen.rows(&self.res.names) > 5; + let scroller = if scrollable { + "gui/sprites/container/creative_inventory/scroller" + } else { + "gui/sprites/container/creative_inventory/scroller_disabled" + }; + batch.sprite( + &self.res.atlas, + scroller, + ox + Creative::SCROLL_X, + oy + Creative::SCROLL_Y + self.creative_screen.scroller_offset(), + ); + } for t in &tabs { let (tx, ty) = Creative::tab_position(t); if self.cursor.0 >= ox + tx @@ -7329,11 +7383,28 @@ impl Game<'_> { } if let Some(stack) = self.carried.clone() { self.draw_item(batch, &stack, self.cursor.0 - 8.0, self.cursor.1 - 8.0); - } else if let Some(item) = hovered { + } + if tab.inventory { + if Self::creative_trash_hovered(batch, self.cursor) { + self.tooltip(batch, "Destroy Item", self.cursor.0, self.cursor.1); + } else { + self.draw_slot_tooltip(batch); + } + } else if self.carried.is_none() + && let Some(item) = hovered + { let name = self.res.names.item(&item); self.tooltip(batch, &name, self.cursor.0, self.cursor.1); } } + fn creative_trash_hovered(batch: &HudBatch, cursor: (f32, f32)) -> bool { + let (ox, oy) = Self::creative_origin(batch); + let (x, y) = Creative::TRASH_POSITION; + cursor.0 >= ox + x + && cursor.0 < ox + x + 16.0 + && cursor.1 >= oy + y + && cursor.1 < oy + y + 16.0 + } fn click_creative(&mut self, right: bool) { let Some(size) = self .renderer @@ -7349,14 +7420,36 @@ impl Game<'_> { for (i, t) in tabs.iter().enumerate() { let (tx, ty) = Creative::tab_position(t); if cx >= ox + tx && cx < ox + tx + 26.0 && cy >= oy + ty && cy < oy + ty + 32.0 { - if t.inventory { - self.screen = Screen::Inventory; - return; - } self.creative_screen.select(i); + self.inv_drag = None; + self.last_inv_click = None; return; } } + if self.creative_inventory_open() { + if Self::creative_trash_hovered(&batch, self.cursor) { + if self.keys.contains(&KeyCode::ShiftLeft) + || self.keys.contains(&KeyCode::ShiftRight) + { + self.inventory.fill(None); + self.armor.fill(None); + self.offhand = None; + self.craft_grid[..4].fill(None); + self.apply_equipment(); + } else { + self.carried = None; + } + self.inv_drag = None; + } else if self.slot_at(&batch, cx, cy).is_some() + || !(cx >= ox + && cx < ox + Creative::WIDTH + && cy >= oy + && cy < oy + Creative::HEIGHT) + { + self.inventory_press(right); + } + return; + } // Holding the scrollbar scrolls as the mouse moves (mouseDragged). if !right && self @@ -7670,7 +7763,7 @@ impl Game<'_> { } else { Vec::new() }; - let hand = if title || self.screen == Screen::Inventory || self.scoping() { + let hand = if title || self.scoping() { Vec::new() } else { self.hand_geometry(alpha) @@ -7861,7 +7954,8 @@ impl Game<'_> { | KeyCode::Digit9 | KeyCode::KeyF | KeyCode::KeyQ - if self.screen == Screen::Inventory && self.inventory_hotkey(key) => {} + if (self.screen == Screen::Inventory || self.creative_inventory_open()) + && self.inventory_hotkey(key) => {} KeyCode::KeyT if self.screen == Screen::Inventory => self.recipe_focus_search(), KeyCode::KeyT if self.screen == Screen::Playing => self.open_chat(""), // key.advancements. @@ -8199,6 +8293,8 @@ impl ApplicationHandler for Game<'_> { } if self.screen == Screen::Inventory { self.loom_drag(); + } + if self.screen == Screen::Inventory || self.creative_inventory_open() { self.inventory_drag_move(); } if self.dragging && self.screen == Screen::Options { @@ -8354,6 +8450,9 @@ impl ApplicationHandler for Game<'_> { Screen::Creative if pressed => { self.click_creative(button == MouseButton::Right); } + Screen::Creative if self.creative_screen.tab().inventory => { + self.inventory_release(); + } Screen::Creative if button == MouseButton::Left => { self.creative_screen.release(); } diff --git a/tests/test_creative_inventory_transition.py b/tests/test_creative_inventory_transition.py new file mode 100644 index 000000000..2c3a5d7b1 --- /dev/null +++ b/tests/test_creative_inventory_transition.py @@ -0,0 +1,58 @@ +"""Exercise the real creative-tab selection branch without a desktop window.""" +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] + + +class CreativeInventoryTransitionTests(unittest.TestCase): + @unittest.skipUnless(shutil.which("rustc"), "rustc is needed for the tab-selection probe") + def test_inventory_tab_stays_in_the_creative_screen(self): + source = (ROOT / "crates/horizon-client/src/game.rs").read_text() + click = source.split("fn click_creative(&mut self, right: bool)", 1)[1] + branch = click.split("if cx >= ox + tx", 1)[1] + branch = branch.split("{", 1)[1].split("\n }\n", 1)[0] + program = """ +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +enum Screen { Creative, Inventory } +struct Tab { inventory: bool } +struct Creative { selected: usize, tabs: [Tab; 2] } +impl Creative { fn select(&mut self, i: usize) { self.selected = i; } } +struct Game { screen: Screen, creative_screen: Creative, inv_drag: Option, last_inv_click: Option } +impl Game { + fn click_tab(&mut self, i: usize) { + let t = &self.creative_screen.tabs[i]; + SELECTION + } +} +#[test] +fn inventory_and_back_preserve_the_creative_screen() { + let mut game = Game { screen: Screen::Creative, + creative_screen: Creative { selected: 0, tabs: [Tab { inventory: false }, Tab { inventory: true }] }, + inv_drag: Some(1), last_inv_click: Some(2) }; + game.click_tab(1); + assert_eq!(game.screen, Screen::Creative); + assert_eq!(game.creative_screen.selected, 1); + assert!(game.inv_drag.is_none() && game.last_inv_click.is_none()); + game.click_tab(0); + assert_eq!(game.screen, Screen::Creative); + assert_eq!(game.creative_screen.selected, 0); +} +""".replace("SELECTION", branch) + with tempfile.TemporaryDirectory(prefix="horizon-creative-tab-") as directory: + path = Path(directory) + (path / "probe.rs").write_text(program) + compiled = subprocess.run( + ["rustc", "--edition=2024", "--test", str(path / "probe.rs"), "-o", str(path / "probe")], + capture_output=True, text=True, check=False, + ) + self.assertEqual(compiled.returncode, 0, compiled.stderr) + result = subprocess.run([str(path / "probe")], capture_output=True, text=True, check=False) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_hand_frame_policy.py b/tests/test_hand_frame_policy.py new file mode 100644 index 000000000..e9a83245a --- /dev/null +++ b/tests/test_hand_frame_policy.py @@ -0,0 +1,62 @@ +"""Compile the real frame hand-selection expression without opening a window.""" +from pathlib import Path +import re +import shutil +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] + + +class HandFramePolicyTests(unittest.TestCase): + @unittest.skipUnless(shutil.which("rustc"), "rustc is needed for the frame-policy probe") + def test_inventory_screen_does_not_suppress_first_person_geometry(self): + source = (ROOT / "crates/horizon-client/src/game.rs").read_text() + match = re.search(r"let hand = (if .*?);\s*let screen =", source, re.S) + self.assertIsNotNone(match, "frame hand-selection seam changed") + selection = match.group(1) + # Only geometry generation is stubbed; execute the actual frame guard. + program = """ +#[derive(Clone, Copy, PartialEq, Eq)] +enum Screen { Playing, Creative, Inventory } +struct Game { screen: Screen, scoping: bool } +impl Game { + fn scoping(&self) -> bool { self.scoping } + fn hand_geometry(&mut self, _: f64) -> Vec { vec![1] } + fn hand_len(&mut self, title: bool) -> usize { + let alpha = 0.0; + let hand = SELECTION; + hand.len() + } +} +#[test] +fn creative_inventory_and_return_keep_the_hand() { + let mut game = Game { screen: Screen::Playing, scoping: false }; + for screen in [Screen::Playing, Screen::Creative, Screen::Inventory, Screen::Playing] { + game.screen = screen; + assert_eq!(game.hand_len(false), 1); + } +} +#[test] +fn title_and_spyglass_still_hide_the_hand() { + let mut game = Game { screen: Screen::Inventory, scoping: false }; + assert_eq!(game.hand_len(true), 0); + game.scoping = true; + assert_eq!(game.hand_len(false), 0); +} +""".replace("SELECTION", selection) + with tempfile.TemporaryDirectory(prefix="horizon-hand-policy-") as directory: + path = Path(directory) + (path / "probe.rs").write_text(program) + compiled = subprocess.run( + ["rustc", "--edition=2024", "--test", str(path / "probe.rs"), "-o", str(path / "probe")], + capture_output=True, text=True, check=False, + ) + self.assertEqual(compiled.returncode, 0, compiled.stderr) + result = subprocess.run([str(path / "probe")], capture_output=True, text=True, check=False) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + + +if __name__ == "__main__": + unittest.main() From 767ed4cb47b714631209cc9e906dc4b40ac9b37d Mon Sep 17 00:00:00 2001 From: Cosmic <84875247+Online5277@users.noreply.github.com> Date: Tue, 13 Oct 2026 19:21:01 -0400 Subject: [PATCH 2/3] Use physical Ctrl state for stack dropping, not toggle sprint --- crates/horizon-client/src/game.rs | 6 ++- crates/horizon-client/src/game/agent.rs | 9 +--- tests/test_drop_modifier_policy.py | 55 +++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 tests/test_drop_modifier_policy.py diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 594eb9325..5156e685c 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -4170,7 +4170,8 @@ impl Game<'_> { (Some(n), _) => self.menu_click(Some(slot), n as i32, Input::Swap), (None, KeyCode::KeyF) => self.menu_click(Some(slot), 40, Input::Swap), (None, KeyCode::KeyQ) => { - let all = self.keys.contains(&KeyCode::ControlLeft); + let all = self.physical_keys.contains(&KeyCode::ControlLeft) + || self.physical_keys.contains(&KeyCode::ControlRight); self.menu_click(Some(slot), i32::from(all), Input::Throw) } _ => false, @@ -8001,7 +8002,8 @@ impl Game<'_> { self.using = None; } KeyCode::KeyQ if self.screen == Screen::Playing => { - let all = self.keys.contains(&KeyCode::ControlLeft); + let all = self.physical_keys.contains(&KeyCode::ControlLeft) + || self.physical_keys.contains(&KeyCode::ControlRight); self.drop_held(all); } KeyCode::Digit1 diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs index 2de1f4c74..8b1b8071e 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -1232,14 +1232,7 @@ impl Game<'_> { self.playing()?; let all = a.pos.first() == Some(&"all"); let before = self.held().map(|s| self.stack_value(s)); - if all { - self.keys.insert(KeyCode::ControlLeft); - } - let result = self.key_pressed(KeyCode::KeyQ); - if all { - self.keys.remove(&KeyCode::ControlLeft); - } - result?; + self.drop_held(all); ok( json!({ "ok": true, "dropped_from": before, "held": self.held().map(|s| self.stack_value(s)) }), ) diff --git a/tests/test_drop_modifier_policy.py b/tests/test_drop_modifier_policy.py new file mode 100644 index 000000000..76415f876 --- /dev/null +++ b/tests/test_drop_modifier_policy.py @@ -0,0 +1,55 @@ +"""Drop modifiers must come from physical Ctrl, not the sprint key mapping.""" +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] + + +class DropModifierPolicyTests(unittest.TestCase): + @unittest.skipUnless(shutil.which("rustc"), "rustc is needed for the drop-policy probe") + def test_playing_and_inventory_drop_ignore_latched_sprint(self): + source = (ROOT / "crates/horizon-client/src/game.rs").read_text() + inventory = source.split("fn inventory_hotkey(", 1)[1] + playing = source.split("KeyCode::KeyQ if self.screen == Screen::Playing =>", 1)[1] + expressions = [section.split("let all = ", 1)[1].split(";", 1)[0] + for section in (inventory, playing)] + program = """ +use std::collections::HashSet; +#[derive(PartialEq, Eq, Hash)] enum KeyCode { ControlLeft, ControlRight } +struct Game { keys: HashSet, physical_keys: HashSet } +impl Game { + fn inventory_drop_all(&self) -> bool { INVENTORY } + fn playing_drop_all(&self) -> bool { PLAYING } +} +#[test] +fn sprint_and_control_are_independent() { + for sprint in [false, true] { + for held in 0..4 { + let ctrl = held != 0; + let mut game = Game { keys: HashSet::new(), physical_keys: HashSet::new() }; + if held & 1 != 0 { game.physical_keys.insert(KeyCode::ControlLeft); } + if held & 2 != 0 { game.physical_keys.insert(KeyCode::ControlRight); } + if sprint { game.keys.insert(KeyCode::ControlLeft); } + assert_eq!(game.inventory_drop_all(), ctrl, "inventory: sprint={sprint}, ctrl={ctrl}"); + assert_eq!(game.playing_drop_all(), ctrl, "playing: sprint={sprint}, ctrl={ctrl}"); + } + } +} +""".replace("INVENTORY", expressions[0]).replace("PLAYING", expressions[1]) + with tempfile.TemporaryDirectory(prefix="horizon-drop-policy-") as directory: + path = Path(directory) + (path / "probe.rs").write_text(program) + compiled = subprocess.run( + ["rustc", "--edition=2024", "--test", str(path / "probe.rs"), "-o", str(path / "probe")], + capture_output=True, text=True, check=False, + ) + self.assertEqual(compiled.returncode, 0, compiled.stderr) + result = subprocess.run([str(path / "probe")], capture_output=True, text=True, check=False) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + + +if __name__ == "__main__": + unittest.main() From e905c104004db1f5cefb0a31eeb397879c12d109 Mon Sep 17 00:00:00 2001 From: Cosmic <84875247+Online5277@users.noreply.github.com> Date: Wed, 14 Oct 2026 12:11:53 -0400 Subject: [PATCH 3/3] Address creative inventory review on current main --- crates/horizon-client/src/creative.rs | 20 ++++ crates/horizon-client/src/game.rs | 115 ++++++++++++++++---- tests/test_creative_inventory_transition.py | 58 ---------- tests/test_drop_modifier_policy.py | 55 ---------- tests/test_hand_frame_policy.py | 62 ----------- 5 files changed, 115 insertions(+), 195 deletions(-) delete mode 100644 tests/test_creative_inventory_transition.py delete mode 100644 tests/test_drop_modifier_policy.py delete mode 100644 tests/test_hand_frame_policy.py diff --git a/crates/horizon-client/src/creative.rs b/crates/horizon-client/src/creative.rs index 6ea7e6b14..7f67aa7e2 100644 --- a/crates/horizon-client/src/creative.rs +++ b/crates/horizon-client/src/creative.rs @@ -443,6 +443,26 @@ mod tests { })); } + #[test] + fn inventory_tab_can_return_to_category_and_search() { + let mut c = creative(90); + for (selected, inventory, search) in [ + (2, true, false), + (0, false, false), + (1, false, true), + (2, true, false), + ] { + c.search = "anvil".into(); + c.scroll = 0.5; + c.select(selected); + assert_eq!(c.selected, selected); + assert_eq!(c.tab().inventory, inventory); + assert_eq!(c.tab().search, search); + assert!(c.search.is_empty()); + assert_eq!(c.scroll, 0.0); + } + } + #[test] fn hidden_tabs_are_skipped() { let c = creative(0); diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 5156e685c..67d4d776b 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -588,6 +588,22 @@ pub struct Resources { /// Enchantments, workstation recipes and brewing (game/stations.rs). stations: horizon_client::stations::StationData, } +/// Ctrl+Q reads the physical keyboard, independently of sprint key mappings. +fn drop_whole_stack(physical_keys: &HashSet) -> bool { + physical_keys.contains(&KeyCode::ControlLeft) || physical_keys.contains(&KeyCode::ControlRight) +} + +fn select_creative_tab( + creative: &mut Creative, + clicks: &mut horizon_client::container_screen::Clicks, + tab: usize, +) { + creative.select(tab); + *clicks = Default::default(); + // Tabs are outside the panel; their release must not drop a carried stack. + clicks.skip_release(); +} + struct Game<'a> { res: &'a mut Resources, collision: &'a BlockCollisionRegistry<'a>, @@ -4170,8 +4186,7 @@ impl Game<'_> { (Some(n), _) => self.menu_click(Some(slot), n as i32, Input::Swap), (None, KeyCode::KeyF) => self.menu_click(Some(slot), 40, Input::Swap), (None, KeyCode::KeyQ) => { - let all = self.physical_keys.contains(&KeyCode::ControlLeft) - || self.physical_keys.contains(&KeyCode::ControlRight); + let all = drop_whole_stack(&self.physical_keys); self.menu_click(Some(slot), i32::from(all), Input::Throw) } _ => false, @@ -4274,16 +4289,21 @@ impl Game<'_> { fn clicked_outside(&self, batch: &HudBatch) -> bool { use horizon_client::container_screen::{Book, Image, clicked_outside}; let (ox, oy) = self.inventory_origin(batch); - let (shift, width) = match self.station.as_ref().map(|s| s.kind) { - Some(stations::Kind::Merchant) => (-50.0, 276), - Some(stations::Kind::Beacon) => (beacon::SHIFT, 230), - _ => (0.0, 176), + let (shift, width, height) = if self.creative_inventory_open() { + (0.0, Creative::WIDTH as i32, Creative::HEIGHT as i32) + } else { + let (shift, width) = match self.station.as_ref().map(|s| s.kind) { + Some(stations::Kind::Merchant) => (-50.0, 276), + Some(stations::Kind::Beacon) => (beacon::SHIFT, 230), + _ => (0.0, 176), + }; + (shift, width, self.screen_height() as i32) }; let image = Image { left: (ox + shift) as i32, top: oy as i32, width, - height: self.screen_height() as i32, + height, }; let book = self .recipe_book_area(batch) @@ -7421,7 +7441,7 @@ impl Game<'_> { for (i, t) in tabs.iter().enumerate() { let (tx, ty) = Creative::tab_position(t); if cx >= ox + tx && cx < ox + tx + 26.0 && cy >= oy + ty && cy < oy + ty + 32.0 { - self.creative_screen.select(i); + select_creative_tab(&mut self.creative_screen, &mut self.inv_clicks, i); self.inv_drag = None; self.last_inv_click = None; return; @@ -7429,9 +7449,7 @@ impl Game<'_> { } if self.creative_inventory_open() { if Self::creative_trash_hovered(&batch, self.cursor) { - if self.keys.contains(&KeyCode::ShiftLeft) - || self.keys.contains(&KeyCode::ShiftRight) - { + if self.has_shift_down() { self.inventory.fill(None); self.armor.fill(None); self.offhand = None; @@ -7441,12 +7459,8 @@ impl Game<'_> { self.carried = None; } self.inv_drag = None; - } else if self.slot_at(&batch, cx, cy).is_some() - || !(cx >= ox - && cx < ox + Creative::WIDTH - && cy >= oy - && cy < oy + Creative::HEIGHT) - { + self.inv_clicks = Default::default(); + } else if self.slot_at(&batch, cx, cy).is_some() || self.clicked_outside(&batch) { self.inventory_press(right); } return; @@ -8002,8 +8016,7 @@ impl Game<'_> { self.using = None; } KeyCode::KeyQ if self.screen == Screen::Playing => { - let all = self.physical_keys.contains(&KeyCode::ControlLeft) - || self.physical_keys.contains(&KeyCode::ControlRight); + let all = drop_whole_stack(&self.physical_keys); self.drop_held(all); } KeyCode::Digit1 @@ -8453,7 +8466,7 @@ impl ApplicationHandler for Game<'_> { self.click_creative(button == MouseButton::Right); } Screen::Creative if self.creative_screen.tab().inventory => { - self.inventory_release(); + self.inventory_mouse_release(button == MouseButton::Right); } Screen::Creative if button == MouseButton::Left => { self.creative_screen.release(); @@ -9280,3 +9293,65 @@ pub fn run() -> Result<()> { } Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + use horizon_client::container_screen::{Button, Clicks, Release, Target}; + use horizon_client::keymapping::{KeyAction, SPRINT, ToggleKeys}; + + #[test] + fn selecting_creative_tabs_does_not_drop_the_carried_stack_on_release() { + let mut creative = Creative::from_json(&serde_json::json!({ + "schema": 2, + "tabs": [{"type": "CATEGORY"}, {"type": "INVENTORY"}] + })) + .unwrap(); + for button in [Button::Left, Button::Right] { + let mut clicks = Clicks::default(); + for tab in [1, 1, 0, 1] { + select_creative_tab(&mut creative, &mut clicks, tab); + assert_eq!(creative.selected, tab); + assert_eq!(creative.tab().inventory, tab == 1); + assert_eq!( + clicks.release( + &Target::::Outside, + button, + false, + false, + false, + false + ), + Release::Nothing + ); + } + } + } + + #[test] + fn stack_drop_requires_physical_ctrl_even_with_sprint_latched() { + for sprint in [false, true] { + let mut toggles = ToggleKeys::default(); + if sprint { + toggles.key(SPRINT, KeyAction::Press, false, true); + toggles.key(SPRINT, KeyAction::Release, false, true); + } + assert_eq!(toggles.is_down(SPRINT), sprint); + + for held in 0..4 { + let mut physical_keys = HashSet::from([KeyCode::ShiftLeft, KeyCode::KeyQ]); + if held & 1 != 0 { + physical_keys.insert(KeyCode::ControlLeft); + } + if held & 2 != 0 { + physical_keys.insert(KeyCode::ControlRight); + } + assert_eq!(drop_whole_stack(&physical_keys), held != 0); + physical_keys.remove(&KeyCode::ControlLeft); + physical_keys.remove(&KeyCode::ControlRight); + assert!(!drop_whole_stack(&physical_keys)); + assert_eq!(toggles.is_down(SPRINT), sprint); + } + } + } +} diff --git a/tests/test_creative_inventory_transition.py b/tests/test_creative_inventory_transition.py deleted file mode 100644 index 2c3a5d7b1..000000000 --- a/tests/test_creative_inventory_transition.py +++ /dev/null @@ -1,58 +0,0 @@ -"""Exercise the real creative-tab selection branch without a desktop window.""" -from pathlib import Path -import shutil -import subprocess -import tempfile -import unittest - -ROOT = Path(__file__).resolve().parents[1] - - -class CreativeInventoryTransitionTests(unittest.TestCase): - @unittest.skipUnless(shutil.which("rustc"), "rustc is needed for the tab-selection probe") - def test_inventory_tab_stays_in_the_creative_screen(self): - source = (ROOT / "crates/horizon-client/src/game.rs").read_text() - click = source.split("fn click_creative(&mut self, right: bool)", 1)[1] - branch = click.split("if cx >= ox + tx", 1)[1] - branch = branch.split("{", 1)[1].split("\n }\n", 1)[0] - program = """ -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -enum Screen { Creative, Inventory } -struct Tab { inventory: bool } -struct Creative { selected: usize, tabs: [Tab; 2] } -impl Creative { fn select(&mut self, i: usize) { self.selected = i; } } -struct Game { screen: Screen, creative_screen: Creative, inv_drag: Option, last_inv_click: Option } -impl Game { - fn click_tab(&mut self, i: usize) { - let t = &self.creative_screen.tabs[i]; - SELECTION - } -} -#[test] -fn inventory_and_back_preserve_the_creative_screen() { - let mut game = Game { screen: Screen::Creative, - creative_screen: Creative { selected: 0, tabs: [Tab { inventory: false }, Tab { inventory: true }] }, - inv_drag: Some(1), last_inv_click: Some(2) }; - game.click_tab(1); - assert_eq!(game.screen, Screen::Creative); - assert_eq!(game.creative_screen.selected, 1); - assert!(game.inv_drag.is_none() && game.last_inv_click.is_none()); - game.click_tab(0); - assert_eq!(game.screen, Screen::Creative); - assert_eq!(game.creative_screen.selected, 0); -} -""".replace("SELECTION", branch) - with tempfile.TemporaryDirectory(prefix="horizon-creative-tab-") as directory: - path = Path(directory) - (path / "probe.rs").write_text(program) - compiled = subprocess.run( - ["rustc", "--edition=2024", "--test", str(path / "probe.rs"), "-o", str(path / "probe")], - capture_output=True, text=True, check=False, - ) - self.assertEqual(compiled.returncode, 0, compiled.stderr) - result = subprocess.run([str(path / "probe")], capture_output=True, text=True, check=False) - self.assertEqual(result.returncode, 0, result.stdout + result.stderr) - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_drop_modifier_policy.py b/tests/test_drop_modifier_policy.py deleted file mode 100644 index 76415f876..000000000 --- a/tests/test_drop_modifier_policy.py +++ /dev/null @@ -1,55 +0,0 @@ -"""Drop modifiers must come from physical Ctrl, not the sprint key mapping.""" -from pathlib import Path -import shutil -import subprocess -import tempfile -import unittest - -ROOT = Path(__file__).resolve().parents[1] - - -class DropModifierPolicyTests(unittest.TestCase): - @unittest.skipUnless(shutil.which("rustc"), "rustc is needed for the drop-policy probe") - def test_playing_and_inventory_drop_ignore_latched_sprint(self): - source = (ROOT / "crates/horizon-client/src/game.rs").read_text() - inventory = source.split("fn inventory_hotkey(", 1)[1] - playing = source.split("KeyCode::KeyQ if self.screen == Screen::Playing =>", 1)[1] - expressions = [section.split("let all = ", 1)[1].split(";", 1)[0] - for section in (inventory, playing)] - program = """ -use std::collections::HashSet; -#[derive(PartialEq, Eq, Hash)] enum KeyCode { ControlLeft, ControlRight } -struct Game { keys: HashSet, physical_keys: HashSet } -impl Game { - fn inventory_drop_all(&self) -> bool { INVENTORY } - fn playing_drop_all(&self) -> bool { PLAYING } -} -#[test] -fn sprint_and_control_are_independent() { - for sprint in [false, true] { - for held in 0..4 { - let ctrl = held != 0; - let mut game = Game { keys: HashSet::new(), physical_keys: HashSet::new() }; - if held & 1 != 0 { game.physical_keys.insert(KeyCode::ControlLeft); } - if held & 2 != 0 { game.physical_keys.insert(KeyCode::ControlRight); } - if sprint { game.keys.insert(KeyCode::ControlLeft); } - assert_eq!(game.inventory_drop_all(), ctrl, "inventory: sprint={sprint}, ctrl={ctrl}"); - assert_eq!(game.playing_drop_all(), ctrl, "playing: sprint={sprint}, ctrl={ctrl}"); - } - } -} -""".replace("INVENTORY", expressions[0]).replace("PLAYING", expressions[1]) - with tempfile.TemporaryDirectory(prefix="horizon-drop-policy-") as directory: - path = Path(directory) - (path / "probe.rs").write_text(program) - compiled = subprocess.run( - ["rustc", "--edition=2024", "--test", str(path / "probe.rs"), "-o", str(path / "probe")], - capture_output=True, text=True, check=False, - ) - self.assertEqual(compiled.returncode, 0, compiled.stderr) - result = subprocess.run([str(path / "probe")], capture_output=True, text=True, check=False) - self.assertEqual(result.returncode, 0, result.stdout + result.stderr) - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/test_hand_frame_policy.py b/tests/test_hand_frame_policy.py deleted file mode 100644 index e9a83245a..000000000 --- a/tests/test_hand_frame_policy.py +++ /dev/null @@ -1,62 +0,0 @@ -"""Compile the real frame hand-selection expression without opening a window.""" -from pathlib import Path -import re -import shutil -import subprocess -import tempfile -import unittest - -ROOT = Path(__file__).resolve().parents[1] - - -class HandFramePolicyTests(unittest.TestCase): - @unittest.skipUnless(shutil.which("rustc"), "rustc is needed for the frame-policy probe") - def test_inventory_screen_does_not_suppress_first_person_geometry(self): - source = (ROOT / "crates/horizon-client/src/game.rs").read_text() - match = re.search(r"let hand = (if .*?);\s*let screen =", source, re.S) - self.assertIsNotNone(match, "frame hand-selection seam changed") - selection = match.group(1) - # Only geometry generation is stubbed; execute the actual frame guard. - program = """ -#[derive(Clone, Copy, PartialEq, Eq)] -enum Screen { Playing, Creative, Inventory } -struct Game { screen: Screen, scoping: bool } -impl Game { - fn scoping(&self) -> bool { self.scoping } - fn hand_geometry(&mut self, _: f64) -> Vec { vec![1] } - fn hand_len(&mut self, title: bool) -> usize { - let alpha = 0.0; - let hand = SELECTION; - hand.len() - } -} -#[test] -fn creative_inventory_and_return_keep_the_hand() { - let mut game = Game { screen: Screen::Playing, scoping: false }; - for screen in [Screen::Playing, Screen::Creative, Screen::Inventory, Screen::Playing] { - game.screen = screen; - assert_eq!(game.hand_len(false), 1); - } -} -#[test] -fn title_and_spyglass_still_hide_the_hand() { - let mut game = Game { screen: Screen::Inventory, scoping: false }; - assert_eq!(game.hand_len(true), 0); - game.scoping = true; - assert_eq!(game.hand_len(false), 0); -} -""".replace("SELECTION", selection) - with tempfile.TemporaryDirectory(prefix="horizon-hand-policy-") as directory: - path = Path(directory) - (path / "probe.rs").write_text(program) - compiled = subprocess.run( - ["rustc", "--edition=2024", "--test", str(path / "probe.rs"), "-o", str(path / "probe")], - capture_output=True, text=True, check=False, - ) - self.assertEqual(compiled.returncode, 0, compiled.stderr) - result = subprocess.run([str(path / "probe")], capture_output=True, text=True, check=False) - self.assertEqual(result.returncode, 0, result.stdout + result.stderr) - - -if __name__ == "__main__": - unittest.main()