| 1 | import { listPlaceableGrounds, listPlaceableBlockPaletteEntries } from "../../../shared/definitions/tiles.js"; |
| 2 | import { listSpawnableMobTypes, mobSpriteName } from "../../../shared/definitions/mobs.js"; |
| 3 | import |
| 4 | { |
| 5 | defaultStateForType, |
| 6 | resolveState, |
| 7 | spriteImageName, |
| 8 | palettePreviewImageName |
| 9 | } from "../../../shared/definitions/tileSprites.js"; |
| 10 | import { itemSpawnerPreviewSpriteName } from "../../../shared/definitions/itemSpawners.js"; |
| 11 | |
| 12 | /** |
| 13 | * Sidebar / modal categories. Labels match the existing Select placeable dialog. |
| 14 | */ |
| 15 | export const PALETTE_TABS = Object.freeze([ |
| 16 | { id: "grounds", label: "Ground tiles" }, |
| 17 | { id: "blocks", label: "Blocks" }, |
| 18 | { id: "spawners", label: "Spawners" } |
| 19 | ]); |
| 20 | |
| 21 | /** |
| 22 | * Placeable cards for one palette category, built from the live registries. |
| 23 | * |
| 24 | * Tile, block, and mob registries are shared by every dimension (a cave can |
| 25 | * place the same grounds and spawners as the overworld). `dimensionId` is |
| 26 | * part of the query so the editor re-lists for the dimension being edited |
| 27 | * instead of freezing an overworld-only snapshot. |
| 28 | * |
| 29 | * @param {"grounds"|"blocks"|"spawners"} tabId |
| 30 | * @param {{ catalog?: object|null, dimensionId?: string|null }} [context] |
| 31 | * @returns {{ key: string, label: string, spriteNames: string[], selection: object }[]} |
| 32 | */ |
| 33 | export function listPaletteEntries (tabId, context = {}) |
| 34 | { |
| 35 | const catalog = context.catalog ?? null; |
| 36 | // Accepted so callers bind this list to the dimension currently open. |
| 37 | // Nothing in the registries is dimension-scoped today. |
| 38 | void context.dimensionId; |
| 39 | |
| 40 | if (tabId === "grounds") |
| 41 | { return listGroundEntries(catalog); } |
| 42 | if (tabId === "blocks") |
| 43 | { return listBlockEntries(catalog); } |
| 44 | if (tabId === "spawners") |
| 45 | { return listSpawnerEntries(); } |
| 46 | return []; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Case-insensitive name filter used by the palette search box. |
| 51 | * @param {string} label |
| 52 | * @param {string} query |
| 53 | */ |
| 54 | export function paletteLabelMatches (label, query) |
| 55 | { |
| 56 | const q = String(query ?? "").trim().toLowerCase(); |
| 57 | if (!q) |
| 58 | { return true; } |
| 59 | return String(label ?? "").toLowerCase().includes(q); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Keys of visible cards that represent the active placement tool. |
| 64 | * A single card for a type stays highlighted when the editor cycles a visual |
| 65 | * state that is not its own card (seasonal grounds, one tree sprite, …). |
| 66 | * Types with several cards (ores, herbs, doors) highlight only the exact state. |
| 67 | * |
| 68 | * @param {ReturnType<typeof listPaletteEntries>} visibleEntries |
| 69 | * @param {ReturnType<typeof listPaletteEntries>} allEntries |
| 70 | * @param {object|null} tool |
| 71 | * @returns {Set<string>} |
| 72 | */ |
| 73 | export function matchingPaletteEntryKeys (visibleEntries, allEntries, tool) |
| 74 | { |
| 75 | const pool = Array.isArray(allEntries) ? allEntries : (visibleEntries ?? []); |
| 76 | const visibleKeys = new Set((visibleEntries ?? []).map((entry) => entry.key)); |
| 77 | let matched = pool.filter((entry) => paletteEntryMatchesTool(entry, tool)); |
| 78 | if (!matched.length && tool && (tool.kind === "ground" || tool.kind === "block")) |
| 79 | { |
| 80 | const sameType = pool.filter((entry) => |
| 81 | entry.selection?.kind === tool.kind && entry.selection?.typeId === tool.typeId |
| 82 | ); |
| 83 | if (sameType.length === 1) |
| 84 | { matched = sameType; } |
| 85 | } |
| 86 | return new Set(matched.map((entry) => entry.key).filter((key) => visibleKeys.has(key))); |
| 87 | } |
| 88 | |
| 89 | function paletteEntryMatchesTool (entry, tool) |
| 90 | { |
| 91 | const selection = entry?.selection; |
| 92 | if (!selection || !tool || selection.kind !== tool.kind) |
| 93 | { return false; } |
| 94 | if (selection.kind === "ground") |
| 95 | { return selection.typeId === tool.typeId; } |
| 96 | if (selection.kind === "block") |
| 97 | { return selection.typeId === tool.typeId && selection.state === tool.state; } |
| 98 | if (selection.kind === "spawner") |
| 99 | { |
| 100 | if (selection.spawnerKind === "item") |
| 101 | { return tool.spawnerKind === "item"; } |
| 102 | return tool.spawnerKind !== "item" && selection.mobTypeId === tool.mobTypeId; |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | function listGroundEntries (catalog) |
| 108 | { |
| 109 | const entries = []; |
| 110 | for (const def of listPlaceableGrounds()) |
| 111 | { |
| 112 | const state = defaultStateForType(catalog, "ground", def.key); |
| 113 | const name = spriteImageName("ground", def.key, resolveState( |
| 114 | catalog?.grounds?.[def.key]?.states ?? [], |
| 115 | state |
| 116 | )); |
| 117 | entries.push({ |
| 118 | key: `ground:${def.id}`, |
| 119 | label: def.displayName, |
| 120 | spriteNames: [palettePreviewImageName("ground", def.key), name], |
| 121 | selection: { kind: "ground", typeId: def.id } |
| 122 | }); |
| 123 | } |
| 124 | return entries; |
| 125 | } |
| 126 | |
| 127 | function listBlockEntries (catalog) |
| 128 | { |
| 129 | return listPlaceableBlockPaletteEntries(catalog).map((entry) => ({ |
| 130 | key: `block:${entry.typeId}:${entry.state ?? ""}`, |
| 131 | label: entry.displayName, |
| 132 | spriteNames: [entry.spriteName], |
| 133 | selection: { |
| 134 | kind: "block", |
| 135 | typeId: entry.typeId, |
| 136 | state: entry.state |
| 137 | } |
| 138 | })); |
| 139 | } |
| 140 | |
| 141 | function listSpawnerEntries () |
| 142 | { |
| 143 | const entries = [{ |
| 144 | key: "spawner:item", |
| 145 | label: "Item Spawner", |
| 146 | spriteNames: [itemSpawnerPreviewSpriteName()], |
| 147 | selection: { kind: "spawner", spawnerKind: "item" } |
| 148 | }]; |
| 149 | for (const def of listSpawnableMobTypes()) |
| 150 | { |
| 151 | entries.push({ |
| 152 | key: `spawner:${def.mobTypeId}`, |
| 153 | label: def.displayName, |
| 154 | spriteNames: [mobSpriteName(def.mobTypeId)], |
| 155 | selection: { kind: "spawner", mobTypeId: def.mobTypeId } |
| 156 | }); |
| 157 | } |
| 158 | return entries; |
| 159 | } |