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) };
}