Files
bworld/client/native/credits_screen.ts
T

63 lines
2.4 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import { canvas, draw_rect, draw_text, measure_text } from "../renderer/mod.ts";
import { GuiScreen } from "../gui/gui_screen.ts";
import { credit_sections } from "../gui/credits_screen.ts";
import { Button, ListBox, TEXT_SCALE, wrap_text } from "../gui/widgets.ts";
const MAX_WIDTH = 1200;
const MARGIN = 32;
const TOP = 90;
const BUTTON_WIDTH = 200;
const BUTTON_HEIGHT = 48;
// the list's own padding around its text
const TEXT_PADDING = 24;
// the credits in the desktop app, drawn in the window with the markdown as it's written, since there's no browser to
// show it as a page. after them, the credits of the libraries the app comes with (see desktop/libraries.ts)
export class TextCreditsScreen extends GuiScreen {
#list = new ListBox(0, 0, () => {});
#back: Button;
#sections: { heading: string; text: string }[];
// what the lines were wrapped for
#wrapped_width = 0;
constructor(on_back: () => void, libraries: string | undefined) {
super();
this.#back = new Button("Back", BUTTON_WIDTH, BUTTON_HEIGHT, on_back);
this.#sections = [...credit_sections(), ...(libraries ? [{ heading: "Libraries", text: libraries }] : [])];
}
on_tick(_delta: number): void {
const list = this.#list;
list.width = Math.min(MAX_WIDTH, canvas.width - MARGIN * 2);
list.height = Math.max(BUTTON_HEIGHT, canvas.height - TOP - BUTTON_HEIGHT - MARGIN * 2);
list.x = (canvas.width - list.width) / 2;
list.y = TOP;
if (list.width !== this.#wrapped_width) {
this.#wrapped_width = list.width;
list.rows = credit_lines(this.#sections, list.width - TEXT_PADDING).map((text) => ({ text }));
}
this.#back.x = (canvas.width - BUTTON_WIDTH) / 2;
this.#back.y = list.y + list.height + MARGIN;
list.handle_input();
this.#back.handle_input();
}
on_render(): void {
draw_rect(0, 0, canvas.width, canvas.height, [0, 0, 0, 0.6]);
const scale = TEXT_SCALE * 2;
draw_text("Credits", (canvas.width - measure_text("Credits", scale)) / 2, TOP / 2 - 12, scale);
this.#list.render();
this.#back.render();
}
on_close(): void {}
}
// every section's heading and text, wrapped at spaces to width pixels
function credit_lines(sections: { heading: string; text: string }[], width: number): string[] {
return sections.flatMap(({ heading, text }) => [heading, "", ...wrap_text(text, width), ""]);
}