Tooltip api

This commit is contained in:
2026-11-05 23:06:37 -03:00
parent 9bc8fd992a
commit 34f001285d
15 changed files with 264 additions and 81 deletions
+12 -19
View File
@@ -6,7 +6,7 @@
// pixels each
import { EverythingRegistry, type ItemRegistry } from "@/common/everything_registry.ts";
import { ItemStack } from "@/common/inventory.ts";
import type { Color, Graphics } from "@/common/mod_api/client.ts";
import type { Color, Graphics, TooltipLine } from "@/common/mod_api/client.ts";
import type { FluidVariant, KeyCode } from "@/common/mod_api/shared.ts";
import { point_inside_rec } from "@/common/utils.ts";
import { AssetManager } from "@/client/assets.ts";
@@ -23,13 +23,19 @@ import {
} from "@/client/renderer/mod.ts";
import { draw_fluid, draw_item } from "@/client/rendering/render_utils.ts";
import { get_sprite_region } from "@/client/sprites.ts";
import { draw_tooltip, item_tooltip, type TooltipLine } from "./tooltip.ts";
import { draw_tooltip, item_tooltip } from "./tooltip.ts";
import { TEXT_HEIGHT, TEXT_SCALE, TextInput } from "./widgets.ts";
import { GUI_ATLAS } from "./gui_atlas.ts";
import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts";
const WHITE: Color = [1, 1, 1, 1];
const GREY: Color = [0.66, 0.66, 0.66, 1];
// a script's tooltip line, copied so it can't change after, in color when it doesn't say
function tooltip_line(line: string | TooltipLine, color: Color): TooltipLine {
if (typeof line === "string") return { text: line, color };
return { text: String(line.text), color: line.color ?? color, icon: line.icon, priority: line.priority };
}
const INPUT_HEIGHT = 32;
const FIELD_BORDER = GUI_ATLAS.text_field.border[0] * GUI_SCALE;
@@ -245,25 +251,12 @@ export class ModGraphics implements Graphics {
return !this.typing && InputManager.is_key_pressed(key as InputKey);
}
tooltip(lines: (string | { text: string; color?: Color })[], x: number, y: number) {
this.#tooltip = {
lines: lines.map((line) =>
typeof line === "string"
? { text: line, color: WHITE }
: { text: line.text, color: line.color ?? WHITE }
),
x: this.#x(x),
y: this.#y(y),
};
tooltip(lines: (string | TooltipLine)[], x: number, y: number) {
this.#tooltip = { lines: lines.map((line) => tooltip_line(line, WHITE)), x: this.#x(x), y: this.#y(y) };
}
item_tooltip(id: string, x: number, y: number, extra: (string | { text: string; color?: Color })[] = []) {
const lines = [
...item_tooltip(new ItemStack(id, 1)),
...extra.map((line) =>
typeof line === "string" ? { text: line, color: GREY } : { text: line.text, color: line.color ?? GREY }
),
];
item_tooltip(id: string, x: number, y: number, extra: (string | TooltipLine)[] = []) {
const lines = item_tooltip(new ItemStack(id, 1), extra.map((line) => tooltip_line(line, GREY)));
this.#tooltip = { lines, x: this.#x(x), y: this.#y(y) };
}
+38 -28
View File
@@ -1,10 +1,12 @@
// 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, what it's worth, then
// its lore
// 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, sell_value } from "@/common/inventory.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";
@@ -14,53 +16,61 @@ 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";
type Color = [number, number, number, number];
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 VALUE_COLOR: Color = [0.95, 0.8, 0.3, 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;
// drawn before what an item sells for
const VALUE_ICON = "bworld:copper_coin";
// from the mouse, so the cursor doesn't cover it
const OFFSET = 16;
export interface TooltipLine {
text: string;
color: Color;
// a texture drawn before the text, a square as tall as the line
icon?: string;
}
// 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, the item's lore from its json, then what the server's scripts said about
// this stack
export function item_tooltip(item: ItemStack): TooltipLine[] {
const lines: TooltipLine[] = [{ text: display_name(item.type_id), color: NAME_COLOR }];
// 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<ItemRegistry>("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 });
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 });
lines.push({
text: `Restores ${info.food.nutrition} hunger`,
color: FOOD_COLOR,
priority: TOOLTIP_PRIORITY.food,
});
}
const value = sell_value(item.type_id, item.data, fluid_rules);
if (value !== undefined) {
lines.push({ icon: VALUE_ICON, text: `${value}`, color: VALUE_COLOR });
}
const static_lore = info?.lore;
for (const lore of [static_lore, item.lore]) {
for (const lore of [info?.lore, item.lore]) {
for (const text of lore?.split("\n") ?? []) {
lines.push({ text, color: LORE_COLOR });
lines.push({ text, color: LORE_COLOR, priority: TOOLTIP_PRIORITY.lore });
}
}
return lines;
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
@@ -109,6 +119,6 @@ export function draw_tooltip(lines: TooltipLine[], mouse_x: number, mouse_y: num
line_height * 2,
);
}
draw_text(line.text, x + PADDING + icon_width(line), line_y, TEXT_SCALE, line.color);
draw_text(line.text, x + PADDING + icon_width(line), line_y, TEXT_SCALE, line.color ?? WHITE);
});
}
+8 -2
View File
@@ -27,6 +27,7 @@ import { AIR_ID } from "@/common/protocol.ts";
import type { Client } from "./client.ts";
import { code_url, fetch_verified } from "./handshake.ts";
import { GuiModScreen } from "./gui/gui_mod_screens.ts";
import { item_tooltips } from "./gui/tooltip.ts";
import { clear_hooks, expose_engine, loading_mod } from "@/common/hooks.ts";
import { common_engine } from "@/common/engine_modules.ts";
import { client_engine } from "./engine_modules.ts";
@@ -165,6 +166,7 @@ export async function load_client_mods(listings: ModListing[], mods: Mod[]) {
const recipes = register_mod_data(mods.map((mod) => ({ id: mod.manifest.id, data: mod.data })));
recipe_book = recipes;
inventory_overlays.length = 0;
item_tooltips.length = 0;
menu_screens.clear();
net_handlers.clear();
block_visuals.clear();
@@ -239,7 +241,7 @@ function shared_context(listing: ModListing): SharedContext {
side: "client",
tags: tag_api(),
blocks: blocks_api(mod),
items: shared_items_api(mod),
items: shared_items_api(mod, fluid_rules),
fluids: {
exists: fluid_exists,
same: same_fluid,
@@ -309,6 +311,10 @@ function client_context(listing: ModListing): ClientContext {
add_inventory_overlay(overlay) {
inventory_overlays.push({ mod, overlay });
},
add_item_tooltip(fn) {
if (typeof fn !== "function") throw new Error(`[${mod}] add_item_tooltip takes a function`);
item_tooltips.push((item, lines) => run_mod(mod, "an item tooltip", () => fn(item, lines)));
},
},
hud: not_yet("hud", "HUD elements") as ClientContext["hud"],
input: not_yet("input", "keybinds") as ClientContext["input"],
@@ -359,7 +365,7 @@ function client_context(listing: ModListing): ClientContext {
},
},
items: {
...shared_items_api(mod),
...shared_items_api(mod, fluid_rules),
mod(id) {
known_item(id);
return mod_name(id);