Merge pull request #393 from ludovic111/fix/quick-craft-highlight
Drag highlight and counts as AbstractContainerScreen draws them
This commit is contained in:
@@ -646,6 +646,12 @@ pub struct Resources {
|
||||
/// Enchantments, workstation recipes and brewing (game/stations.rs).
|
||||
stations: horizon_client::stations::StationData,
|
||||
}
|
||||
/// A quick craft drag as the screen draws it: each dragged slot's count and
|
||||
/// whether the slot caps it (None: hidden), and what stays in hand.
|
||||
struct DragView {
|
||||
slots: HashMap<usize, Option<(u32, bool)>>,
|
||||
left: Option<u32>,
|
||||
}
|
||||
/// Ctrl+Q reads the physical keyboard, independently of sprint key mappings.
|
||||
fn drop_whole_stack(physical_keys: &HashSet<KeyCode>) -> bool {
|
||||
physical_keys.contains(&KeyCode::ControlLeft) || physical_keys.contains(&KeyCode::ControlRight)
|
||||
@@ -3471,10 +3477,99 @@ impl Game<'_> {
|
||||
// `GuiGraphics.renderItem`: the resolved item model (tints, foil and
|
||||
// special renderers included), then the decorations.
|
||||
self.draw_item_model(batch, stack, x, y);
|
||||
self.draw_item_decorations(batch, stack, x, y);
|
||||
self.draw_item_decorations(batch, stack, x, y, None);
|
||||
}
|
||||
/// `GuiGraphicsExtractor.itemDecorations`: the count and damage bar.
|
||||
fn draw_item_decorations(&mut self, batch: &mut HudBatch, stack: &ItemStack, x: f32, y: f32) {
|
||||
/// `extractSlot` of slot `slot` holding `stack` at (x, y): during a quick
|
||||
/// craft drag a dragged slot shows what it would hold over a white
|
||||
/// highlight (its count yellow when the slot caps it), and hides while
|
||||
/// it is the drag's only slot.
|
||||
fn draw_slot_item(
|
||||
&mut self,
|
||||
batch: &mut HudBatch,
|
||||
drag: Option<&DragView>,
|
||||
slot: usize,
|
||||
stack: Option<ItemStack>,
|
||||
x: f32,
|
||||
y: f32,
|
||||
) {
|
||||
let (stack, capped) = match drag.and_then(|d| d.slots.get(&slot)) {
|
||||
Some(None) => return,
|
||||
Some(Some((count, capped))) => {
|
||||
batch.fill(&self.res.atlas, x, y, 16.0, 16.0, rgba(255, 255, 255, 128));
|
||||
let carried = self
|
||||
.carried
|
||||
.clone()
|
||||
.map(|c| ItemStack { count: *count, ..c });
|
||||
(carried, *capped)
|
||||
}
|
||||
None => (stack, false),
|
||||
};
|
||||
if let Some(stack) = stack {
|
||||
self.draw_item_model(batch, &stack, x, y);
|
||||
let text = capped.then(|| format!("\u{a7}e{}", stack.count));
|
||||
self.draw_item_decorations(batch, &stack, x, y, text);
|
||||
}
|
||||
}
|
||||
/// `extractCarriedItem`: the carried stack at the cursor, showing what a
|
||||
/// quick craft drag over several slots leaves in hand.
|
||||
fn draw_carried(&mut self, batch: &mut HudBatch) {
|
||||
let Some(mut stack) = self.carried.clone() else {
|
||||
return;
|
||||
};
|
||||
if let Some(left) = self.drag_view().and_then(|d| d.left) {
|
||||
if left == 0 {
|
||||
return;
|
||||
}
|
||||
stack.count = left;
|
||||
}
|
||||
self.draw_item(batch, &stack, self.cursor.0 - 8.0, self.cursor.1 - 8.0);
|
||||
}
|
||||
/// The quick craft drag in progress, as the menu engine previews it.
|
||||
fn drag_view(&mut self) -> Option<DragView> {
|
||||
let (ty, slots) = self.inv_drag.clone()?;
|
||||
let mut view = None;
|
||||
self.menu_session(|menu, env, to_menu| {
|
||||
let menu_slots: Option<Vec<usize>> = slots.iter().map(|&g| to_menu(g)).collect();
|
||||
let shown = match menu.quick_craft_preview(env, ty, &menu_slots?) {
|
||||
Some((shown, left)) => {
|
||||
let slots = slots.iter().copied().zip(shown).collect();
|
||||
DragView {
|
||||
slots,
|
||||
left: Some(left),
|
||||
}
|
||||
}
|
||||
// A single dragged slot is hidden.
|
||||
None => DragView {
|
||||
slots: slots.iter().map(|&g| (g, None)).collect(),
|
||||
left: None,
|
||||
},
|
||||
};
|
||||
view = Some(shown);
|
||||
Some(false)
|
||||
});
|
||||
view
|
||||
}
|
||||
/// Whether a quick craft drag takes `slot`
|
||||
/// (`shouldAddSlotToQuickCraft`); screens the menu engine doesn't
|
||||
/// model take every slot.
|
||||
fn quick_craft_accepts(&mut self, slot: usize, ty: i32, dragged: usize) -> bool {
|
||||
let mut accepts = true;
|
||||
self.menu_session(|menu, env, to_menu| {
|
||||
accepts = to_menu(slot).is_some_and(|s| menu.quick_craft_accepts(env, s, ty, dragged));
|
||||
Some(false)
|
||||
});
|
||||
accepts
|
||||
}
|
||||
/// `GuiGraphicsExtractor.itemDecorations`: the count (or `count_text`)
|
||||
/// and damage bar.
|
||||
fn draw_item_decorations(
|
||||
&mut self,
|
||||
batch: &mut HudBatch,
|
||||
stack: &ItemStack,
|
||||
x: f32,
|
||||
y: f32,
|
||||
count_text: Option<String>,
|
||||
) {
|
||||
// GuiGraphicsExtractor.itemDecorations, laid out at the origin and
|
||||
// moved to (x, y). Horizon tracks no item cooldowns to shade yet.
|
||||
let max_damage = self
|
||||
@@ -3484,7 +3579,7 @@ impl Game<'_> {
|
||||
.map_or(0, |info| info.max_damage as i32);
|
||||
let decorations = hud_overlays::ItemDecorations {
|
||||
count: stack.count,
|
||||
count_text: None,
|
||||
count_text,
|
||||
bar: hud_overlays::damage_bar(stack.damage as i32, max_damage),
|
||||
cooldown: 0.0,
|
||||
};
|
||||
@@ -4017,6 +4112,7 @@ impl Game<'_> {
|
||||
.map(|(name, result)| (name.to_owned(), result))
|
||||
}
|
||||
fn draw_inventory_slots(&mut self, batch: &mut HudBatch, ox: f32, oy: f32) {
|
||||
let drag = self.drag_view();
|
||||
for i in self.slots() {
|
||||
let (sx, sy) = self.slot_position(i);
|
||||
if i == 504 && self.offhand.is_none() {
|
||||
@@ -4037,9 +4133,7 @@ impl Game<'_> {
|
||||
);
|
||||
}
|
||||
let stack = self.slot_view(i);
|
||||
if let Some(stack) = stack {
|
||||
self.draw_item(batch, &stack, ox + sx, oy + sy);
|
||||
}
|
||||
self.draw_slot_item(batch, drag.as_ref(), i, stack, ox + sx, oy + sy);
|
||||
}
|
||||
}
|
||||
fn draw_inventory(&mut self, batch: &mut HudBatch) {
|
||||
@@ -4184,9 +4278,7 @@ impl Game<'_> {
|
||||
rgba(255, 255, 255, 128),
|
||||
);
|
||||
}
|
||||
if let Some(stack) = self.carried.clone() {
|
||||
self.draw_item(batch, &stack, self.cursor.0 - 8.0, self.cursor.1 - 8.0);
|
||||
}
|
||||
self.draw_carried(batch);
|
||||
self.draw_slot_tooltip(batch);
|
||||
if self.container.is_none() && self.craft_size == 2 && !self.mount_active() {
|
||||
self.draw_inventory_effects(batch, (ox, oy), 176);
|
||||
@@ -4761,8 +4853,13 @@ impl Game<'_> {
|
||||
return;
|
||||
}
|
||||
if self.carried.is_some() {
|
||||
let first = if display { Vec::new() } else { vec![slot] };
|
||||
self.inv_drag = Some((i32::from(right), first));
|
||||
let ty = i32::from(right);
|
||||
let first = if !display && self.quick_craft_accepts(slot, ty, 0) {
|
||||
vec![slot]
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
self.inv_drag = Some((ty, first));
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -4784,7 +4881,7 @@ impl Game<'_> {
|
||||
}
|
||||
/// Mouse movement while a quick craft drag is running.
|
||||
fn inventory_drag_move(&mut self) {
|
||||
let Some((_, slots)) = self.inv_drag.as_ref() else {
|
||||
let Some((ty, slots)) = self.inv_drag.clone() else {
|
||||
return;
|
||||
};
|
||||
let Some(r) = self.renderer.as_ref() else {
|
||||
@@ -4795,6 +4892,7 @@ impl Game<'_> {
|
||||
&& slot != 200
|
||||
&& slot < Creative::DISPLAY
|
||||
&& !slots.contains(&slot)
|
||||
&& self.quick_craft_accepts(slot, ty, slots.len())
|
||||
&& let Some((_, slots)) = self.inv_drag.as_mut()
|
||||
{
|
||||
slots.push(slot);
|
||||
@@ -7973,17 +8071,18 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
let drag = self.drag_view();
|
||||
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 stack = self.inventory[c].clone();
|
||||
self.draw_slot_item(batch, drag.as_ref(), c, stack.clone(), x, y);
|
||||
if let Some(stack) = stack
|
||||
&& 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;
|
||||
@@ -8009,9 +8108,7 @@ impl Game<'_> {
|
||||
self.tooltip(batch, &t.title, self.cursor.0, self.cursor.1);
|
||||
}
|
||||
}
|
||||
if let Some(stack) = self.carried.clone() {
|
||||
self.draw_item(batch, &stack, self.cursor.0 - 8.0, self.cursor.1 - 8.0);
|
||||
}
|
||||
self.draw_carried(batch);
|
||||
if tab.inventory {
|
||||
if Self::creative_trash_hovered(batch, self.cursor) {
|
||||
self.tooltip(batch, "Destroy Item", self.cursor.0, self.cursor.1);
|
||||
|
||||
@@ -512,8 +512,8 @@ impl Game<'_> {
|
||||
);
|
||||
stack_tip = self.slot_mut(slot).clone();
|
||||
}
|
||||
if let Some(stack) = self.carried.clone() {
|
||||
self.draw_item(batch, &stack, self.cursor.0 - 8.0, self.cursor.1 - 8.0);
|
||||
if self.carried.is_some() {
|
||||
self.draw_carried(batch);
|
||||
} else if let Some(stack) = stack_tip {
|
||||
let lines = self.stack_tooltip(&stack);
|
||||
self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1);
|
||||
|
||||
@@ -1126,7 +1126,7 @@ impl Game<'_> {
|
||||
self.draw_item_model(batch, stack, *x as f32, *y as f32)
|
||||
}
|
||||
ItemCall::Decorations => {
|
||||
self.draw_item_decorations(batch, stack, *x as f32, *y as f32)
|
||||
self.draw_item_decorations(batch, stack, *x as f32, *y as f32, None)
|
||||
}
|
||||
},
|
||||
Call::Tooltip(lines) => {
|
||||
|
||||
@@ -1805,14 +1805,13 @@ impl Game<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
let drag = self.drag_view();
|
||||
for i in self.station_slots().unwrap_or_default() {
|
||||
let Some((sx, sy)) = self.slot_position_any(i) else {
|
||||
continue;
|
||||
};
|
||||
let stack = self.slot_mut(i).clone();
|
||||
if let Some(stack) = stack {
|
||||
self.draw_item(batch, &stack, ox + sx, oy + sy);
|
||||
}
|
||||
self.draw_slot_item(batch, drag.as_ref(), i, stack, ox + sx, oy + sy);
|
||||
}
|
||||
if let Some(slot) = self.slot_at(batch, self.cursor.0, self.cursor.1) {
|
||||
let (sx, sy) = self.slot_position(slot);
|
||||
@@ -1831,8 +1830,8 @@ impl Game<'_> {
|
||||
tooltip = Some(self.stack_tooltip(&stack));
|
||||
}
|
||||
}
|
||||
if let Some(stack) = self.carried.clone() {
|
||||
self.draw_item(batch, &stack, self.cursor.0 - 8.0, self.cursor.1 - 8.0);
|
||||
if self.carried.is_some() {
|
||||
self.draw_carried(batch);
|
||||
} else if let Some(lines) = tooltip {
|
||||
self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1);
|
||||
}
|
||||
|
||||
@@ -761,11 +761,11 @@ impl Game<'_> {
|
||||
}
|
||||
// Slots, hover highlight, carried stack, tooltip.
|
||||
let slots: Vec<usize> = (0..36).chain([INPUT, INPUT + 1, RESULT]).collect();
|
||||
let drag = self.drag_view();
|
||||
for i in slots {
|
||||
let (sx, sy) = self.slot_position(i);
|
||||
if let Some(stack) = self.slot_mut(i).clone() {
|
||||
self.draw_item(batch, &stack, ox + sx, oy + sy);
|
||||
}
|
||||
let stack = self.slot_mut(i).clone();
|
||||
self.draw_slot_item(batch, drag.as_ref(), i, stack, ox + sx, oy + sy);
|
||||
}
|
||||
if let Some(slot) = self.slot_at(batch, self.cursor.0, self.cursor.1) {
|
||||
let (sx, sy) = self.slot_position(slot);
|
||||
@@ -789,8 +789,8 @@ impl Game<'_> {
|
||||
&& local.0 < 186.0 + SHIFT + 22.0 + 1.0
|
||||
&& local.1 >= 35.0 - 1.0
|
||||
&& local.1 < 35.0 + 21.0 + 1.0;
|
||||
if let Some(stack) = self.carried.clone() {
|
||||
self.draw_item(batch, &stack, self.cursor.0 - 8.0, self.cursor.1 - 8.0);
|
||||
if self.carried.is_some() {
|
||||
self.draw_carried(batch);
|
||||
} else if deprecated {
|
||||
let text = self
|
||||
.res
|
||||
|
||||
@@ -1685,6 +1685,60 @@ impl Menu {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `AbstractContainerScreen.shouldAddSlotToQuickCraft`: whether a drag
|
||||
/// of type `ty` over `dragged` slots so far takes slot `s` too.
|
||||
pub fn quick_craft_accepts(&self, env: &Env, s: usize, ty: i32, dragged: usize) -> bool {
|
||||
let Some(carried) = self.carried.as_ref() else {
|
||||
return false;
|
||||
};
|
||||
s < self.slots.len()
|
||||
&& (carried.count as usize > dragged || ty == 2)
|
||||
&& self.quick_replace(s, carried, env)
|
||||
&& self.may_place(env, s, carried)
|
||||
&& self.can_drag_to(s)
|
||||
}
|
||||
|
||||
/// What a quick craft drag of type `ty` over `slots` shows
|
||||
/// (`AbstractContainerScreen.extractSlot` and
|
||||
/// `recalculateQuickCraftRemaining`): each slot's would-be count and
|
||||
/// whether the slot caps it (a yellow count), then the count left in
|
||||
/// hand. Nothing while the drag spans a single slot.
|
||||
pub fn quick_craft_preview(
|
||||
&self,
|
||||
env: &Env,
|
||||
ty: i32,
|
||||
slots: &[usize],
|
||||
) -> Option<(Vec<Option<(u32, bool)>>, u32)> {
|
||||
let carried = self.carried.as_ref()?;
|
||||
if slots.len() < 2 {
|
||||
return None;
|
||||
}
|
||||
let carried_max = Self::max_stack(env, carried);
|
||||
// AbstractContainerMenu.getQuickCraftPlaceCount.
|
||||
let place = match ty {
|
||||
0 => carried.count / slots.len() as u32,
|
||||
1 => 1,
|
||||
2 => carried_max,
|
||||
_ => carried.count,
|
||||
};
|
||||
let mut left = i64::from(if ty == 2 { carried_max } else { carried.count });
|
||||
let mut shown = Vec::with_capacity(slots.len());
|
||||
for &s in slots {
|
||||
let carry = self.slot(s).map_or(0, |i| i.count);
|
||||
let max = carried_max.min(self.slot_max(env, s, carried));
|
||||
if ty != 2 {
|
||||
left -= i64::from((place + carry).min(max)) - i64::from(carry);
|
||||
}
|
||||
shown.push(
|
||||
(s < self.slots.len()
|
||||
&& self.quick_replace(s, carried, env)
|
||||
&& self.can_drag_to(s))
|
||||
.then(|| ((place + carry).min(max), place + carry > max)),
|
||||
);
|
||||
}
|
||||
Some((shown, left.max(0) as u32))
|
||||
}
|
||||
|
||||
/// `canDragTo`: ItemPickerMenu keeps drags off its display slots.
|
||||
fn can_drag_to(&self, s: usize) -> bool {
|
||||
self.slots[s].role != Role::CreativeDisplay
|
||||
@@ -1889,6 +1943,16 @@ mod tests {
|
||||
|
||||
/// An item tab's ItemPickerMenu: the 45 display slots show `shown`.
|
||||
fn with_picker(shown: &str, test: impl FnOnce(&Env, &mut Menu)) {
|
||||
with_menu(Kind::ItemPicker, |env, menu| {
|
||||
assert_eq!(menu.slot_count(), 54);
|
||||
for i in 0..45 {
|
||||
menu.set_slot(env, i, stack(shown, 1));
|
||||
}
|
||||
test(env, menu);
|
||||
});
|
||||
}
|
||||
|
||||
fn with_menu(kind: Kind, 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() {
|
||||
@@ -1905,15 +1969,42 @@ mod tests {
|
||||
banners: &banners,
|
||||
trade: None,
|
||||
};
|
||||
let mut menu = Menu::new(Kind::ItemPicker);
|
||||
let mut menu = Menu::new(kind);
|
||||
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 a_quick_craft_drag_previews_each_slot_and_the_rest_in_hand() {
|
||||
with_menu(Kind::Inventory, |env, menu| {
|
||||
// 9 + 36 player slots: 9 is the first main inventory slot.
|
||||
menu.set_slot(env, 10, stack("stone", 60));
|
||||
menu.set_slot(env, 11, stack("dirt", 1));
|
||||
menu.carried = stack("stone", 20);
|
||||
// shouldAddSlotToQuickCraft: same item or empty, placeable, not the result.
|
||||
assert!(menu.quick_craft_accepts(env, 9, 0, 0));
|
||||
assert!(menu.quick_craft_accepts(env, 10, 0, 1));
|
||||
assert!(!menu.quick_craft_accepts(env, 11, 0, 1));
|
||||
assert!(!menu.quick_craft_accepts(env, 0, 0, 1));
|
||||
// Stone has no place in an armor slot (5).
|
||||
assert!(!menu.quick_craft_accepts(env, 5, 0, 1));
|
||||
// A right drag stops once every carried item has a slot.
|
||||
assert!(!menu.quick_craft_accepts(env, 12, 1, 20));
|
||||
// A left drag over three slots: 6 each, the 60 capped at 64 (yellow), 2 + 4 left.
|
||||
assert_eq!(menu.quick_craft_preview(env, 0, &[9]), None);
|
||||
let (shown, left) = menu.quick_craft_preview(env, 0, &[9, 10, 12]).unwrap();
|
||||
assert_eq!(
|
||||
shown,
|
||||
vec![Some((6, false)), Some((64, true)), Some((6, false))]
|
||||
);
|
||||
assert_eq!(left, 20 - 6 - 4 - 6);
|
||||
// A right drag: one each.
|
||||
let (shown, left) = menu.quick_craft_preview(env, 1, &[9, 10]).unwrap();
|
||||
assert_eq!(shown, vec![Some((1, false)), Some((61, false))]);
|
||||
assert_eq!(left, 18);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn item_picker_quick_move_deletes_a_hotbar_stack() {
|
||||
with_picker("stone", |env, menu| {
|
||||
|
||||
Reference in New Issue
Block a user