Files
bworld/mods/dev/scripts/schematic_editor.ts
2026-11-22 16:19:10 -03:00

138 lines
5.1 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// a block for working on schematics in game. right clicking it opens a screen listing every registered schematic. the
// one picked is kept in its data, and can be placed above the block, cleared away again, or saved after changing it
import type { ClientContext } from "cubi/client";
import type { Player, Position, ServerContext } from "cubi/server";
const EDITOR = "dev:schematic_editor";
// the server tells a player to open the screen, and the client tells the server what they picked or pressed
const OPEN = "dev:schematic_editor_open";
const SELECT = "dev:schematic_editor_select";
const ACTION = "dev:schematic_editor_action";
interface OpenMessage {
ids: string[];
selected?: string;
}
type Action = "place" | "clear" | "save";
interface EditorData {
schematic?: string;
// the box the last placed schematic fills, both corners in it, for clear and save
placed?: { from: Position; to: Position };
}
export function register_schematic_editor(ctx: ServerContext) {
// the editor each player last opened, by player id, so what they do goes to that block
const opened = new Map<string, Position>();
const editor_of = (player: Player) => {
const at = opened.get(player.id);
const block = at && ctx.world.get_block_ref(at.x, at.y, at.z);
return block?.id === EDITOR ? block : undefined;
};
ctx.components.register_block(EDITOR, {
on_interact(block, _params, player) {
opened.set(player.id, { x: block.x, y: block.y, z: block.z });
const message: OpenMessage = { ids: ctx.schematics.ids(), selected: block.data?.schematic };
ctx.net.send(player, OPEN, message);
return true;
},
});
ctx.net.on<string>(SELECT, (player, schematic) => {
const block = editor_of(player);
if (!block || !ctx.schematics.ids().includes(schematic)) return;
block.data = { ...block.data, schematic };
});
ctx.net.on<Action>(ACTION, async (player, action) => {
const block = editor_of(player);
if (!block) return;
const data: EditorData = block.data ?? {};
if (action === "place") {
const schematic = data.schematic && ctx.schematics.get(data.schematic);
if (!schematic) return player.send_message("Pick a schematic first");
// a smaller one placed over a bigger one would leave the rest of it behind
if (data.placed) ctx.world.fill(data.placed.from, data.placed.to, "cubi:air");
const from = { x: block.x, y: block.y + 1, z: block.z };
const [sx, sy, sz] = schematic.size;
ctx.schematics.place(schematic.id!, { position: from });
block.data = {
...data,
placed: { from, to: { x: from.x + sx - 1, y: from.y + sy - 1, z: from.z + sz - 1 } },
};
} else if (action === "clear") {
if (!data.placed) return;
ctx.world.fill(data.placed.from, data.placed.to, "cubi:air");
block.data = { ...data, placed: undefined };
} else if (action === "save") {
if (!data.placed) return player.send_message("Place a schematic first");
try {
player.send_message(
`Saved ${data.schematic} to ${await ctx.schematics.export(data.placed.from, data.placed.to)}`,
);
} catch (e) {
player.send_message(`Couldn't save it: ${(e as Error).message}`);
}
}
});
}
// how many schematics fit on a page of the list, and the size of the buttons
const PAGE_SIZE = 10;
const BUTTON_WIDTH = 200;
const BUTTON_HEIGHT = 20;
export function register_schematic_editor_screen(ctx: ClientContext) {
ctx.net.on<OpenMessage>(OPEN, ({ ids, selected }) => {
const pages = Math.max(1, Math.ceil(ids.length / PAGE_SIZE));
let page = Math.floor(Math.max(0, ids.indexOf(selected ?? "")) / PAGE_SIZE);
ctx.ui.open({
on_render(g) {
const width = BUTTON_WIDTH + 20;
const height = (PAGE_SIZE + 2) * BUTTON_HEIGHT + 50;
const left = (g.width - width) / 2;
const top = (g.height - height) / 2;
const x = left + 10;
g.panel(left, top, width, height);
g.text(`Schematic: ${selected ?? "none"}`, x, top + 8);
const shown = ids.slice(page * PAGE_SIZE, (page + 1) * PAGE_SIZE);
shown.forEach((id, i) => {
const label = id === selected ? `> ${id} <` : id;
if (g.button(label, x, top + 22 + i * BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT)) {
selected = id;
ctx.net.send(SELECT, id);
}
});
const pager = top + 26 + PAGE_SIZE * BUTTON_HEIGHT;
const arrow = 30;
if (page > 0 && g.button("<", x, pager, arrow, BUTTON_HEIGHT)) page--;
if (page < pages - 1 && g.button(">", x + BUTTON_WIDTH - arrow, pager, arrow, BUTTON_HEIGHT)) page++;
const label = `${page + 1} / ${pages}`;
g.text(
label,
x + (BUTTON_WIDTH - g.measure_text(label)) / 2,
pager + (BUTTON_HEIGHT - g.text_height) / 2,
);
const actions = pager + BUTTON_HEIGHT + 4;
const action_width = BUTTON_WIDTH / 4;
(["place", "clear", "save"] as const).forEach((action, i) => {
const name = action[0].toUpperCase() + action.slice(1);
if (g.button(name, x + i * action_width, actions, action_width, BUTTON_HEIGHT)) {
ctx.net.send(ACTION, action);
}
});
// the preview isn't built yet
g.button("Preview", x + 3 * action_width, actions, action_width, BUTTON_HEIGHT);
},
});
});
}