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
+47 -4
View File
@@ -136,18 +136,61 @@ interface Graphics {
text_input(id: string, x: number, y: number, w: number): string; // what's typed in it so far
key_pressed(key: string): boolean; // KeyboardEvent.code, like "KeyE". false while a text input has the keyboard
tooltip(lines: (string | { text: string; color?: Color })[], x: number, y: number): void; // over everything
item_tooltip(id: string, x: number, y: number, extra?: (string | { text: string; color?: Color })[]): void;
tooltip(lines: (string | TooltipLine)[], x: number, y: number): void; // over everything
item_tooltip(id: string, x: number, y: number, extra?: (string | TooltipLine)[]): void;
consume_click(): void; // nothing under it gets this click
block_mouse(x: number, y: number, w: number, h: number): void; // the screen underneath ignores the mouse there
}
```
Colors are `[r, g, b, a]` from 0 to 1. The last `tooltip` drawn in a frame is the one shown, and `item_tooltip` shows
the game's own tooltip for an item (its name, food, durability and lore) with extra lines under it.
Colors are `[r, g, b, a]` from 0 to 1. The last `tooltip` drawn in a frame is the one shown, and `item_tooltip` shows an
item's tooltip the way a slot does, see [Item Tooltips](#item-tooltips), with extra lines under it. `tooltip` lines are
white unless they say, and `item_tooltip`'s extra lines grey. Either can have an icon.
`slider` is in the type but not built yet.
## Item Tooltips
`ctx.ui.add_item_tooltip` adds lines to every item's tooltip, in slots and in `g.item_tooltip`, like NeoForge's
`ItemTooltipEvent`. The function gets the stack and the lines so far, the game's and those of mods loaded earlier, and
pushes its own:
```ts
import { type ClientContext, TOOLTIP_PRIORITY } from "cubi/client";
export function setup(ctx: ClientContext) {
ctx.ui.add_item_tooltip((item, lines) => {
const value = ctx.items.sell_value(item.id, item.data);
if (value === undefined) return;
lines.push({ icon: "bworld:copper_coin", text: String(value), priority: TOOLTIP_PRIORITY.food + 1 });
});
}
```
That's the base game's sell value line. Each line is:
```ts
interface TooltipLine {
text: string;
color?: Color; // white when not set
icon?: string; // a texture drawn before the text, as tall as the line
priority?: number; // where it goes, TOOLTIP_PRIORITY.default when not set
}
```
Once every function has run, the lines are sorted by priority, lowest first. Lines with the same priority keep the order
they were added in: the game's first, then mods' in load order. `TOOLTIP_PRIORITY` says where the game's own lines are,
so a mod's can go between them:
| Priority | Value | What's there |
| --------- | ----- | ----------------------------------------------------------------------------------------------- |
| `name` | 0 | The item's name |
| `fluid` | 100 | The fluid a full container holds and how much |
| `food` | 200 | How much hunger it restores |
| `default` | 500 | Lines that don't set a priority |
| `lore` | 1000 | The item's `lore`, then what the server's [`get_lore`](../items/index.md#item-components) added |
| `extra` | 2000 | `g.item_tooltip`'s extra lines that don't set a priority |
## Reading Game State
Client scripts can read what the client knows, to show it: