Tooltip api
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -255,3 +255,7 @@ ctx.items.sell_value(id, data); // the same for a stack with this data, with a f
|
||||
player.give_item("bworld:carrot", 3); // what doesn't fit drops at their feet
|
||||
player.exchange_held({ id: "bworld:bucket", count: 1, data }); // one of the held item becomes this
|
||||
```
|
||||
|
||||
Client and shared scripts have `ctx.items.sell_value(id, data)` too, worked out the same way with that side's
|
||||
[fluid rules](../resources/fluids.md#rules), so a client can show what a full bottle of juice is worth, like the base
|
||||
game's [tooltip line](../gui/index.md#item-tooltips) does.
|
||||
|
||||
@@ -90,7 +90,7 @@ and they can only be registered during `setup`.
|
||||
|
||||
The rules are in the shared script because both sides need them: the server for tooltips, and clients to draw the fluid.
|
||||
Shared scripts have `ctx.items.exists(id)`, `ctx.items.name(id)`, `ctx.items.components(id)` and
|
||||
`ctx.items.sell_value(id)` for rules that read an item, like syrup named after its tree.
|
||||
`ctx.items.sell_value(id, data?)` for rules that read an item, like syrup named after its tree.
|
||||
|
||||
## Using Fluids
|
||||
|
||||
@@ -147,7 +147,8 @@ tooltip says what's in it and how much, like "Birch Log Syrup, 250 mB".
|
||||
A full one sells for its own `base_sell_value` and what its fluid is worth on top: the fluid's value for 1000 mB times
|
||||
how much it holds, rounded up to a whole number. A jar of maple syrup worth 40 a bucket sells for 10, and the jar's own
|
||||
value when it has one. It can't be sold if neither has a value. Server scripts get this from
|
||||
`ctx.items.sell_value(id, data)`, which the base game's shipping bin sells by, and tooltips show it.
|
||||
`ctx.items.sell_value(id, data)`, which the base game's shipping bin sells by. Client and shared scripts have it too,
|
||||
and the base game shows it in [tooltips](../gui/index.md#item-tooltips).
|
||||
|
||||
A server script fills and empties one through its [fluid handler](#fluid-handlers), and `ctx.items.fluid_capacity(id)`
|
||||
says how much one holds.
|
||||
|
||||
Reference in New Issue
Block a user