Change whole gui system
This commit is contained in:
+22
-9
@@ -2,7 +2,7 @@
|
||||
// see docs/modding/networking/index.md
|
||||
import { AIR } from "$/common/constants.ts";
|
||||
import { BlockRegistry, EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import type { ClientContext, InventoryOverlay } from "$/common/mod_api/client.ts";
|
||||
import type { ClientContext, ClientMenu, InventoryOverlay, MenuScreen } from "$/common/mod_api/client.ts";
|
||||
import { ModListing, ModLoadError, RecipeBook, register_mod_data } from "$/common/mod_loader.ts";
|
||||
import { type Bmod, read_bmod } from "$/common/bmod.ts";
|
||||
import { display_name } from "$/common/utils.ts";
|
||||
@@ -10,6 +10,7 @@ import { tag_api } from "$/common/tags.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import type { Client } from "./client.ts";
|
||||
import { code_url, fetch_verified } from "./handshake.ts";
|
||||
import { GuiModScreen } from "./gui/gui_mod_screens.ts";
|
||||
|
||||
// each loaded mod's credits file, for the credits screen
|
||||
export const mod_credits: { name: string; version: string; text: string }[] = [];
|
||||
@@ -20,6 +21,9 @@ let client: Client | undefined;
|
||||
// what mods draw on inventory screens, see GuiInventoryScreen
|
||||
export const inventory_overlays: { mod: string; overlay: InventoryOverlay }[] = [];
|
||||
|
||||
// each menu type's screen, see ctx.ui.register_menu_screen
|
||||
export const menu_screens = new Map<string, { mod: string; create: (menu: ClientMenu) => MenuScreen }>();
|
||||
|
||||
// mod channels, see ctx.net: who listens on each channel
|
||||
const net_handlers = new Map<string, { mod: string; handler: (data: unknown) => void }[]>();
|
||||
|
||||
@@ -68,6 +72,7 @@ export async function load_client_mods(listings: ModListing[], bmods: Bmod[]) {
|
||||
const recipes = register_mod_data(bmods.map((bmod) => ({ id: bmod.manifest.id, data: bmod.data })));
|
||||
recipe_book = recipes;
|
||||
inventory_overlays.length = 0;
|
||||
menu_screens.clear();
|
||||
net_handlers.clear();
|
||||
mod_names.clear();
|
||||
for (const listing of listings) mod_names.set(listing.id, listing.name);
|
||||
@@ -101,12 +106,9 @@ function client_context(listing: ModListing): ClientContext {
|
||||
return client;
|
||||
};
|
||||
|
||||
const not_yet_fn = (name: string) => () => {
|
||||
throw new Error(`[${mod}] ctx.ui.${name} isn't implemented yet (custom screens, see docs/modding_plan.md)`);
|
||||
};
|
||||
const own = (channel: string) => {
|
||||
if (!channel.startsWith(`${mod}:`)) {
|
||||
throw new Error(`[${mod}] channel ${channel} must be in the namespace "${mod}"`);
|
||||
const own = (id: string, what = "channel") => {
|
||||
if (!id.startsWith(`${mod}:`)) {
|
||||
throw new Error(`[${mod}] ${what} ${id} must be in the namespace "${mod}"`);
|
||||
}
|
||||
};
|
||||
const known_item = (id: string) => {
|
||||
@@ -118,8 +120,19 @@ function client_context(listing: ModListing): ClientContext {
|
||||
return {
|
||||
mod: { id: mod, version: listing.version },
|
||||
ui: {
|
||||
register_screen: not_yet_fn("register_screen"),
|
||||
open: not_yet_fn("open"),
|
||||
register_menu_screen(type, create) {
|
||||
own(type, "menu type");
|
||||
if (menu_screens.has(type)) throw new Error(`[${mod}] ${type} already has a screen`);
|
||||
menu_screens.set(type, { mod, create });
|
||||
},
|
||||
open(screen) {
|
||||
if (typeof screen?.on_render !== "function") throw new Error(`[${mod}] a screen needs an on_render`);
|
||||
const game = need_client();
|
||||
game.push_screen(new GuiModScreen(game, mod, screen));
|
||||
},
|
||||
close() {
|
||||
need_client().pop_screen();
|
||||
},
|
||||
add_inventory_overlay(overlay) {
|
||||
inventory_overlays.push({ mod, overlay });
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user