Start maybe working on a native app

This commit is contained in:
2026-11-22 17:38:28 -03:00
parent 681d3afb4d
commit a552883795
31 changed files with 1881 additions and 678 deletions
+10 -9
View File
@@ -55,7 +55,7 @@ export class CreditsScreen extends GuiScreen {
const content = document.createElement("div");
content.style.cssText = "overflow-y:auto;flex:1;min-height:0;padding-right:8px;";
content.innerHTML = this.#sections().map(({ heading, text }) =>
content.innerHTML = credit_sections().map(({ heading, text }) =>
`<section><h2 style="border-bottom:1px solid #374151;padding-bottom:4px">${escape_html(heading)}</h2>` +
`${markdown.parse(text, { async: false })}</section>`
).join("");
@@ -72,14 +72,6 @@ export class CreditsScreen extends GuiScreen {
document.body.append(this.#overlay);
}
#sections() {
const sections = [{ heading: "cubi", text: AssetManager.instance.get<string>("cubi:assets_text") ?? "" }];
for (const mod of mod_credits) {
sections.push({ heading: `${mod.name} ${mod.version}`, text: mod.text });
}
return sections;
}
on_tick(_delta: number): void {}
on_render(): void {
@@ -91,6 +83,15 @@ export class CreditsScreen extends GuiScreen {
}
}
// the engine's assets/ASSETS.md, then each loaded mod's credits file, as markdown
export function credit_sections(): { heading: string; text: string }[] {
const sections = [{ heading: "cubi", text: AssetManager.instance.get<string>("cubi:assets_text") ?? "" }];
for (const mod of mod_credits) {
sections.push({ heading: `${mod.name} ${mod.version}`, text: mod.text });
}
return sections;
}
function escape_html(text: string) {
return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
}
+3 -15
View File
@@ -10,6 +10,7 @@ import { RENDER_LAYERS } from "@/common/everything_registry.ts";
import { atlas_png } from "@/client/atlas.ts";
import { RenderBenchmark } from "@/client/benchmark/benchmark.ts";
import { format_report } from "@/client/benchmark/report.ts";
import { platform } from "@/client/platform.ts";
// how often the totals that go through every chunk are worked out again, so the overlay doesn't slow the frames it's
// measuring
@@ -113,7 +114,7 @@ export class DebugOverlay {
if (copy) {
const switches = { ...render_debug, ...level_renderer.draw };
lines.push(Object.entries(switches).map(([key, on]) => ` ${key}: ${on}`).join("\n"));
copy_text(lines.join("\n")).catch(() => {});
platform.write_clipboard(lines.join("\n")).catch(() => {});
}
}
@@ -137,7 +138,7 @@ export class DebugOverlay {
// right after the benchmark, firefox and safari only let the page copy from a click
#copy_benchmark() {
copy_text(this.#benchmark_report!).then(
platform.write_clipboard(this.#benchmark_report!).then(
() => this.#benchmark_status = "The benchmark's results are on the clipboard",
() => this.#benchmark_status = "Click above to copy the benchmark's results",
);
@@ -187,16 +188,3 @@ export class DebugOverlay {
}
}
}
// pages that aren't https or localhost have no clipboard api, like a server on another computer, but the old way
// still copies from a click
function copy_text(text: string): Promise<void> {
if (navigator.clipboard) return navigator.clipboard.writeText(text);
const area = document.createElement("textarea");
area.value = text;
document.body.append(area);
area.select();
const copied = document.execCommand("copy");
area.remove();
return copied ? Promise.resolve() : Promise.reject(new Error("Couldn't copy"));
}
+7 -4
View File
@@ -8,7 +8,7 @@ import { InputManager } from "@/client/input_manager.ts";
import { canvas, draw_rect, draw_text, measure_text } from "@/client/renderer/mod.ts";
import { server_address } from "@/client/handshake.ts";
import { GuiScreen } from "./gui_screen.ts";
import { CreditsScreen } from "./credits_screen.ts";
import { platform } from "../platform.ts";
import type { JoinServer } from "./title_screen.ts";
import { Button, EditBox, ListBox, TEXT_HEIGHT, TEXT_SCALE, TextInput } from "./widgets.ts";
@@ -37,7 +37,7 @@ export class MainMenuScreen extends GuiScreen {
new Button("Singleplayer", WIDTH, ROW_HEIGHT, () => show(new WorldListScreen(show, join))),
multiplayer,
mods,
new Button("Credits", WIDTH, ROW_HEIGHT, () => show(new CreditsScreen(back))),
new Button("Credits", WIDTH, ROW_HEIGHT, () => show(platform.credits_screen(back))),
];
}
@@ -145,7 +145,7 @@ class WorldListScreen extends GuiScreen {
this.#status.set(`Starting ${world.name}...`);
try {
await desktop("play", { world: world.name });
await this.#join(server_address(location.host), SINGLEPLAYER_NAME, (text) => this.#status.set(text));
await this.#join(server_address(platform.page.host), SINGLEPLAYER_NAME, (text) => this.#status.set(text));
} catch (e) {
this.#status.fail(e);
this.#busy = false;
@@ -294,7 +294,10 @@ function draw_centered(text: string, y: number, scale: number) {
// asks the desktop app, see desktop/main.ts. throws with what the app said went wrong
async function desktop(path: string, body: unknown = {}): Promise<Response> {
const response = await fetch(`/desktop/${path}`, { method: "POST", body: JSON.stringify(body) });
const response = await fetch(new URL(`/desktop/${path}`, platform.page), {
method: "POST",
body: JSON.stringify(body),
});
if (!response.ok) throw new Error(await response.text());
return response;
}
+2 -2
View File
@@ -5,7 +5,7 @@ import type { Client } from "@/client/client.ts";
import { canvas, draw_rect, draw_text, measure_text } from "@/client/renderer/mod.ts";
import { GuiScreen } from "./gui_screen.ts";
import { Button, TEXT_HEIGHT } from "./widgets.ts";
import { CreditsScreen } from "./credits_screen.ts";
import { platform } from "../platform.ts";
const BUTTON_WIDTH = 440;
const BUTTON_HEIGHT = 48;
@@ -20,7 +20,7 @@ export class PauseScreen extends GuiScreen {
this.buttons = [
new Button("Back to game", BUTTON_WIDTH, BUTTON_HEIGHT, () => client.pop_screen()),
new Button("Credits", BUTTON_WIDTH, BUTTON_HEIGHT, () => {
client.push_screen(new CreditsScreen(() => client.pop_screen()));
client.push_screen(platform.credits_screen(() => client.pop_screen()));
}),
new Button("Disconnect", BUTTON_WIDTH, BUTTON_HEIGHT, () => client.disconnect()),
];
+11 -11
View File
@@ -7,7 +7,7 @@ import { canvas, draw_rect, draw_text, measure_text } from "@/client/renderer/mo
import { default_server, HandshakeError, server_address, ServerAddress } from "@/client/handshake.ts";
import { default_player_name } from "@/client/network.ts";
import { GuiScreen } from "./gui_screen.ts";
import { CreditsScreen } from "./credits_screen.ts";
import { platform } from "../platform.ts";
import { Button, EditBox, TEXT_HEIGHT, TEXT_SCALE, TextInput } from "./widgets.ts";
const LAST_SERVER_KEY = "cubi:last_server";
@@ -40,7 +40,7 @@ export class TitleScreen extends GuiScreen {
// the desktop app for this computer's os
download_button = new Button(`Download for ${download_os().name}`, WIDTH, ROW_HEIGHT, download_desktop_app);
// open over the title screen, it gets the input while it's there
#credits: CreditsScreen | undefined;
#credits: GuiScreen | undefined;
status = "";
status_is_error = false;
@@ -118,7 +118,7 @@ export class TitleScreen extends GuiScreen {
}
#open_credits() {
this.#credits ??= new CreditsScreen(() => this.#close_credits());
this.#credits ??= platform.credits_screen(() => this.#close_credits());
}
#close_credits() {
@@ -188,18 +188,18 @@ export class TitleScreen extends GuiScreen {
// what was typed last time, unless the page was opened with ?server= or ?name=
function initial_server() {
const page = new URL(location.href);
const page = platform.page;
return page.searchParams.has("server") ? default_server(page) : recall(LAST_SERVER_KEY) ?? default_server(page);
}
function initial_name() {
const page = new URL(location.href);
const page = platform.page;
return page.searchParams.has("name") ? default_player_name() : recall(LAST_NAME_KEY) ?? "";
}
function recall(key: string): string | undefined {
try {
return localStorage.getItem(key) ?? undefined;
return platform.storage.getItem(key) ?? undefined;
} catch {
return undefined;
}
@@ -207,7 +207,7 @@ function recall(key: string): string | undefined {
function remember(key: string, value: string) {
try {
localStorage.setItem(key, value);
platform.storage.setItem(key, value);
} catch {
// private windows and blocked storage just start empty next time
}
@@ -215,7 +215,7 @@ function remember(key: string, value: string) {
// the page is the desktop app's, see desktop/main.ts
export function in_desktop_app() {
return new URL(location.href).searchParams.has("singleplayer");
return platform.page.searchParams.has("singleplayer");
}
// the desktop app's build for the os the browser is on, which the server has in /dist/, see DESKTOP_DOWNLOADS. everything that isn't windows
@@ -237,11 +237,11 @@ function download_desktop_app() {
// or ?name=, so the fields show what was used last. the desktop app keeps its ?singleplayer and ?server=, for its
// main menu
export function back_to_title() {
const page = new URL(location.href);
const next = new URL(location.pathname, location.href);
const page = platform.page;
const next = new URL(page.pathname, page);
if (in_desktop_app()) {
next.searchParams.set("server", page.searchParams.get("server") ?? "");
next.searchParams.set("singleplayer", "");
}
location.href = next.href;
platform.restart(next);
}
+3 -2
View File
@@ -5,6 +5,7 @@ import { point_inside_rec } from "@/common/utils.ts";
import { InputManager, type KeyCode } from "@/client/input_manager.ts";
import { draw_rect, draw_rect_stroke, draw_text, measure_text } from "@/client/renderer/mod.ts";
import { draw_gui_sprite } from "./gui_sprites.ts";
import { platform } from "@/client/platform.ts";
export const TEXT_SCALE = 2;
// how tall the font is at scale 1
@@ -124,14 +125,14 @@ export class TextInput {
const copy = InputManager.is_shortcut_pressed("c");
const cut = InputManager.is_shortcut_pressed("x");
if ((copy || cut) && this.anchor !== this.caret) {
navigator.clipboard?.writeText(this.selected_text()).catch(() => {});
platform.write_clipboard(this.selected_text()).catch(() => {});
if (cut) {
this.delete_to(this.caret);
}
}
if (InputManager.is_shortcut_pressed("v")) {
// the browser may ask first, it's put in once it answers
navigator.clipboard?.readText().then((text) => this.insert(text), () => {});
platform.read_clipboard().then((text) => this.insert(text), () => {});
}
for (const char of InputManager.get_typed_characters()) {
this.insert(char);