Files
bworld/client/gui/gui_player_inventory.ts
T

39 lines
1.5 KiB
TypeScript

import { GuiMenuScreen } from "./gui_screen.ts";
import { ClientInventories } from "../inventory.ts";
import { ClientMessage, PLAYER_MENU_EXTRA } from "$/common/protocol.ts";
import { player_menu } from "../menu.ts";
import type { ModGraphics } from "./mod_graphics.ts";
const WIDTH = 176;
const HEIGHT = 166;
const SLOT = 18;
// around the slots, like minecraft's
const MARGIN = 7;
// menu 0's screen: the crafting grid over the inventory, and any slots mods added. the recipes live on the server,
// which fills in the result
export class GuiPlayerInventory extends GuiMenuScreen {
constructor(inventories: ClientInventories, send: (message: ClientMessage) => void) {
super(inventories, send, player_menu(inventories), WIDTH, HEIGHT);
}
render_background(g: ModGraphics): void {
g.panel(0, 0, WIDTH, HEIGHT);
// the slots mods added outside the panel get one of their own, around them with the same margin
const outside = this.menu.slots.slice(PLAYER_MENU_EXTRA)
.filter((s) => s.x + SLOT < 0 || s.x > WIDTH || s.y + SLOT < 0 || s.y > HEIGHT);
if (outside.length > 0) {
const left = Math.min(...outside.map((s) => s.x)) - MARGIN;
const top = Math.min(...outside.map((s) => s.y)) - MARGIN;
const right = Math.max(...outside.map((s) => s.x)) + SLOT + MARGIN;
const bottom = Math.max(...outside.map((s) => s.y)) + SLOT + MARGIN;
g.panel(left, top, right - left, bottom - top);
}
}
render_labels(g: ModGraphics): void {
g.text(this.menu.title, 26, 6);
g.text("Inventory", 8, 73);
}
}