Files
bworld/client/gui/confirm_mods_screen.ts

90 lines
3.5 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import { InputManager } from "@/client/input_manager.ts";
import { canvas, draw_rect, draw_text, measure_text } from "@/client/renderer/mod.ts";
import type { ServerAddress, Welcome } from "@/client/handshake.ts";
import { GuiScreen } from "./gui_screen.ts";
import { Button, LIST_ROW_HEIGHT, ListBox, TEXT_HEIGHT, TEXT_SCALE, wrap_text } from "./widgets.ts";
const MAX_WIDTH = 720;
const MARGIN = 32;
const GAP = 20;
const BUTTON_HEIGHT = 48;
const TITLE_SCALE = 4;
const LINE_HEIGHT = TEXT_HEIGHT * TEXT_SCALE + 6;
const WARNING = "This server runs these mods in the game. They can do anything the game can, including reading what " +
"it saved. Only join servers you trust.";
// asks before running the mods of a server that isn't the page's own, see docs/modding/concepts/security.md. it's
// shown over the screen that's joining, and answer gets whether the player wants to join
export class ConfirmModsScreen extends GuiScreen {
#title: string;
#warning: string[] = [];
// what the warning was wrapped for
#wrapped_width = 0;
#list = new ListBox(0, 0, () => {});
#cancel: Button;
#join: Button;
constructor(address: ServerAddress, welcome: Welcome, answer: (join: boolean) => void) {
super();
this.#title = `Join ${address.base.host}?`;
this.#list.rows = welcome.mods.map((mod) => {
// it can change anything about the game, not just add to it
const access = mod.engine_access ? ", changes the engine" : "";
return { text: `${mod.name} ${mod.version} (${mod.id}${access})` };
});
this.#cancel = new Button("Cancel", 0, BUTTON_HEIGHT, () => answer(false));
this.#join = new Button("Join", 0, BUTTON_HEIGHT, () => answer(true));
}
on_tick(_delta: number): void {
const width = Math.min(MAX_WIDTH, canvas.width - MARGIN * 2);
if (width !== this.#wrapped_width) {
this.#wrapped_width = width;
this.#warning = wrap_text(WARNING, width);
}
// the list gets what room the title, the warning and the buttons leave
const above = this.#warning_y + this.#warning.length * LINE_HEIGHT + GAP;
const room = canvas.height - MARGIN * 2 - above - GAP - BUTTON_HEIGHT;
const list = this.#list;
list.width = width;
list.height = Math.max(LIST_ROW_HEIGHT, Math.min(list.rows.length * LIST_ROW_HEIGHT, room));
this.x = (canvas.width - width) / 2;
this.y = Math.max(MARGIN, (canvas.height - above - list.height - GAP - BUTTON_HEIGHT) / 2);
list.x = this.x;
list.y = this.y + above;
const half = (width - GAP) / 2;
for (const [i, button] of [this.#cancel, this.#join].entries()) {
button.width = half;
button.x = this.x + i * (half + GAP);
button.y = list.y + list.height + GAP;
}
// only a click joins, never a key pressed for something else, like the enter that joined the server
if (InputManager.is_key_pressed("Escape")) return this.#cancel.on_press();
list.handle_input();
this.#cancel.handle_input();
this.#join.handle_input();
}
on_render(): void {
draw_rect(0, 0, canvas.width, canvas.height, [0, 0, 0, 0.85]);
draw_text(this.#title, (canvas.width - measure_text(this.#title, TITLE_SCALE)) / 2, this.y, TITLE_SCALE);
for (const [i, line] of this.#warning.entries()) {
draw_text(line, this.x, this.y + this.#warning_y + i * LINE_HEIGHT, TEXT_SCALE, [0.85, 0.85, 0.85, 1]);
}
this.#list.render();
this.#cancel.render();
this.#join.render();
}
on_close(): void {}
// below the title, from the screen's top
get #warning_y() {
return TEXT_HEIGHT * TITLE_SCALE + GAP;
}
}