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"; /** * Select-tool modal: Ground / Blocks / Spawners, auto-built from definitions. */ export function initPaletteModal ({ onSelect, catalog, imageUrlFor }) { const backdrop = document.getElementById("palette-backdrop"); const modal = document.getElementById("palette-modal"); const tabs = document.getElementById("palette-tabs"); const panels = document.getElementById("palette-panels"); const searchInput = document.getElementById("palette-search"); let open = false; let activeTab = "grounds"; let searchQuery = ""; const tabsSpec = [ { id: "grounds", label: "Ground tiles" }, { id: "blocks", label: "Blocks" }, { id: "spawners", label: "Spawners" } ]; function show (visible) { open = visible; backdrop.hidden = !visible; modal.hidden = !visible; if (visible) { searchQuery = ""; if (searchInput) { searchInput.value = ""; } render(); searchInput?.focus(); } } function matchesSearch (label) { const q = searchQuery.trim().toLowerCase(); if (!q) { return true; } return String(label ?? "").toLowerCase().includes(q); } function render () { tabs.innerHTML = ""; for (const tab of tabsSpec) { const button = document.createElement("button"); button.type = "button"; button.className = `settings-tab${tab.id === activeTab ? " active" : ""}`; button.textContent = tab.label; button.addEventListener("click", () => { activeTab = tab.id; render(); }); tabs.append(button); } panels.innerHTML = ""; const grid = document.createElement("div"); grid.className = "palette-grid"; let shown = 0; if (activeTab === "grounds") { for (const def of listPlaceableGrounds()) { if (!matchesSearch(def.displayName)) { continue; } shown += 1; const state = defaultStateForType(catalog, "ground", def.key); const name = spriteImageName("ground", def.key, resolveState( catalog.grounds?.[def.key]?.states ?? [], state )); const previewName = palettePreviewImageName("ground", def.key); grid.append(tileCard({ label: def.displayName, url: imageUrlFor?.(previewName) ?? imageUrlFor?.(name), onClick () { onSelect({ kind: "ground", typeId: def.id }); show(false); } })); } } else if (activeTab === "blocks") { for (const entry of listPlaceableBlockPaletteEntries(catalog)) { if (!matchesSearch(entry.displayName)) { continue; } shown += 1; grid.append(tileCard({ label: entry.displayName, url: imageUrlFor?.(entry.spriteName), onClick () { onSelect({ kind: "block", typeId: entry.typeId, state: entry.state }); show(false); } })); } } else { if (matchesSearch("Item Spawner")) { shown += 1; grid.append(tileCard({ label: "Item Spawner", url: imageUrlFor?.(itemSpawnerPreviewSpriteName()), onClick () { onSelect({ kind: "spawner", spawnerKind: "item" }); show(false); } })); } for (const def of listSpawnableMobTypes()) { if (!matchesSearch(def.displayName)) { continue; } shown += 1; grid.append(tileCard({ label: def.displayName, url: imageUrlFor?.(mobSpriteName(def.mobTypeId)), onClick () { onSelect({ kind: "spawner", mobTypeId: def.mobTypeId }); show(false); } })); } } if (shown === 0) { const empty = document.createElement("p"); empty.className = "hint"; empty.textContent = searchQuery.trim() ? "No matches for that search." : "Nothing to show."; panels.append(empty); } else { panels.append(grid); } } document.getElementById("open-palette").addEventListener("click", () => show(true)); document.getElementById("palette-close").addEventListener("click", () => show(false)); backdrop.addEventListener("click", () => show(false)); searchInput?.addEventListener("input", () => { searchQuery = searchInput.value ?? ""; render(); }); return { isOpen: () => open, close: () => show(false) }; } function tileCard ({ label, url, onClick }) { const button = document.createElement("button"); button.type = "button"; button.className = "palette-card"; const thumb = document.createElement("div"); thumb.className = "palette-thumb"; if (url) { const img = document.createElement("img"); img.alt = ""; img.src = url; img.onerror = () => { img.remove(); thumb.textContent = label.slice(0, 1); }; thumb.append(img); } else { thumb.textContent = label.slice(0, 1); } const caption = document.createElement("span"); caption.textContent = label; button.append(thumb, caption); button.addEventListener("click", onClick); return button; }