Files
bworld/docs/modding/gui/index.md
T
2026-10-08 13:42:18 -03:00

173 lines
8.1 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 |
| Forms and HUD elements | 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 script'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. `mods/recipe_viewer` is one.
```ts
import type { ClientContext } from "bworld/client";
export function setup(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
text(text: string, x: number, y: number, options?: { scale?: number; color?: [r, g, b, a] }): void; // scale 1 is normal
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;
item(id: string, x: number, y: number, count?: number): void; // an item as a slot shows it
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 | { 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;
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.
`slider` is in the type but not built yet.
## Reading Game State
Client scripts 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.biome_at(x, z); // and ctx.world.season_at(x, z), undefined where nothing's loaded
ctx.world.time; // see Time
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.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 during `setup`.
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 script, like Bedrock's server forms. They'll be menus whose screens the engine draws.
- **HUD elements** (`ctx.hud.register`): drawing over the world every frame.
- **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.