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
+47 -28
View File
@@ -15,8 +15,17 @@ import {
type RecipeLayout,
SLOT,
} from "./categories.ts";
import { BUTTON, fluid_cell, item_cell, ItemList, LABEL_COLOR, type Mode, SLOT_COLOR } from "./item_list.ts";
import { type Ingredient, inputs, outputs, type Recipe } from "./recipes.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;
@@ -54,23 +63,28 @@ export class RecipeIndex {
return this.#categories;
}
// each category with recipes that make the item, or use it. like jei, the uses of where a category is made, like
// a furnace, are all of that category's recipes
find(item: string, mode: Mode): { category: RecipeCategory; recipes: Recipe[] }[] {
// 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 recipes = mode === "recipes"
? of_type.filter((recipe) => outputs(recipe).some((output) => output.ids.includes(item)))
: category.catalysts.includes(item)
? of_type
: of_type.filter((recipe) => inputs(recipe, this.ctx.tags).some((input) => input.ids.includes(item)));
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 {
item: string;
entry: Entry;
mode: Mode;
// which of find's categories, and which page of it
category: number;
@@ -85,16 +99,16 @@ export class RecipesScreen implements Screen {
private ctx: ClientContext,
private index: RecipeIndex,
private list: ItemList,
item: string,
entry: Entry,
mode: Mode,
) {
this.#view = { item, mode, category: 0, page: 0 };
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.item, this.#view.mode);
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);
@@ -108,7 +122,7 @@ export class RecipesScreen implements Screen {
// 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: string | undefined;
let hovered: Entry | undefined;
const catalysts = category.catalysts;
if (catalysts.length > 0) {
const width = CATALYST_BORDER * 2 + SLOT;
@@ -117,7 +131,7 @@ export class RecipesScreen implements Screen {
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 = id;
if (this.#cell(g, id, x + CATALYST_BORDER, y, { slot: true })) hovered = { kind: "item", id };
});
} else {
g.panel(left, top, WIDTH, height);
@@ -149,7 +163,7 @@ export class RecipesScreen implements Screen {
hovered = this.#draw_recipe(g, category, recipe, x, y) ?? hovered;
});
hovered = this.list.render(g, left + WIDTH, (item, mode) => this.#open(item, mode)) ?? 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;
@@ -157,12 +171,12 @@ export class RecipesScreen implements Screen {
if (g.key_pressed(this.ctx.options.key_inventory)) this.ctx.ui.close();
}
// shows the recipes for or with another item, coming back to this one with backspace. nothing happens when there
// aren't any
#open(item: string, mode: Mode) {
if (this.index.find(item, mode).length === 0) return;
// 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 = { item, mode, category: 0, page: 0 };
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
@@ -200,9 +214,9 @@ export class RecipesScreen implements Screen {
};
}
// one recipe, its category drawing it with its top left at x, y. returns the hovered item
#draw_recipe(g: Graphics, category: RecipeCategory, recipe: Recipe, x: number, y: number): string | undefined {
let hovered: string | undefined;
// 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) {
@@ -212,9 +226,14 @@ export class RecipesScreen implements Screen {
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 = id;
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 };
}
},
fluid: (fluid, amount, dx, dy, tooltip) => fluid_cell(g, this.ctx, fluid, amount, x + dx, y + dy, tooltip),
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 }),
};
@@ -223,6 +242,6 @@ export class RecipesScreen implements Screen {
}
#cell(g: Graphics, id: string, x: number, y: number, options: Parameters<typeof item_cell>[6]) {
return item_cell(g, this.ctx, (item, mode) => this.#open(item, mode), id, x, y, options);
return item_cell(g, this.ctx, (entry, mode) => this.#open(entry, mode), id, x, y, options);
}
}