280 lines
8.8 KiB
TypeScript
280 lines
8.8 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// 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. 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";
|
|
import type { FluidVariant, KeyCode } from "@/common/mod_api/shared.ts";
|
|
import { point_inside_rec } from "@/common/utils.ts";
|
|
import { AssetManager } from "@/client/assets.ts";
|
|
import { InputManager, type KeyCode as InputKey } from "@/client/input_manager.ts";
|
|
import {
|
|
begin_clip,
|
|
canvas,
|
|
draw_rect,
|
|
draw_text,
|
|
draw_texture_region,
|
|
end_clip,
|
|
measure_text,
|
|
Texture,
|
|
} from "@/client/renderer/mod.ts";
|
|
import { draw_fluid, draw_item } from "@/client/rendering/render_utils.ts";
|
|
import { get_sprite_region } from "@/client/sprites.ts";
|
|
import { draw_tooltip, item_tooltip, type TooltipLine } from "./tooltip.ts";
|
|
import { TEXT_HEIGHT, TEXT_SCALE, TextInput } from "./widgets.ts";
|
|
import { GUI_ATLAS } from "./gui_atlas.ts";
|
|
import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts";
|
|
|
|
const WHITE: Color = [1, 1, 1, 1];
|
|
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, 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 / S;
|
|
}
|
|
|
|
get 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: (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),
|
|
};
|
|
}
|
|
|
|
// a text input has the keyboard, so keybinds like the inventory key shouldn't fire
|
|
get typing() {
|
|
return this.#focused !== undefined;
|
|
}
|
|
|
|
// call before a frame's drawing
|
|
begin() {
|
|
this.#tooltip = undefined;
|
|
this.#blocked = [];
|
|
this.#clicks_consumed = false;
|
|
}
|
|
|
|
// call after, draws the tooltip over everything
|
|
end() {
|
|
if (this.#tooltip) draw_tooltip(this.#tooltip.lines, this.#tooltip.x, this.#tooltip.y);
|
|
}
|
|
|
|
// whether the screen underneath should ignore the mouse
|
|
mouse_blocked() {
|
|
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(this.#x(x), this.#y(y), w * S, h * S, color);
|
|
}
|
|
|
|
panel(x: number, y: number, w: number, h: number) {
|
|
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, this.#x(x), this.#y(y), TEXT_SCALE * (options.scale ?? 1), options.color ?? WHITE);
|
|
}
|
|
|
|
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, part = [0, 0, 1, 1]) {
|
|
const atlas = AssetManager.instance.get<Texture>("cubi:textures");
|
|
const region = get_sprite_region(id);
|
|
const [u, v, pw, ph] = part;
|
|
draw_texture_region(
|
|
atlas,
|
|
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,
|
|
);
|
|
}
|
|
|
|
progress(
|
|
empty: string,
|
|
full: string,
|
|
x: number,
|
|
y: number,
|
|
w: number,
|
|
h: number,
|
|
filled: number,
|
|
direction: "right" | "up" = "right",
|
|
) {
|
|
// nothing filled for NaN too, like progress / max before there's a max
|
|
const done = filled > 0 ? Math.min(1, filled) : 0;
|
|
this.texture(empty, x, y, w, h);
|
|
if (direction === "right") {
|
|
this.texture(full, x, y, w * done, h, [0, 0, done, 1]);
|
|
} else {
|
|
this.texture(full, x, y + h * (1 - done), w, h * done, [0, 1 - done, 1, done]);
|
|
}
|
|
}
|
|
|
|
item(id: string, x: number, y: number, count = 1) {
|
|
if (!EverythingRegistry.get<ItemRegistry>("items", id)) throw new Error(`unknown item ${id}`);
|
|
const stack = new ItemStack(id, count);
|
|
// the count only shows when it's more than one
|
|
draw_item(stack, this.#x(x), this.#y(y), count !== 1 && stack.max_amount !== 1);
|
|
}
|
|
|
|
fluid(fluid: FluidVariant, x: number, y: number, filled = 1) {
|
|
draw_fluid(fluid, this.#x(x), this.#y(y), filled);
|
|
}
|
|
|
|
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();
|
|
} finally {
|
|
end_clip();
|
|
}
|
|
}
|
|
|
|
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);
|
|
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, 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;
|
|
}
|
|
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 [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: cx, y: cy, width: cw, height: INPUT_HEIGHT }, cx + 6);
|
|
}
|
|
if (this.mouse.pressed) {
|
|
if (hovered) {
|
|
this.#focused = id;
|
|
this.consume_click();
|
|
} else if (this.#focused === id) {
|
|
this.#focused = undefined;
|
|
}
|
|
}
|
|
const focused = this.#focused === id;
|
|
if (focused) {
|
|
// what was typed this frame goes in before enter or escape lets go of the keyboard
|
|
input.handle_keys();
|
|
if (InputManager.is_key_pressed("Escape") || InputManager.is_key_pressed("Enter")) {
|
|
this.#focused = undefined;
|
|
}
|
|
}
|
|
|
|
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_canvas(cx + FIELD_BORDER, cy + 2, cw - FIELD_BORDER * 2, INPUT_HEIGHT - 4, () => {
|
|
if (focused) {
|
|
input.draw_selection(cx + 6, cy + 6, INPUT_HEIGHT - 12);
|
|
}
|
|
draw_text(input.value, cx + 6, cy + 4, TEXT_SCALE, WHITE);
|
|
if (focused && TextInput.caret_visible()) {
|
|
draw_rect(input.caret_x(cx + 6), cy + 6, 2, INPUT_HEIGHT - 12, WHITE);
|
|
}
|
|
});
|
|
return input.value;
|
|
}
|
|
|
|
slider(): number {
|
|
throw new Error("g.slider isn't implemented yet");
|
|
}
|
|
|
|
key_pressed(key: KeyCode): boolean {
|
|
return !this.typing && InputManager.is_key_pressed(key as InputKey);
|
|
}
|
|
|
|
tooltip(lines: (string | { text: string; color?: Color })[], x: number, y: number) {
|
|
this.#tooltip = {
|
|
lines: lines.map((line) =>
|
|
typeof line === "string"
|
|
? { text: line, color: WHITE }
|
|
: { text: line.text, color: line.color ?? WHITE }
|
|
),
|
|
x: this.#x(x),
|
|
y: this.#y(y),
|
|
};
|
|
}
|
|
|
|
item_tooltip(id: string, x: number, y: number, extra: (string | { text: string; color?: Color })[] = []) {
|
|
const lines = [
|
|
...item_tooltip(new ItemStack(id, 1)),
|
|
...extra.map((line) =>
|
|
typeof line === "string" ? { text: line, color: GREY } : { text: line.text, color: line.color ?? GREY }
|
|
),
|
|
];
|
|
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: this.#x(x), y: this.#y(y), w: w * S, h: h * S });
|
|
}
|
|
|
|
consume_click() {
|
|
this.#clicks_consumed = true;
|
|
InputManager.consume_mouse(0);
|
|
InputManager.consume_mouse(2);
|
|
}
|
|
}
|