BlueGit

timfa questward

Files

questward / client / src / editor / dialoguesModal.js

dialoguesModal.js

dialoguesModal.js 11.8 KiB Download Raw
1import { normalizeDialogueTree } from "/shared/quests/normalize.js";
2import { emptyConditionSet, normalizeConditionSet } from "/shared/quests/conditions.js";
3
4export function initDialoguesModal ({ socket, MessageType })
5{
6 const backdrop = document.getElementById("dialogues-backdrop");
7 const modal = document.getElementById("dialogues-modal");
8 const listEl = document.getElementById("dialogues-list");
9 const editorEl = document.getElementById("dialogues-editor");
10 const saveBtn = document.getElementById("dialogues-save");
11 const deleteBtn = document.getElementById("dialogues-delete");
12 let open = false;
13 /** @type {any[]} */
14 let dialogues = [];
15 /** @type {any|null} */
16 let draft = null;
17 let selectedId = null;
18
19 function show (visible)
20 {
21 open = visible;
22 backdrop.hidden = !visible;
23 modal.hidden = !visible;
24 if (!visible)
25 {
26 draft = null;
27 selectedId = null;
28 }
29 }
30
31 function selectDialogue (dialogueId)
32 {
33 const found = dialogues.find((d) => d.dialogueId === dialogueId);
34 if (!found)
35 {
36 draft = null;
37 selectedId = null;
38 render();
39 return;
40 }
41 selectedId = found.dialogueId;
42 draft = structuredClone(found);
43 render();
44 }
45
46 function renderList ()
47 {
48 listEl.replaceChildren();
49 for (const tree of dialogues)
50 {
51 const btn = document.createElement("button");
52 btn.type = "button";
53 const unique = tree.unique ? " · unique" : "";
54 const assigned = tree.assignedSpawnerCount != null
55 ? ` (${tree.assignedSpawnerCount})`
56 : "";
57 btn.textContent = `${tree.label || tree.dialogueId}${unique}${assigned}`;
58 if (tree.dialogueId === selectedId)
59 { btn.classList.add("active"); }
60 btn.addEventListener("click", () => selectDialogue(tree.dialogueId));
61 listEl.append(btn);
62 }
63 }
64
65 function renderEditor ()
66 {
67 editorEl.replaceChildren();
68 saveBtn.disabled = !draft;
69 deleteBtn.disabled = !draft;
70 if (!draft)
71 {
72 const hint = document.createElement("p");
73 hint.className = "content-editor-hint";
74 hint.textContent = "Select a dialogue or create a new one.";
75 editorEl.append(hint);
76 return;
77 }
78
79 editorEl.append(
80 field("Dialogue id", textInput(draft.dialogueId, (v) => { draft.dialogueId = v; })),
81 field("Label", textInput(draft.label, (v) => { draft.label = v; }))
82 );
83
84 const uniqueLabel = document.createElement("label");
85 const uniqueBox = document.createElement("input");
86 uniqueBox.type = "checkbox";
87 uniqueBox.checked = !!draft.unique;
88 uniqueBox.addEventListener("change", () => { draft.unique = uniqueBox.checked; });
89 uniqueLabel.append(uniqueBox, document.createTextNode(" Unique (at most one spawner)"));
90 editorEl.append(uniqueLabel);
91
92 const assigned = document.createElement("p");
93 assigned.className = "content-editor-hint";
94 assigned.textContent = `Assigned spawners: ${draft.assignedSpawnerCount ?? 0}`;
95 editorEl.append(assigned);
96
97 editorEl.append(field("Entry step id", textInput(draft.entryStepId ?? "", (v) =>
98 {
99 draft.entryStepId = v;
100 })));
101
102 const stepsHeader = sectionHeader("Steps", () =>
103 {
104 draft.steps.push({
105 stepId: `step-${draft.steps.length + 1}`,
106 label: "New step",
107 lines: [{ speaker: "npc", text: "…", effects: [] }],
108 exit: { kind: "end" }
109 });
110 renderEditor();
111 });
112 editorEl.append(stepsHeader);
113
114 draft.steps.forEach((step, stepIndex) =>
115 {
116 const box = document.createElement("div");
117 box.className = "content-editor-section";
118 box.append(sectionHeader(`Step ${stepIndex + 1}`, null, () =>
119 {
120 draft.steps.splice(stepIndex, 1);
121 if (draft.steps.length === 0)
122 {
123 draft.steps.push({
124 stepId: "greeting",
125 label: "Greeting",
126 lines: [{ speaker: "npc", text: "…", effects: [] }],
127 exit: { kind: "end" }
128 });
129 }
130 renderEditor();
131 }));
132 box.append(
133 field("stepId", textInput(step.stepId, (v) => { step.stepId = v; })),
134 field("Label", textInput(step.label, (v) => { step.label = v; }))
135 );
136
137 const linesHead = sectionHeader("Lines", () =>
138 {
139 step.lines.push({ speaker: "npc", text: "…", effects: [] });
140 renderEditor();
141 });
142 box.append(linesHead);
143 step.lines.forEach((line, lineIndex) =>
144 {
145 const row = document.createElement("div");
146 row.className = "content-editor-row";
147 const speaker = document.createElement("select");
148 for (const s of ["npc", "player"])
149 {
150 const opt = document.createElement("option");
151 opt.value = s;
152 opt.textContent = s;
153 if (line.speaker === s)
154 { opt.selected = true; }
155 speaker.append(opt);
156 }
157 speaker.addEventListener("change", () =>
158 {
159 line.speaker = speaker.value;
160 });
161 const text = document.createElement("input");
162 text.type = "text";
163 text.value = line.text ?? "";
164 text.style.flex = "1";
165 text.addEventListener("input", () => { line.text = text.value; });
166 const remove = document.createElement("button");
167 remove.type = "button";
168 remove.textContent = "×";
169 remove.addEventListener("click", () =>
170 {
171 step.lines.splice(lineIndex, 1);
172 if (step.lines.length === 0)
173 { step.lines.push({ speaker: "npc", text: "…", effects: [] }); }
174 renderEditor();
175 });
176 row.append(speaker, text, remove);
177 box.append(row);
178 });
179
180 const exitKind = document.createElement("select");
181 for (const k of ["end", "triggers", "branches"])
182 {
183 const opt = document.createElement("option");
184 opt.value = k;
185 opt.textContent = k;
186 if ((step.exit?.kind ?? "end") === k)
187 { opt.selected = true; }
188 exitKind.append(opt);
189 }
190 exitKind.addEventListener("change", () =>
191 {
192 if (exitKind.value === "triggers")
193 {
194 step.exit = {
195 kind: "triggers",
196 triggers: step.exit?.triggers?.length
197 ? step.exit.triggers
198 : [{
199 trigger: "continue",
200 label: "Continue",
201 text: "Continue",
202 nextStepId: null,
203 when: emptyConditionSet(),
204 effects: []
205 }]
206 };
207 }
208 else if (exitKind.value === "branches")
209 {
210 step.exit = {
211 kind: "branches",
212 branches: step.exit?.branches?.length
213 ? step.exit.branches
214 : [{
215 label: "default",
216 when: emptyConditionSet(),
217 nextStepId: null,
218 effects: []
219 }]
220 };
221 }
222 else
223 { step.exit = { kind: "end" }; }
224 renderEditor();
225 });
226 box.append(field("Exit kind", exitKind));
227
228 if (step.exit?.kind === "triggers")
229 {
230 const trigHead = sectionHeader("Triggers", () =>
231 {
232 step.exit.triggers.push({
233 trigger: `choice-${step.exit.triggers.length + 1}`,
234 label: "Choice",
235 text: "Choice",
236 nextStepId: null,
237 when: emptyConditionSet(),
238 effects: []
239 });
240 renderEditor();
241 });
242 box.append(trigHead);
243 step.exit.triggers.forEach((trig, trigIndex) =>
244 {
245 const tBox = document.createElement("div");
246 tBox.className = "content-editor-section";
247 tBox.append(sectionHeader(`Trigger ${trigIndex + 1}`, null, () =>
248 {
249 step.exit.triggers.splice(trigIndex, 1);
250 renderEditor();
251 }));
252 tBox.append(
253 field("trigger", textInput(trig.trigger, (v) => { trig.trigger = v; })),
254 field("label", textInput(trig.label, (v) => { trig.label = v; })),
255 field("text (button)", textInput(trig.text, (v) => { trig.text = v; })),
256 field("nextStepId (blank=end)", textInput(trig.nextStepId ?? "", (v) =>
257 {
258 trig.nextStepId = v.trim() || null;
259 }))
260 );
261 tBox.append(whenJson(trig.when, (next) => { trig.when = next; }));
262 box.append(tBox);
263 });
264 }
265 else if (step.exit?.kind === "branches")
266 {
267 const branchHead = sectionHeader("Branches", () =>
268 {
269 step.exit.branches.push({
270 label: `branch-${step.exit.branches.length + 1}`,
271 when: emptyConditionSet(),
272 nextStepId: null,
273 effects: []
274 });
275 renderEditor();
276 });
277 box.append(branchHead);
278 step.exit.branches.forEach((branch, branchIndex) =>
279 {
280 const bBox = document.createElement("div");
281 bBox.className = "content-editor-section";
282 bBox.append(sectionHeader(`Branch ${branchIndex + 1}`, null, () =>
283 {
284 step.exit.branches.splice(branchIndex, 1);
285 renderEditor();
286 }));
287 bBox.append(
288 field("label", textInput(branch.label ?? "", (v) => { branch.label = v; })),
289 field("nextStepId (blank=end)", textInput(branch.nextStepId ?? "", (v) =>
290 {
291 branch.nextStepId = v.trim() || null;
292 }))
293 );
294 bBox.append(whenJson(branch.when, (next) => { branch.when = next; }));
295 box.append(bBox);
296 });
297 }
298
299 editorEl.append(box);
300 });
301 }
302
303 function render ()
304 {
305 renderList();
306 renderEditor();
307 }
308
309 document.getElementById("open-dialogues").addEventListener("click", () =>
310 {
311 socket.send({ type: MessageType.editorLoadDialogues });
312 show(true);
313 render();
314 });
315 document.getElementById("dialogues-close").addEventListener("click", () => show(false));
316 backdrop.addEventListener("click", () => show(false));
317 document.getElementById("dialogues-new").addEventListener("click", () =>
318 {
319 const base = normalizeDialogueTree({
320 dialogueId: `dialogue-${dialogues.length + 1}`,
321 label: "New dialogue",
322 unique: false
323 });
324 draft = { ...structuredClone(base), assignedSpawnerCount: 0 };
325 selectedId = null;
326 render();
327 });
328 saveBtn.addEventListener("click", () =>
329 {
330 if (!draft)
331 { return; }
332 socket.send({ type: MessageType.editorSaveDialogue, dialogue: draft });
333 });
334 deleteBtn.addEventListener("click", () =>
335 {
336 if (!draft?.dialogueId)
337 { return; }
338 if (!window.confirm(`Delete dialogue "${draft.dialogueId}"?`))
339 { return; }
340 socket.send({ type: MessageType.editorDeleteDialogue, dialogueId: draft.dialogueId });
341 });
342
343 return {
344 isOpen: () => open,
345 getDialogues: () => dialogues,
346 handleMessage (message)
347 {
348 if (message.type !== MessageType.editorDialogues)
349 { return; }
350 dialogues = (message.dialogues ?? []).map((d) =>
351 {
352 const tree = normalizeDialogueTree(d);
353 return {
354 ...tree,
355 assignedSpawnerCount: d.assignedSpawnerCount ?? 0
356 };
357 });
358 if (draft)
359 {
360 const still = dialogues.find((d) => d.dialogueId === draft.dialogueId);
361 if (still)
362 {
363 selectedId = still.dialogueId;
364 draft = structuredClone(still);
365 }
366 else if (selectedId)
367 {
368 draft = null;
369 selectedId = null;
370 }
371 }
372 if (open)
373 { render(); }
374 }
375 };
376}
377
378function field (label, input)
379{
380 const wrap = document.createElement("label");
381 wrap.append(document.createTextNode(label), input);
382 return wrap;
383}
384
385function textInput (value, onChange)
386{
387 const input = document.createElement("input");
388 input.type = "text";
389 input.value = value ?? "";
390 input.addEventListener("input", () => onChange(input.value));
391 return input;
392}
393
394function sectionHeader (title, onAdd, onRemove)
395{
396 const head = document.createElement("div");
397 head.className = "content-editor-section-header";
398 const strong = document.createElement("strong");
399 strong.textContent = title;
400 head.append(strong);
401 const actions = document.createElement("div");
402 actions.className = "content-editor-row";
403 if (onAdd)
404 {
405 const add = document.createElement("button");
406 add.type = "button";
407 add.textContent = "+";
408 add.title = "Add";
409 add.addEventListener("click", onAdd);
410 actions.append(add);
411 }
412 if (onRemove)
413 {
414 const remove = document.createElement("button");
415 remove.type = "button";
416 remove.textContent = "×";
417 remove.title = "Remove";
418 remove.addEventListener("click", onRemove);
419 actions.append(remove);
420 }
421 head.append(actions);
422 return head;
423}
424
425function whenJson (when, onChange)
426{
427 const details = document.createElement("details");
428 const summary = document.createElement("summary");
429 summary.textContent = "when (condition JSON)";
430 const area = document.createElement("textarea");
431 area.rows = 4;
432 area.value = JSON.stringify(normalizeConditionSet(when), null, 2);
433 area.addEventListener("change", () =>
434 {
435 try
436 {
437 onChange(normalizeConditionSet(JSON.parse(area.value)));
438 }
439 catch
440 { /* ignore invalid */ }
441 });
442 details.append(summary, area);
443 return details;
444}