Hud api
This commit is contained in:
@@ -7,7 +7,7 @@
|
||||
import { EverythingRegistry, type ItemRegistry } from "@/common/everything_registry.ts";
|
||||
import { ItemStack } from "@/common/inventory.ts";
|
||||
import type { Color, Graphics, TooltipLine } from "@/common/mod_api/client.ts";
|
||||
import type { FluidVariant, KeyCode } from "@/common/mod_api/shared.ts";
|
||||
import type { FluidVariant, ItemStack as ApiItemStack, 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";
|
||||
@@ -25,11 +25,13 @@ import { draw_fluid, draw_item } from "@/client/rendering/render_utils.ts";
|
||||
import { get_sprite_region } from "@/client/sprites.ts";
|
||||
import { draw_tooltip, item_tooltip } from "./tooltip.ts";
|
||||
import { TEXT_HEIGHT, TEXT_SCALE, TextInput } from "./widgets.ts";
|
||||
import { GUI_ATLAS } from "./gui_atlas.ts";
|
||||
import { GUI_ATLAS, type GuiSprite } 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];
|
||||
// text's shadow, see Graphics.text
|
||||
const SHADOW = [0.15, 0.15, 0.15];
|
||||
|
||||
// a script's tooltip line, copied so it can't change after, in color when it doesn't say
|
||||
function tooltip_line(line: string | TooltipLine, color: Color): TooltipLine {
|
||||
@@ -118,8 +120,13 @@ export class ModGraphics implements Graphics {
|
||||
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);
|
||||
text(text: string, x: number, y: number, options: { scale?: number; color?: Color; shadow?: boolean } = {}) {
|
||||
const scale = TEXT_SCALE * (options.scale ?? 1);
|
||||
const color = options.color ?? WHITE;
|
||||
if (options.shadow) {
|
||||
draw_text(text, this.#x(x) + scale, this.#y(y) + scale, scale, [...SHADOW, color[3]]);
|
||||
}
|
||||
draw_text(text, this.#x(x), this.#y(y), scale, color);
|
||||
}
|
||||
|
||||
measure_text(text: string, scale = 1) {
|
||||
@@ -163,8 +170,28 @@ export class ModGraphics implements Graphics {
|
||||
}
|
||||
}
|
||||
|
||||
item(id: string, x: number, y: number, count = 1) {
|
||||
sprite(id: string, x: number, y: number, options: { width?: number; height?: number; color?: Color } = {}) {
|
||||
const name = id.startsWith("cubi:") ? id.slice("cubi:".length) : undefined;
|
||||
if (!name || !Object.hasOwn(GUI_ATLAS, name)) throw new Error(`unknown gui sprite ${id}`);
|
||||
const { width, height, color } = options;
|
||||
draw_gui_sprite(
|
||||
name as GuiSprite,
|
||||
this.#x(x),
|
||||
this.#y(y),
|
||||
width === undefined ? undefined : width * S,
|
||||
height === undefined ? undefined : height * S,
|
||||
color,
|
||||
);
|
||||
}
|
||||
|
||||
item(item: string | ApiItemStack, x: number, y: number, count = 1) {
|
||||
const id = typeof item === "string" ? item : item.id;
|
||||
if (!EverythingRegistry.get<ItemRegistry>("items", id)) throw new Error(`unknown item ${id}`);
|
||||
if (typeof item !== "string") {
|
||||
// like a slot
|
||||
draw_item(ItemStack.from_data(structuredClone(item)), this.#x(x), this.#y(y));
|
||||
return;
|
||||
}
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user