Recipe viewer
This commit is contained in:
+2
-1
@@ -150,7 +150,8 @@ export class Client {
|
||||
#handle_keybinds(closed_screen: boolean) {
|
||||
const player = this.player;
|
||||
|
||||
if (!closed_screen) {
|
||||
// typing into a screen's text field, keys are letters, not keybinds
|
||||
if (!closed_screen && !this.screen?.typing) {
|
||||
this.#handle_keys();
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ import { ClientInventories } from "../inventory.ts";
|
||||
import { canvas, Texture } from "../renderer/mod.ts";
|
||||
import { draw_item, draw_nine_slice } from "../rendering/render_utils.ts";
|
||||
import { draw_tooltip, item_tooltip } from "./tooltip.ts";
|
||||
import { ModGraphics } from "./mod_graphics.ts";
|
||||
import { inventory_overlays } from "../mods.ts";
|
||||
|
||||
export class Slot {
|
||||
container: ContainerKey;
|
||||
@@ -34,6 +36,11 @@ export abstract class GuiScreen {
|
||||
abstract on_tick(_delta: number): void;
|
||||
abstract on_render(): void;
|
||||
abstract on_close(): void;
|
||||
|
||||
// something in it has the keyboard, like a text field, so keybinds (inventory, drop) don't fire
|
||||
get typing(): boolean {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// a screen of slots. the server owns the contents: clicks are sent to it and guessed locally
|
||||
@@ -46,6 +53,8 @@ export class GuiInventoryScreen extends GuiScreen {
|
||||
hovering: Slot | undefined;
|
||||
inventory_width: number = 500;
|
||||
inventory_height: number = 500;
|
||||
// what mods draw over it, like a recipe viewer
|
||||
graphics = new ModGraphics();
|
||||
|
||||
constructor(inventories: ClientInventories, send: (message: ClientMessage) => void) {
|
||||
super();
|
||||
@@ -64,11 +73,13 @@ export class GuiInventoryScreen extends GuiScreen {
|
||||
}
|
||||
}
|
||||
|
||||
override get typing() {
|
||||
return this.graphics.typing;
|
||||
}
|
||||
|
||||
on_tick(_delta: number): void {
|
||||
this.x = canvas.width / 2 - (this.inventory_width / 2);
|
||||
this.y = canvas.height / 2 - (this.inventory_height / 2);
|
||||
|
||||
this.handle_interaction();
|
||||
}
|
||||
|
||||
on_render(): void {
|
||||
@@ -100,6 +111,14 @@ export class GuiInventoryScreen extends GuiScreen {
|
||||
}
|
||||
}
|
||||
|
||||
// mods' overlays go on top and get the mouse first, then the slots get what they left
|
||||
this.#render_overlays();
|
||||
if (this.graphics.mouse_blocked()) {
|
||||
this.hovering = undefined;
|
||||
} else {
|
||||
this.handle_interaction();
|
||||
}
|
||||
|
||||
const mouse = InputManager.get_mouse_position();
|
||||
if (this.inventories.cursor.item) {
|
||||
draw_item(this.inventories.cursor.item, mouse.x, mouse.y);
|
||||
@@ -110,6 +129,29 @@ export class GuiInventoryScreen extends GuiScreen {
|
||||
draw_tooltip(item_tooltip(item), mouse.x, mouse.y);
|
||||
}
|
||||
}
|
||||
this.graphics.end();
|
||||
}
|
||||
|
||||
#render_overlays() {
|
||||
const g = this.graphics;
|
||||
g.begin();
|
||||
const hovered = this.hovering && this.get_container(this.hovering.container)?.get_item(this.hovering.index);
|
||||
const view = {
|
||||
x: this.x,
|
||||
y: this.y,
|
||||
width: this.inventory_width,
|
||||
height: this.inventory_height,
|
||||
hovered_item: hovered ? { id: hovered.type_id, count: hovered.amount, data: hovered.data } : undefined,
|
||||
};
|
||||
for (const entry of [...inventory_overlays]) {
|
||||
try {
|
||||
entry.overlay.on_render(g, view);
|
||||
} catch (e) {
|
||||
// a broken overlay is taken off, not left to throw every frame
|
||||
console.error(`[${entry.mod}] inventory overlay threw, removing it:`, e);
|
||||
inventory_overlays.splice(inventory_overlays.indexOf(entry), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
on_close(): void {
|
||||
|
||||
@@ -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;
|
||||
+45
-4
@@ -1,11 +1,12 @@
|
||||
// loads the mods the server lists: downloads their .bmod files, registers their data, then runs their client scripts.
|
||||
// see "Delivery to clients" in MODS.md
|
||||
import { AIR } from "$/common/constants.ts";
|
||||
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import type { ClientContext } from "$/common/mod_api/client.ts";
|
||||
import { ModListing, ModLoadError, register_mod_data } from "$/common/mod_loader.ts";
|
||||
import { BlockRegistry, EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
|
||||
import type { ClientContext, InventoryOverlay } from "$/common/mod_api/client.ts";
|
||||
import { ModListing, ModLoadError, RecipeBook, register_mod_data } from "$/common/mod_loader.ts";
|
||||
import { type Bmod, read_bmod } from "$/common/bmod.ts";
|
||||
import type { OreJson } from "$/common/mod_data.ts";
|
||||
import { display_name } from "$/common/utils.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import type { Client } from "./client.ts";
|
||||
import { code_url, fetch_verified } from "./handshake.ts";
|
||||
@@ -19,6 +20,13 @@ export const worldgen_mods: { scripts: { mod: string; url: string }[]; ores: Ore
|
||||
// set once the game is running, mods can't look at it during setup
|
||||
let client: Client | undefined;
|
||||
|
||||
// what mods draw on inventory screens, see GuiInventoryScreen
|
||||
export const inventory_overlays: { mod: string; overlay: InventoryOverlay }[] = [];
|
||||
|
||||
// the loaded mods' recipes, and their names by id
|
||||
let recipe_book = new RecipeBook();
|
||||
const mod_names = new Map<string, string>();
|
||||
|
||||
export function set_mods_client(game_client: Client) {
|
||||
client = game_client;
|
||||
}
|
||||
@@ -47,6 +55,10 @@ export async function load_client_mods(listings: ModListing[], bmods: Bmod[]) {
|
||||
}
|
||||
|
||||
const recipes = register_mod_data(bmods.map((bmod) => ({ id: bmod.manifest.id, data: bmod.data })));
|
||||
recipe_book = recipes;
|
||||
inventory_overlays.length = 0;
|
||||
mod_names.clear();
|
||||
for (const listing of listings) mod_names.set(listing.id, listing.name);
|
||||
|
||||
worldgen_mods.ores = recipes.ores;
|
||||
worldgen_mods.scripts = bmods.flatMap((bmod) =>
|
||||
@@ -80,9 +92,24 @@ function client_context(listing: ModListing): ClientContext {
|
||||
return client;
|
||||
};
|
||||
|
||||
const not_yet_fn = (name: string) => () => {
|
||||
throw new Error(`[${mod}] ctx.ui.${name} isn't implemented yet (Custom screens in MODS.md)`);
|
||||
};
|
||||
const known_item = (id: string) => {
|
||||
const item = EverythingRegistry.get<ItemRegistry>("items", id);
|
||||
if (!item) throw new Error(`[${mod}] unknown item ${id}`);
|
||||
return item;
|
||||
};
|
||||
|
||||
return {
|
||||
mod: { id: mod, version: listing.version },
|
||||
ui: not_yet("ui", "step 7") as ClientContext["ui"],
|
||||
ui: {
|
||||
register_screen: not_yet_fn("register_screen"),
|
||||
open: not_yet_fn("open"),
|
||||
add_inventory_overlay(overlay) {
|
||||
inventory_overlays.push({ mod, overlay });
|
||||
},
|
||||
},
|
||||
hud: not_yet("hud", "step 7") as ClientContext["hud"],
|
||||
input: not_yet("input", "step 8") as ClientContext["input"],
|
||||
net: not_yet("net", "step 8") as ClientContext["net"],
|
||||
@@ -105,6 +132,20 @@ function client_context(listing: ModListing): ClientContext {
|
||||
return need_client().level.time;
|
||||
},
|
||||
},
|
||||
items: {
|
||||
ids: () => EverythingRegistry.entries("items").map(([id]) => id),
|
||||
exists: (id) => EverythingRegistry.get("items", id) !== undefined,
|
||||
name: (id) => (known_item(id), display_name(id)),
|
||||
mod(id) {
|
||||
known_item(id);
|
||||
const namespace = id.split(":")[0];
|
||||
return mod_names.get(namespace) ?? namespace;
|
||||
},
|
||||
},
|
||||
recipes: {
|
||||
list: (type) =>
|
||||
structuredClone(type ? recipe_book.all.filter((recipe) => recipe.type === type) : recipe_book.all),
|
||||
},
|
||||
log: (...args) => console.log(`[${mod}]`, ...args),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user