Recipe viewer

This commit is contained in:
2026-09-27 01:25:47 -03:00
parent e4c9a2f7f4
commit 7f4a8719de
12 changed files with 849 additions and 13 deletions
+44 -2
View File
@@ -8,6 +8,8 @@ import { ClientInventories } from "../inventory.ts";
import { canvas, Texture } from "../renderer/mod.ts";
import { draw_item, draw_nine_slice } from "../rendering/render_utils.ts";
import { draw_tooltip, item_tooltip } from "./tooltip.ts";
import { ModGraphics } from "./mod_graphics.ts";
import { inventory_overlays } from "../mods.ts";
export class Slot {
container: ContainerKey;
@@ -34,6 +36,11 @@ export abstract class GuiScreen {
abstract on_tick(_delta: number): void;
abstract on_render(): void;
abstract on_close(): void;
// something in it has the keyboard, like a text field, so keybinds (inventory, drop) don't fire
get typing(): boolean {
return false;
}
}
// a screen of slots. the server owns the contents: clicks are sent to it and guessed locally
@@ -46,6 +53,8 @@ export class GuiInventoryScreen extends GuiScreen {
hovering: Slot | undefined;
inventory_width: number = 500;
inventory_height: number = 500;
// what mods draw over it, like a recipe viewer
graphics = new ModGraphics();
constructor(inventories: ClientInventories, send: (message: ClientMessage) => void) {
super();
@@ -64,11 +73,13 @@ export class GuiInventoryScreen extends GuiScreen {
}
}
override get typing() {
return this.graphics.typing;
}
on_tick(_delta: number): void {
this.x = canvas.width / 2 - (this.inventory_width / 2);
this.y = canvas.height / 2 - (this.inventory_height / 2);
this.handle_interaction();
}
on_render(): void {
@@ -100,6 +111,14 @@ export class GuiInventoryScreen extends GuiScreen {
}
}
// mods' overlays go on top and get the mouse first, then the slots get what they left
this.#render_overlays();
if (this.graphics.mouse_blocked()) {
this.hovering = 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);
@@ -110,6 +129,29 @@ export class GuiInventoryScreen extends GuiScreen {
draw_tooltip(item_tooltip(item), mouse.x, mouse.y);
}
}
this.graphics.end();
}
#render_overlays() {
const g = this.graphics;
g.begin();
const hovered = this.hovering && this.get_container(this.hovering.container)?.get_item(this.hovering.index);
const view = {
x: this.x,
y: this.y,
width: this.inventory_width,
height: this.inventory_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(g, 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);
}
}
}
on_close(): void {