diff --git a/crates/horizon-client/src/creative.rs b/crates/horizon-client/src/creative.rs index b1eddd48b..6fd527e9e 100644 --- a/crates/horizon-client/src/creative.rs +++ b/crates/horizon-client/src/creative.rs @@ -96,6 +96,26 @@ impl Creative { _ => None, } } + /// Game slot id of an item tab's first display slot (ItemPickerMenu's + /// `CustomCreativeSlot`s, 9 by 5); its hotbar row keeps ids 0..9. + pub const DISPLAY: usize = 900; + + /// Visible slots of an item tab (game slot ids). + pub fn tab_slots() -> impl Iterator { + (Self::DISPLAY..Self::DISPLAY + 45).chain(0..9) + } + + /// ItemPickerMenu's slot positions. + pub fn tab_slot_position(slot: usize) -> Option<(f32, f32)> { + match slot { + 0..9 => Some((9.0 + slot as f32 * 18.0, 112.0)), + _ if (Self::DISPLAY..Self::DISPLAY + 45).contains(&slot) => { + let i = slot - Self::DISPLAY; + Some((9.0 + (i % 9) as f32 * 18.0, 18.0 + (i / 9) as f32 * 18.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; @@ -387,10 +407,172 @@ fn split_id(id: &str) -> (String, String) { None => ("minecraft".to_owned(), id.to_owned()), } } +/// What a click on an item tab's display slot does besides changing the +/// carried stack. +#[derive(Clone, PartialEq, Debug)] +pub enum DisplayClick { + Nothing, + /// SWAP: the hotbar slot gets a full stack of the item. + Hotbar(usize, ItemStack), + /// THROW: a copy of the item is dropped. + Drop(ItemStack), +} + +/// `CreativeModeInventoryScreen.slotClicked` on a display slot +/// (`slot.container == CONTAINER`): `shown` is the slot's item (None past +/// the tab's end) and `max` gives an item's max stack size. The tab's items +/// are never changed: clicks make copies. +pub fn click_display( + carried: &mut Option, + shown: Option<&ItemStack>, + max: impl Fn(&ItemStack) -> u32, + button: i32, + input: crate::menu::Input, +) -> DisplayClick { + use crate::menu::Input; + let copy = |s: &ItemStack, count: u32| ItemStack { count, ..s.clone() }; + let quick_key = input == Input::QuickMove; + match input { + Input::Swap => { + return match shown { + Some(s) => DisplayClick::Hotbar(button as usize, copy(s, max(s))), + None => DisplayClick::Nothing, + }; + } + Input::Clone => { + if carried.is_none() + && let Some(s) = shown + { + *carried = Some(copy(s, max(s))); + } + return DisplayClick::Nothing; + } + Input::Throw => { + return match shown { + Some(s) => DisplayClick::Drop(copy(s, if button == 0 { 1 } else { max(s) })), + None => DisplayClick::Nothing, + }; + } + _ => {} + } + match (carried.as_mut(), shown) { + (Some(c), Some(s)) if c.item == s.item && c.extra == s.extra && c.damage == s.damage => { + // isSameItemSameComponents: grow, fill or shrink what is carried. + if button == 0 { + if quick_key { + c.count = max(c); + } else if c.count < max(c) { + c.count += 1; + } + } else { + c.count -= 1; + } + } + (None, Some(s)) => { + *carried = Some(copy(s, if quick_key { max(s) } else { s.count })); + } + (Some(c), _) => { + if button == 0 { + *carried = None; + } else { + c.count -= 1; + } + } + (None, None) => {} + } + if carried.as_ref().is_some_and(|c| c.count == 0) { + *carried = None; + } + DisplayClick::Nothing +} + #[cfg(test)] mod tests { use super::*; + fn stack(item: &str, count: u32) -> ItemStack { + ItemStack { + item: format!("minecraft:{item}").into(), + count, + damage: 0, + extra: None, + } + } + + /// One click on a display slot showing one stone (max 64; pearls 16). + fn display( + carried: Option, + button: i32, + input: crate::menu::Input, + ) -> (Option, DisplayClick) { + let mut carried = carried; + let max = |s: &ItemStack| if s.item.ends_with("pearl") { 16 } else { 64 }; + let effect = click_display(&mut carried, Some(&stack("stone", 1)), max, button, input); + (carried, effect) + } + + #[test] + fn display_slot_clicks_follow_slot_clicked() { + use crate::menu::Input::*; + // An empty hand takes the shown stack, or a full one with Shift. + assert_eq!(display(None, 0, Pickup).0, Some(stack("stone", 1))); + assert_eq!(display(None, 1, Pickup).0, Some(stack("stone", 1))); + assert_eq!(display(None, 0, QuickMove).0, Some(stack("stone", 64))); + // The same item grows by one (left), fills (Shift) or shrinks (right). + assert_eq!( + display(Some(stack("stone", 5)), 0, Pickup).0, + Some(stack("stone", 6)) + ); + assert_eq!( + display(Some(stack("stone", 64)), 0, Pickup).0, + Some(stack("stone", 64)) + ); + assert_eq!( + display(Some(stack("stone", 5)), 0, QuickMove).0, + Some(stack("stone", 64)) + ); + assert_eq!( + display(Some(stack("stone", 5)), 1, Pickup).0, + Some(stack("stone", 4)) + ); + assert_eq!(display(Some(stack("stone", 1)), 1, QuickMove).0, None); + // Another item is deleted (left) or loses one (right). + assert_eq!(display(Some(stack("ender_pearl", 9)), 0, Pickup).0, None); + assert_eq!(display(Some(stack("ender_pearl", 9)), 0, QuickMove).0, None); + assert_eq!( + display(Some(stack("ender_pearl", 9)), 1, Pickup).0, + Some(stack("ender_pearl", 8)) + ); + // Number keys fill a hotbar slot, Q drops copies, pick block clones. + assert_eq!( + display(None, 3, Swap).1, + DisplayClick::Hotbar(3, stack("stone", 64)) + ); + assert_eq!( + display(None, 0, Throw).1, + DisplayClick::Drop(stack("stone", 1)) + ); + assert_eq!( + display(None, 1, Throw).1, + DisplayClick::Drop(stack("stone", 64)) + ); + assert_eq!(display(None, 2, Clone).0, Some(stack("stone", 64))); + assert_eq!( + display(Some(stack("dirt", 2)), 2, Clone).0, + Some(stack("dirt", 2)) + ); + } + + #[test] + fn an_empty_display_slot_deletes_or_shrinks_the_carried_stack() { + use crate::menu::Input::Pickup; + let mut carried = Some(stack("dirt", 3)); + click_display(&mut carried, None, |_| 64, 1, Pickup); + assert_eq!(carried, Some(stack("dirt", 2))); + click_display(&mut carried, None, |_| 64, 0, Pickup); + assert_eq!(carried, None); + } + fn creative(items: usize) -> Creative { let mut stacks: Vec = ["anvil", "crafting_table", "wooden_hoe"] .iter() diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index fb53ba984..03ea52642 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -3837,8 +3837,12 @@ impl Game<'_> { fn creative_inventory_open(&self) -> bool { self.screen == Screen::Creative && self.creative_screen.tab().inventory } + /// An item tab of the creative screen (its ItemPickerMenu). + fn creative_tab_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() { + if self.screen == Screen::Creative { return Self::creative_origin(batch); } let (x, y) = batch.centered_origin(176.0, self.screen_height()); @@ -3854,6 +3858,9 @@ impl Game<'_> { if self.creative_inventory_open() { return Creative::inventory_slots().collect(); } + if self.creative_tab_open() { + return Creative::tab_slots().collect(); + } if let Some(v) = self.station_slots().or_else(|| self.mount_slots()) { return v; } @@ -3892,6 +3899,11 @@ impl Game<'_> { { return position; } + if self.creative_tab_open() + && let Some(position) = Creative::tab_slot_position(i) + { + return position; + } if let Some(p) = self .station_slot_position(i) .or_else(|| self.machine_slot_position(i)) @@ -4250,8 +4262,35 @@ impl Game<'_> { button: i32, input: horizon_client::menu::Input, ) -> bool { + if let Some(slot) = slot + && slot >= Creative::DISPLAY + && self.creative_tab_open() + { + self.creative_display_click(slot, button, input); + return true; + } self.menu_ops(&[(slot, button, input)]) } + /// A click on an item tab's display slot: the screen's own handling + /// (`creative::click_display`), never the menu's. + fn creative_display_click( + &mut self, + slot: usize, + button: i32, + input: horizon_client::menu::Input, + ) { + use horizon_client::creative::{DisplayClick, click_display}; + let items = self.creative_screen.items(&self.res.names); + let first = self.creative_screen.first_row(&self.res.names); + let shown = items.get(first * 9 + slot - Creative::DISPLAY); + let catalog = &self.res.items; + let max = |s: &ItemStack| catalog.get(&s.item).map_or(64, |i| i.max_stack); + match click_display(&mut self.carried, shown, max, button, input) { + DisplayClick::Hotbar(i, stack) => self.inventory[i] = Some(stack), + DisplayClick::Drop(stack) => self.player_drop(stack), + DisplayClick::Nothing => {} + } + } /// Several clicks on one menu instance (a quick craft drag is three). fn menu_ops(&mut self, ops: &[(Option, i32, horizon_client::menu::Input)]) -> bool { use horizon_client::menu::Input; @@ -4367,7 +4406,12 @@ impl Game<'_> { .station .as_ref() .is_some_and(|s| s.kind == stations::Kind::Merchant); - let (kind, game_of): (Kind, Vec>) = if merchant_open { + let (kind, game_of): (Kind, Vec>) = if self.creative_tab_open() { + // ItemPickerMenu: the display slots (clicked by the screen), then the hotbar. + let mut v = vec![None; 45]; + v.extend((0..9).map(Some)); + (Kind::ItemPicker, v) + } else if merchant_open { // MerchantMenu: two payment slots, the result, then the inventory. let mut v: Vec> = vec![ Some(stations::INPUT), @@ -4662,8 +4706,11 @@ impl Game<'_> { && !shift && slot != 200 { + // An item tab's display slots take no drags or PICKUP_ALL. + let display = slot >= Creative::DISPLAY; let now = std::time::Instant::now(); let double = !right + && !display && self.carried.is_some() && self.last_inv_click.is_some_and(|(at, last, was_right)| { last == slot && !was_right && now.duration_since(at).as_millis() < 250 @@ -4676,10 +4723,25 @@ impl Game<'_> { return; } if self.carried.is_some() { - self.inv_drag = Some((i32::from(right), vec![slot])); + let first = if display { Vec::new() } else { vec![slot] }; + self.inv_drag = Some((i32::from(right), first)); return; } } + if self.creative_tab_open() { + // CreativeModeInventoryScreen goes through the same mouseClicked: + // a click now, or a drag that its release ends. + match input { + Some(input) => { + self.menu_click(slot, i32::from(right), input); + } + None if self.inv_clicks.quick_crafting().is_some() => { + self.inv_drag = Some((i32::from(right), Vec::new())); + } + None => {} + } + return; + } self.click_inventory(right); } /// Mouse movement while a quick craft drag is running. @@ -4693,6 +4755,7 @@ impl Game<'_> { let batch = HudBatch::new([r.config.width, r.config.height]); if let Some(slot) = self.slot_at(&batch, self.cursor.0, self.cursor.1) && slot != 200 + && slot < Creative::DISPLAY && !slots.contains(&slot) && let Some((_, slots)) = self.inv_drag.as_mut() { @@ -4730,7 +4793,7 @@ 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, height) = if self.creative_inventory_open() { + let (shift, width, height) = if self.screen == Screen::Creative { (0.0, Creative::WIDTH as i32, Creative::HEIGHT as i32) } else { let (shift, width) = match self.station.as_ref().map(|s| s.kind) { @@ -7980,45 +8043,8 @@ impl Game<'_> { { return; } - let shift = self.has_shift_down(); - 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 (x, y) = (ox + 9.0 + c as f32 * 18.0, oy + 18.0 + r as f32 * 18.0); - if !(cx >= x && cx < x + 16.0 && cy >= y && cy < y + 16.0) { - continue; - } - // Clicking the grid with an item deletes it; otherwise take a stack. - if self.carried.take().is_some() { - return; - } - let Some(stack) = items.get((first + r) * 9 + c) else { - return; - }; - let max = self.res.items.get(&stack.item).map_or(64, |i| i.max_stack); - let stack = ItemStack { - count: if right { 1 } else { max }, - ..stack.clone() - }; - if shift { - let _ = self.give(stack); - } else { - self.carried = Some(stack); - } - return; - } - } - for c in 0..9 { - let (x, y) = (ox + 9.0 + c as f32 * 18.0, oy + 112.0); - if cx >= x && cx < x + 16.0 && cy >= y && cy < y + 16.0 { - std::mem::swap(&mut self.carried, &mut self.inventory[c]); - return; - } - } - // Clicking outside the panel throws the carried stack away. - if !(cx >= ox && cx < ox + Creative::WIDTH && cy >= oy && cy < oy + Creative::HEIGHT) { - self.carried = None; + if self.slot_at(&batch, cx, cy).is_some() || self.clicked_outside(&batch) { + self.inventory_press(right); } } /// Copy the live player and world state into the `Level` that gets saved. @@ -8524,7 +8550,8 @@ impl Game<'_> { | KeyCode::Digit9 | KeyCode::KeyF | KeyCode::KeyQ - if (self.screen == Screen::Inventory || self.creative_inventory_open()) + if (self.screen == Screen::Inventory + || (self.screen == Screen::Creative && !self.creative_screen.tab().search)) && 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(""), @@ -8879,7 +8906,7 @@ impl ApplicationHandler for Game<'_> { if self.screen == Screen::Inventory { self.loom_drag(); } - if self.screen == Screen::Inventory || self.creative_inventory_open() { + if matches!(self.screen, Screen::Inventory | Screen::Creative) { self.inventory_drag_move(); } if let Some(title) = &mut self.title @@ -9078,12 +9105,12 @@ impl ApplicationHandler for Game<'_> { Screen::Creative if pressed => { self.click_creative(button == MouseButton::Right); } - Screen::Creative if self.creative_screen.tab().inventory => { + Screen::Creative => { + if button == MouseButton::Left { + self.creative_screen.release(); + } self.inventory_mouse_release(button == MouseButton::Right); } - Screen::Creative if button == MouseButton::Left => { - self.creative_screen.release(); - } Screen::Paused if pressed && button == MouseButton::Left && self.pause_menu => { diff --git a/crates/horizon-client/src/menu.rs b/crates/horizon-client/src/menu.rs index 8c3e4e340..19143852f 100644 --- a/crates/horizon-client/src/menu.rs +++ b/crates/horizon-client/src/menu.rs @@ -73,6 +73,9 @@ pub enum Kind { Loom, /// `MerchantMenu`: two payment slots, the result, then the inventory. Merchant, + /// `CreativeModeInventoryScreen.ItemPickerMenu`: an item tab's 45 + /// display slots, then the hotbar. + ItemPicker, } impl Kind { @@ -94,6 +97,7 @@ impl Kind { "crafter_be" => Self::CrafterBlock, "loom" => Self::Loom, "merchant" => Self::Merchant, + "item_picker" => Self::ItemPicker, _ => return None, }) } @@ -129,6 +133,9 @@ enum Role { LoomResult, /// `MerchantResultSlot`. MerchantResult, + /// `CustomCreativeSlot`: a creative tab's display slot, which the + /// screen clicks itself; drags and PICKUP_ALL pass it by. + CreativeDisplay, } #[derive(Clone, Copy)] @@ -308,6 +315,16 @@ impl Menu { player(&mut slots); data = 1; } + Kind::ItemPicker => { + containers[MAIN] = vec![None; 45]; + slots.extend(main(45, Role::CreativeDisplay)); + // addInventoryHotbarSlots. + slots.extend((0..9).map(|index| Slot { + container: PLAYER, + index, + role: Role::Plain, + })); + } Kind::Merchant => { containers[MAIN] = vec![None; 2]; slots.extend(main(2, Role::Plain)); @@ -574,7 +591,7 @@ impl Menu { fn may_place(&self, env: &Env, s: usize, stack: &ItemStack) -> bool { match self.slots[s].role { - Role::Plain => true, + Role::Plain | Role::CreativeDisplay => true, Role::Armor(eq) => env .items .get(&stack.item) @@ -1034,6 +1051,13 @@ impl Menu { let mut check_count = true; let mut after_take = false; match self.kind { + Kind::ItemPicker => { + // ItemPickerMenu.quickMoveStack: a hotbar stack is deleted. + if index >= total - 9 { + self.put(env, index, None); + } + return None; + } Kind::Chest(rows) => { check_count = false; let n = rows * 9; @@ -1357,6 +1381,7 @@ impl Menu { if self.quick_replace(s, &carried, env) && self.may_place(env, s, &carried) && (self.quick_type == 2 || carried.count as usize > self.quick_slots.len()) + && self.can_drag_to(s) && !self.quick_slots.contains(&s) { self.quick_slots.push(s); @@ -1377,6 +1402,7 @@ impl Menu { && self.may_place(env, s, &carried) && (self.quick_type == 2 || carried.count as usize >= self.quick_slots.len()) + && self.can_drag_to(s) { let have = self.slot(s).map_or(0, |c| c.count); let max = @@ -1626,6 +1652,11 @@ impl Menu { } /// The PICKUP_ALL branch (double click). + /// `canDragTo`: ItemPickerMenu keeps drags off its display slots. + fn can_drag_to(&self, s: usize) -> bool { + self.slots[s].role != Role::CreativeDisplay + } + fn pickup_all(&mut self, env: &Env, s: usize, button: i32) { let Some(carried) = self.carried.clone() else { return; @@ -1645,6 +1676,7 @@ impl Menu { let have = self.carried.clone().expect("some"); // MerchantMenu.canTakeItemForPickAll is always false. if self.kind != Kind::Merchant + && self.slots[t].role != Role::CreativeDisplay && self.has_item(t) && self.quick_replace(t, &have, env) && self.may_pickup(env, t) @@ -1810,3 +1842,80 @@ impl Menu { } } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::{banner::Banners, brewing::Brewing, crafting::Recipes}; + + fn stack(item: &str, count: u32) -> Option { + Some(ItemStack { + item: format!("minecraft:{item}").into(), + count, + damage: 0, + extra: None, + }) + } + + /// An item tab's ItemPickerMenu: the 45 display slots show `shown`. + fn with_picker(shown: &str, test: impl FnOnce(&Env, &mut Menu)) { + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../.local/horizon-client/26.3"); + if !root.join("items.json").exists() { + return; + } + let items = Items::catalog(&root).unwrap(); + let recipes = Recipes::load(&root, &items).unwrap(); + let brewing = Brewing::load(&root, &items); + let banners = Banners::load(&root); + let env = Env { + items: &items, + recipes: &recipes, + brewing: &brewing, + banners: &banners, + trade: None, + }; + let mut menu = Menu::new(Kind::ItemPicker); + menu.creative = true; + assert_eq!(menu.slot_count(), 54); + for i in 0..45 { + menu.set_slot(&env, i, stack(shown, 1)); + } + test(&env, &mut menu); + } + + #[test] + fn item_picker_quick_move_deletes_a_hotbar_stack() { + with_picker("stone", |env, menu| { + menu.set_slot(env, 45, stack("dirt", 12)); + menu.set_slot(env, 46, stack("dirt", 5)); + menu.click(env, 45, 0, Input::QuickMove).unwrap(); + assert_eq!(menu.player_item(0), None); + // Nothing moves anywhere: the other hotbar stack and the tab stay. + assert_eq!(menu.player_item(1), stack("dirt", 5).as_ref()); + assert_eq!(menu.slot(0), stack("stone", 1).as_ref()); + assert_eq!(menu.carried, None); + }); + } + + #[test] + fn item_picker_drags_and_pickup_all_skip_the_display_slots() { + with_picker("dirt", |env, menu| { + // A right-drag over a display slot and two hotbar slots. + menu.carried = stack("dirt", 10); + for (slot, header) in [(-999, 0), (3, 1), (45, 1), (47, 1), (-999, 2)] { + menu.click(env, slot, header | (1 << 2), Input::QuickCraft) + .unwrap(); + } + assert_eq!(menu.player_item(0), stack("dirt", 1).as_ref()); + assert_eq!(menu.player_item(2), stack("dirt", 1).as_ref()); + assert_eq!(menu.slot(3), stack("dirt", 1).as_ref()); + assert_eq!(menu.carried, stack("dirt", 8)); + // A double click gathers from the hotbar only. + menu.click(env, 48, 0, Input::PickupAll).unwrap(); + assert_eq!(menu.carried, stack("dirt", 10)); + assert_eq!(menu.player_item(0), None); + assert_eq!(menu.slot(0), stack("dirt", 1).as_ref()); + }); + } +}