Improve recipe viewer
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
// every item, right of the inventory and of the recipes screen, like jei's ingredient list overlay: the page and its
|
||||
// arrows on top, the items, and the search box under them
|
||||
import type { ClientContext, Color, Graphics } from "bworld/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];
|
||||
|
||||
// show the recipes that make an item, or the ones that use it
|
||||
export type Mode = "recipes" | "uses";
|
||||
export type Open = (item: string, mode: Mode) => void;
|
||||
|
||||
// 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 },
|
||||
]);
|
||||
if (mouse.pressed) {
|
||||
g.consume_click();
|
||||
open(id, "recipes");
|
||||
} else if (mouse.right_pressed) {
|
||||
g.consume_click();
|
||||
open(id, "uses");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export class ItemList {
|
||||
// kept between screens, like jei
|
||||
page = 0;
|
||||
// set on the first frame, once every mod's items are loaded
|
||||
#items: string[] | undefined;
|
||||
|
||||
constructor(private ctx: ClientContext) {}
|
||||
|
||||
// in the space from left to the window's right edge. returns the hovered item
|
||||
render(g: Graphics, left: number, open: Open): string | undefined {
|
||||
this.#items ??= this.ctx.items.ids().filter((id) => id !== "bworld:air");
|
||||
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.#items.filter((id) => matches_search(this.ctx, id, 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: string | undefined;
|
||||
items.slice(this.page * per_page, (this.page + 1) * per_page).forEach((id, i) => {
|
||||
const x = left + (i % columns) * SLOT;
|
||||
const y = top + Math.floor(i / columns) * SLOT;
|
||||
if (item_cell(g, this.ctx, open, id, x, y)) hovered = id;
|
||||
});
|
||||
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, like jei
|
||||
function matches_search(ctx: ClientContext, id: string, search: string): boolean {
|
||||
if (search === "") return true;
|
||||
return search.split(/\s+/).every((word) => {
|
||||
if (word.startsWith("@")) {
|
||||
const mod = word.slice(1);
|
||||
return id.split(":")[0].includes(mod) || ctx.items.mod(id).toLowerCase().includes(mod);
|
||||
}
|
||||
return ctx.items.name(id).toLowerCase().includes(word) || id.includes(word);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user