150 lines
5.8 KiB
TypeScript
150 lines
5.8 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// every item and fluid, right of the inventory and of the recipes screen, like jei's ingredient list overlay: the page
|
|
// and its arrows on top, the items then the fluids, and the search box under them
|
|
import type { ClientContext, Color, FluidVariant, Graphics } from "cubi/client";
|
|
import { SLOT } from "./categories.ts";
|
|
|
|
const GAP = 5;
|
|
export const BUTTON = 13;
|
|
// catppuccin mocha, like the engine's gui: crust, pink, subtext 0 and blue
|
|
export const SLOT_COLOR: Color = [0.07, 0.07, 0.11, 0.6];
|
|
const HOVER_COLOR: Color = [0.96, 0.76, 0.91, 0.35];
|
|
export const LABEL_COLOR: Color = [0.65, 0.68, 0.78, 1];
|
|
const MOD_COLOR: Color = [0.54, 0.71, 0.98, 1];
|
|
// the engine's for the fluid in an item's tooltip
|
|
const FLUID_COLOR: Color = [0.55, 0.75, 0.95, 1];
|
|
|
|
// show the recipes that make an item, or the ones that use it
|
|
export type Mode = "recipes" | "uses";
|
|
// what recipes are looked up for, like jei's ingredient types. a fluid is its id alone, whatever its data, so plain
|
|
// juice stands for every juice
|
|
export type Entry = { kind: "item" | "fluid"; id: string };
|
|
export type Open = (entry: Entry, mode: Mode) => void;
|
|
|
|
// looked up by a left click for its recipes, a right click for its uses
|
|
function click(g: Graphics, open: Open, entry: Entry) {
|
|
if (g.mouse.pressed) {
|
|
g.consume_click();
|
|
open(entry, "recipes");
|
|
} else if (g.mouse.right_pressed) {
|
|
g.consume_click();
|
|
open(entry, "uses");
|
|
}
|
|
}
|
|
|
|
// one item that can be hovered for its tooltip and clicked for its recipes (right click, its uses). true when hovered
|
|
export function item_cell(
|
|
g: Graphics,
|
|
ctx: ClientContext,
|
|
open: Open,
|
|
id: string,
|
|
x: number,
|
|
y: number,
|
|
options: { slot?: boolean; count?: number; tooltip?: string[] } = {},
|
|
): boolean {
|
|
if (options.slot) g.rect(x, y, SLOT, SLOT, SLOT_COLOR);
|
|
g.item(id, x, y, options.count ?? 1);
|
|
const { mouse } = g;
|
|
if (mouse.x < x || mouse.y < y || mouse.x >= x + SLOT || mouse.y >= y + SLOT) return false;
|
|
g.rect(x, y, SLOT, SLOT, HOVER_COLOR);
|
|
g.item_tooltip(id, mouse.x, mouse.y, [
|
|
...(options.tooltip ?? []),
|
|
{ text: id, color: LABEL_COLOR },
|
|
{ text: ctx.items.mod(id), color: MOD_COLOR },
|
|
]);
|
|
click(g, open, { kind: "item", id });
|
|
return true;
|
|
}
|
|
|
|
// a fluid in a slot, hovered for its name and amount (none in the list) and clicked like an item. true when hovered
|
|
export function fluid_cell(
|
|
g: Graphics,
|
|
ctx: ClientContext,
|
|
open: Open,
|
|
fluid: FluidVariant,
|
|
x: number,
|
|
y: number,
|
|
options: { amount?: number; tooltip?: string[] } = {},
|
|
): boolean {
|
|
g.rect(x, y, SLOT, SLOT, SLOT_COLOR);
|
|
g.fluid(fluid, x, y);
|
|
const { mouse } = g;
|
|
if (mouse.x < x || mouse.y < y || mouse.x >= x + SLOT || mouse.y >= y + SLOT) return false;
|
|
g.rect(x, y, SLOT, SLOT, HOVER_COLOR);
|
|
g.tooltip(
|
|
[
|
|
ctx.fluids.name(fluid),
|
|
...(options.amount === undefined ? [] : [{ text: `${options.amount} mB`, color: FLUID_COLOR }]),
|
|
...(options.tooltip ?? []).map((text) => ({ text, color: LABEL_COLOR })),
|
|
{ text: fluid.id, color: LABEL_COLOR },
|
|
{ text: ctx.fluids.mod(fluid.id), color: MOD_COLOR },
|
|
],
|
|
mouse.x,
|
|
mouse.y,
|
|
);
|
|
click(g, open, { kind: "fluid", id: fluid.id });
|
|
return true;
|
|
}
|
|
|
|
export class ItemList {
|
|
// kept between screens, like jei
|
|
page = 0;
|
|
// set on the first frame, once every mod's items and fluids are loaded
|
|
#entries: Entry[] | undefined;
|
|
|
|
constructor(private ctx: ClientContext) {}
|
|
|
|
// in the space from left to the window's right edge. returns the hovered item or fluid
|
|
render(g: Graphics, left: number, open: Open): Entry | undefined {
|
|
this.#entries ??= [
|
|
...this.ctx.items.ids().filter((id) => id !== "cubi:air").map((id) => ({ kind: "item" as const, id })),
|
|
...this.ctx.fluids.ids().map((id) => ({ kind: "fluid" as const, id })),
|
|
];
|
|
left += GAP;
|
|
const columns = Math.floor((g.width - GAP - left) / SLOT);
|
|
const top = GAP + BUTTON + 2;
|
|
const rows = Math.floor((g.height - GAP - BUTTON - 2 - top) / SLOT);
|
|
if (columns < 2 || rows < 1) return undefined;
|
|
const width = columns * SLOT;
|
|
|
|
const search = g.text_input("search", left, g.height - GAP - BUTTON, width).trim().toLowerCase();
|
|
const items = this.#entries.filter((entry) => matches_search(this.ctx, entry, search));
|
|
const per_page = columns * rows;
|
|
const pages = Math.max(1, Math.ceil(items.length / per_page));
|
|
this.page = Math.min(this.page, pages - 1);
|
|
|
|
if (g.button("<", left, GAP, BUTTON, BUTTON)) this.page = (this.page + pages - 1) % pages;
|
|
if (g.button(">", left + width - BUTTON, GAP, BUTTON, BUTTON)) this.page = (this.page + 1) % pages;
|
|
const label = `${this.page + 1}/${pages}`;
|
|
g.text(label, left + (width - g.measure_text(label)) / 2, GAP + (BUTTON - g.text_height) / 2);
|
|
|
|
let hovered: Entry | undefined;
|
|
items.slice(this.page * per_page, (this.page + 1) * per_page).forEach((entry, i) => {
|
|
const x = left + (i % columns) * SLOT;
|
|
const y = top + Math.floor(i / columns) * SLOT;
|
|
const shown = entry.kind === "item"
|
|
? item_cell(g, this.ctx, open, entry.id, x, y)
|
|
: fluid_cell(g, this.ctx, open, { id: entry.id }, x, y);
|
|
if (shown) hovered = entry;
|
|
});
|
|
if (items.length === 0) g.text("Nothing found", left, top, { color: [0.8, 0.8, 0.8, 1] });
|
|
return hovered;
|
|
}
|
|
}
|
|
|
|
// by name or id, or "@mod" for a mod's items and fluids, like jei
|
|
function matches_search(ctx: ClientContext, { kind, id }: Entry, search: string): boolean {
|
|
if (search === "") return true;
|
|
const name = kind === "item" ? ctx.items.name(id) : ctx.fluids.name({ id });
|
|
const mod_name = kind === "item" ? ctx.items.mod(id) : ctx.fluids.mod(id);
|
|
return search.split(/\s+/).every((word) => {
|
|
if (word.startsWith("@")) {
|
|
const mod = word.slice(1);
|
|
return id.split(":")[0].includes(mod) || mod_name.toLowerCase().includes(mod);
|
|
}
|
|
return name.toLowerCase().includes(word) || id.includes(word);
|
|
});
|
|
}
|