Files
bworld/client/gui/gui_chat.ts
T
2026-10-12 15:09:22 -03:00

176 lines
6.2 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import { GuiScreen } from "./gui_screen.ts";
import { canvas, draw_rect, draw_text, measure_text } from "$/client/renderer/mod.ts";
import { InputManager } from "../input_manager.ts";
import type { Client } from "../client.ts";
import { MAX_CHAT_LENGTH, type ServerMessage } from "$/common/protocol.ts";
import type { Suggestion } from "$/common/commands.ts";
import { TEXT_HEIGHT, TextInput } from "./widgets.ts";
const SCALE = 2;
const INPUT_HEIGHT = 32;
// how many suggestions show at once, the list scrolls with the selection past that
const VISIBLE_SUGGESTIONS = 10;
const ROW_HEIGHT = TEXT_HEIGHT * SCALE + 2;
const PADDING = 4;
const TOOLTIP_GAP = 16;
const SELECTED: [number, number, number, number] = [1, 1, 0.33, 1];
const TOOLTIP_COLOR: [number, number, number, number] = [0.6, 0.6, 0.6, 1];
export class GuiChat extends GuiScreen {
client: Client;
input: TextInput;
// what the server suggests for the command being typed, like minecraft's CommandSuggestions. they replace the
// text from start up to the caret
suggestions: Suggestion[] = [];
suggestions_start = 0;
// highlighted with the arrows, tab puts it in. -1 is none yet
selected = -1;
// the text up to the caret suggestions were last asked for, and the id of that request. only its answer is used
#asked = "";
#request_id = 0;
// tab just put in a suggestion, so another tab goes to the next one instead of asking again
#cycling = false;
constructor(client: Client, text = "") {
super();
this.client = client;
this.input = new TextInput(text, MAX_CHAT_LENGTH);
}
override on_render(): void {
draw_rect(0, 0, canvas.width, canvas.height, [0, 0, 0, 0.8]);
const y = canvas.height - INPUT_HEIGHT;
this.client.chat.render(true, y - 8);
draw_rect(0, y, canvas.width, canvas.height, [0, 0, 0, 0.4]);
this.input.draw_selection(0, y + 4, INPUT_HEIGHT - 4, SCALE);
draw_text(this.input.value, 0, y, SCALE, [1, 1, 1]);
draw_rect(this.input.caret_x(0, SCALE), y + 4, 1, INPUT_HEIGHT - 4);
this.#render_suggestions(y);
}
override on_tick(_delta: number): void {
this.#handle_suggestion_keys();
const y = canvas.height - INPUT_HEIGHT;
this.input.handle_mouse({ x: 0, y, width: canvas.width, height: INPUT_HEIGHT }, 0, SCALE);
this.input.handle_keys();
this.#ask_for_suggestions();
if (InputManager.is_key_pressed("Enter")) {
this.submit();
}
}
override on_close(): void {}
submit() {
// commands like /give run on the server too
if (this.input.value.trim().length > 0) {
this.client.connection.send({ type: "chat", text: this.input.value });
}
this.client.pop_screen();
}
// from the packet listener
receive_suggestions(message: Extract<ServerMessage, { type: "suggestions" }>) {
if (message.id !== this.#request_id) {
return;
}
this.suggestions = message.suggestions;
this.suggestions_start = message.start;
this.selected = -1;
this.#cycling = false;
}
#handle_suggestion_keys() {
const count = this.suggestions.length;
if (count === 0) {
return;
}
if (InputManager.is_key_pressed("ArrowUp")) {
this.selected = this.selected <= 0 ? count - 1 : this.selected - 1;
this.#cycling = false;
}
if (InputManager.is_key_pressed("ArrowDown")) {
this.selected = (this.selected + 1) % count;
this.#cycling = false;
}
if (InputManager.is_key_pressed("Tab")) {
const back = InputManager.is_key_down("ShiftLeft") || InputManager.is_key_down("ShiftRight");
if (this.selected === -1) {
this.selected = back ? count - 1 : 0;
} else if (this.#cycling) {
this.selected = (this.selected + (back ? count - 1 : 1)) % count;
}
this.#apply(this.suggestions[this.selected]);
this.#cycling = true;
}
}
// puts it in place of what's typed from start to the caret
#apply(suggestion: Suggestion) {
const value = this.input.value;
const caret = this.input.caret;
const start = Math.min(this.suggestions_start, caret);
this.input.value = (value.slice(0, start) + suggestion.text + value.slice(caret)).slice(0, MAX_CHAT_LENGTH);
this.input.move_caret(start + suggestion.text.length);
// the list stays for tabbing through, it's asked for again once they type
this.#asked = this.input.value.slice(0, this.input.caret);
}
// only commands have suggestions, and only when what's before the caret changed
#ask_for_suggestions() {
const before_caret = this.input.value.slice(0, this.input.caret);
if (before_caret === this.#asked) {
return;
}
this.#asked = before_caret;
this.#request_id += 1;
this.suggestions = [];
this.selected = -1;
this.#cycling = false;
if (before_caret.startsWith("/")) {
this.client.connection.send({ type: "suggest", id: this.#request_id, text: before_caret });
}
}
// a list going up from the input, lined up under the word it replaces
#render_suggestions(input_y: number) {
const count = this.suggestions.length;
if (count === 0) {
return;
}
const first = Math.max(0, Math.min(this.selected - VISIBLE_SUGGESTIONS + 1, count - VISIBLE_SUGGESTIONS));
const shown = this.suggestions.slice(first, first + VISIBLE_SUGGESTIONS);
const text_width = Math.max(...shown.map((s) => measure_text(s.text, SCALE)));
const tooltip_width = Math.max(0, ...shown.map((s) => s.tooltip ? measure_text(s.tooltip, SCALE) : 0));
const width = text_width + (tooltip_width > 0 ? TOOLTIP_GAP + tooltip_width : 0) + PADDING * 2;
const height = shown.length * ROW_HEIGHT + PADDING * 2;
const start = Math.min(this.suggestions_start, this.input.value.length);
const x = Math.max(
0,
Math.min(measure_text(this.input.value.slice(0, start), SCALE) - PADDING, canvas.width - width),
);
const y = input_y - height;
draw_rect(x, y, width, height, [0, 0, 0, 0.9]);
shown.forEach((suggestion, i) => {
const row_y = y + PADDING + i * ROW_HEIGHT;
const selected = first + i === this.selected;
draw_text(suggestion.text, x + PADDING, row_y, SCALE, selected ? SELECTED : [0.8, 0.8, 0.8, 1]);
if (suggestion.tooltip) {
draw_text(suggestion.tooltip, x + PADDING + text_width + TOOLTIP_GAP, row_y, SCALE, TOOLTIP_COLOR);
}
});
// more above or below
if (first > 0) draw_rect(x, y, width, 1, [1, 1, 1, 0.6]);
if (first + shown.length < count) draw_rect(x, y + height - 1, width, 1, [1, 1, 1, 0.6]);
}
}