220 lines
6.6 KiB
TypeScript
220 lines
6.6 KiB
TypeScript
import { SLOT_SIZE } from "$/common/constants.ts";
|
|
import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts";
|
|
import { click_slot, Container } from "$/common/inventory.ts";
|
|
import { ClientMessage, ContainerKey, CRAFTING_RESULT_SLOT } 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 } from "../renderer/mod.ts";
|
|
import { draw_item } 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";
|
|
|
|
// how far past the slot's edge its highlight goes
|
|
const HIGHLIGHT_OVERHANG = 3 * GUI_SCALE;
|
|
|
|
export class Slot {
|
|
container: ContainerKey;
|
|
index: number;
|
|
// relative to screen top left
|
|
x: number;
|
|
y: number;
|
|
// can only be taken from, like the crafting or furnace result
|
|
output: boolean;
|
|
|
|
constructor(container: ContainerKey, index: number, x: number, y: number, output = false) {
|
|
this.container = container;
|
|
this.index = index;
|
|
this.x = x;
|
|
this.y = y;
|
|
this.output = output;
|
|
}
|
|
}
|
|
|
|
export abstract class GuiScreen {
|
|
x: number = 0;
|
|
y: number = 0;
|
|
|
|
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
|
|
// so they feel instant, then whatever the server syncs back wins
|
|
export class GuiInventoryScreen extends GuiScreen {
|
|
inventories: ClientInventories;
|
|
send: (message: ClientMessage) => void;
|
|
|
|
slots: Slot[] = [];
|
|
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();
|
|
this.inventories = inventories;
|
|
this.send = send;
|
|
}
|
|
|
|
get_container(key: ContainerKey): Container | undefined {
|
|
switch (key) {
|
|
case "inventory":
|
|
return this.inventories.inventory;
|
|
case "crafting":
|
|
return this.inventories.crafting;
|
|
case "screen":
|
|
return this.inventories.screen;
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
on_render(): void {
|
|
for (const slot of this.slots) {
|
|
draw_gui_sprite("slot", this.x + slot.x, this.y + slot.y);
|
|
}
|
|
|
|
// 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") => {
|
|
if (this.hovering) {
|
|
const x = this.x + this.hovering.x - HIGHLIGHT_OVERHANG;
|
|
draw_gui_sprite(sprite, x, this.y + this.hovering.y - HIGHLIGHT_OVERHANG);
|
|
}
|
|
};
|
|
highlight("slot_highlight_back");
|
|
for (const slot of this.slots) {
|
|
const item = this.get_container(slot.container)?.get_item(slot.index);
|
|
if (item) {
|
|
draw_item(item, this.x + slot.x, this.y + slot.y);
|
|
}
|
|
}
|
|
highlight("slot_highlight_front");
|
|
|
|
// 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);
|
|
} else if (this.hovering) {
|
|
// what's in the slot under the mouse, while not carrying anything
|
|
const item = this.get_container(this.hovering.container)?.get_item(this.hovering.index);
|
|
if (item) {
|
|
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 {
|
|
this.send({ type: "close_screen" });
|
|
|
|
// the server puts these back in the inventory, show it right away
|
|
const { inventory, crafting, cursor } = this.inventories;
|
|
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;
|
|
const mouse = InputManager.get_mouse_position();
|
|
|
|
for (const slot of this.slots) {
|
|
const hovering = point_inside_rec(mouse.x, mouse.y, this.x + slot.x, this.y + slot.y, SLOT_SIZE, SLOT_SIZE);
|
|
if (!hovering) {
|
|
continue;
|
|
}
|
|
this.hovering = slot;
|
|
|
|
for (const button of [0, 2]) {
|
|
if (InputManager.is_mouse_pressed(button)) {
|
|
InputManager.consume_mouse(button);
|
|
this.click(slot, button);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
click(slot: Slot, button: number) {
|
|
this.send({ type: "click", container: slot.container, index: slot.index, button });
|
|
|
|
// output slots depend on server side things like recipes, just wait for the server
|
|
const container = this.get_container(slot.container);
|
|
if (container && !slot.output) {
|
|
click_slot(container, slot.index, this.inventories.cursor, button);
|
|
}
|
|
}
|
|
}
|
|
|
|
// generic methods that is often needed
|
|
|
|
export function add_player_inventory(handler: GuiInventoryScreen, offset_x: number, offset_y: number) {
|
|
for (let i = 0; i < 3; ++i) {
|
|
for (let l = 0; l < 9; ++l) {
|
|
handler.slots.push(
|
|
new Slot("inventory", l + i * 9 + 9, offset_x + l * SLOT_SIZE, offset_y + i * SLOT_SIZE),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function add_player_hotbar(handler: GuiInventoryScreen, offset_x: number, offset_y: number) {
|
|
for (let i = 0; i < 9; ++i) {
|
|
handler.slots.push(new Slot("inventory", i, offset_x + i * SLOT_SIZE, offset_y));
|
|
}
|
|
}
|