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