BlueGit

timfa questward

Files

questward / client / src / editor / paletteModal.js

paletteModal.js

paletteModal.js 3.6 KiB Download Raw
1import { listPlaceableGrounds, listPlaceableBlockPaletteEntries } from "/shared/definitions/tiles.js";
2import { listSpawnableMobTypes, mobSpriteName } from "/shared/definitions/mobs.js";
3import { defaultStateForType, resolveState, spriteImageName } 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 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 grid.append(tileCard({
62 label: def.displayName,
63 url: imageUrlFor?.(name),
64 onClick ()
65 {
66 onSelect({ kind: "ground", typeId: def.id });
67 show(false);
68 }
69 }));
70 }
71 }
72 else if (activeTab === "blocks")
73 {
74 for (const entry of listPlaceableBlockPaletteEntries(catalog))
75 {
76 grid.append(tileCard({
77 label: entry.displayName,
78 url: imageUrlFor?.(entry.spriteName),
79 onClick ()
80 {
81 onSelect({
82 kind: "block",
83 typeId: entry.typeId,
84 state: entry.state
85 });
86 show(false);
87 }
88 }));
89 }
90 }
91 else
92 {
93 for (const def of listSpawnableMobTypes())
94 {
95 grid.append(tileCard({
96 label: def.displayName,
97 url: imageUrlFor?.(mobSpriteName(def.mobTypeId)),
98 onClick ()
99 {
100 onSelect({ kind: "spawner", mobTypeId: def.mobTypeId });
101 show(false);
102 }
103 }));
104 }
105 }
106 panels.append(grid);
107 }
108
109 document.getElementById("open-palette").addEventListener("click", () => show(true));
110 document.getElementById("palette-close").addEventListener("click", () => show(false));
111 backdrop.addEventListener("click", () => show(false));
112
113 return {
114 isOpen: () => open,
115 close: () => show(false)
116 };
117}
118
119function tileCard ({ label, url, onClick })
120{
121 const button = document.createElement("button");
122 button.type = "button";
123 button.className = "palette-card";
124 const thumb = document.createElement("div");
125 thumb.className = "palette-thumb";
126 if (url)
127 {
128 const img = document.createElement("img");
129 img.alt = "";
130 img.src = url;
131 img.onerror = () =>
132 {
133 img.remove();
134 thumb.textContent = label.slice(0, 1);
135 };
136 thumb.append(img);
137 }
138 else
139 { thumb.textContent = label.slice(0, 1); }
140 const caption = document.createElement("span");
141 caption.textContent = label;
142 button.append(thumb, caption);
143 button.addEventListener("click", onClick);
144 return button;
145}