Keep the inventory tab in the creative screen

This commit is contained in:
Cosmic
2026-10-13 19:15:54 -04:00
parent b2462d54c4
commit 9f0df29d6c
4 changed files with 351 additions and 80 deletions
+52
View File
@@ -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<Item = usize> {
(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);
+179 -80
View File
@@ -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<usize> {
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<String> = 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();
}
@@ -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<u8>, last_inv_click: Option<u8> }
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()
+62
View File
@@ -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<u8> { 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()