| 1 | import { listPlaceableGrounds, listPlaceableBlockPaletteEntries } from "/shared/definitions/tiles.js"; |
| 2 | import { listSpawnableMobTypes, mobSpriteName } from "/shared/definitions/mobs.js"; |
| 3 | import { defaultStateForType, resolveState, spriteImageName, palettePreviewImageName } from "/shared/definitions/tileSprites.js"; |
| 4 | import { itemSpawnerPreviewSpriteName } from "/shared/definitions/itemSpawners.js"; |
| 5 | |
| 6 | /** |
| 7 | * Select-tool modal: Ground / Blocks / Spawners, auto-built from definitions. |
| 8 | */ |
| 9 | export function initPaletteModal ({ onSelect, catalog, imageUrlFor }) |
| 10 | { |
| 11 | const backdrop = document.getElementById("palette-backdrop"); |
| 12 | const modal = document.getElementById("palette-modal"); |
| 13 | const tabs = document.getElementById("palette-tabs"); |
| 14 | const panels = document.getElementById("palette-panels"); |
| 15 | const searchInput = document.getElementById("palette-search"); |
| 16 | let open = false; |
| 17 | let activeTab = "grounds"; |
| 18 | let searchQuery = ""; |
| 19 | |
| 20 | const tabsSpec = [ |
| 21 | { id: "grounds", label: "Ground tiles" }, |
| 22 | { id: "blocks", label: "Blocks" }, |
| 23 | { id: "spawners", label: "Spawners" } |
| 24 | ]; |
| 25 | |
| 26 | function show (visible) |
| 27 | { |
| 28 | open = visible; |
| 29 | backdrop.hidden = !visible; |
| 30 | modal.hidden = !visible; |
| 31 | if (visible) |
| 32 | { |
| 33 | searchQuery = ""; |
| 34 | if (searchInput) |
| 35 | { searchInput.value = ""; } |
| 36 | render(); |
| 37 | searchInput?.focus(); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | function matchesSearch (label) |
| 42 | { |
| 43 | const q = searchQuery.trim().toLowerCase(); |
| 44 | if (!q) |
| 45 | { return true; } |
| 46 | return String(label ?? "").toLowerCase().includes(q); |
| 47 | } |
| 48 | |
| 49 | function render () |
| 50 | { |
| 51 | tabs.innerHTML = ""; |
| 52 | for (const tab of tabsSpec) |
| 53 | { |
| 54 | const button = document.createElement("button"); |
| 55 | button.type = "button"; |
| 56 | button.className = `settings-tab${tab.id === activeTab ? " active" : ""}`; |
| 57 | button.textContent = tab.label; |
| 58 | button.addEventListener("click", () => |
| 59 | { |
| 60 | activeTab = tab.id; |
| 61 | render(); |
| 62 | }); |
| 63 | tabs.append(button); |
| 64 | } |
| 65 | |
| 66 | panels.innerHTML = ""; |
| 67 | const grid = document.createElement("div"); |
| 68 | grid.className = "palette-grid"; |
| 69 | let shown = 0; |
| 70 | if (activeTab === "grounds") |
| 71 | { |
| 72 | for (const def of listPlaceableGrounds()) |
| 73 | { |
| 74 | if (!matchesSearch(def.displayName)) |
| 75 | { continue; } |
| 76 | shown += 1; |
| 77 | const state = defaultStateForType(catalog, "ground", def.key); |
| 78 | const name = spriteImageName("ground", def.key, resolveState( |
| 79 | catalog.grounds?.[def.key]?.states ?? [], |
| 80 | state |
| 81 | )); |
| 82 | const previewName = palettePreviewImageName("ground", def.key); |
| 83 | grid.append(tileCard({ |
| 84 | label: def.displayName, |
| 85 | url: imageUrlFor?.(previewName) ?? imageUrlFor?.(name), |
| 86 | onClick () |
| 87 | { |
| 88 | onSelect({ kind: "ground", typeId: def.id }); |
| 89 | show(false); |
| 90 | } |
| 91 | })); |
| 92 | } |
| 93 | } |
| 94 | else if (activeTab === "blocks") |
| 95 | { |
| 96 | for (const entry of listPlaceableBlockPaletteEntries(catalog)) |
| 97 | { |
| 98 | if (!matchesSearch(entry.displayName)) |
| 99 | { continue; } |
| 100 | shown += 1; |
| 101 | grid.append(tileCard({ |
| 102 | label: entry.displayName, |
| 103 | url: imageUrlFor?.(entry.spriteName), |
| 104 | onClick () |
| 105 | { |
| 106 | onSelect({ |
| 107 | kind: "block", |
| 108 | typeId: entry.typeId, |
| 109 | state: entry.state |
| 110 | }); |
| 111 | show(false); |
| 112 | } |
| 113 | })); |
| 114 | } |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | if (matchesSearch("Item Spawner")) |
| 119 | { |
| 120 | shown += 1; |
| 121 | grid.append(tileCard({ |
| 122 | label: "Item Spawner", |
| 123 | url: imageUrlFor?.(itemSpawnerPreviewSpriteName()), |
| 124 | onClick () |
| 125 | { |
| 126 | onSelect({ kind: "spawner", spawnerKind: "item" }); |
| 127 | show(false); |
| 128 | } |
| 129 | })); |
| 130 | } |
| 131 | for (const def of listSpawnableMobTypes()) |
| 132 | { |
| 133 | if (!matchesSearch(def.displayName)) |
| 134 | { continue; } |
| 135 | shown += 1; |
| 136 | grid.append(tileCard({ |
| 137 | label: def.displayName, |
| 138 | url: imageUrlFor?.(mobSpriteName(def.mobTypeId)), |
| 139 | onClick () |
| 140 | { |
| 141 | onSelect({ kind: "spawner", mobTypeId: def.mobTypeId }); |
| 142 | show(false); |
| 143 | } |
| 144 | })); |
| 145 | } |
| 146 | } |
| 147 | if (shown === 0) |
| 148 | { |
| 149 | const empty = document.createElement("p"); |
| 150 | empty.className = "hint"; |
| 151 | empty.textContent = searchQuery.trim() |
| 152 | ? "No matches for that search." |
| 153 | : "Nothing to show."; |
| 154 | panels.append(empty); |
| 155 | } |
| 156 | else |
| 157 | { panels.append(grid); } |
| 158 | } |
| 159 | |
| 160 | document.getElementById("open-palette").addEventListener("click", () => show(true)); |
| 161 | document.getElementById("palette-close").addEventListener("click", () => show(false)); |
| 162 | backdrop.addEventListener("click", () => show(false)); |
| 163 | searchInput?.addEventListener("input", () => |
| 164 | { |
| 165 | searchQuery = searchInput.value ?? ""; |
| 166 | render(); |
| 167 | }); |
| 168 | |
| 169 | return { |
| 170 | isOpen: () => open, |
| 171 | close: () => show(false) |
| 172 | }; |
| 173 | } |
| 174 | |
| 175 | function tileCard ({ label, url, onClick }) |
| 176 | { |
| 177 | const button = document.createElement("button"); |
| 178 | button.type = "button"; |
| 179 | button.className = "palette-card"; |
| 180 | const thumb = document.createElement("div"); |
| 181 | thumb.className = "palette-thumb"; |
| 182 | if (url) |
| 183 | { |
| 184 | const img = document.createElement("img"); |
| 185 | img.alt = ""; |
| 186 | img.src = url; |
| 187 | img.onerror = () => |
| 188 | { |
| 189 | img.remove(); |
| 190 | thumb.textContent = label.slice(0, 1); |
| 191 | }; |
| 192 | thumb.append(img); |
| 193 | } |
| 194 | else |
| 195 | { thumb.textContent = label.slice(0, 1); } |
| 196 | const caption = document.createElement("span"); |
| 197 | caption.textContent = label; |
| 198 | button.append(thumb, caption); |
| 199 | button.addEventListener("click", onClick); |
| 200 | return button; |
| 201 | } |