Fluids on the recipeviewer !

This commit is contained in:
2026-10-28 13:26:56 -03:00
parent 3cc33ea10c
commit e525681328
7 changed files with 151 additions and 83 deletions
+49 -30
View File
@@ -1,8 +1,8 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// 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
// 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 "bworld/client";
import { SLOT } from "./categories.ts";
@@ -18,7 +18,21 @@ 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";
export type Open = (item: string, mode: Mode) => void;
// 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(
@@ -40,54 +54,54 @@ export function item_cell(
{ 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");
}
click(g, open, { kind: "item", id });
return true;
}
// an amount of a fluid in a slot, hovered for its name and amount. fluids have no recipes of their own to open
// 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,
amount: number,
x: number,
y: number,
tooltip: string[] = [],
) {
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;
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),
{ text: `${amount} mB`, color: FLUID_COLOR },
...tooltip.map((text) => ({ text, color: LABEL_COLOR })),
...(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 are loaded
#items: string[] | undefined;
// 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
render(g: Graphics, left: number, open: Open): string | undefined {
this.#items ??= this.ctx.items.ids().filter((id) => id !== "bworld:air");
// 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 !== "bworld: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;
@@ -96,7 +110,7 @@ export class ItemList {
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 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);
@@ -106,25 +120,30 @@ export class ItemList {
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) => {
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;
if (item_cell(g, this.ctx, open, id, x, y)) hovered = id;
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, like jei
function matches_search(ctx: ClientContext, id: string, search: string): boolean {
// 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) || ctx.items.mod(id).toLowerCase().includes(mod);
return id.split(":")[0].includes(mod) || mod_name.toLowerCase().includes(mod);
}
return ctx.items.name(id).toLowerCase().includes(word) || id.includes(word);
return name.toLowerCase().includes(word) || id.includes(word);
});
}