// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // the box next to the mouse saying what an item is, like minecraft's: its name, the fluid in it, its food, mods' lines, // then its lore import { EverythingRegistry, ItemRegistry } from "@/common/everything_registry.ts"; import { contained_fluid, type ItemStack } from "@/common/inventory.ts"; import { type Color, TOOLTIP_PRIORITY, type TooltipLine } from "@/common/mod_api/client.ts"; import type { ItemStack as ApiItemStack } from "@/common/mod_api/shared.ts"; import { display_name } from "@/common/utils.ts"; import { fluid_rules } from "@/client/fluid_rules.ts"; import { canvas, draw_text, draw_texture_region, measure_text, type Texture } from "@/client/renderer/mod.ts"; import { AssetManager } from "@/client/assets.ts"; import type { MenuTank } from "@/common/protocol.ts"; import { get_sprite_region } from "@/client/sprites.ts"; import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts"; import { TEXT_HEIGHT, TEXT_SCALE } from "./widgets.ts"; export type { TooltipLine }; const NAME_COLOR: Color = [1, 1, 1, 1]; const LORE_COLOR: Color = [0.66, 0.66, 0.66, 1]; const FOOD_COLOR: Color = [0.85, 0.6, 0.35, 1]; const FLUID_COLOR: Color = [0.55, 0.75, 0.95, 1]; const WHITE: Color = [1, 1, 1, 1]; // from the edge of the tooltip sprite to the text const PADDING = 4 * GUI_SCALE; const LINE_GAP = 4; // between a line's icon and its text const ICON_GAP = 4; // from the mouse, so the cursor doesn't cover it const OFFSET = 16; // what client scripts add to every item's tooltip, in load order, see ctx.ui.add_item_tooltip. client/mods.ts fills it, // each wrapped so one throwing is logged under its mod export const item_tooltips: ((item: ApiItemStack, lines: TooltipLine[]) => void)[] = []; // its name, the fluid a full container holds, its food, the lines mods add, then the item's lore from its json and what // the server's scripts said about this stack. extra lines go under all of it unless they have a priority export function item_tooltip(item: ItemStack, extra: TooltipLine[] = []): TooltipLine[] { const lines: TooltipLine[] = [ { text: display_name(item.type_id), color: NAME_COLOR, priority: TOOLTIP_PRIORITY.name }, ]; const info = EverythingRegistry.get("items", item.type_id); const fluid = contained_fluid(item.type_id, item.data); if (fluid) { lines.push({ text: `${fluid_rules.name(fluid)}, ${info!.fluid_container!.capacity} mB`, color: FLUID_COLOR, priority: TOOLTIP_PRIORITY.fluid, }); } if (info?.food) { lines.push({ text: `Restores ${info.food.nutrition} hunger`, color: FOOD_COLOR, priority: TOOLTIP_PRIORITY.food, }); } for (const lore of [info?.lore, item.lore]) { for (const text of lore?.split("\n") ?? []) { lines.push({ text, color: LORE_COLOR, priority: TOOLTIP_PRIORITY.lore }); } } if (item_tooltips.length) { const stack = { id: item.type_id, count: item.amount, data: item.data }; for (const fn of item_tooltips) fn(stack, lines); } for (const line of extra) lines.push({ ...line, priority: line.priority ?? TOOLTIP_PRIORITY.extra }); // sort is stable, so lines with the same priority stay in the order they were added return lines .filter((line) => typeof line?.text === "string") .sort((a, b) => (a.priority ?? TOOLTIP_PRIORITY.default) - (b.priority ?? TOOLTIP_PRIORITY.default)); } // a menu's fluid slot, like modern industrialization's: the fluid, how much of the tank it fills, and what clicking it // with a fluid container does export function tank_tooltip(tank: MenuTank, output: boolean): TooltipLine[] { return [ tank.fluid ? { text: fluid_rules.name(tank.fluid), color: NAME_COLOR } : { text: "Empty", color: LORE_COLOR }, { text: `${tank.amount} / ${tank.capacity} mB`, color: FLUID_COLOR }, { text: output ? "Fluid Output, Click to Extract" : "Fluid Input, Click to Insert or Extract", color: LORE_COLOR, }, ]; } // below and right of the mouse, moved back inside the window when it would go past its edge export function draw_tooltip(lines: TooltipLine[], mouse_x: number, mouse_y: number) { const line_height = TEXT_HEIGHT * TEXT_SCALE; const icon_width = (line: TooltipLine) => line.icon ? line_height + ICON_GAP : 0; const width = Math.max(...lines.map((line) => icon_width(line) + measure_text(line.text, TEXT_SCALE))) + PADDING * 2; const height = lines.length * line_height + (lines.length - 1) * LINE_GAP + PADDING * 2; let x = mouse_x + OFFSET; let y = mouse_y + OFFSET; if (x + width > canvas.width) x = mouse_x - OFFSET - width; if (y + height > canvas.height) y = canvas.height - height; x = Math.max(0, x); y = Math.max(0, y); draw_gui_sprite("tooltip", x, y, width, height); const atlas = AssetManager.instance.get("cubi:textures"); lines.forEach((line, i) => { const line_y = y + PADDING + i * (line_height + LINE_GAP); if (line.icon) { const { x: sx, y: sy, width: sw, height: sh } = get_sprite_region(line.icon); draw_texture_region( atlas, sx, sy, sw, sh, x + PADDING - line_height / 2, line_y - line_height / 2, line_height * 2, line_height * 2, ); } draw_text(line.text, x + PADDING + icon_width(line), line_y, TEXT_SCALE, line.color ?? WHITE); }); }