Change whole gui system
This commit is contained in:
+54
-16
@@ -35,36 +35,69 @@ export interface ClientContext {
|
||||
log(...args: unknown[]): void;
|
||||
}
|
||||
|
||||
export interface ModScreen<Props = unknown> {
|
||||
on_open?(): void;
|
||||
on_tick?(dt: number): void;
|
||||
// a screen with no menu, only on this client, like neoforge's Screen. see docs/modding/gui/index.md
|
||||
export interface Screen {
|
||||
// when it opens, and whenever the window changes size. in gui pixels
|
||||
on_init?(width: number, height: number): void;
|
||||
// 20 times a second
|
||||
on_tick?(): void;
|
||||
on_render(g: Graphics): void;
|
||||
// the server sent new props for this screen
|
||||
on_props?(props: Props): void;
|
||||
// true when not set
|
||||
should_close_on_escape?(): boolean;
|
||||
// the player closed it, with escape
|
||||
on_close?(): void;
|
||||
// return true to keep the screen open when escape is pressed
|
||||
on_escape?(): boolean;
|
||||
// it's gone for any reason, including another screen replacing it
|
||||
on_removed?(): void;
|
||||
}
|
||||
|
||||
// the client side of a menu the server opened, see docs/modding/inventories/menus.md
|
||||
export interface ClientMenu {
|
||||
readonly type: Id;
|
||||
readonly title: string;
|
||||
// what the server's build function set, sent once
|
||||
readonly open_data: unknown;
|
||||
// the server's latest values of its add_data numbers
|
||||
readonly data: Readonly<Record<string, number>>;
|
||||
// like clickMenuButton, the server's on_button decides what it does
|
||||
click_button(id: number): void;
|
||||
close(): void;
|
||||
}
|
||||
|
||||
// draws a menu, like neoforge's AbstractContainerScreen. the engine puts it in the middle of the window, and draws
|
||||
// the menu's slots, the item on the cursor and item tooltips. g's 0, 0 is the screen's top left
|
||||
export interface MenuScreen {
|
||||
// in gui pixels, like imageWidth and imageHeight
|
||||
width: number;
|
||||
height: number;
|
||||
// under the slots
|
||||
on_render_background?(g: Graphics): void;
|
||||
// over the slots
|
||||
on_render_labels?(g: Graphics): void;
|
||||
// 20 times a second, like containerTick
|
||||
on_tick?(): void;
|
||||
}
|
||||
|
||||
export type Color = [number, number, number, number];
|
||||
|
||||
// immediate mode drawing, like the debug ui, styled with the gui sprites (assets/sprites/gui.png)
|
||||
// immediate mode drawing, like the debug ui, styled with the gui sprites (assets/sprites/gui.png). everything is in
|
||||
// gui pixels, like neoforge's: 18 of them are a slot
|
||||
export interface Graphics {
|
||||
readonly width: number;
|
||||
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?: Color): void;
|
||||
panel(x: number, y: number, w: number, h: number): void;
|
||||
// how tall a line of text is at scale 1, the game's text size
|
||||
readonly text_height: number;
|
||||
text(text: string, x: number, y: number, options?: { scale?: number; color?: Color }): void;
|
||||
measure_text(text: string, scale?: number): number;
|
||||
texture(id: Id, x: number, y: number, w: number, h: number): void;
|
||||
// part is the part of the texture to draw, [u, v, width, height] from 0 to 1, like a progress bar's filled part
|
||||
texture(id: Id, x: number, y: number, w: number, h: number, part?: [number, number, number, number]): void;
|
||||
item(id: Id, x: number, y: number, count?: number): void;
|
||||
clip(x: number, y: number, w: number, h: number, draw: () => void): void;
|
||||
button(label: string, x: number, y: number, w: number, h: number): boolean;
|
||||
text_input(id: string, x: number, y: number, w: number): string;
|
||||
slider(id: string, x: number, y: number, w: number, min: number, max: number): number;
|
||||
// server synced slots, same rules as container screens
|
||||
slots(container: string, layout: { slot: number; x: number; y: number }[], x: number, y: number): void;
|
||||
// false while a text input has the keyboard
|
||||
key_pressed(key: KeyCode): boolean;
|
||||
// shown next to the mouse over everything else, the last one this frame wins
|
||||
@@ -77,10 +110,12 @@ export interface Graphics {
|
||||
block_mouse(x: number, y: number, w: number, h: number): void;
|
||||
}
|
||||
|
||||
// drawn on every inventory screen (the player's inventory, chests, furnaces...), over it, like just enough items'
|
||||
// item list. it gets the screen's box to lay itself out around
|
||||
// drawn on every menu's screen (the player's inventory, chests, furnaces...), over it, like just enough items' item
|
||||
// list. it gets the screen's box to lay itself out around
|
||||
export interface InventoryOverlay {
|
||||
on_render(g: Graphics, screen: InventoryScreenView): void;
|
||||
// the screen closed for any reason, like the player closing their inventory
|
||||
on_close?(): void;
|
||||
}
|
||||
|
||||
export interface InventoryScreenView {
|
||||
@@ -93,9 +128,12 @@ export interface InventoryScreenView {
|
||||
}
|
||||
|
||||
export interface ClientUi {
|
||||
register_screen<Props>(id: Id, create: (props: Props) => ModScreen<Props>): void;
|
||||
// open a screen that doesn't involve the server, like a settings page
|
||||
open<Props>(id: Id, props: Props): void;
|
||||
// the screen for a menu type of this mod, like RegisterMenuScreensEvent. called each time one opens
|
||||
register_menu_screen(type: Id, create: (menu: ClientMenu) => MenuScreen): void;
|
||||
// a screen that doesn't involve the server, like a settings page, over whatever's open
|
||||
open(screen: Screen): void;
|
||||
// closes the screen on top
|
||||
close(): void;
|
||||
add_inventory_overlay(overlay: InventoryOverlay): void;
|
||||
}
|
||||
|
||||
|
||||
+58
-43
@@ -18,6 +18,7 @@ export interface ServerContext {
|
||||
items: ItemApi;
|
||||
recipes: RecipeApi;
|
||||
tags: TagApi;
|
||||
menus: MenuRegistry;
|
||||
ui: ServerUi;
|
||||
net: ServerNet;
|
||||
storage: ModStorage;
|
||||
@@ -62,7 +63,7 @@ export interface ComponentRegistry {
|
||||
register_item<P>(id: Id, component: ItemComponent<P>): void;
|
||||
}
|
||||
|
||||
// commands, fabric's (brigadier's) way: see common/commands.ts and docs/modding/misc/commands.md
|
||||
// commands, brigadier's way like neoforge's: see common/commands.ts and docs/modding/misc/commands.md
|
||||
|
||||
// who ran a command
|
||||
export interface CommandSource {
|
||||
@@ -79,7 +80,7 @@ export interface Command {
|
||||
}
|
||||
|
||||
export interface CommandRegistry {
|
||||
// a tree of literal() and argument() nodes, like fabric's dispatcher.register(literal("heal").then(...))
|
||||
// a tree of literal() and argument() nodes, like neoforge's dispatcher.register(literal("heal").then(...))
|
||||
register(node: CommandNode<CommandSource>): void;
|
||||
register(name: string, command: Command): void;
|
||||
literal(name: string): CommandNode<CommandSource>;
|
||||
@@ -304,7 +305,7 @@ export interface RecipeApi {
|
||||
list(type: Id): any[];
|
||||
}
|
||||
|
||||
// guis
|
||||
// forms, not built yet: menus whose screens the engine provides, see docs/modding_plan.md
|
||||
|
||||
export interface ActionForm {
|
||||
title: string;
|
||||
@@ -331,60 +332,74 @@ export interface ModalForm {
|
||||
|
||||
export type FormResult<T> = ({ canceled: true } & Partial<T>) | ({ canceled: false } & T);
|
||||
|
||||
// menus, like neoforge's: the server side of a gui, see docs/modding/inventories/menus.md
|
||||
|
||||
export type SlotFilter = "smeltable" | "fuel" | ((item: ItemStack) => boolean);
|
||||
|
||||
export interface ContainerScreenOptions {
|
||||
container: Container;
|
||||
// x and y in slot units, below the player's inventory. can be fractional
|
||||
layout: {
|
||||
slot: number;
|
||||
x: number;
|
||||
y: number;
|
||||
// what can be put in, checked on the server
|
||||
filter?: SlotFilter;
|
||||
// can only be taken from, all at once, like the furnace result
|
||||
output_only?: boolean;
|
||||
// runs when a player takes from an output_only slot, before they get it. false stops them taking it
|
||||
on_take?: (item: ItemStack) => boolean;
|
||||
// a texture drawn in the slot while it's empty, like minecraft's armor slots
|
||||
icon?: Id;
|
||||
}[];
|
||||
// height of the screen's area in slots, fits the layout and bars when not set
|
||||
rows?: number;
|
||||
// progress bars, filled with the property value divided by the property max
|
||||
bars?: {
|
||||
x: number;
|
||||
y: number;
|
||||
value: string;
|
||||
max: string;
|
||||
direction: "up" | "right";
|
||||
empty_texture: Id;
|
||||
full_texture: Id;
|
||||
}[];
|
||||
export interface SlotOptions {
|
||||
// gui pixels from the screen's top left to the slot's 18 pixel frame
|
||||
x: number;
|
||||
y: number;
|
||||
// what can be put in, checked on the server
|
||||
filter?: SlotFilter;
|
||||
// can only be taken from, all at once, like the furnace result
|
||||
output_only?: boolean;
|
||||
// runs when a player takes from an output_only slot, before they get it. false stops them taking it
|
||||
on_take?: (item: ItemStack) => boolean;
|
||||
// a texture drawn in the slot while it's empty, like minecraft's armor slots
|
||||
icon?: Id;
|
||||
}
|
||||
|
||||
export interface ScreenHandle<Props = unknown> {
|
||||
// what a menu type's build function gets, like neoforge's server menu constructor
|
||||
export interface MenuBuilder {
|
||||
title: string;
|
||||
// sent to the client once when it opens, any json up to 16 KB, like IContainerFactory's extra data
|
||||
open_data: unknown;
|
||||
// a slot of a container from ctx.containers, or of player.inventory. returns its menu slot index
|
||||
add_slot(container: Container, slot: number, options: SlotOptions): number;
|
||||
// the player's 36 slots, like addStandardInventorySlots: the rest of the inventory in 3 rows from x and y, the
|
||||
// hotbar 58 pixels under them. added in inventory order, so inventory slot i is the returned index plus i
|
||||
add_player_inventory(x: number, y: number): number;
|
||||
// a number the screen shows, like DataSlot: read every tick and sent when it changes
|
||||
add_data(name: string, get: () => number): void;
|
||||
// checked every tick, false closes it
|
||||
still_valid(check: (player: Player) => boolean): void;
|
||||
// closes it when the player is over 8 blocks from the block, or it's been broken or replaced
|
||||
still_valid_near(block: BlockRef): void;
|
||||
// a button the client pressed, like clickMenuButton. any number can come, check it
|
||||
on_button(fn: (player: Player, id: number) => void): void;
|
||||
// shift clicks: the menu slot indexes the stack in this one tries, in order. the default rule when not set
|
||||
quick_move(fn: (player: Player, index: number) => number[]): void;
|
||||
on_tick(fn: () => void): void;
|
||||
// when it closes for any reason: the player closed it, left, opened another, or it was closed
|
||||
on_close(fn: () => void): void;
|
||||
}
|
||||
|
||||
export interface MenuHandle {
|
||||
readonly player: Player;
|
||||
readonly open: boolean;
|
||||
// numbers the bars show, synced to the player when they change
|
||||
set_property(id: string, value: number): void;
|
||||
update(props: Props): void; // custom screens only
|
||||
close(): void;
|
||||
// when it closes for any reason: the player closed it, left, opened another screen, or it was closed
|
||||
on_close(fn: () => void): void;
|
||||
}
|
||||
|
||||
export interface MenuType<Args> {
|
||||
readonly id: Id;
|
||||
// like openMenu: builds a menu for the player with these args and opens it, closing whatever they had open
|
||||
open(player: Player, args: Args): MenuHandle;
|
||||
}
|
||||
|
||||
export interface MenuRegistry {
|
||||
// like registering a MenuType. the client script gives it a screen with ctx.ui.register_menu_screen
|
||||
register<Args = undefined>(
|
||||
id: Id,
|
||||
build: (menu: MenuBuilder, args: Args, player: Player) => void,
|
||||
): MenuType<Args>;
|
||||
}
|
||||
|
||||
export interface ServerUi {
|
||||
message_form(player: Player, form: MessageForm): Promise<FormResult<{ selection: 0 | 1 }>>;
|
||||
action_form(player: Player, form: ActionForm): Promise<FormResult<{ selection: number }>>;
|
||||
modal_form(player: Player, form: ModalForm): Promise<FormResult<{ values: (boolean | number | string)[] }>>;
|
||||
open_container(player: Player, options: ContainerScreenOptions): ScreenHandle;
|
||||
open_screen<Props>(
|
||||
player: Player,
|
||||
id: Id,
|
||||
props: Props,
|
||||
options?: { containers?: Record<string, Container> },
|
||||
): ScreenHandle<Props>;
|
||||
}
|
||||
|
||||
// mod channels
|
||||
|
||||
Reference in New Issue
Block a user