Awesome menu and saves
This commit is contained in:
@@ -0,0 +1,300 @@
|
||||
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// the desktop app's title screen, a main menu like luanti's. singleplayer lists the worlds in the app's data folder,
|
||||
// to play one, make one or pick its mods. the app does the files and runs the world's server, see desktop/main.ts
|
||||
import type { ModEntry, WorldEntry } from "@/desktop/saves.ts";
|
||||
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 type { JoinServer } from "./title_screen.ts";
|
||||
import { Button, EditBox, ListBox, TEXT_HEIGHT, TEXT_SCALE, TextInput } from "./widgets.ts";
|
||||
|
||||
const WIDTH = 440;
|
||||
const ROW_HEIGHT = 48;
|
||||
const GAP = 20;
|
||||
const LIST_WIDTH = 640;
|
||||
const HALF_WIDTH = (LIST_WIDTH - GAP) / 2;
|
||||
const TOP = 100;
|
||||
// the name players have in singleplayer worlds, which what they carry is saved under
|
||||
const SINGLEPLAYER_NAME = "singleplayer";
|
||||
|
||||
type Show = (screen: GuiScreen) => void;
|
||||
|
||||
export class MainMenuScreen extends GuiScreen {
|
||||
#buttons: Button[];
|
||||
|
||||
constructor(show: Show, join: JoinServer) {
|
||||
super();
|
||||
const back = () => show(new MainMenuScreen(show, join));
|
||||
// there's nothing behind these yet
|
||||
const multiplayer = new Button("Multiplayer", WIDTH, ROW_HEIGHT, () => {});
|
||||
const mods = new Button("Mods", WIDTH, ROW_HEIGHT, () => {});
|
||||
multiplayer.active = mods.active = false;
|
||||
this.#buttons = [
|
||||
new Button("Singleplayer", WIDTH, ROW_HEIGHT, () => show(new WorldListScreen(show, join))),
|
||||
multiplayer,
|
||||
mods,
|
||||
new Button("Credits", WIDTH, ROW_HEIGHT, () => show(new CreditsScreen(back))),
|
||||
];
|
||||
}
|
||||
|
||||
on_tick(_delta: number): void {
|
||||
let y = (canvas.height - this.#buttons.length * (ROW_HEIGHT + GAP)) / 2 + 40;
|
||||
for (const button of this.#buttons) {
|
||||
button.x = (canvas.width - WIDTH) / 2;
|
||||
button.y = y;
|
||||
y += ROW_HEIGHT + GAP;
|
||||
button.handle_input();
|
||||
}
|
||||
}
|
||||
|
||||
on_render(): void {
|
||||
draw_centered("Cubi", this.#buttons[0].y - 140, 8);
|
||||
for (const button of this.#buttons) button.render();
|
||||
}
|
||||
|
||||
on_close(): void {}
|
||||
}
|
||||
|
||||
// the worlds in the app's saves folder
|
||||
class WorldListScreen extends GuiScreen {
|
||||
#show: Show;
|
||||
#join: JoinServer;
|
||||
#worlds: WorldEntry[] = [];
|
||||
#mods: ModEntry[] = [];
|
||||
#list = new ListBox(LIST_WIDTH, 0, (row, double) => {
|
||||
this.#list.selected = row;
|
||||
if (double) this.#play();
|
||||
});
|
||||
#play_button = new Button("Play", HALF_WIDTH, ROW_HEIGHT, () => this.#play());
|
||||
#new_button = new Button("New world", HALF_WIDTH, ROW_HEIGHT, () => this.#edit());
|
||||
#mods_button = new Button("Mods", HALF_WIDTH, ROW_HEIGHT, () => this.#edit(this.#selected));
|
||||
#back_button = new Button("Back", HALF_WIDTH, ROW_HEIGHT, () => this.#back());
|
||||
#status = new Status();
|
||||
#busy = true;
|
||||
|
||||
// select is the world selected at first, like one that was just made
|
||||
constructor(show: Show, join: JoinServer, select?: string) {
|
||||
super();
|
||||
this.#show = show;
|
||||
this.#join = join;
|
||||
this.#status.set("Loading worlds...");
|
||||
desktop("singleplayer").then((response) => response.json()).then(
|
||||
({ worlds, mods }: { worlds: WorldEntry[]; mods: ModEntry[] }) => {
|
||||
this.#worlds = worlds;
|
||||
this.#mods = mods;
|
||||
this.#list.rows = worlds.map((world) => ({ text: world.name }));
|
||||
this.#list.selected = Math.max(0, worlds.findIndex((world) => world.name === select));
|
||||
this.#status.set(worlds.length === 0 ? "There are no worlds yet, make one with New world" : "");
|
||||
this.#busy = false;
|
||||
},
|
||||
(e) => this.#status.fail(e),
|
||||
);
|
||||
}
|
||||
|
||||
get #selected(): WorldEntry | undefined {
|
||||
return this.#worlds[this.#list.selected];
|
||||
}
|
||||
|
||||
on_tick(_delta: number): void {
|
||||
const buttons = [this.#play_button, this.#new_button, this.#mods_button, this.#back_button];
|
||||
const x = (canvas.width - LIST_WIDTH) / 2;
|
||||
this.#list.x = x;
|
||||
this.#list.y = TOP;
|
||||
this.#list.height = canvas.height - TOP - 2 * (ROW_HEIGHT + GAP) - 120;
|
||||
for (const [i, button] of buttons.entries()) {
|
||||
button.x = x + (i % 2) * (HALF_WIDTH + GAP);
|
||||
button.y = TOP + this.#list.height + GAP + Math.floor(i / 2) * (ROW_HEIGHT + GAP);
|
||||
}
|
||||
this.#play_button.active = this.#mods_button.active = !this.#busy && this.#selected !== undefined;
|
||||
this.#new_button.active = !this.#busy;
|
||||
|
||||
if (InputManager.is_key_pressed("Escape")) return this.#back();
|
||||
if (InputManager.is_key_pressed("Enter")) this.#play();
|
||||
if (!this.#busy) this.#list.handle_input();
|
||||
for (const button of buttons) button.handle_input();
|
||||
}
|
||||
|
||||
on_render(): void {
|
||||
draw_centered("Singleplayer", TOP / 2, 4);
|
||||
this.#list.render();
|
||||
for (const button of [this.#play_button, this.#new_button, this.#mods_button, this.#back_button]) {
|
||||
button.render();
|
||||
}
|
||||
this.#status.render(this.#back_button.y + ROW_HEIGHT + GAP);
|
||||
}
|
||||
|
||||
on_close(): void {}
|
||||
|
||||
#back() {
|
||||
if (!this.#busy || this.#status.error) this.#show(new MainMenuScreen(this.#show, this.#join));
|
||||
}
|
||||
|
||||
// a new world, or the mods of one there is
|
||||
#edit(world?: WorldEntry) {
|
||||
this.#show(new WorldModsScreen(this.#show, this.#join, this.#mods, world));
|
||||
}
|
||||
|
||||
async #play() {
|
||||
const world = this.#selected;
|
||||
if (!world || this.#busy) return;
|
||||
this.#busy = true;
|
||||
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));
|
||||
} catch (e) {
|
||||
this.#status.fail(e);
|
||||
this.#busy = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// a new world's name and mods, or the mods of one there is. turning a mod on turns on the mods it needs, and turning
|
||||
// one off turns off the mods that need it
|
||||
class WorldModsScreen extends GuiScreen {
|
||||
#show: Show;
|
||||
#join: JoinServer;
|
||||
#mods: ModEntry[];
|
||||
#world: WorldEntry | undefined;
|
||||
#enabled: Set<string>;
|
||||
#name = new EditBox(LIST_WIDTH, ROW_HEIGHT, new TextInput("", 32), "World name");
|
||||
#list = new ListBox(LIST_WIDTH, 0, (row) => this.#toggle(this.#mods[row].id));
|
||||
#save_button: Button;
|
||||
#cancel_button: Button;
|
||||
#status = new Status();
|
||||
#busy = false;
|
||||
|
||||
constructor(show: Show, join: JoinServer, mods: ModEntry[], world?: WorldEntry) {
|
||||
super();
|
||||
this.#show = show;
|
||||
this.#join = join;
|
||||
this.#mods = mods;
|
||||
this.#world = world;
|
||||
// a new world starts with every mod. a world's mods that aren't in the folder anymore are dropped when it's saved
|
||||
this.#enabled = new Set(mods.map(({ id }) => id).filter((id) => !world || world.mods.includes(id)));
|
||||
this.#name.focused = !world;
|
||||
this.#save_button = new Button(world ? "Save" : "Create", HALF_WIDTH, ROW_HEIGHT, () => this.#save());
|
||||
this.#cancel_button = new Button("Cancel", HALF_WIDTH, ROW_HEIGHT, () => this.#back(world?.name));
|
||||
this.#update_rows();
|
||||
if (mods.length === 0) this.#status.set("There are no mods in the mods folder");
|
||||
}
|
||||
|
||||
on_tick(_delta: number): void {
|
||||
const x = (canvas.width - LIST_WIDTH) / 2;
|
||||
let y = TOP;
|
||||
if (!this.#world) {
|
||||
this.#name.x = x;
|
||||
this.#name.y = y;
|
||||
y += ROW_HEIGHT + GAP;
|
||||
}
|
||||
this.#list.x = x;
|
||||
this.#list.y = y;
|
||||
this.#list.height = canvas.height - y - ROW_HEIGHT - GAP - 120;
|
||||
this.#save_button.x = x;
|
||||
this.#cancel_button.x = x + HALF_WIDTH + GAP;
|
||||
this.#save_button.y = this.#cancel_button.y = y + this.#list.height + GAP;
|
||||
this.#save_button.active = this.#name.active = !this.#busy;
|
||||
|
||||
if (InputManager.is_key_pressed("Escape")) return this.#back(this.#world?.name);
|
||||
if (InputManager.is_key_pressed("Enter")) this.#save();
|
||||
this.#name.handle_input();
|
||||
this.#list.handle_input();
|
||||
this.#save_button.handle_input();
|
||||
this.#cancel_button.handle_input();
|
||||
}
|
||||
|
||||
on_render(): void {
|
||||
draw_centered(this.#world ? `${this.#world.name}'s mods` : "New world", TOP / 2, 4);
|
||||
if (!this.#world) this.#name.render();
|
||||
this.#list.render();
|
||||
this.#save_button.render();
|
||||
this.#cancel_button.render();
|
||||
this.#status.render(this.#save_button.y + ROW_HEIGHT + GAP);
|
||||
}
|
||||
|
||||
on_close(): void {}
|
||||
|
||||
#toggle(id: string) {
|
||||
const by_id = new Map(this.#mods.map((mod) => [mod.id, mod]));
|
||||
const on = (id: string) => {
|
||||
this.#enabled.add(id);
|
||||
for (const dependency of by_id.get(id)?.dependencies ?? []) {
|
||||
if (by_id.has(dependency) && !this.#enabled.has(dependency)) on(dependency);
|
||||
}
|
||||
};
|
||||
const off = (id: string) => {
|
||||
this.#enabled.delete(id);
|
||||
for (const mod of this.#mods) {
|
||||
if (mod.dependencies.includes(id) && this.#enabled.has(mod.id)) off(mod.id);
|
||||
}
|
||||
};
|
||||
(this.#enabled.has(id) ? off : on)(id);
|
||||
this.#update_rows();
|
||||
}
|
||||
|
||||
#update_rows() {
|
||||
this.#list.rows = this.#mods.map((mod) => ({
|
||||
text: `${mod.name} ${mod.version}`,
|
||||
checked: this.#enabled.has(mod.id),
|
||||
}));
|
||||
}
|
||||
|
||||
async #save() {
|
||||
if (this.#busy) return;
|
||||
this.#busy = true;
|
||||
const world = this.#world?.name ?? this.#name.value.trim();
|
||||
try {
|
||||
await desktop(this.#world ? "world_mods" : "create_world", { world, mods: [...this.#enabled] });
|
||||
this.#back(world);
|
||||
} catch (e) {
|
||||
this.#status.fail(e);
|
||||
this.#busy = false;
|
||||
}
|
||||
}
|
||||
|
||||
#back(select?: string) {
|
||||
this.#show(new WorldListScreen(this.#show, this.#join, select));
|
||||
}
|
||||
}
|
||||
|
||||
// what's happening, or what went wrong, under a screen's buttons. a line each for messages that have several, like
|
||||
// a world's mods' problems
|
||||
class Status {
|
||||
text = "";
|
||||
error = false;
|
||||
|
||||
set(text: string) {
|
||||
this.text = text;
|
||||
this.error = false;
|
||||
}
|
||||
|
||||
fail(e: unknown) {
|
||||
this.text = e instanceof Error ? e.message : String(e);
|
||||
this.error = true;
|
||||
}
|
||||
|
||||
render(y: number) {
|
||||
for (const line of this.text.split("\n").filter((line) => line !== "")) {
|
||||
const width = measure_text(line, TEXT_SCALE);
|
||||
const x = (canvas.width - width) / 2;
|
||||
draw_rect(x - 8, y - 4, width + 16, TEXT_HEIGHT * TEXT_SCALE + 8, [0, 0, 0, 0.5]);
|
||||
draw_text(line, x, y, TEXT_SCALE, this.error ? [1, 0.45, 0.45, 1] : [1, 1, 1, 1]);
|
||||
y += TEXT_HEIGHT * TEXT_SCALE + 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function draw_centered(text: string, y: number, scale: number) {
|
||||
draw_text(text, (canvas.width - measure_text(text, scale)) / 2, y - (TEXT_HEIGHT * scale) / 2, scale);
|
||||
}
|
||||
|
||||
// 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) });
|
||||
if (!response.ok) throw new Error(await response.text());
|
||||
return response;
|
||||
}
|
||||
+19
-48
@@ -23,8 +23,8 @@ const TITLE_SCALE = 8;
|
||||
// for the player when it fails
|
||||
export type JoinServer = (address: ServerAddress, name: string, status: (text: string) => void) => Promise<void>;
|
||||
|
||||
// the first thing players see, like minecraft's title and multiplayer screens rolled into one: a name, a server,
|
||||
// and a button to join it. in the desktop app there's also singleplayer, which joins the app's own server
|
||||
// the first thing players see in the browser, like minecraft's title and multiplayer screens rolled into one: a
|
||||
// name, a server, and a button to join it. the desktop app has a main menu instead, see main_menu.ts
|
||||
export class TitleScreen extends GuiScreen {
|
||||
#join_server: JoinServer;
|
||||
|
||||
@@ -36,15 +36,9 @@ export class TitleScreen extends GuiScreen {
|
||||
);
|
||||
server = new EditBox(WIDTH, ROW_HEIGHT, new TextInput(initial_server(), 256, (char) => char !== " "), "host:port");
|
||||
join_button = new Button("Join server", WIDTH, ROW_HEIGHT, () => this.#join());
|
||||
// the page was served by a game server of its own to play on alone, see desktop/main.ts
|
||||
singleplayer_button = has_singleplayer()
|
||||
? new Button("Singleplayer", WIDTH, ROW_HEIGHT, () => this.#join_singleplayer())
|
||||
: undefined;
|
||||
credits_button = new Button("Credits", WIDTH, ROW_HEIGHT, () => this.#open_credits());
|
||||
// in the browser, the desktop app for this computer's os
|
||||
download_button = has_singleplayer()
|
||||
? undefined
|
||||
: new Button(`Download for ${download_os().name}`, WIDTH, ROW_HEIGHT, download_desktop_app);
|
||||
// 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;
|
||||
|
||||
@@ -86,10 +80,9 @@ export class TitleScreen extends GuiScreen {
|
||||
if (InputManager.is_key_pressed("Enter")) {
|
||||
this.#join();
|
||||
}
|
||||
this.singleplayer_button?.handle_input();
|
||||
this.join_button.handle_input();
|
||||
this.credits_button.handle_input();
|
||||
this.download_button?.handle_input();
|
||||
this.download_button.handle_input();
|
||||
}
|
||||
|
||||
on_render(): void {
|
||||
@@ -105,16 +98,15 @@ export class TitleScreen extends GuiScreen {
|
||||
this.#label("Name", this.name);
|
||||
this.#label("Server", this.server);
|
||||
this.name.render();
|
||||
this.singleplayer_button?.render();
|
||||
this.server.render();
|
||||
this.join_button.render();
|
||||
this.credits_button.render();
|
||||
this.download_button?.render();
|
||||
this.download_button.render();
|
||||
|
||||
if (this.status) {
|
||||
const width = measure_text(this.status, TEXT_SCALE);
|
||||
const x = (canvas.width - width) / 2;
|
||||
const y = (this.download_button ?? this.credits_button).y + ROW_HEIGHT + GAP;
|
||||
const y = this.download_button.y + ROW_HEIGHT + GAP;
|
||||
draw_rect(x - 8, y - 4, width + 16, TEXT_HEIGHT * TEXT_SCALE + 8, [0, 0, 0, 0.5]);
|
||||
draw_text(this.status, x, y, TEXT_SCALE, this.status_is_error ? [1, 0.45, 0.45, 1] : [1, 1, 1, 1]);
|
||||
}
|
||||
@@ -136,28 +128,17 @@ export class TitleScreen extends GuiScreen {
|
||||
|
||||
#layout() {
|
||||
const x = (canvas.width - WIDTH) / 2;
|
||||
const singleplayer_height = this.singleplayer_button ? ROW_HEIGHT + GAP : 0;
|
||||
const download_height = this.download_button ? ROW_HEIGHT + GAP : 0;
|
||||
const total = 2 * (LABEL_GAP + ROW_HEIGHT + GAP) + singleplayer_height + 2 * ROW_HEIGHT + GAP +
|
||||
download_height;
|
||||
const total = 2 * (LABEL_GAP + ROW_HEIGHT + GAP) + 3 * ROW_HEIGHT + 2 * GAP;
|
||||
let y = (canvas.height - total) / 2 + 40;
|
||||
for (const field of [this.name, this.server]) {
|
||||
field.x = x;
|
||||
field.y = y + LABEL_GAP;
|
||||
y += LABEL_GAP + ROW_HEIGHT + GAP;
|
||||
if (field === this.name && this.singleplayer_button) {
|
||||
this.singleplayer_button.x = x;
|
||||
this.singleplayer_button.y = y;
|
||||
y += singleplayer_height;
|
||||
}
|
||||
}
|
||||
this.join_button.x = x;
|
||||
this.join_button.y = y;
|
||||
this.credits_button.x = x;
|
||||
this.credits_button.y = y + ROW_HEIGHT + GAP;
|
||||
if (this.download_button) {
|
||||
this.download_button.x = x;
|
||||
this.download_button.y = this.credits_button.y + ROW_HEIGHT + GAP;
|
||||
for (const button of [this.join_button, this.credits_button, this.download_button]) {
|
||||
button.x = x;
|
||||
button.y = y;
|
||||
y += ROW_HEIGHT + GAP;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,18 +162,10 @@ export class TitleScreen extends GuiScreen {
|
||||
await this.#connect(address);
|
||||
}
|
||||
|
||||
// the server that served this page
|
||||
async #join_singleplayer() {
|
||||
if (this.#joining) {
|
||||
return;
|
||||
}
|
||||
await this.#connect(server_address(location.host), "Starting singleplayer...");
|
||||
}
|
||||
|
||||
async #connect(address: ServerAddress, status = `Connecting to ${address.base.host}...`) {
|
||||
async #connect(address: ServerAddress) {
|
||||
remember(LAST_NAME_KEY, this.name.value);
|
||||
this.#set_joining(true);
|
||||
this.status = status;
|
||||
this.status = `Connecting to ${address.base.host}...`;
|
||||
this.status_is_error = false;
|
||||
try {
|
||||
await this.#join_server(address, this.name.value, (text) => this.status = text);
|
||||
@@ -205,9 +178,6 @@ export class TitleScreen extends GuiScreen {
|
||||
#set_joining(joining: boolean) {
|
||||
this.#joining = joining;
|
||||
this.name.active = this.server.active = this.join_button.active = !joining;
|
||||
if (this.singleplayer_button) {
|
||||
this.singleplayer_button.active = !joining;
|
||||
}
|
||||
}
|
||||
|
||||
#show_error(message: string) {
|
||||
@@ -243,7 +213,8 @@ function remember(key: string, value: string) {
|
||||
}
|
||||
}
|
||||
|
||||
function has_singleplayer() {
|
||||
// the page is the desktop app's, see desktop/main.ts
|
||||
export function in_desktop_app() {
|
||||
return new URL(location.href).searchParams.has("singleplayer");
|
||||
}
|
||||
|
||||
@@ -263,12 +234,12 @@ function download_desktop_app() {
|
||||
}
|
||||
|
||||
// a server's mods can't be unloaded, so going back to the title screen starts the page over. without ?server=
|
||||
// or ?name=, so the fields show what was used last. the desktop app keeps its ?singleplayer and ?server=, its saved
|
||||
// fields don't last between launches and the page's own address isn't a server to join
|
||||
// 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);
|
||||
if (has_singleplayer()) {
|
||||
if (in_desktop_app()) {
|
||||
next.searchParams.set("server", page.searchParams.get("server") ?? "");
|
||||
next.searchParams.set("singleplayer", "");
|
||||
}
|
||||
|
||||
+76
-1
@@ -3,7 +3,7 @@
|
||||
|
||||
import { point_inside_rec } from "@/common/utils.ts";
|
||||
import { InputManager, type KeyCode } from "@/client/input_manager.ts";
|
||||
import { draw_rect, draw_text, measure_text } from "@/client/renderer/mod.ts";
|
||||
import { draw_rect, draw_rect_stroke, draw_text, measure_text } from "@/client/renderer/mod.ts";
|
||||
import { draw_gui_sprite } from "./gui_sprites.ts";
|
||||
|
||||
export const TEXT_SCALE = 2;
|
||||
@@ -345,3 +345,78 @@ export class EditBox {
|
||||
return this.x + 10;
|
||||
}
|
||||
}
|
||||
|
||||
const LIST_ROW_HEIGHT = 40;
|
||||
const CHECK_SIZE = 20;
|
||||
|
||||
// rows of text that scroll with the mouse wheel, like minecraft's selection lists. a row can have a box ticked or not
|
||||
// in front, for lists of things to turn on and off
|
||||
export class ListBox {
|
||||
x = 0;
|
||||
y = 0;
|
||||
width: number;
|
||||
height: number;
|
||||
rows: { text: string; checked?: boolean }[] = [];
|
||||
selected = -1;
|
||||
// clicking a row, double is whether it was the second click of a double click
|
||||
on_click: (row: number, double: boolean) => void;
|
||||
#scroll = 0;
|
||||
#hovered = -1;
|
||||
#last_click = { row: -1, time: -Infinity };
|
||||
|
||||
constructor(width: number, height: number, on_click: (row: number, double: boolean) => void) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.on_click = on_click;
|
||||
}
|
||||
|
||||
get #visible() {
|
||||
return Math.max(1, Math.floor(this.height / LIST_ROW_HEIGHT));
|
||||
}
|
||||
|
||||
handle_input() {
|
||||
const mouse = InputManager.get_mouse_position();
|
||||
const inside = point_inside_rec(mouse.x, mouse.y, this.x, this.y, this.width, this.height);
|
||||
const max_scroll = Math.max(0, this.rows.length - this.#visible);
|
||||
if (inside) this.#scroll += Math.sign(InputManager.get_wheel_delta());
|
||||
this.#scroll = Math.max(0, Math.min(max_scroll, this.#scroll));
|
||||
|
||||
const row = this.#scroll + Math.floor((mouse.y - this.y) / LIST_ROW_HEIGHT);
|
||||
this.#hovered = inside && row < this.rows.length ? row : -1;
|
||||
if (this.#hovered >= 0 && InputManager.is_mouse_pressed(0)) {
|
||||
InputManager.consume_mouse(0);
|
||||
const now = performance.now();
|
||||
const double = this.#last_click.row === row && now - this.#last_click.time < DOUBLE_CLICK_MS;
|
||||
this.#last_click = double ? { row: -1, time: -Infinity } : { row, time: now };
|
||||
this.on_click(row, double);
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
draw_rect(this.x, this.y, this.width, this.height, [0, 0, 0, 0.5]);
|
||||
const shown = this.rows.slice(this.#scroll, this.#scroll + this.#visible);
|
||||
for (const [i, row] of shown.entries()) {
|
||||
const index = this.#scroll + i;
|
||||
const y = this.y + i * LIST_ROW_HEIGHT;
|
||||
if (index === this.selected) {
|
||||
draw_rect(this.x, y, this.width, LIST_ROW_HEIGHT, SELECTION_COLOR);
|
||||
} else if (index === this.#hovered) {
|
||||
draw_rect(this.x, y, this.width, LIST_ROW_HEIGHT, [1, 1, 1, 0.1]);
|
||||
}
|
||||
let text_x = this.x + 12;
|
||||
if (row.checked !== undefined) {
|
||||
const box_y = y + (LIST_ROW_HEIGHT - CHECK_SIZE) / 2;
|
||||
draw_rect_stroke(text_x, box_y, CHECK_SIZE, CHECK_SIZE, [1, 1, 1, 1], 2);
|
||||
if (row.checked) draw_rect(text_x + 5, box_y + 5, CHECK_SIZE - 10, CHECK_SIZE - 10);
|
||||
text_x += CHECK_SIZE + 12;
|
||||
}
|
||||
draw_text(row.text, text_x, y + (LIST_ROW_HEIGHT - TEXT_HEIGHT * TEXT_SCALE) / 2, TEXT_SCALE);
|
||||
}
|
||||
// how far down it's scrolled, when it doesn't all fit
|
||||
if (this.rows.length > this.#visible) {
|
||||
const bar = this.height * this.#visible / this.rows.length;
|
||||
const bar_y = this.y + (this.height - bar) * this.#scroll / (this.rows.length - this.#visible);
|
||||
draw_rect(this.x + this.width - 6, bar_y, 6, bar, [1, 1, 1, 0.5]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user