Change whole gui system

This commit is contained in:
2026-10-01 16:22:02 -03:00
parent 013a4fbb97
commit deba36260e
48 changed files with 1925 additions and 1302 deletions
+64 -43
View File
@@ -1,6 +1,6 @@
// the immediate mode drawing mods' client scripts get (Graphics in common/mod_api/client.ts), on top of the renderer.
// one per screen, so text inputs keep their text and focus while it's open
import { SLOT_SIZE } from "$/common/constants.ts";
// one per screen, so text inputs keep their text and focus while it's open. mods work in gui pixels, GUI_SCALE canvas
// pixels each
import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
import { ItemStack } from "$/common/inventory.ts";
import type { Color, Graphics } from "$/common/mod_api/client.ts";
@@ -30,28 +30,39 @@ const GREY: Color = [0.66, 0.66, 0.66, 1];
const INPUT_HEIGHT = 32;
const FIELD_BORDER = GUI_ATLAS.text_field.border[0] * GUI_SCALE;
// gui pixels to the canvas's, see GUI_SCALE
const S = GUI_SCALE;
export class ModGraphics implements Graphics {
// where gui pixel 0, 0 is on the canvas: a menu screen's top left, or the window's
origin_x = 0;
origin_y = 0;
// text inputs by id, and which one has the keyboard
#inputs = new Map<string, TextInput>();
#focused: string | undefined;
#tooltip: { lines: TooltipLine[]; x: number; y: number } | undefined;
// areas the screen underneath shouldn't take the mouse in, this frame
// areas the screen underneath shouldn't take the mouse in this frame, on the canvas
#blocked: { x: number; y: number; w: number; h: number }[] = [];
#clicks_consumed = false;
// the whole window, in gui pixels
get width() {
return canvas.width;
return canvas.width / S;
}
get height() {
return canvas.height;
return canvas.height / S;
}
get text_height() {
return TEXT_HEIGHT * TEXT_SCALE / S;
}
get mouse() {
const { x, y } = InputManager.get_mouse_position();
return {
x,
y,
x: (x - this.origin_x) / S,
y: (y - this.origin_y) / S,
down: InputManager.is_mouse_down(0),
pressed: !this.#clicks_consumed && InputManager.is_mouse_pressed(0),
right_pressed: !this.#clicks_consumed && InputManager.is_mouse_pressed(2),
@@ -77,39 +88,49 @@ export class ModGraphics implements Graphics {
// whether the screen underneath should ignore the mouse
mouse_blocked() {
const { x, y } = this.mouse;
const { x, y } = InputManager.get_mouse_position();
return this.#clicks_consumed || this.#blocked.some((r) => point_inside_rec(x, y, r.x, r.y, r.w, r.h));
}
// a gui position on the canvas
#x(x: number) {
return this.origin_x + x * S;
}
#y(y: number) {
return this.origin_y + y * S;
}
rect(x: number, y: number, w: number, h: number, color: Color = WHITE) {
draw_rect(x, y, w, h, color);
draw_rect(this.#x(x), this.#y(y), w * S, h * S, color);
}
panel(x: number, y: number, w: number, h: number) {
draw_gui_sprite("panel", x, y, w, h);
draw_gui_sprite("panel", this.#x(x), this.#y(y), w * S, h * S);
}
text(text: string, x: number, y: number, options: { scale?: number; color?: Color } = {}) {
draw_text(text, x, y, options.scale ?? TEXT_SCALE, options.color ?? WHITE);
draw_text(text, this.#x(x), this.#y(y), TEXT_SCALE * (options.scale ?? 1), options.color ?? WHITE);
}
measure_text(text: string, scale = TEXT_SCALE) {
return measure_text(text, scale);
measure_text(text: string, scale = 1) {
return measure_text(text, TEXT_SCALE * scale) / S;
}
texture(id: string, x: number, y: number, w: number, h: number) {
texture(id: string, x: number, y: number, w: number, h: number, part = [0, 0, 1, 1]) {
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
const region = get_sprite_region(id);
const [u, v, pw, ph] = part;
draw_texture_region(
atlas,
region.x,
region.y,
region.width,
region.height,
x,
y,
w,
h,
region.x + region.width * u,
region.y + region.height * v,
region.width * pw,
region.height * ph,
this.#x(x),
this.#y(y),
w * S,
h * S,
);
}
@@ -118,10 +139,14 @@ export class ModGraphics implements Graphics {
const stack = new ItemStack(id, count);
// the count only shows when it's more than one
if (count === 1) stack.max_amount = 1;
draw_item(stack, x, y);
draw_item(stack, this.#x(x), this.#y(y));
}
clip(x: number, y: number, w: number, h: number, draw: () => void) {
this.#clip_canvas(this.#x(x), this.#y(y), w * S, h * S, draw);
}
#clip_canvas(x: number, y: number, w: number, h: number, draw: () => void) {
begin_clip(x, y, w, h);
try {
draw();
@@ -132,9 +157,10 @@ export class ModGraphics implements Graphics {
button(label: string, x: number, y: number, w: number, h: number): boolean {
const hovered = point_inside_rec(this.mouse.x, this.mouse.y, x, y, w, h);
draw_gui_sprite(hovered ? "button_highlighted" : "button", x, y, w, h);
const [cx, cy, cw, ch] = [this.#x(x), this.#y(y), w * S, h * S];
draw_gui_sprite(hovered ? "button_highlighted" : "button", cx, cy, cw, ch);
const text_width = measure_text(label, TEXT_SCALE);
draw_text(label, x + (w - text_width) / 2, y + (h - TEXT_HEIGHT * TEXT_SCALE) / 2, TEXT_SCALE, WHITE);
draw_text(label, cx + (cw - text_width) / 2, cy + (ch - TEXT_HEIGHT * TEXT_SCALE) / 2, TEXT_SCALE, WHITE);
if (hovered && this.mouse.pressed) {
this.consume_click();
return true;
@@ -142,15 +168,17 @@ export class ModGraphics implements Graphics {
return false;
}
// INPUT_HEIGHT canvas pixels tall
text_input(id: string, x: number, y: number, w: number): string {
let input = this.#inputs.get(id);
if (!input) {
input = new TextInput("", 256);
this.#inputs.set(id, input);
}
const hovered = point_inside_rec(this.mouse.x, this.mouse.y, x, y, w, INPUT_HEIGHT);
const [cx, cy, cw] = [this.#x(x), this.#y(y), w * S];
const hovered = point_inside_rec(this.mouse.x, this.mouse.y, x, y, w, INPUT_HEIGHT / S);
if (this.#focused === id || (hovered && this.mouse.pressed)) {
input.handle_mouse({ x, y, width: w, height: INPUT_HEIGHT }, x + 6);
input.handle_mouse({ x: cx, y: cy, width: cw, height: INPUT_HEIGHT }, cx + 6);
}
if (this.mouse.pressed) {
if (hovered) {
@@ -169,15 +197,15 @@ export class ModGraphics implements Graphics {
}
}
draw_gui_sprite(focused ? "text_field_highlighted" : "text_field", x, y, w, INPUT_HEIGHT);
draw_gui_sprite(focused ? "text_field_highlighted" : "text_field", cx, cy, cw, INPUT_HEIGHT);
// inside the field's border, so long text doesn't go over it
this.clip(x + FIELD_BORDER, y + 2, w - FIELD_BORDER * 2, INPUT_HEIGHT - 4, () => {
this.#clip_canvas(cx + FIELD_BORDER, cy + 2, cw - FIELD_BORDER * 2, INPUT_HEIGHT - 4, () => {
if (focused) {
input.draw_selection(x + 6, y + 6, INPUT_HEIGHT - 12);
input.draw_selection(cx + 6, cy + 6, INPUT_HEIGHT - 12);
}
draw_text(input.value, x + 6, y + 4, TEXT_SCALE, WHITE);
draw_text(input.value, cx + 6, cy + 4, TEXT_SCALE, WHITE);
if (focused && TextInput.caret_visible()) {
draw_rect(input.caret_x(x + 6), y + 6, 2, INPUT_HEIGHT - 12, WHITE);
draw_rect(input.caret_x(cx + 6), cy + 6, 2, INPUT_HEIGHT - 12, WHITE);
}
});
return input.value;
@@ -187,10 +215,6 @@ export class ModGraphics implements Graphics {
throw new Error("g.slider isn't implemented yet");
}
slots(): void {
throw new Error("g.slots isn't implemented yet");
}
key_pressed(key: KeyCode): boolean {
return !this.typing && InputManager.is_key_pressed(key as InputKey);
}
@@ -202,8 +226,8 @@ export class ModGraphics implements Graphics {
? { text: line, color: WHITE }
: { text: line.text, color: line.color ?? WHITE }
),
x,
y,
x: this.#x(x),
y: this.#y(y),
};
}
@@ -214,11 +238,11 @@ export class ModGraphics implements Graphics {
typeof line === "string" ? { text: line, color: GREY } : { text: line.text, color: line.color ?? GREY }
),
];
this.#tooltip = { lines, x, y };
this.#tooltip = { lines, x: this.#x(x), y: this.#y(y) };
}
block_mouse(x: number, y: number, w: number, h: number) {
this.#blocked.push({ x, y, w, h });
this.#blocked.push({ x: this.#x(x), y: this.#y(y), w: w * S, h: h * S });
}
consume_click() {
@@ -227,6 +251,3 @@ export class ModGraphics implements Graphics {
InputManager.consume_mouse(2);
}
}
// how big an item is drawn, for laying out around g.item
export const ITEM_SIZE = SLOT_SIZE;