| 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 | |
| 5 | /** |
| 6 | * Select-tool modal: Ground / Blocks / Spawners, auto-built from definitions. |
| 7 | */ |
| 8 | export function initPaletteModal ({ onSelect, catalog, imageUrlFor }) |
| 9 | { |
| 10 | const backdrop = document.getElementById("palette-backdrop"); |
| 11 | const modal = document.getElementById("palette-modal"); |
| 12 | const tabs = document.getElementById("palette-tabs"); |
| 13 | const panels = document.getElementById("palette-panels"); |
| 14 | let open = false; |
| 15 | let activeTab = "grounds"; |
| 16 | |
| 17 | const tabsSpec = [ |
| 18 | { id: "grounds", label: "Ground tiles" }, |
| 19 | { id: "blocks", label: "Blocks" }, |
| 20 | { id: "spawners", label: "Spawners" } |
| 21 | ]; |
| 22 | |
| 23 | function show (visible) |
| 24 | { |
| 25 | open = visible; |
| 26 | backdrop.hidden = !visible; |
| 27 | modal.hidden = !visible; |
| 28 | if (visible) |
| 29 | { render(); } |
| 30 | } |
| 31 | |
| 32 | function render () |
| 33 | { |
| 34 | tabs.innerHTML = ""; |
| 35 | for (const tab of tabsSpec) |
| 36 | { |
| 37 | const button = document.createElement("button"); |
| 38 | button.type = "button"; |
| 39 | button.className = `settings-tab${tab.id === activeTab ? " active" : ""}`; |
| 40 | button.textContent = tab.label; |
| 41 | button.addEventListener("click", () => |
| 42 | { |
| 43 | activeTab = tab.id; |
| 44 | render(); |
| 45 | }); |
| 46 | tabs.append(button); |
| 47 | } |
| 48 | |
| 49 | panels.innerHTML = ""; |
| 50 | const grid = document.createElement("div"); |
| 51 | grid.className = "palette-grid"; |
| 52 | if (activeTab === "grounds") |
| 53 | { |
| 54 | for (const def of listPlaceableGrounds()) |
| 55 | { |
| 56 | const state = defaultStateForType(catalog, "ground", def.key); |
| 57 | const name = spriteImageName("ground", def.key, resolveState( |
| 58 | catalog.grounds?.[def.key]?.states ?? [], |
| 59 | state |
| 60 | )); |
| 61 | const previewName = palettePreviewImageName("ground", def.key); |
| 62 | grid.append(tileCard({ |
| 63 | label: def.displayName, |
| 64 | url: imageUrlFor?.(previewName) ?? imageUrlFor?.(name), |
| 65 | onClick () |
| 66 | { |
| 67 | onSelect({ kind: "ground", typeId: def.id }); |
| 68 | show(false); |
| 69 | } |
| 70 | })); |
| 71 | } |
| 72 | } |
| 73 | else if (activeTab === "blocks") |
| 74 | { |
| 75 | for (const entry of listPlaceableBlockPaletteEntries(catalog)) |
| 76 | { |
| 77 | grid.append(tileCard({ |
| 78 | label: entry.displayName, |
| 79 | url: imageUrlFor?.(entry.spriteName), |
| 80 | onClick () |
| 81 | { |
| 82 | onSelect({ |
| 83 | kind: "block", |
| 84 | typeId: entry.typeId, |
| 85 | state: entry.state |
| 86 | }); |
| 87 | show(false); |
| 88 | } |
| 89 | })); |
| 90 | } |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | for (const def of listSpawnableMobTypes()) |
| 95 | { |
| 96 | grid.append(tileCard({ |
| 97 | label: def.displayName, |
| 98 | url: imageUrlFor?.(mobSpriteName(def.mobTypeId)), |
| 99 | onClick () |
| 100 | { |
| 101 | onSelect({ kind: "spawner", mobTypeId: def.mobTypeId }); |
| 102 | show(false); |
| 103 | } |
| 104 | })); |
| 105 | } |
| 106 | } |
| 107 | panels.append(grid); |
| 108 | } |
| 109 | |
| 110 | document.getElementById("open-palette").addEventListener("click", () => show(true)); |
| 111 | document.getElementById("palette-close").addEventListener("click", () => show(false)); |
| 112 | backdrop.addEventListener("click", () => show(false)); |
| 113 | |
| 114 | return { |
| 115 | isOpen: () => open, |
| 116 | close: () => show(false) |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | function tileCard ({ label, url, onClick }) |
| 121 | { |
| 122 | const button = document.createElement("button"); |
| 123 | button.type = "button"; |
| 124 | button.className = "palette-card"; |
| 125 | const thumb = document.createElement("div"); |
| 126 | thumb.className = "palette-thumb"; |
| 127 | if (url) |
| 128 | { |
| 129 | const img = document.createElement("img"); |
| 130 | img.alt = ""; |
| 131 | img.src = url; |
| 132 | img.onerror = () => |
| 133 | { |
| 134 | img.remove(); |
| 135 | thumb.textContent = label.slice(0, 1); |
| 136 | }; |
| 137 | thumb.append(img); |
| 138 | } |
| 139 | else |
| 140 | { thumb.textContent = label.slice(0, 1); } |
| 141 | const caption = document.createElement("span"); |
| 142 | caption.textContent = label; |
| 143 | button.append(thumb, caption); |
| 144 | button.addEventListener("click", onClick); |
| 145 | return button; |
| 146 | } |