BlueGit

timfa questward

Files

questward / client / src / editor / paletteModal.js

paletteModal.js

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