Files
bworld/client/gui/gui_screen.ts
T
2026-10-31 17:35:27 -03:00

282 lines
9.3 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import { SLOT_SIZE } from "$/common/constants.ts";
import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts";
import { click_slot } from "$/common/inventory.ts";
import { ClientMessage, CRAFTING_RESULT_SLOT, PLAYER_MENU } from "$/common/protocol.ts";
import { point_inside_rec } from "../../common/utils.ts";
import { InputManager } from "../input_manager.ts";
import { ClientInventories } from "../inventory.ts";
import { canvas, draw_rect, draw_texture_region, Texture } from "../renderer/mod.ts";
import { AssetManager } from "../assets.ts";
import { get_sprite_region } from "../sprites.ts";
import { draw_fluid, draw_item } from "../rendering/render_utils.ts";
import { draw_tooltip, item_tooltip, tank_tooltip } from "./tooltip.ts";
import { ModGraphics } from "./mod_graphics.ts";
import { inventory_overlays } from "../mods.ts";
import type { ClientFluidSlot, ClientMenu, ClientSlot } from "../menu.ts";
// how far past the slot's edge its highlight goes
const HIGHLIGHT_OVERHANG = 3 * GUI_SCALE;
export abstract class GuiScreen {
x: number = 0;
y: number = 0;
abstract on_tick(_delta: number): void;
abstract on_render(): void;
// the player closed it
abstract on_close(): void;
// it's gone for any reason, after on_close when the player closed it
on_removed(): void {}
// something in it has the keyboard, like a text field, so keybinds (inventory, drop) don't fire
get typing(): boolean {
return false;
}
close_on_escape(): boolean {
return true;
}
}
// a menu's screen, like minecraft's AbstractContainerScreen: in the middle of the window, it draws the menu's slots and
// fluid slots, the item on the cursor and tooltips, and sends clicks. the server owns what's in the slots: clicks are guessed
// locally so they feel instant, then whatever the server syncs back wins. subclasses draw the rest
export abstract class GuiMenuScreen extends GuiScreen {
inventories: ClientInventories;
send: (message: ClientMessage) => void;
menu: ClientMenu;
// in gui pixels
width: number;
height: number;
hovering: ClientSlot | undefined;
hovering_fluid: ClientFluidSlot | undefined;
// what the screen and mods draw with, in gui pixels
graphics = new ModGraphics();
constructor(
inventories: ClientInventories,
send: (message: ClientMessage) => void,
menu: ClientMenu,
width: number,
height: number,
) {
super();
this.inventories = inventories;
this.send = send;
this.menu = menu;
this.width = width;
this.height = height;
}
override get typing() {
return this.graphics.typing;
}
on_tick(_delta: number): void {
this.x = Math.floor((canvas.width - this.width * GUI_SCALE) / 2);
this.y = Math.floor((canvas.height - this.height * GUI_SCALE) / 2);
}
// whether the world behind it is darkened, and mods' inventory overlays show on it
get dim_background(): boolean {
return true;
}
get inventory_overlays(): boolean {
return true;
}
// under the slots, from the screen's top left
abstract render_background(g: ModGraphics): void;
// over the slots
abstract render_labels(g: ModGraphics): void;
on_render(): void {
if (this.dim_background) draw_rect(0, 0, canvas.width, canvas.height, [0, 0, 0, 0.8]);
const g = this.graphics;
g.begin();
g.origin_x = this.x;
g.origin_y = this.y;
this.render_background(g);
this.#render_slots();
this.render_labels(g);
// mods' overlays go on top and get the mouse first, then the slots get what they left
g.origin_x = g.origin_y = 0;
if (this.inventory_overlays) this.#render_overlays();
if (g.mouse_blocked()) {
this.hovering = undefined;
this.hovering_fluid = undefined;
} else {
this.handle_interaction();
}
const mouse = InputManager.get_mouse_position();
if (this.inventories.cursor.item) {
draw_item(this.inventories.cursor.item, mouse.x, mouse.y);
} else if (this.hovering) {
// what's in the slot under the mouse, while not carrying anything
const item = this.hovering.container.get_item(this.hovering.index);
if (item) {
draw_tooltip(item_tooltip(item), mouse.x, mouse.y);
}
} else if (this.hovering_fluid) {
draw_tooltip(tank_tooltip(this.hovering_fluid.tank, this.hovering_fluid.output === true), mouse.x, mouse.y);
}
g.end();
}
#slot_x(slot: { x: number }) {
return this.x + slot.x * GUI_SCALE;
}
#slot_y(slot: { y: number }) {
return this.y + slot.y * GUI_SCALE;
}
#render_slots() {
for (const slot of this.menu.slots) {
draw_gui_sprite("slot", this.#slot_x(slot), this.#slot_y(slot));
}
// like modern industrialization's, an empty one shows a droplet
for (const slot of this.menu.fluid_slots) {
draw_gui_sprite(slot.tank.fluid ? "slot" : "fluid_slot", this.#slot_x(slot), this.#slot_y(slot));
}
// like minecraft's, the highlight is a bit bigger than the slot, part of it under the item and part over
const highlight = (sprite: "slot_highlight_back" | "slot_highlight_front") => {
const hovered = this.hovering ?? this.hovering_fluid;
if (hovered) {
const x = this.#slot_x(hovered) - HIGHLIGHT_OVERHANG;
draw_gui_sprite(sprite, x, this.#slot_y(hovered) - HIGHLIGHT_OVERHANG);
}
};
highlight("slot_highlight_back");
// the whole slot in its fluid while there's any, whatever the amount, like modern industrialization's
for (const slot of this.menu.fluid_slots) {
if (slot.tank.fluid) draw_fluid(slot.tank.fluid, this.#slot_x(slot), this.#slot_y(slot));
}
for (const slot of this.menu.slots) {
const item = slot.container.get_item(slot.index);
if (item) {
draw_item(item, this.#slot_x(slot), this.#slot_y(slot));
} else if (slot.icon) {
// where an item's sprite would be
const icon = get_sprite_region(slot.icon);
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
const [x, y, size] = [this.#slot_x(slot) + 3, this.#slot_y(slot) + 3, SLOT_SIZE - 6];
draw_texture_region(atlas, icon.x, icon.y, icon.width, icon.height, x, y, size, size);
}
}
highlight("slot_highlight_front");
}
#render_overlays() {
const hovered = this.hovering && this.hovering.container.get_item(this.hovering.index);
const view = {
menu_type: this.menu.id === PLAYER_MENU ? undefined : this.menu.type,
x: this.x / GUI_SCALE,
y: this.y / GUI_SCALE,
width: this.width,
height: this.height,
hovered_item: hovered ? { id: hovered.type_id, count: hovered.amount, data: hovered.data } : undefined,
};
for (const entry of [...inventory_overlays]) {
try {
entry.overlay.on_render(this.graphics, view);
} catch (e) {
// a broken overlay is taken off, not left to throw every frame
console.error(`[${entry.mod}] inventory overlay threw, removing it:`, e);
inventory_overlays.splice(inventory_overlays.indexOf(entry), 1);
}
}
}
override on_removed(): void {
if (!this.inventory_overlays) return;
for (const entry of [...inventory_overlays]) {
try {
entry.overlay.on_close?.();
} catch (e) {
console.error(`[${entry.mod}] inventory overlay's on_close threw, removing it:`, e);
inventory_overlays.splice(inventory_overlays.indexOf(entry), 1);
}
}
}
on_close(): void {
this.send({ type: "close_menu", menu: this.menu.id });
// the server puts these back in the inventory, show it right away
const { inventory, crafting, cursor } = this.inventories;
if (this.menu.id === PLAYER_MENU) {
for (let i = 0; i < CRAFTING_RESULT_SLOT; i++) {
const item = crafting.get_item(i);
if (item) {
inventory.add_item(item);
crafting.set_item(i, undefined);
}
}
crafting.set_item(CRAFTING_RESULT_SLOT, undefined);
}
if (cursor.item) {
inventory.add_item(cursor.item);
cursor.item = undefined;
}
}
handle_interaction() {
this.hovering = undefined;
this.hovering_fluid = undefined;
const mouse = InputManager.get_mouse_position();
for (const [index, slot] of this.menu.fluid_slots.entries()) {
const [x, y] = [this.#slot_x(slot), this.#slot_y(slot)];
if (!point_inside_rec(mouse.x, mouse.y, x, y, SLOT_SIZE, SLOT_SIZE)) {
continue;
}
this.hovering_fluid = slot;
for (const button of [0, 2]) {
if (InputManager.is_mouse_pressed(button)) {
InputManager.consume_mouse(button);
// what the cursor's container turns into depends on the tank, the server says
this.send({ type: "click_fluid", menu: this.menu.id, slot: index });
}
}
return;
}
for (const [index, slot] of this.menu.slots.entries()) {
const [x, y] = [this.#slot_x(slot), this.#slot_y(slot)];
if (!point_inside_rec(mouse.x, mouse.y, x, y, SLOT_SIZE, SLOT_SIZE)) {
continue;
}
this.hovering = slot;
for (const button of [0, 2]) {
if (InputManager.is_mouse_pressed(button)) {
InputManager.consume_mouse(button);
this.click(index, button);
return;
}
}
}
}
click(index: number, button: number) {
const slot = this.menu.slots[index];
const shift = InputManager.is_key_down("ShiftLeft") || InputManager.is_key_down("ShiftRight");
this.send({ type: "click", menu: this.menu.id, slot: index, button, shift });
// output slots depend on server side things like recipes, and where a shift click goes on slot filters only the
// server has, just wait for the server
if (!slot.output && !shift) {
click_slot(slot.container, slot.index, this.inventories.cursor, button, slot.max_count);
}
}
}