Files
2026-11-04 21:51:26 -03:00

246 lines
9.4 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the recipes that make or use an item, a screen of its own like jei's RecipesGui: a tab for each category above the
// panel, where the category is made down its left side, the category and page with their arrows at the top, then as
// many recipes as fit, one under the other. the item list stays on the right. escape or the inventory key go back to
// the inventory, and backspace to what was shown before
import type { ClientContext, Graphics, Screen } from "cubi/client";
import {
crabpot_recipes,
fishing_recipes,
make_categories,
type RecipeCategory,
type RecipeLayout,
SLOT,
} from "./categories.ts";
import {
BUTTON,
type Entry,
fluid_cell,
item_cell,
ItemList,
LABEL_COLOR,
type Mode,
SLOT_COLOR,
} from "./item_list.ts";
import { fluid_inputs, fluid_outputs, type Ingredient, inputs, outputs, type Recipe } from "./recipes.ts";
// jei's sizes, in gui pixels
const WIDTH = 198;
const MAX_HEIGHT = 350;
const BORDER = 6;
const NAV_PADDING = 2;
const TAB = 24;
const TAB_OVERLAP = 3;
const TAB_INSET = 2;
const CATALYST_BORDER = 5;
// how far the catalysts' panel goes under the recipes'
const CATALYST_OVERLAP = 4;
const RECIPE_SPACING = 8;
// alternatives, like "any vegetable", take turns this often
const CYCLE_MS = 1000;
const UNSELECTED_TAB: [number, number, number, number] = [0, 0, 0, 0.35];
// every category and its recipes, read once every mod's are loaded, on the first lookup
export class RecipeIndex {
#categories: RecipeCategory[] | undefined;
#recipes: Recipe[] = [];
constructor(private ctx: ClientContext) {}
get categories(): RecipeCategory[] {
if (!this.#categories) {
this.#recipes = [
...this.ctx.recipes.list(),
...fishing_recipes(this.ctx),
...crabpot_recipes(this.ctx),
];
this.#categories = make_categories(this.ctx, this.#recipes);
}
return this.#categories;
}
// each category with recipes that make the item or fluid, or use it. like jei, the uses of where a category is
// made, like a furnace, are all of that category's recipes
find({ kind, id }: Entry, mode: Mode): { category: RecipeCategory; recipes: Recipe[] }[] {
const has = (recipe: Recipe) => {
if (kind === "fluid") {
const fluids = mode === "recipes" ? fluid_outputs(recipe) : fluid_inputs(recipe);
return fluids.some(({ fluid }) => fluid.id === id);
}
const items = mode === "recipes" ? outputs(recipe) : inputs(recipe, this.ctx.tags);
return items.some((item) => item.ids.includes(id));
};
return this.categories.flatMap((category) => {
const of_type = this.#recipes.filter((recipe) => recipe.type === category.type);
const made_in = kind === "item" && mode === "uses" && category.catalysts.includes(id);
const recipes = made_in ? of_type : of_type.filter(has);
return recipes.length > 0 ? [{ category, recipes }] : [];
});
}
}
interface View {
entry: Entry;
mode: Mode;
// which of find's categories, and which page of it
category: number;
page: number;
}
export class RecipesScreen implements Screen {
#view: View;
#history: View[] = [];
constructor(
private ctx: ClientContext,
private index: RecipeIndex,
private list: ItemList,
entry: Entry,
mode: Mode,
) {
this.#view = { entry, mode, category: 0, page: 0 };
}
on_render(g: Graphics) {
// over the world, like every screen
g.rect(0, 0, g.width, g.height, [0, 0, 0, 0.8]);
const results = this.index.find(this.#view.entry, this.#view.mode);
if (results.length === 0) return;
const view = this.#view;
view.category = Math.min(view.category, results.length - 1);
const { category, recipes } = results[view.category];
const left = Math.floor((g.width - WIDTH) / 2);
const top = TAB - TAB_OVERLAP + BORDER;
const height = Math.min(MAX_HEIGHT, g.height - top - BORDER);
const header_rows = BORDER + BUTTON * 2 + NAV_PADDING;
const recipes_top = top + header_rows + BORDER;
// the panels: tabs and where it's made go under the recipes' panel's edges
const tabs = this.#tabs(g, results, left, top);
let hovered: Entry | undefined;
const catalysts = category.catalysts;
if (catalysts.length > 0) {
const width = CATALYST_BORDER * 2 + SLOT;
const x = left - width + CATALYST_OVERLAP;
g.panel(x, recipes_top, width, catalysts.length * SLOT + CATALYST_BORDER * 2);
g.panel(left, top, WIDTH, height);
catalysts.forEach((id, i) => {
const y = recipes_top + CATALYST_BORDER + i * SLOT;
if (this.#cell(g, id, x + CATALYST_BORDER, y, { slot: true })) hovered = { kind: "item", id };
});
} else {
g.panel(left, top, WIDTH, height);
}
tabs.draw_icons();
// the category and the page, each between its arrows
const row = (y: number, label: string, count: number, current: number, set: (index: number) => void) => {
if (g.button("<", left + BORDER, y, BUTTON, BUTTON)) set((current + count - 1) % count);
if (g.button(">", left + WIDTH - BORDER - BUTTON, y, BUTTON, BUTTON)) set((current + 1) % count);
g.text(label, left + (WIDTH - g.measure_text(label)) / 2, y + (BUTTON - g.text_height) / 2);
};
row(top + BORDER, category.title, results.length, view.category, (i) => {
view.category = i;
view.page = 0;
});
const per_page = Math.max(
1,
Math.floor((top + height - BORDER - recipes_top + RECIPE_SPACING) / (category.height + RECIPE_SPACING)),
);
const pages = Math.ceil(recipes.length / per_page);
view.page = Math.min(view.page, pages - 1);
row(top + BORDER + BUTTON + NAV_PADDING, `${view.page + 1}/${pages}`, pages, view.page, (i) => view.page = i);
// the page's recipes, in the middle
const x = left + Math.floor((WIDTH - category.width) / 2);
recipes.slice(view.page * per_page, (view.page + 1) * per_page).forEach((recipe, i) => {
const y = recipes_top + i * (category.height + RECIPE_SPACING);
hovered = this.#draw_recipe(g, category, recipe, x, y) ?? hovered;
});
hovered = this.list.render(g, left + WIDTH, (entry, mode) => this.#open(entry, mode)) ?? hovered;
if (hovered && g.key_pressed("KeyR")) this.#open(hovered, "recipes");
if (hovered && g.key_pressed("KeyU")) this.#open(hovered, "uses");
if (g.key_pressed("Backspace")) this.#view = this.#history.pop() ?? this.#view;
// back to the inventory, like jei's recipes screen
if (g.key_pressed(this.ctx.options.key_inventory)) this.ctx.ui.close();
}
// shows the recipes for or with another item or fluid, coming back to this one with backspace. nothing happens when
// there aren't any
#open(entry: Entry, mode: Mode) {
if (this.index.find(entry, mode).length === 0) return;
this.#history.push(this.#view);
this.#view = { entry, mode, category: 0, page: 0 };
}
// a tab per category, as many as fit around the selected one, each showing where it's made or what it makes. the
// panels are drawn now, under the recipes' panel, and their icons after it
#tabs(g: Graphics, results: { category: RecipeCategory; recipes: Recipe[] }[], left: number, top: number) {
const view = this.#view;
const fit = Math.floor((WIDTH - TAB_INSET * 2) / TAB);
const first = Math.max(0, Math.min(view.category - Math.floor(fit / 2), results.length - fit));
const shown = results.slice(first, first + fit).map(({ category, recipes }, i) => ({
category,
// where it's made, or what its first recipe makes
icon: category.catalysts[0] ?? outputs(recipes[0])[0]?.ids[0],
index: first + i,
x: left + TAB_INSET + i * TAB,
y: top - TAB + TAB_OVERLAP,
}));
for (const { index, x, y } of shown) {
g.panel(x, y, TAB, TAB);
if (index !== view.category) g.rect(x, y, TAB, TAB, UNSELECTED_TAB);
}
return {
draw_icons: () => {
const { mouse } = g;
for (const { category, icon, index, x, y } of shown) {
if (icon) g.item(icon, x + (TAB - SLOT) / 2, y + (TAB - SLOT) / 2 - 1);
if (mouse.x < x || mouse.y < y || mouse.x >= x + TAB || mouse.y >= y + TAB - TAB_OVERLAP) continue;
g.tooltip([category.title], mouse.x, mouse.y);
if (mouse.pressed) {
g.consume_click();
view.category = index;
view.page = 0;
}
}
},
};
}
// one recipe, its category drawing it with its top left at x, y. returns the hovered item or fluid
#draw_recipe(g: Graphics, category: RecipeCategory, recipe: Recipe, x: number, y: number): Entry | undefined {
let hovered: Entry | undefined;
const layout: RecipeLayout = {
slot: (ingredient: Ingredient | undefined, dx, dy, tooltip = []) => {
if (!ingredient || ingredient.ids.length === 0) {
g.rect(x + dx, y + dy, SLOT, SLOT, SLOT_COLOR);
return;
}
const id = ingredient.ids[Math.floor(Date.now() / CYCLE_MS) % ingredient.ids.length];
const any = ingredient.ids.length > 1 ? [`Any of ${ingredient.ids.length}`] : [];
const options = { slot: true, count: ingredient.count, tooltip: [...any, ...tooltip] };
if (this.#cell(g, id, x + dx, y + dy, options)) hovered = { kind: "item", id };
},
fluid: (fluid, amount, dx, dy, tooltip) => {
const open = (entry: Entry, mode: Mode) => this.#open(entry, mode);
if (fluid_cell(g, this.ctx, open, fluid, x + dx, y + dy, { amount, tooltip })) {
hovered = { kind: "fluid", id: fluid.id };
}
},
texture: (id, dx, dy, w, h) => g.texture(id, x + dx, y + dy, w, h),
text: (text, dx, dy) => g.text(text, x + dx, y + dy, { color: LABEL_COLOR }),
};
category.draw(recipe, layout);
return hovered;
}
#cell(g: Graphics, id: string, x: number, y: number, options: Parameters<typeof item_cell>[6]) {
return item_cell(g, this.ctx, (entry, mode) => this.#open(entry, mode), id, x, y, options);
}
}