253 lines
12 KiB
Markdown
253 lines
12 KiB
Markdown
# Screens
|
|
|
|
There are a few ways for a mod to show players a GUI. Pick the first one that's enough:
|
|
|
|
| Way | Code | For |
|
|
| ----------------------------------------- | ------------------ | --------------------------------------------------------- |
|
|
| [Menus](../inventories/menus.md) | server, and client | Inventories and machines: slots, filters, progress bars |
|
|
| [Plain screens](#plain-screens) | client only | Screens with nothing the server checks: maps, guide books |
|
|
| [Inventory overlays](#inventory-overlays) | client only | Drawing next to every menu's screen, like a recipe list |
|
|
| [HUD layers](hud.md) | client only | Drawing over the world while playing, like health bars |
|
|
| Forms | not built yet | See [below](#not-built-yet) |
|
|
|
|
Everything is in **GUI pixels**, like NeoForge's: 18 of them are a slot, and the game draws each one 3 screen pixels
|
|
wide.
|
|
|
|
## Plain Screens
|
|
|
|
A screen with no menu is only on the client, like NeoForge's `Screen`. Open one with `ctx.ui.open(screen)`, over
|
|
whatever's open:
|
|
|
|
```ts
|
|
let smelted = 0;
|
|
ctx.net.on("copper_tools:stats", (data: { smelted: number }) => {
|
|
smelted = data.smelted;
|
|
ctx.ui.open({
|
|
on_render(g) {
|
|
g.panel(g.width / 2 - 70, 20, 140, 40);
|
|
g.text(`Smelted: ${smelted}`, g.width / 2 - 60, 30);
|
|
},
|
|
});
|
|
});
|
|
```
|
|
|
|
```ts
|
|
interface Screen {
|
|
on_init?(width: number, height: number): void; // opening, and whenever the window changes size
|
|
on_tick?(): void; // 20 times a second
|
|
on_render(g: Graphics): void; // every frame
|
|
should_close_on_escape?(): boolean; // true when not set
|
|
on_close?(): void; // the player closed it
|
|
on_removed?(): void; // it's gone for any reason, including another screen replacing it
|
|
}
|
|
```
|
|
|
|
The server can't open one directly. Like a NeoForge payload handler calling `setScreen`, the server sends a
|
|
[mod channel](../networking/index.md) message, and the client phase's handler opens it, as above. A screen that needs to
|
|
tell the server something sends a message back the same way. `ctx.ui.close()` closes the screen on top. A screen that
|
|
throws is closed, with the error in the console.
|
|
|
|
## Inventory Overlays
|
|
|
|
An inventory overlay draws on every menu's screen (the player's inventory, chests, furnaces and every mod's menus), over
|
|
the screen, like Just Enough Items' item list. A menu screen with `inventory_overlays: false` has none, like the base
|
|
game's fishing minigame. `mods/recipe_viewer` is one.
|
|
|
|
```ts
|
|
import type { ClientContext } from "cubi/client";
|
|
|
|
export function client(ctx: ClientContext) {
|
|
ctx.ui.add_inventory_overlay({
|
|
on_render(g, screen) {
|
|
// screen is the inventory's box, and what's in the slot under the mouse
|
|
const x = screen.x + screen.width + 5;
|
|
if (screen.hovered_item) g.text(ctx.items.name(screen.hovered_item.id), x, 5);
|
|
},
|
|
});
|
|
}
|
|
```
|
|
|
|
`on_render` runs every frame the screen is open, with a [`Graphics`](#graphics) to draw with and the screen's box, in
|
|
GUI pixels from the window's top left:
|
|
|
|
```ts
|
|
interface InventoryScreenView {
|
|
readonly menu_type: string | undefined; // the open menu's type, undefined on the player's own inventory
|
|
readonly x: number;
|
|
readonly y: number;
|
|
readonly width: number;
|
|
readonly height: number;
|
|
readonly hovered_item: ItemStack | undefined; // what's in the slot under the mouse
|
|
}
|
|
```
|
|
|
|
Overlays get the mouse before the slots:
|
|
|
|
- A click an overlay uses, with `g.button` or `g.consume_click()`, doesn't reach the slots.
|
|
- `g.block_mouse(x, y, w, h)` keeps the slots from reacting to the mouse under something drawn over them.
|
|
- While a `g.text_input` has the keyboard, keybinds like the inventory key don't fire. Text inputs edit like chat:
|
|
selecting with shift or the mouse, ctrl+a, and copy, cut and paste.
|
|
|
|
`on_close()`, if the overlay has one, runs when the screen closes for any reason, like the player closing their
|
|
inventory.
|
|
|
|
To show something bigger than fits around the screen, open a [screen](#plain-screens) over it with `ctx.ui.open`, like
|
|
the recipe viewer's recipes screen does, the way Just Enough Items opens its recipes GUI. Escape goes back to the screen
|
|
underneath.
|
|
|
|
An overlay that throws is removed, with the error in the console.
|
|
|
|
## Graphics
|
|
|
|
`Graphics` is an immediate mode drawing API: every frame, draw everything again, and ask widgets like `button` what
|
|
happened. It's styled with the game's GUI sprites. Positions and sizes are GUI pixels, from the window's top left, or
|
|
from a menu screen's.
|
|
|
|
```ts
|
|
interface Graphics {
|
|
readonly width: number; // the window, in gui pixels
|
|
readonly height: number;
|
|
readonly text_height: number; // a line of text at scale 1
|
|
readonly mouse: { x: number; y: number; down: boolean; pressed: boolean; right_pressed: boolean };
|
|
|
|
rect(x: number, y: number, w: number, h: number, color?: [r, g, b, a]): void;
|
|
panel(x: number, y: number, w: number, h: number): void; // the game's panel background
|
|
// scale 1 is normal. shadow draws it again under, in dark grey, so it reads over the world
|
|
text(
|
|
text: string,
|
|
x: number,
|
|
y: number,
|
|
options?: { scale?: number; color?: [r, g, b, a]; shadow?: boolean },
|
|
): void;
|
|
measure_text(text: string, scale?: number): number;
|
|
// any texture. part is [u, v, width, height] from 0 to 1, the part of it to draw, like a progress bar's filled part
|
|
texture(id: string, x: number, y: number, w: number, h: number, part?: [u, v, w, h]): void;
|
|
// full drawn over empty, filled from 0 to 1 of the way from the left, or from the bottom when direction is "up"
|
|
progress(
|
|
empty: string,
|
|
full: string,
|
|
x: number,
|
|
y: number,
|
|
w: number,
|
|
h: number,
|
|
filled: number,
|
|
direction?: "right" | "up",
|
|
): void;
|
|
// one of the engine's gui sprites, like "cubi:heart_full", at its size unless given one. color multiplies it
|
|
sprite(id: string, x: number, y: number, options?: { width?: number; height?: number; color?: [r, g, b, a] }): void;
|
|
// an item as a slot shows it. a stack draws with its count and data, like a worn tool's durability bar
|
|
item(item: string | ItemStack, x: number, y: number, count?: number): void;
|
|
// a fluid as a slot shows it, like { id: "bworld:juice", data: { item: "bworld:apple" } }. filled is how much of
|
|
// the slot it fills from the bottom up, 0 to 1, like a tank's amount / capacity
|
|
fluid(fluid: FluidVariant, x: number, y: number, filled?: number): void;
|
|
clip(x: number, y: number, w: number, h: number, draw: () => void): void; // only draws inside the box
|
|
|
|
button(label: string, x: number, y: number, w: number, h: number): boolean; // true the frame it's clicked
|
|
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 | 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 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 client(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
|
|
|
|
The client phase can read what the client knows, to show it:
|
|
|
|
```ts
|
|
ctx.player.name;
|
|
ctx.player.position; // { x, y, z }
|
|
ctx.player.is_swimming(); // see ../entities/players.md#swimming
|
|
ctx.player.biome; // and ctx.player.season, see ../worldgen/biomes.md#seasons
|
|
ctx.world.get_block(x, y, z); // a block id, what this client sees
|
|
ctx.world.get_client_data<number>(x, y, z, "speed"); // see ../blocks/tiledata.md#client-data, a copy
|
|
ctx.world.biome_at(x, y, z); // and ctx.world.season_at(x, y, z), undefined where nothing's loaded
|
|
ctx.world.time; // see Time, and ctx.world.day and ctx.world.hour like the server's
|
|
ctx.player.health; // and the rest of the vitals, the hotbar and the attack cooldown, see hud.md
|
|
ctx.world.min_y; // and ctx.world.max_y, see ../worldgen/terrain.md#height
|
|
ctx.items.ids(); // every item, in load order
|
|
ctx.items.exists(id);
|
|
ctx.items.name(id); // "Cooked Rice"
|
|
ctx.items.mod(id); // the name of the mod that added it
|
|
ctx.items.components(id); // its components and their params, like the server's, to show what they mean
|
|
ctx.blocks.exists(id);
|
|
ctx.blocks.components(id); // the same for a block, like what a crab pot catches with each bait
|
|
ctx.fluids.ids(); // every fluid, in load order
|
|
ctx.fluids.mod(id); // the name of the mod that added it
|
|
ctx.fluids.name(fluid); // and color, exists and same, see ../resources/fluids.md#using-fluids
|
|
ctx.loot.table(id); // a loot table file's JSON, or undefined. tables server phases make aren't here
|
|
ctx.recipes.list(type?); // every recipe as its JSON, including mods' own types
|
|
ctx.options.key_inventory; // the key the player has bound, like Minecraft's Options key mappings
|
|
```
|
|
|
|
`ctx.player`, `ctx.world` and `ctx.options` only work once the player is in the world, not while the client phase runs.
|
|
|
|
To draw blocks that move, like a shaft that turns, register a [visual](../blocks/visuals.md) with
|
|
`ctx.render.register_visual`.
|
|
|
|
The key mappings are `key_forward`, `key_back`, `key_left`, `key_right`, `key_jump`, `key_sprint`, `key_sneak`,
|
|
`key_inventory`, `key_drop` and `key_chat`. A screen that should close on the inventory key, like the recipe viewer's,
|
|
checks `g.key_pressed(ctx.options.key_inventory)` and calls `ctx.ui.close()`, the way JEI checks `keyInventory`.
|
|
|
|
## Not Built Yet
|
|
|
|
The client and server APIs have types for these, but calling them throws for now:
|
|
|
|
- **Forms** (`ctx.ui.message_form`, `action_form` and `modal_form` on the server): simple dialogs defined entirely by
|
|
the server phase, like Bedrock's server forms. They'll be menus whose screens the engine draws.
|
|
- **Keybinds** (`ctx.input.bind`). Until then, an inventory overlay can check `g.key_pressed` while an inventory is
|
|
open.
|
|
|
|
See [the modding plan](../../modding_plan.md) for how they're meant to work.
|