Files
bworld/client/native/credits_screen.ts
T

79 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 } 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
export class TextCreditsScreen extends GuiScreen {
#list = new ListBox(0, 0, () => {});
#back: Button;
// what the lines were wrapped for
#wrapped_width = 0;
constructor(on_back: () => void) {
super();
this.#back = new Button("Back", BUTTON_WIDTH, BUTTON_HEIGHT, on_back);
}
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(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(width: number): string[] {
const lines: string[] = [];
for (const { heading, text } of credit_sections()) {
lines.push(heading, "");
for (const line of text.split("\n")) {
let current = "";
for (const word of line.split(" ")) {
const next = current ? `${current} ${word}` : word;
if (current && measure_text(next, TEXT_SCALE) > width) {
lines.push(current);
current = word;
} else {
current = next;
}
}
lines.push(current);
}
lines.push("");
}
return lines;
}