118 lines
5.4 KiB
Markdown
118 lines
5.4 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 |
|
|
| -------------------------------------------- | ------------- | ---------------------------------------------------------- |
|
|
| [Container screens](../inventories/menus.md) | server only | Inventories and machines: slots, filters, progress bars |
|
|
| [Inventory overlays](#inventory-overlays) | client only | Drawing next to every inventory screen, like a recipe list |
|
|
| Forms, custom screens and HUD elements | not built yet | See [below](#not-built-yet) |
|
|
|
|
## Inventory Overlays
|
|
|
|
An inventory overlay draws on every inventory screen (the player's inventory, chests, furnaces and every other container
|
|
screen), 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 + 16;
|
|
if (screen.hovered_item) g.text(ctx.items.name(screen.hovered_item.id), x, 16);
|
|
},
|
|
});
|
|
}
|
|
```
|
|
|
|
`on_render` runs every frame the screen is open, with a [`Graphics`](#graphics) to draw with and the screen's box:
|
|
|
|
```ts
|
|
interface InventoryScreenView {
|
|
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.
|
|
|
|
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.
|
|
|
|
```ts
|
|
interface Graphics {
|
|
readonly width: number; // the screen, in pixels
|
|
readonly height: number;
|
|
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;
|
|
measure_text(text: string, scale?: number): number;
|
|
texture(id: string, x: number, y: number, w: number, h: number): void; // any texture
|
|
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` and `slots` are 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.world.get_block(x, y, z); // a block id, what this client sees
|
|
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.player` and `ctx.world` only work once the player is in the world, not during `setup`.
|
|
|
|
## 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.
|
|
- **Custom screens** (`ctx.ui.register_screen` and `open` on the client, `open_screen` on the server): whole screens
|
|
drawn with `Graphics`, opened by the server with some props.
|
|
- **HUD elements** (`ctx.hud.register`): drawing over the world every frame.
|
|
- **Keybinds** (`ctx.input.bind`).
|
|
- **Mod channels** (`ctx.net`), for a mod's client and server scripts to talk. Without them, a client script can't send
|
|
anything to the server.
|
|
|
|
See [the modding plan](../../modding_plan.md) for how they're meant to work.
|