Improve commands

This commit is contained in:
2026-09-27 00:38:55 -03:00
parent 535bd36d91
commit 8c7ca92891
13 changed files with 1095 additions and 109 deletions
+4
View File
@@ -185,6 +185,10 @@ export class Client {
if (InputManager.is_key_pressed(options.key_chat) && !this.screen) {
this.push_screen(new GuiChat(this));
}
// straight into a command, like minecraft
if (InputManager.is_key_pressed("Slash") && !this.screen) {
this.push_screen(new GuiChat(this, "/"));
}
// browsers eat the escape that lets go of the mouse, so losing the mouse pauses too
const lost_mouse = InputManager.take_lost_pointer_lock();
if (InputManager.is_key_pressed("Escape")) {
+133 -8
View File
@@ -1,32 +1,60 @@
import { GuiScreen } from "./gui_screen.ts";
import { canvas, draw_rect, draw_text } from "$/client/renderer/mod.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 } from "$/common/protocol.ts";
import { TextInput } from "./widgets.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 = new TextInput("", MAX_CHAT_LENGTH);
input: TextInput;
constructor(client: Client) {
// 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 - 32;
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]);
draw_text(this.input.value, 0, y, 2, [1, 1, 1]);
draw_rect(this.input.caret_x(0, 2), y + 4, 1, 32 - 4);
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();
this.input.handle_keys();
this.#ask_for_suggestions();
if (InputManager.is_key_pressed("Enter")) {
this.submit();
}
@@ -41,4 +69,101 @@ export class GuiChat extends GuiScreen {
}
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.caret = Math.min(start + suggestion.text.length, this.input.value.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]);
}
}
+1
View File
@@ -58,6 +58,7 @@ export type KeyCode =
| "Home"
| "End"
| "Backspace"
| "Slash"
| "Delete"
// f
| "F1"
+6
View File
@@ -2,6 +2,7 @@ import type { ServerMessage } from "$/common/protocol.ts";
import { Container, ItemStack } from "$/common/inventory.ts";
import type { Client } from "./client.ts";
import { GuiContainer } from "./gui/gui_container.ts";
import { GuiChat } from "./gui/gui_chat.ts";
import { RemotePlayer } from "./entity/remote_player.ts";
import { ItemEntity } from "./entity/item_entity.ts";
@@ -86,6 +87,11 @@ export class ClientPacketListener {
case "chat":
client.chat.add(message.from ? `<${message.from}> ${message.text}` : message.text);
break;
case "suggestions":
if (client.screen instanceof GuiChat) {
client.screen.receive_suggestions(message);
}
break;
case "container": {
let container = message.container === "screen" ? inventories.screen : inventories[message.container];
if (message.container === "screen" && container?.size !== message.items.length) {