Recipe viewer
This commit is contained in:
@@ -0,0 +1,224 @@
|
||||
// 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, TEXTURE_SIZE } from "$/common/constants.ts";
|
||||
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 { 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_item, draw_nine_slice } 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";
|
||||
|
||||
const WHITE: Color = [1, 1, 1, 1];
|
||||
const GREY: Color = [0.66, 0.66, 0.66, 1];
|
||||
const INPUT_HEIGHT = 32;
|
||||
|
||||
export class ModGraphics implements Graphics {
|
||||
// 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
|
||||
#blocked: { x: number; y: number; w: number; h: number }[] = [];
|
||||
#clicks_consumed = false;
|
||||
|
||||
get width() {
|
||||
return canvas.width;
|
||||
}
|
||||
|
||||
get height() {
|
||||
return canvas.height;
|
||||
}
|
||||
|
||||
get mouse() {
|
||||
const { x, y } = InputManager.get_mouse_position();
|
||||
return {
|
||||
x,
|
||||
y,
|
||||
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 } = this.mouse;
|
||||
return this.#clicks_consumed || this.#blocked.some((r) => point_inside_rec(x, y, r.x, r.y, r.w, r.h));
|
||||
}
|
||||
|
||||
rect(x: number, y: number, w: number, h: number, color: Color = WHITE) {
|
||||
draw_rect(x, y, w, h, color);
|
||||
}
|
||||
|
||||
panel(x: number, y: number, w: number, h: number) {
|
||||
draw_nine_slice(AssetManager.instance.get<Texture>("bworld:ui"), 160, 0, 16, 16, 4, 4, 4, 4, x, y, w, h);
|
||||
}
|
||||
|
||||
text(text: string, x: number, y: number, options: { scale?: number; color?: Color } = {}) {
|
||||
draw_text(text, x, y, options.scale ?? TEXT_SCALE, options.color ?? WHITE);
|
||||
}
|
||||
|
||||
measure_text(text: string, scale = TEXT_SCALE) {
|
||||
return measure_text(text, scale);
|
||||
}
|
||||
|
||||
texture(id: string, x: number, y: number, w: number, h: number) {
|
||||
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
|
||||
const region = get_sprite_region(id);
|
||||
draw_texture_region(
|
||||
atlas,
|
||||
region.x * TEXTURE_SIZE,
|
||||
region.y * TEXTURE_SIZE,
|
||||
TEXTURE_SIZE,
|
||||
TEXTURE_SIZE,
|
||||
x,
|
||||
y,
|
||||
w,
|
||||
h,
|
||||
);
|
||||
}
|
||||
|
||||
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
|
||||
if (count === 1) stack.max_amount = 1;
|
||||
draw_item(stack, x, y);
|
||||
}
|
||||
|
||||
clip(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 [sx, sy] = hovered ? [224, 16] : [176, 0];
|
||||
draw_nine_slice(AssetManager.instance.get<Texture>("bworld:ui"), sx, sy, 16, 16, 4, 4, 4, 4, x, y, w, h);
|
||||
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);
|
||||
if (hovered && this.mouse.pressed) {
|
||||
this.consume_click();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
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);
|
||||
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_rect(x, y, w, INPUT_HEIGHT, focused ? WHITE : GREY);
|
||||
draw_rect(x + 2, y + 2, w - 4, INPUT_HEIGHT - 4, [0, 0, 0, 1]);
|
||||
this.clip(x + 2, y + 2, w - 4, INPUT_HEIGHT - 4, () => {
|
||||
draw_text(input.value, x + 6, y + 4, TEXT_SCALE, WHITE);
|
||||
if (focused && TextInput.caret_visible()) {
|
||||
draw_rect(input.caret_x(x + 6), y + 6, 2, INPUT_HEIGHT - 12, WHITE);
|
||||
}
|
||||
});
|
||||
return input.value;
|
||||
}
|
||||
|
||||
slider(): number {
|
||||
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);
|
||||
}
|
||||
|
||||
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,
|
||||
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, y };
|
||||
}
|
||||
|
||||
block_mouse(x: number, y: number, w: number, h: number) {
|
||||
this.#blocked.push({ x, y, w, h });
|
||||
}
|
||||
|
||||
consume_click() {
|
||||
this.#clicks_consumed = true;
|
||||
InputManager.consume_mouse(0);
|
||||
InputManager.consume_mouse(2);
|
||||
}
|
||||
}
|
||||
|
||||
// how big an item is drawn, for laying out around g.item
|
||||
export const ITEM_SIZE = SLOT_SIZE;
|
||||
Reference in New Issue
Block a user