Start maybe working on a native app
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
// 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;
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// the desktop app's client: a deno worker drawing into the app's window with webgpu, see desktop/native.ts. the
|
||||
// window sends what the player does (messages.ts), and does what needs the os, like grabbing the mouse. the browser's
|
||||
// client starts at ../main.ts
|
||||
import { type Platform, set_platform } from "../platform.ts";
|
||||
import { InputManager } from "../input_manager.ts";
|
||||
import { init_window, resize_canvas } from "../renderer/mod.ts";
|
||||
import { show_fatal_error } from "../fatal.ts";
|
||||
import { start_client } from "../client_loop.ts";
|
||||
import { TextCreditsScreen } from "./credits_screen.ts";
|
||||
import type { ClientMessage, WindowMessage } from "./messages.ts";
|
||||
|
||||
const post = (message: ClientMessage) => self.postMessage(message);
|
||||
|
||||
// what the game asked for, the window does it
|
||||
let grabbed = false;
|
||||
let focused = true;
|
||||
// the window let go of the mouse while the game had it, see Platform.take_lost_mouse
|
||||
let lost_mouse = false;
|
||||
|
||||
// frames are each a task of their own, so input, the network and the workers' messages get handled in between.
|
||||
// waiting for the display's refresh happens while drawing, the window's surface holds back frames the display can't
|
||||
// show yet. a timer, not a MessageChannel like the browser's: deno handles a channel's messages before timers,
|
||||
// fetches and the gpu's callbacks, so frames one after another through one would hold those back
|
||||
let shown = true;
|
||||
let next_frame: ((time: number) => void) | undefined;
|
||||
let frame_waiting = false;
|
||||
const shown_listeners: (() => void)[] = [];
|
||||
|
||||
function run_frame() {
|
||||
frame_waiting = false;
|
||||
const frame = next_frame;
|
||||
next_frame = undefined;
|
||||
frame?.(performance.now());
|
||||
}
|
||||
|
||||
function request_frame(frame: (time: number) => void) {
|
||||
next_frame = frame;
|
||||
// a hidden window gets no frames, drawing to it could wait until it's shown again
|
||||
if (shown) wait_for_frame();
|
||||
}
|
||||
|
||||
function wait_for_frame() {
|
||||
if (frame_waiting) return;
|
||||
frame_waiting = true;
|
||||
setTimeout(run_frame, 0);
|
||||
}
|
||||
|
||||
function set_shown(now_shown: boolean) {
|
||||
if (shown === now_shown) return;
|
||||
shown = now_shown;
|
||||
if (!shown) return;
|
||||
for (const listener of shown_listeners) listener();
|
||||
if (next_frame) wait_for_frame();
|
||||
}
|
||||
|
||||
// what was asked of the window, by id, waiting for its answer
|
||||
let next_id = 0;
|
||||
const asking = new Map<number, (answer: string | boolean) => void>();
|
||||
|
||||
function ask<T extends string | boolean>(question: (id: number) => ClientMessage): Promise<T> {
|
||||
const id = next_id++;
|
||||
post(question(id));
|
||||
return new Promise((resolve) => asking.set(id, resolve as (answer: string | boolean) => void));
|
||||
}
|
||||
|
||||
function native_platform(page: URL, stored: Record<string, string>): Platform {
|
||||
const storage = new Map(Object.entries(stored));
|
||||
return {
|
||||
page,
|
||||
restart(url) {
|
||||
post({ type: "restart", page: url.href });
|
||||
},
|
||||
request_frame,
|
||||
on_shown(listener) {
|
||||
shown_listeners.push(listener);
|
||||
},
|
||||
// kept by the window, which saves it with the app's data
|
||||
storage: {
|
||||
getItem: (key) => storage.get(key) ?? null,
|
||||
setItem(key, value) {
|
||||
storage.set(key, value);
|
||||
post({ type: "store", key, value });
|
||||
},
|
||||
},
|
||||
// the window saves the world when it closes
|
||||
confirm_leaving() {},
|
||||
show_fatal_error(message) {
|
||||
post({ type: "fatal", message });
|
||||
},
|
||||
confirm_mods(address, welcome) {
|
||||
const mods = 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 `• ${mod.name} ${mod.version} (${mod.id}${access})`;
|
||||
});
|
||||
const text = "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.\n\n${mods.join("\n")}`;
|
||||
const title = `Join ${address.base.host}?`;
|
||||
return ask((id) => ({ type: "ask", id, title, text, yes: "Join", no: "Cancel" }));
|
||||
},
|
||||
credits_screen: (on_back) => new TextCreditsScreen(on_back),
|
||||
read_clipboard: () => ask((id) => ({ type: "read_clipboard", id })),
|
||||
write_clipboard(text) {
|
||||
post({ type: "write_clipboard", text });
|
||||
return Promise.resolve();
|
||||
},
|
||||
set_mouse_grabbed(grab) {
|
||||
if (grab === grabbed) return;
|
||||
grabbed = grab;
|
||||
// it's no longer where it was, see InputManager.move_cursor
|
||||
if (grab) {
|
||||
InputManager.mouse_x = -1;
|
||||
InputManager.mouse_y = -1;
|
||||
}
|
||||
post({ type: "grab", grab });
|
||||
},
|
||||
is_mouse_grabbed: () => grabbed && focused,
|
||||
take_lost_mouse() {
|
||||
const lost = lost_mouse;
|
||||
lost_mouse = false;
|
||||
return lost;
|
||||
},
|
||||
toggle_fullscreen() {
|
||||
post({ type: "fullscreen" });
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function start(message: Extract<WindowMessage, { type: "start" }>) {
|
||||
set_platform(native_platform(new URL(message.page), message.storage));
|
||||
const { surface: handles, width, height } = message;
|
||||
const surface = new Deno.UnsafeWindowSurface({
|
||||
system: handles.system,
|
||||
windowHandle: Deno.UnsafePointer.create(handles.window),
|
||||
displayHandle: Deno.UnsafePointer.create(handles.display),
|
||||
width,
|
||||
height,
|
||||
});
|
||||
// pointers to the window were all that needed ffi. mods run in here too, they don't get it
|
||||
await Deno.permissions.revoke({ name: "ffi" });
|
||||
try {
|
||||
await init_window(surface);
|
||||
} catch (e) {
|
||||
show_fatal_error(e instanceof Error ? e.message : String(e));
|
||||
throw e;
|
||||
}
|
||||
resize_canvas(width, height);
|
||||
await start_client();
|
||||
}
|
||||
|
||||
self.onmessage = (event: MessageEvent<WindowMessage>) => {
|
||||
const message = event.data;
|
||||
switch (message.type) {
|
||||
case "start":
|
||||
start(message);
|
||||
break;
|
||||
case "resize":
|
||||
resize_canvas(message.width, message.height);
|
||||
break;
|
||||
case "key":
|
||||
if (message.down) InputManager.key_down(message.code, message.key, message.shortcut);
|
||||
else InputManager.key_up(message.code);
|
||||
break;
|
||||
case "text":
|
||||
InputManager.type_text(message.text);
|
||||
break;
|
||||
case "button":
|
||||
if (message.down) InputManager.mouse_down(message.button);
|
||||
else InputManager.mouse_up(message.button);
|
||||
break;
|
||||
case "cursor":
|
||||
InputManager.move_cursor(message.x, message.y);
|
||||
break;
|
||||
case "look":
|
||||
if (grabbed) InputManager.look(message.x, message.y);
|
||||
break;
|
||||
case "scroll":
|
||||
InputManager.scroll(message.amount);
|
||||
break;
|
||||
case "focus":
|
||||
focused = message.focused;
|
||||
if (focused) break;
|
||||
InputManager.release_all();
|
||||
// the window let go of it, the game asks again once it wants it
|
||||
if (grabbed) {
|
||||
lost_mouse = true;
|
||||
grabbed = false;
|
||||
post({ type: "grab", grab: false });
|
||||
}
|
||||
break;
|
||||
case "shown":
|
||||
set_shown(message.shown);
|
||||
break;
|
||||
case "clipboard":
|
||||
asking.get(message.id)?.(message.text);
|
||||
asking.delete(message.id);
|
||||
break;
|
||||
case "answer":
|
||||
asking.get(message.id)?.(message.yes);
|
||||
asking.delete(message.id);
|
||||
break;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// what the desktop app's window (desktop/native.ts, on deno's main thread) and the client running in a worker (see
|
||||
// main.ts here) tell each other. the window has the os's events and the client draws into it
|
||||
|
||||
// the window's surface, for deno's UnsafeWindowSurface. handles are pointers' addresses, pointers can't be posted
|
||||
export interface SurfaceHandles {
|
||||
system: "cocoa" | "win32" | "x11" | "wayland";
|
||||
window: bigint;
|
||||
display: bigint;
|
||||
}
|
||||
|
||||
export type WindowMessage =
|
||||
| {
|
||||
type: "start";
|
||||
page: string;
|
||||
surface: SurfaceHandles;
|
||||
width: number;
|
||||
height: number;
|
||||
// what the client saved, see NativePlatform's storage
|
||||
storage: Record<string, string>;
|
||||
}
|
||||
// in pixels, what's drawn to is this big now
|
||||
| { type: "resize"; width: number; height: number }
|
||||
// code like a KeyboardEvent's, key the letter for a shortcut (see InputManager.key_down), "" otherwise
|
||||
| { type: "key"; down: boolean; code: string; key: string; shortcut: boolean }
|
||||
| { type: "text"; text: string }
|
||||
// buttons numbered like the dom's
|
||||
| { type: "button"; down: boolean; button: number }
|
||||
// the cursor, in pixels from the window's top left
|
||||
| { type: "cursor"; x: number; y: number }
|
||||
// the grabbed mouse moved
|
||||
| { type: "look"; x: number; y: number }
|
||||
// down is positive, about 100 for a notch, like the dom's
|
||||
| { type: "scroll"; amount: number }
|
||||
// the window lost or got focus, losing it lets go of the mouse
|
||||
| { type: "focus"; focused: boolean }
|
||||
// whether the window can be seen
|
||||
| { type: "shown"; shown: boolean }
|
||||
// answers, by the id they were asked with
|
||||
| { type: "clipboard"; id: number; text: string }
|
||||
| { type: "answer"; id: number; yes: boolean };
|
||||
|
||||
export type ClientMessage =
|
||||
| { type: "grab"; grab: boolean }
|
||||
| { type: "fullscreen" }
|
||||
// start the client over at page, see Platform.restart
|
||||
| { type: "restart"; page: string }
|
||||
| { type: "fatal"; message: string }
|
||||
// a question with a yes and a no, answered with "answer"
|
||||
| { type: "ask"; id: number; title: string; text: string; yes: string; no: string }
|
||||
| { type: "read_clipboard"; id: number }
|
||||
| { type: "write_clipboard"; text: string }
|
||||
| { type: "store"; key: string; value: string };
|
||||
Reference in New Issue
Block a user