Change the gui

This commit is contained in:
2026-09-27 14:52:30 -03:00
parent d0cbec1846
commit 29e61a235b
20 changed files with 451 additions and 359 deletions
+14 -29
View File
@@ -3,11 +3,15 @@ import { canvas, draw_rect, draw_texture_region, Texture } from "$/client/render
import { AssetManager } from "../assets.ts";
import { SLOT_SIZE, TEXTURE_SIZE } from "$/common/constants.ts";
import { ClientMessage, ScreenLayout } from "$/common/protocol.ts";
import { draw_nine_slice } from "../rendering/render_utils.ts";
import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts";
import { get_sprite_region } from "$/client/sprites.ts";
import { ClientInventories } from "../inventory.ts";
const PADDING = 10;
// around the slots, and between the hotbar, the rest of the inventory and what's below, like minecraft's
const MARGIN = 7 * GUI_SCALE;
const GAP = 4 * GUI_SCALE;
// where the screen's own slots start, below the player's
const AREA_Y = MARGIN + GAP * 2 + SLOT_SIZE * 4;
// a screen opened by the server (chest, furnace, ...), drawn from the layout it sent
export class GuiContainer extends GuiInventoryScreen {
@@ -24,16 +28,15 @@ export class GuiContainer extends GuiInventoryScreen {
this.layout = layout;
this.properties = properties;
this.inventory_width = PADDING * 2 + SLOT_SIZE * 9;
this.inventory_height = PADDING * 4 + SLOT_SIZE * (4 + layout.rows);
this.inventory_width = MARGIN * 2 + SLOT_SIZE * 9;
this.inventory_height = MARGIN * 2 + GAP * 2 + SLOT_SIZE * (4 + layout.rows);
add_player_hotbar(this, PADDING, PADDING);
add_player_inventory(this, PADDING, PADDING * 2 + SLOT_SIZE);
add_player_hotbar(this, MARGIN, MARGIN);
add_player_inventory(this, MARGIN, MARGIN + GAP + SLOT_SIZE);
const area_y = PADDING * 3 + SLOT_SIZE * 4;
for (const slot of layout.slots) {
this.slots.push(
new Slot("screen", slot.index, PADDING + slot.x * SLOT_SIZE, area_y + slot.y * SLOT_SIZE, slot.output),
new Slot("screen", slot.index, MARGIN + slot.x * SLOT_SIZE, AREA_Y + slot.y * SLOT_SIZE, slot.output),
);
}
}
@@ -41,23 +44,7 @@ export class GuiContainer extends GuiInventoryScreen {
override on_render(): void {
draw_rect(0, 0, canvas.width, canvas.height, [0, 0, 0, 0.8]);
const ui = AssetManager.instance.get<Texture>("bworld:ui");
draw_nine_slice(
ui,
160,
0,
16,
16,
4,
4,
4,
4,
this.x,
this.y,
this.inventory_width,
this.inventory_height,
);
draw_gui_sprite("panel", this.x, this.y, this.inventory_width, this.inventory_height);
this.draw_bars();
@@ -66,11 +53,9 @@ export class GuiContainer extends GuiInventoryScreen {
draw_bars() {
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
const area_y = PADDING * 3 + SLOT_SIZE * 4;
for (const bar of this.layout.bars) {
const x = this.x + PADDING + bar.x * SLOT_SIZE;
const y = this.y + area_y + bar.y * SLOT_SIZE;
const x = this.x + MARGIN + bar.x * SLOT_SIZE;
const y = this.y + AREA_Y + bar.y * SLOT_SIZE;
const max = this.properties[bar.max] ?? 0;
const pct = max > 0 ? Math.max(0, Math.min(1, (this.properties[bar.value] ?? 0) / max)) : 0;