Change whole gui system
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
// screens mods' client scripts make, see docs/modding/gui/index.md: the screens of their menus, and screens with no
|
||||
// menu. a mod's screen that throws is closed, with the error in the console
|
||||
import { TICK_DELTA } from "$/common/constants.ts";
|
||||
import type { ClientMenu as ModClientMenu, MenuScreen, Screen } from "$/common/mod_api/client.ts";
|
||||
import { canvas } from "../renderer/mod.ts";
|
||||
import type { Client } from "../client.ts";
|
||||
import type { ClientMenu } from "../menu.ts";
|
||||
import { GuiMenuScreen, GuiScreen } from "./gui_screen.ts";
|
||||
import { ModGraphics } from "./mod_graphics.ts";
|
||||
import { GUI_SCALE } from "./gui_sprites.ts";
|
||||
|
||||
// runs on_tick 20 times a second from the frames' deltas, like the game's own ticks
|
||||
class Ticker {
|
||||
#pending = 0;
|
||||
|
||||
run(delta: number, tick: () => void) {
|
||||
this.#pending += delta;
|
||||
for (let i = 0; this.#pending >= TICK_DELTA && i < 10; i++) {
|
||||
this.#pending -= TICK_DELTA;
|
||||
tick();
|
||||
}
|
||||
this.#pending = Math.min(this.#pending, TICK_DELTA);
|
||||
}
|
||||
}
|
||||
|
||||
// a menu the server opened, drawn by the screen its mod registered for the menu type
|
||||
export class GuiModMenuScreen extends GuiMenuScreen {
|
||||
#client: Client;
|
||||
#mod: string;
|
||||
#screen: MenuScreen;
|
||||
#ticker = new Ticker();
|
||||
|
||||
constructor(client: Client, mod: string, menu: ClientMenu, create: (menu: ModClientMenu) => MenuScreen) {
|
||||
const send = client.connection.send.bind(client.connection);
|
||||
// this, once super has run
|
||||
const self: { screen?: GuiModMenuScreen } = {};
|
||||
const api: ModClientMenu = {
|
||||
type: menu.type,
|
||||
title: menu.title,
|
||||
open_data: menu.open_data,
|
||||
get data() {
|
||||
return menu.data;
|
||||
},
|
||||
click_button: (id) => {
|
||||
if (!Number.isInteger(id)) throw new Error(`a menu button is a whole number, not ${id}`);
|
||||
if (self.screen && client.screens.includes(self.screen)) {
|
||||
send({ type: "menu_button", menu: menu.id, button: id });
|
||||
}
|
||||
},
|
||||
close: () => {
|
||||
if (self.screen) client.close_screen(self.screen);
|
||||
},
|
||||
};
|
||||
const screen = create(api);
|
||||
if (!(screen.width > 0 && screen.height > 0)) throw new Error(`the screen for ${menu.type} has no size`);
|
||||
super(client.player.inventories, send, menu, screen.width, screen.height);
|
||||
this.#client = client;
|
||||
this.#mod = mod;
|
||||
this.#screen = screen;
|
||||
self.screen = this;
|
||||
}
|
||||
|
||||
#guarded(what: string, fn: () => void) {
|
||||
try {
|
||||
fn();
|
||||
} catch (e) {
|
||||
console.error(`[${this.#mod}] the ${this.menu.type} screen's ${what} threw, closing it:`, e);
|
||||
this.#client.close_screen(this);
|
||||
}
|
||||
}
|
||||
|
||||
override on_tick(delta: number): void {
|
||||
super.on_tick(delta);
|
||||
const tick = this.#screen.on_tick;
|
||||
if (tick) this.#ticker.run(delta, () => this.#guarded("on_tick", () => tick.call(this.#screen)));
|
||||
}
|
||||
|
||||
render_background(g: ModGraphics): void {
|
||||
const render = this.#screen.on_render_background;
|
||||
if (render) this.#guarded("on_render_background", () => render.call(this.#screen, g));
|
||||
}
|
||||
|
||||
render_labels(g: ModGraphics): void {
|
||||
const render = this.#screen.on_render_labels;
|
||||
if (render) this.#guarded("on_render_labels", () => render.call(this.#screen, g));
|
||||
}
|
||||
}
|
||||
|
||||
// a screen with no menu, opened by a client script
|
||||
export class GuiModScreen extends GuiScreen {
|
||||
#mod: string;
|
||||
#screen: Screen;
|
||||
#client: Client;
|
||||
#ticker = new Ticker();
|
||||
graphics = new ModGraphics();
|
||||
// the window size on_init last saw, it runs again when it changes
|
||||
#size = "";
|
||||
|
||||
constructor(client: Client, mod: string, screen: Screen) {
|
||||
super();
|
||||
this.#client = client;
|
||||
this.#mod = mod;
|
||||
this.#screen = screen;
|
||||
}
|
||||
|
||||
#guarded(what: string, fn: () => void) {
|
||||
try {
|
||||
fn();
|
||||
} catch (e) {
|
||||
console.error(`[${this.#mod}] a screen's ${what} threw, closing it:`, e);
|
||||
this.#client.close_screen(this);
|
||||
}
|
||||
}
|
||||
|
||||
override get typing() {
|
||||
return this.graphics.typing;
|
||||
}
|
||||
|
||||
override close_on_escape(): boolean {
|
||||
let close = true;
|
||||
const should = this.#screen.should_close_on_escape;
|
||||
if (should) this.#guarded("should_close_on_escape", () => close = should.call(this.#screen) !== false);
|
||||
return close;
|
||||
}
|
||||
|
||||
on_tick(delta: number): void {
|
||||
const size = `${canvas.width}x${canvas.height}`;
|
||||
if (size !== this.#size) {
|
||||
this.#size = size;
|
||||
const init = this.#screen.on_init;
|
||||
const [w, h] = [canvas.width / GUI_SCALE, canvas.height / GUI_SCALE];
|
||||
if (init) this.#guarded("on_init", () => init.call(this.#screen, w, h));
|
||||
}
|
||||
const tick = this.#screen.on_tick;
|
||||
if (tick) this.#ticker.run(delta, () => this.#guarded("on_tick", () => tick.call(this.#screen)));
|
||||
}
|
||||
|
||||
on_render(): void {
|
||||
this.graphics.begin();
|
||||
this.#guarded("on_render", () => this.#screen.on_render(this.graphics));
|
||||
this.graphics.end();
|
||||
}
|
||||
|
||||
on_close(): void {
|
||||
const close = this.#screen.on_close;
|
||||
if (close) this.#guarded("on_close", () => close.call(this.#screen));
|
||||
}
|
||||
|
||||
override on_removed(): void {
|
||||
const removed = this.#screen.on_removed;
|
||||
if (removed) this.#guarded("on_removed", () => removed.call(this.#screen));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user