import { listPlaceableGrounds, listPlaceableBlockPaletteEntries } from "../../../shared/definitions/tiles.js"; import { listSpawnableMobTypes, mobSpriteName } from "../../../shared/definitions/mobs.js"; import { defaultStateForType, resolveState, spriteImageName, palettePreviewImageName } from "../../../shared/definitions/tileSprites.js"; import { itemSpawnerPreviewSpriteName } from "../../../shared/definitions/itemSpawners.js"; /** * Sidebar / modal categories. Labels match the existing Select placeable dialog. */ export const PALETTE_TABS = Object.freeze([ { id: "grounds", label: "Ground tiles" }, { id: "blocks", label: "Blocks" }, { id: "spawners", label: "Spawners" } ]); /** * Placeable cards for one palette category, built from the live registries. * * Tile, block, and mob registries are shared by every dimension (a cave can * place the same grounds and spawners as the overworld). `dimensionId` is * part of the query so the editor re-lists for the dimension being edited * instead of freezing an overworld-only snapshot. * * @param {"grounds"|"blocks"|"spawners"} tabId * @param {{ catalog?: object|null, dimensionId?: string|null }} [context] * @returns {{ key: string, label: string, spriteNames: string[], selection: object }[]} */ export function listPaletteEntries (tabId, context = {}) { const catalog = context.catalog ?? null; // Accepted so callers bind this list to the dimension currently open. // Nothing in the registries is dimension-scoped today. void context.dimensionId; if (tabId === "grounds") { return listGroundEntries(catalog); } if (tabId === "blocks") { return listBlockEntries(catalog); } if (tabId === "spawners") { return listSpawnerEntries(); } return []; } /** * Case-insensitive name filter used by the palette search box. * @param {string} label * @param {string} query */ export function paletteLabelMatches (label, query) { const q = String(query ?? "").trim().toLowerCase(); if (!q) { return true; } return String(label ?? "").toLowerCase().includes(q); } /** * Keys of visible cards that represent the active placement tool. * A single card for a type stays highlighted when the editor cycles a visual * state that is not its own card (seasonal grounds, one tree sprite, …). * Types with several cards (ores, herbs, doors) highlight only the exact state. * * @param {ReturnType} visibleEntries * @param {ReturnType} allEntries * @param {object|null} tool * @returns {Set} */ export function matchingPaletteEntryKeys (visibleEntries, allEntries, tool) { const pool = Array.isArray(allEntries) ? allEntries : (visibleEntries ?? []); const visibleKeys = new Set((visibleEntries ?? []).map((entry) => entry.key)); let matched = pool.filter((entry) => paletteEntryMatchesTool(entry, tool)); if (!matched.length && tool && (tool.kind === "ground" || tool.kind === "block")) { const sameType = pool.filter((entry) => entry.selection?.kind === tool.kind && entry.selection?.typeId === tool.typeId ); if (sameType.length === 1) { matched = sameType; } } return new Set(matched.map((entry) => entry.key).filter((key) => visibleKeys.has(key))); } function paletteEntryMatchesTool (entry, tool) { const selection = entry?.selection; if (!selection || !tool || selection.kind !== tool.kind) { return false; } if (selection.kind === "ground") { return selection.typeId === tool.typeId; } if (selection.kind === "block") { return selection.typeId === tool.typeId && selection.state === tool.state; } if (selection.kind === "spawner") { if (selection.spawnerKind === "item") { return tool.spawnerKind === "item"; } return tool.spawnerKind !== "item" && selection.mobTypeId === tool.mobTypeId; } return false; } function listGroundEntries (catalog) { const entries = []; for (const def of listPlaceableGrounds()) { const state = defaultStateForType(catalog, "ground", def.key); const name = spriteImageName("ground", def.key, resolveState( catalog?.grounds?.[def.key]?.states ?? [], state )); entries.push({ key: `ground:${def.id}`, label: def.displayName, spriteNames: [palettePreviewImageName("ground", def.key), name], selection: { kind: "ground", typeId: def.id } }); } return entries; } function listBlockEntries (catalog) { return listPlaceableBlockPaletteEntries(catalog).map((entry) => ({ key: `block:${entry.typeId}:${entry.state ?? ""}`, label: entry.displayName, spriteNames: [entry.spriteName], selection: { kind: "block", typeId: entry.typeId, state: entry.state } })); } function listSpawnerEntries () { const entries = [{ key: "spawner:item", label: "Item Spawner", spriteNames: [itemSpawnerPreviewSpriteName()], selection: { kind: "spawner", spawnerKind: "item" } }]; for (const def of listSpawnableMobTypes()) { entries.push({ key: `spawner:${def.mobTypeId}`, label: def.displayName, spriteNames: [mobSpriteName(def.mobTypeId)], selection: { kind: "spawner", mobTypeId: def.mobTypeId } }); } return entries; }