diff --git a/crates/horizon-client/src/creative.rs b/crates/horizon-client/src/creative.rs index b696348f2..7f67aa7e2 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,52 @@ 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 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 823f76cd6..dcc1c667c 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -591,6 +591,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>, @@ -3443,7 +3459,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) } @@ -3454,6 +3476,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; } @@ -3484,6 +3509,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)) @@ -3593,6 +3623,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; @@ -3719,30 +3775,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); @@ -4204,7 +4237,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.keys.contains(&KeyCode::ControlLeft); + let all = drop_whole_stack(&self.physical_keys); self.menu_click(Some(slot), i32::from(all), Input::Throw) } _ => false, @@ -4307,16 +4340,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) @@ -7182,63 +7220,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 @@ -7251,11 +7326,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 @@ -7271,14 +7363,30 @@ 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); + select_creative_tab(&mut self.creative_screen, &mut self.inv_clicks, 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.has_shift_down() { + 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; + self.inv_clicks = Default::default(); + } else if self.slot_at(&batch, cx, cy).is_some() || self.clicked_outside(&batch) { + self.inventory_press(right); + } + return; + } // Holding the scrollbar scrolls as the mouse moves (mouseDragged). if !right && self @@ -7593,7 +7701,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) @@ -7784,7 +7892,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. @@ -7830,7 +7939,7 @@ impl Game<'_> { self.using = None; } KeyCode::KeyQ if self.screen == Screen::Playing => { - let all = self.keys.contains(&KeyCode::ControlLeft); + let all = drop_whole_stack(&self.physical_keys); self.drop_held(all); } KeyCode::Digit1 @@ -8122,6 +8231,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 { @@ -8277,6 +8388,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_mouse_release(button == MouseButton::Right); + } Screen::Creative if button == MouseButton::Left => { self.creative_screen.release(); } @@ -9075,3 +9189,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/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)) }), )