Change whole gui system

This commit is contained in:
2026-10-01 16:22:02 -03:00
parent 013a4fbb97
commit deba36260e
48 changed files with 1925 additions and 1302 deletions
+61 -18
View File
@@ -2,16 +2,54 @@
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) |
| 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 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.
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";
@@ -20,14 +58,15 @@ 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);
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:
`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 {
@@ -46,24 +85,30 @@ Overlays get the mouse before the slots:
- 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. The recipe viewer goes back to its item list then.
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.
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 screen, in pixels
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;
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;
texture(id: string, x: number, y: number, w: number, h: number): void; // any texture
// 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
@@ -81,7 +126,7 @@ interface Graphics {
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.
`slider` is in the type but not built yet.
## Reading Game State
@@ -106,9 +151,7 @@ ctx.recipes.list(type?); // every recipe as its JSON, including mods' own types
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.
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.