| 1 | import { normalizeDialogueTree, normalizeQuestDef } from "/shared/quests/normalize.js"; |
| 2 | import { emptyConditionSet } from "/shared/quests/conditions.js"; |
| 3 | import { |
| 4 | dialogueTakeIsUnguaranteed, |
| 5 | unguaranteedDialogueTakes |
| 6 | } from "/shared/quests/dialoguePathTakes.js"; |
| 7 | import { itemDefinition, listAllItemPickerEntries } from "/shared/definitions/items.js"; |
| 8 | import { renderConditionSet } from "./conditionSetEditor.js"; |
| 9 | import { initDialogueSimulate } from "./dialogueSimulate.js"; |
| 10 | |
| 11 | export function initDialoguesModal ({ |
| 12 | socket, |
| 13 | MessageType, |
| 14 | describeWorldTile = null, |
| 15 | centerEditorOnTile = null, |
| 16 | getDimensionOptions = null |
| 17 | }) |
| 18 | { |
| 19 | const backdrop = document.getElementById("dialogues-backdrop"); |
| 20 | const modal = document.getElementById("dialogues-modal"); |
| 21 | const listEl = document.getElementById("dialogues-list"); |
| 22 | const editorEl = document.getElementById("dialogues-editor"); |
| 23 | const saveBtn = document.getElementById("dialogues-save"); |
| 24 | const deleteBtn = document.getElementById("dialogues-delete"); |
| 25 | const simulateBtn = document.getElementById("dialogues-simulate"); |
| 26 | let open = false; |
| 27 | /** @type {any[]} */ |
| 28 | let dialogues = []; |
| 29 | /** @type {ReturnType<typeof normalizeQuestDef>[]} */ |
| 30 | let questCatalog = []; |
| 31 | /** @type {any|null} */ |
| 32 | let draft = null; |
| 33 | let selectedId = null; |
| 34 | |
| 35 | const simulate = initDialogueSimulate({ |
| 36 | getTree: () => (draft ? normalizeDialogueTree(draft) : null), |
| 37 | getQuestTitle (questId) |
| 38 | { |
| 39 | const quest = questCatalog.find((q) => q.questId === questId); |
| 40 | return quest?.title || questId; |
| 41 | } |
| 42 | }); |
| 43 | |
| 44 | function show (visible) |
| 45 | { |
| 46 | open = visible; |
| 47 | backdrop.hidden = !visible; |
| 48 | modal.hidden = !visible; |
| 49 | if (!visible) |
| 50 | { |
| 51 | draft = null; |
| 52 | selectedId = null; |
| 53 | closeInlineItemSearch(); |
| 54 | simulate.close(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** @type {null | { |
| 59 | * step: object, |
| 60 | * field: "itemRewards" | "itemTakes", |
| 61 | * resultsEl: HTMLElement, |
| 62 | * inputEl: HTMLInputElement |
| 63 | * }} */ |
| 64 | let itemPickContext = null; |
| 65 | |
| 66 | function closeInlineItemSearch () |
| 67 | { |
| 68 | if (itemPickContext?.resultsEl) |
| 69 | { itemPickContext.resultsEl.replaceChildren(); } |
| 70 | itemPickContext = null; |
| 71 | } |
| 72 | |
| 73 | function openInlineItemSearch (step, field, inputEl, resultsEl) |
| 74 | { |
| 75 | itemPickContext = { step, field, inputEl, resultsEl }; |
| 76 | inputEl.value = ""; |
| 77 | renderInlineItemSearch(""); |
| 78 | inputEl.focus(); |
| 79 | } |
| 80 | |
| 81 | function renderInlineItemSearch (query) |
| 82 | { |
| 83 | if (!itemPickContext) |
| 84 | { return; } |
| 85 | const { step, field, resultsEl } = itemPickContext; |
| 86 | resultsEl.replaceChildren(); |
| 87 | const q = String(query ?? "").trim().toLowerCase(); |
| 88 | const taken = new Set((step[field] ?? []).map((row) => row.itemId)); |
| 89 | const matches = listAllItemPickerEntries().filter((item) => |
| 90 | { |
| 91 | if (taken.has(item.itemId)) |
| 92 | { return false; } |
| 93 | if (!q) |
| 94 | { return true; } |
| 95 | return item.displayName.toLowerCase().includes(q) |
| 96 | || item.key.toLowerCase().includes(q) |
| 97 | || String(item.itemId).includes(q); |
| 98 | }).slice(0, 40); |
| 99 | if (!matches.length) |
| 100 | { |
| 101 | resultsEl.textContent = "No matching items."; |
| 102 | return; |
| 103 | } |
| 104 | for (const item of matches) |
| 105 | { |
| 106 | const row = document.createElement("button"); |
| 107 | row.type = "button"; |
| 108 | row.className = "shop-item-search-row"; |
| 109 | const name = document.createElement("span"); |
| 110 | name.className = "shop-item-search-row-name"; |
| 111 | name.textContent = item.displayName; |
| 112 | const key = document.createElement("span"); |
| 113 | key.className = "shop-item-search-row-key"; |
| 114 | key.textContent = `${item.key} · #${item.itemId}`; |
| 115 | row.append(name, key); |
| 116 | row.addEventListener("click", () => |
| 117 | { |
| 118 | if (!Array.isArray(step[field])) |
| 119 | { step[field] = []; } |
| 120 | step[field].push({ itemId: item.itemId, quantity: 1 }); |
| 121 | closeInlineItemSearch(); |
| 122 | renderEditor(); |
| 123 | }); |
| 124 | resultsEl.append(row); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | function selectDialogue (dialogueId) |
| 129 | { |
| 130 | const found = dialogues.find((d) => d.dialogueId === dialogueId); |
| 131 | if (!found) |
| 132 | { |
| 133 | draft = null; |
| 134 | selectedId = null; |
| 135 | render(); |
| 136 | return; |
| 137 | } |
| 138 | selectedId = found.dialogueId; |
| 139 | draft = structuredClone(found); |
| 140 | render(); |
| 141 | } |
| 142 | |
| 143 | function renderList () |
| 144 | { |
| 145 | listEl.replaceChildren(); |
| 146 | for (const tree of dialogues) |
| 147 | { |
| 148 | const btn = document.createElement("button"); |
| 149 | btn.type = "button"; |
| 150 | const unique = tree.unique ? " · unique" : ""; |
| 151 | const assigned = tree.assignedSpawnerCount != null |
| 152 | ? ` (${tree.assignedSpawnerCount})` |
| 153 | : ""; |
| 154 | btn.textContent = `${tree.label || tree.dialogueId}${unique}${assigned}`; |
| 155 | if (tree.dialogueId === selectedId) |
| 156 | { btn.classList.add("active"); } |
| 157 | btn.addEventListener("click", () => selectDialogue(tree.dialogueId)); |
| 158 | listEl.append(btn); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | function addStep () |
| 163 | { |
| 164 | draft.steps.push({ |
| 165 | stepId: `step-${draft.steps.length + 1}`, |
| 166 | lines: [{ speaker: "npc", text: "…", effects: [] }], |
| 167 | itemRewards: [], |
| 168 | itemTakes: [], |
| 169 | questRewards: [], |
| 170 | exit: { kind: "end" } |
| 171 | }); |
| 172 | renderEditor(); |
| 173 | } |
| 174 | |
| 175 | /** Update yellow take warnings without rebuilding the editor (keeps scroll + focus). */ |
| 176 | function refreshDialogueTakeHighlights () |
| 177 | { |
| 178 | if (!draft || !editorEl) |
| 179 | { return; } |
| 180 | const warnings = unguaranteedDialogueTakes(draft); |
| 181 | for (const row of editorEl.querySelectorAll("[data-take-step-id][data-take-item-id]")) |
| 182 | { |
| 183 | const stepId = row.getAttribute("data-take-step-id"); |
| 184 | const itemId = Number(row.getAttribute("data-take-item-id")); |
| 185 | const bad = dialogueTakeIsUnguaranteed(warnings, stepId, itemId); |
| 186 | row.classList.toggle("dialogue-item-take-unguaranteed", bad); |
| 187 | row.title = bad |
| 188 | ? "Not guaranteed by every branch/trigger path to this step." |
| 189 | : ""; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | function renderEditor () |
| 194 | { |
| 195 | editorEl.replaceChildren(); |
| 196 | saveBtn.disabled = !draft; |
| 197 | deleteBtn.disabled = !draft; |
| 198 | if (simulateBtn) |
| 199 | { simulateBtn.disabled = !draft; } |
| 200 | if (!draft) |
| 201 | { |
| 202 | const hint = document.createElement("p"); |
| 203 | hint.className = "content-editor-hint"; |
| 204 | hint.textContent = "Select a dialogue or create a new one."; |
| 205 | editorEl.append(hint); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | editorEl.append( |
| 210 | field("Dialogue id", textInput(draft.dialogueId, (v) => { draft.dialogueId = v; })), |
| 211 | field("Label", textInput(draft.label, (v) => { draft.label = v; })) |
| 212 | ); |
| 213 | |
| 214 | const uniqueLabel = document.createElement("label"); |
| 215 | const uniqueBox = document.createElement("input"); |
| 216 | uniqueBox.type = "checkbox"; |
| 217 | uniqueBox.checked = !!draft.unique; |
| 218 | uniqueBox.addEventListener("change", () => { draft.unique = uniqueBox.checked; }); |
| 219 | uniqueLabel.append(uniqueBox, document.createTextNode(" Unique (at most one spawner)")); |
| 220 | editorEl.append(uniqueLabel); |
| 221 | |
| 222 | const assigned = document.createElement("p"); |
| 223 | assigned.className = "content-editor-hint"; |
| 224 | assigned.textContent = `Assigned spawners: ${draft.assignedSpawnerCount ?? 0}`; |
| 225 | editorEl.append(assigned); |
| 226 | |
| 227 | editorEl.append(field("Entry step id", textInput(draft.entryStepId ?? "", (v) => |
| 228 | { |
| 229 | draft.entryStepId = v; |
| 230 | }))); |
| 231 | |
| 232 | const stepsTitle = document.createElement("div"); |
| 233 | stepsTitle.className = "content-editor-section-header"; |
| 234 | const stepsStrong = document.createElement("strong"); |
| 235 | stepsStrong.textContent = "Steps"; |
| 236 | stepsTitle.append(stepsStrong); |
| 237 | editorEl.append(stepsTitle); |
| 238 | |
| 239 | const takeWarnings = unguaranteedDialogueTakes(draft); |
| 240 | |
| 241 | draft.steps.forEach((step, stepIndex) => |
| 242 | { |
| 243 | const box = document.createElement("div"); |
| 244 | box.className = "content-editor-section"; |
| 245 | box.append(sectionHeader(`Step ${stepIndex + 1}`, null, () => |
| 246 | { |
| 247 | draft.steps.splice(stepIndex, 1); |
| 248 | if (draft.steps.length === 0) |
| 249 | { |
| 250 | draft.steps.push({ |
| 251 | stepId: "greeting", |
| 252 | lines: [{ speaker: "npc", text: "…", effects: [] }], |
| 253 | itemRewards: [], |
| 254 | itemTakes: [], |
| 255 | questRewards: [], |
| 256 | exit: { kind: "end" } |
| 257 | }); |
| 258 | } |
| 259 | renderEditor(); |
| 260 | })); |
| 261 | box.append(field("stepId", textInput(step.stepId, (v) => { step.stepId = v; }))); |
| 262 | |
| 263 | const linesHead = sectionHeader("Lines", () => |
| 264 | { |
| 265 | step.lines.push({ speaker: "npc", text: "…", effects: [] }); |
| 266 | renderEditor(); |
| 267 | }); |
| 268 | box.append(linesHead); |
| 269 | step.lines.forEach((line, lineIndex) => |
| 270 | { |
| 271 | const row = document.createElement("div"); |
| 272 | row.className = "content-editor-row"; |
| 273 | const speaker = document.createElement("select"); |
| 274 | for (const s of ["npc", "player"]) |
| 275 | { |
| 276 | const opt = document.createElement("option"); |
| 277 | opt.value = s; |
| 278 | opt.textContent = s; |
| 279 | if (line.speaker === s) |
| 280 | { opt.selected = true; } |
| 281 | speaker.append(opt); |
| 282 | } |
| 283 | speaker.addEventListener("change", () => |
| 284 | { |
| 285 | line.speaker = speaker.value; |
| 286 | }); |
| 287 | const text = document.createElement("input"); |
| 288 | text.type = "text"; |
| 289 | text.value = line.text ?? ""; |
| 290 | text.style.flex = "1"; |
| 291 | text.addEventListener("input", () => { line.text = text.value; }); |
| 292 | row.append(speaker, text, removeButton(() => |
| 293 | { |
| 294 | step.lines.splice(lineIndex, 1); |
| 295 | if (step.lines.length === 0) |
| 296 | { step.lines.push({ speaker: "npc", text: "…", effects: [] }); } |
| 297 | renderEditor(); |
| 298 | })); |
| 299 | box.append(row); |
| 300 | }); |
| 301 | |
| 302 | if (!Array.isArray(step.itemRewards)) |
| 303 | { step.itemRewards = []; } |
| 304 | const searchWrap = document.createElement("div"); |
| 305 | searchWrap.className = "dialogue-reward-item-search"; |
| 306 | searchWrap.hidden = true; |
| 307 | const searchInput = document.createElement("input"); |
| 308 | searchInput.type = "search"; |
| 309 | searchInput.placeholder = "Search items…"; |
| 310 | searchInput.autocomplete = "off"; |
| 311 | searchInput.addEventListener("input", () => renderInlineItemSearch(searchInput.value)); |
| 312 | const searchResults = document.createElement("div"); |
| 313 | searchResults.className = "dialogue-reward-item-results"; |
| 314 | searchWrap.append(searchInput, searchResults); |
| 315 | const rewardsHead = sectionHeader("Item rewards", () => |
| 316 | { |
| 317 | searchWrap.hidden = false; |
| 318 | openInlineItemSearch(step, "itemRewards", searchInput, searchResults); |
| 319 | }); |
| 320 | box.append(rewardsHead); |
| 321 | const rewardsHint = document.createElement("p"); |
| 322 | rewardsHint.className = "content-editor-hint"; |
| 323 | rewardsHint.textContent = "Granted after all lines, before triggers/branches."; |
| 324 | box.append(rewardsHint); |
| 325 | if (!step.itemRewards.length) |
| 326 | { |
| 327 | const empty = document.createElement("p"); |
| 328 | empty.className = "content-editor-hint"; |
| 329 | empty.textContent = "No item rewards."; |
| 330 | box.append(empty); |
| 331 | } |
| 332 | step.itemRewards.forEach((reward, rewardIndex) => |
| 333 | { |
| 334 | const def = itemDefinition(reward.itemId); |
| 335 | const row = document.createElement("div"); |
| 336 | row.className = "spawner-drops-row"; |
| 337 | const name = document.createElement("span"); |
| 338 | name.className = "spawner-sells-name"; |
| 339 | name.textContent = def?.displayName ?? `Item ${reward.itemId}`; |
| 340 | const qty = document.createElement("input"); |
| 341 | qty.type = "number"; |
| 342 | qty.min = "1"; |
| 343 | qty.step = "1"; |
| 344 | qty.value = String(Math.max(1, reward.quantity | 0 || 1)); |
| 345 | qty.title = "Quantity"; |
| 346 | qty.addEventListener("input", () => |
| 347 | { |
| 348 | step.itemRewards[rewardIndex].quantity = Math.max(1, Number(qty.value) || 1) | 0; |
| 349 | }); |
| 350 | row.append(name, qty, removeButton(() => |
| 351 | { |
| 352 | step.itemRewards.splice(rewardIndex, 1); |
| 353 | renderEditor(); |
| 354 | })); |
| 355 | box.append(row); |
| 356 | }); |
| 357 | box.append(searchWrap); |
| 358 | |
| 359 | if (!Array.isArray(step.itemTakes)) |
| 360 | { step.itemTakes = []; } |
| 361 | const takeSearchWrap = document.createElement("div"); |
| 362 | takeSearchWrap.className = "dialogue-reward-item-search"; |
| 363 | takeSearchWrap.hidden = true; |
| 364 | const takeSearchInput = document.createElement("input"); |
| 365 | takeSearchInput.type = "search"; |
| 366 | takeSearchInput.placeholder = "Search items…"; |
| 367 | takeSearchInput.autocomplete = "off"; |
| 368 | takeSearchInput.addEventListener("input", () => renderInlineItemSearch(takeSearchInput.value)); |
| 369 | const takeSearchResults = document.createElement("div"); |
| 370 | takeSearchResults.className = "dialogue-reward-item-results"; |
| 371 | takeSearchWrap.append(takeSearchInput, takeSearchResults); |
| 372 | const takesHead = sectionHeader("Item takes", () => |
| 373 | { |
| 374 | takeSearchWrap.hidden = false; |
| 375 | openInlineItemSearch(step, "itemTakes", takeSearchInput, takeSearchResults); |
| 376 | }); |
| 377 | box.append(takesHead); |
| 378 | const takesHint = document.createElement("p"); |
| 379 | takesHint.className = "content-editor-hint"; |
| 380 | takesHint.textContent = "Removed from the bag after all lines (soft: takes what the player has). Yellow = branch/trigger path does not guarantee the player has them."; |
| 381 | box.append(takesHint); |
| 382 | if (!step.itemTakes.length) |
| 383 | { |
| 384 | const empty = document.createElement("p"); |
| 385 | empty.className = "content-editor-hint"; |
| 386 | empty.textContent = "No item takes."; |
| 387 | box.append(empty); |
| 388 | } |
| 389 | step.itemTakes.forEach((take, takeIndex) => |
| 390 | { |
| 391 | const def = itemDefinition(take.itemId); |
| 392 | const row = document.createElement("div"); |
| 393 | row.className = "spawner-drops-row"; |
| 394 | row.setAttribute("data-take-step-id", step.stepId); |
| 395 | row.setAttribute("data-take-item-id", String(take.itemId)); |
| 396 | if (dialogueTakeIsUnguaranteed(takeWarnings, step.stepId, take.itemId)) |
| 397 | { |
| 398 | row.classList.add("dialogue-item-take-unguaranteed"); |
| 399 | row.title = "Not guaranteed by every branch/trigger path to this step."; |
| 400 | } |
| 401 | const name = document.createElement("span"); |
| 402 | name.className = "spawner-sells-name"; |
| 403 | name.textContent = def?.displayName ?? `Item ${take.itemId}`; |
| 404 | const qty = document.createElement("input"); |
| 405 | qty.type = "number"; |
| 406 | qty.min = "1"; |
| 407 | qty.step = "1"; |
| 408 | qty.value = String(Math.max(1, take.quantity | 0 || 1)); |
| 409 | qty.title = "Quantity"; |
| 410 | qty.addEventListener("input", () => |
| 411 | { |
| 412 | step.itemTakes[takeIndex].quantity = Math.max(1, Number(qty.value) || 1) | 0; |
| 413 | refreshDialogueTakeHighlights(); |
| 414 | }); |
| 415 | row.append(name, qty, removeButton(() => |
| 416 | { |
| 417 | step.itemTakes.splice(takeIndex, 1); |
| 418 | renderEditor(); |
| 419 | })); |
| 420 | box.append(row); |
| 421 | }); |
| 422 | box.append(takeSearchWrap); |
| 423 | |
| 424 | if (!Array.isArray(step.questRewards)) |
| 425 | { step.questRewards = []; } |
| 426 | const questRewardsHead = sectionHeader("Quest rewards", () => |
| 427 | { |
| 428 | const available = questCatalog.find((q) => !step.questRewards.includes(q.questId)); |
| 429 | if (!available) |
| 430 | { return; } |
| 431 | step.questRewards.push(available.questId); |
| 432 | renderEditor(); |
| 433 | }); |
| 434 | box.append(questRewardsHead); |
| 435 | const questHint = document.createElement("p"); |
| 436 | questHint.className = "content-editor-hint"; |
| 437 | questHint.textContent = "Started after all lines (with item rewards), before triggers/branches."; |
| 438 | box.append(questHint); |
| 439 | if (!step.questRewards.length) |
| 440 | { |
| 441 | const empty = document.createElement("p"); |
| 442 | empty.className = "content-editor-hint"; |
| 443 | empty.textContent = "No quest rewards."; |
| 444 | box.append(empty); |
| 445 | } |
| 446 | step.questRewards.forEach((questId, rewardIndex) => |
| 447 | { |
| 448 | const row = document.createElement("div"); |
| 449 | row.className = "spawner-drops-row"; |
| 450 | const select = document.createElement("select"); |
| 451 | const taken = new Set(step.questRewards); |
| 452 | const catalog = questCatalog.length |
| 453 | ? questCatalog |
| 454 | : [{ questId, title: questId }]; |
| 455 | for (const quest of catalog) |
| 456 | { |
| 457 | if (taken.has(quest.questId) && quest.questId !== questId) |
| 458 | { continue; } |
| 459 | const opt = document.createElement("option"); |
| 460 | opt.value = quest.questId; |
| 461 | opt.textContent = quest.title |
| 462 | ? `${quest.title} (${quest.questId})` |
| 463 | : quest.questId; |
| 464 | select.append(opt); |
| 465 | } |
| 466 | if (![...select.options].some((o) => o.value === questId)) |
| 467 | { |
| 468 | const missing = document.createElement("option"); |
| 469 | missing.value = questId; |
| 470 | missing.textContent = `${questId} (missing)`; |
| 471 | select.append(missing); |
| 472 | } |
| 473 | select.value = questId; |
| 474 | select.addEventListener("change", () => |
| 475 | { |
| 476 | step.questRewards[rewardIndex] = select.value; |
| 477 | renderEditor(); |
| 478 | }); |
| 479 | row.append(select, removeButton(() => |
| 480 | { |
| 481 | step.questRewards.splice(rewardIndex, 1); |
| 482 | renderEditor(); |
| 483 | })); |
| 484 | box.append(row); |
| 485 | }); |
| 486 | |
| 487 | const exitKind = document.createElement("select"); |
| 488 | for (const k of ["end", "triggers", "branches"]) |
| 489 | { |
| 490 | const opt = document.createElement("option"); |
| 491 | opt.value = k; |
| 492 | opt.textContent = k; |
| 493 | if ((step.exit?.kind ?? "end") === k) |
| 494 | { opt.selected = true; } |
| 495 | exitKind.append(opt); |
| 496 | } |
| 497 | exitKind.addEventListener("change", () => |
| 498 | { |
| 499 | if (exitKind.value === "triggers") |
| 500 | { |
| 501 | step.exit = { |
| 502 | kind: "triggers", |
| 503 | triggers: step.exit?.triggers?.length |
| 504 | ? step.exit.triggers |
| 505 | : [{ |
| 506 | trigger: "continue", |
| 507 | text: "Continue", |
| 508 | nextStepId: null, |
| 509 | when: emptyConditionSet(), |
| 510 | effects: [] |
| 511 | }] |
| 512 | }; |
| 513 | } |
| 514 | else if (exitKind.value === "branches") |
| 515 | { |
| 516 | step.exit = { |
| 517 | kind: "branches", |
| 518 | branches: step.exit?.branches?.length |
| 519 | ? step.exit.branches |
| 520 | : [{ |
| 521 | when: emptyConditionSet(), |
| 522 | nextStepId: null, |
| 523 | effects: [] |
| 524 | }] |
| 525 | }; |
| 526 | } |
| 527 | else |
| 528 | { step.exit = { kind: "end" }; } |
| 529 | renderEditor(); |
| 530 | }); |
| 531 | box.append(field("Exit kind", exitKind)); |
| 532 | |
| 533 | if (step.exit?.kind === "triggers") |
| 534 | { |
| 535 | const trigHead = sectionHeader("Triggers", () => |
| 536 | { |
| 537 | step.exit.triggers.push({ |
| 538 | trigger: `choice-${step.exit.triggers.length + 1}`, |
| 539 | text: "Choice", |
| 540 | nextStepId: null, |
| 541 | when: emptyConditionSet(), |
| 542 | effects: [] |
| 543 | }); |
| 544 | renderEditor(); |
| 545 | }); |
| 546 | box.append(trigHead); |
| 547 | step.exit.triggers.forEach((trig, trigIndex) => |
| 548 | { |
| 549 | const tBox = document.createElement("div"); |
| 550 | tBox.className = "content-editor-section"; |
| 551 | tBox.append(sectionHeader(`Trigger ${trigIndex + 1}`, null, () => |
| 552 | { |
| 553 | step.exit.triggers.splice(trigIndex, 1); |
| 554 | renderEditor(); |
| 555 | })); |
| 556 | tBox.append( |
| 557 | field("trigger", textInput(trig.trigger, (v) => { trig.trigger = v; })), |
| 558 | field("text (button)", textInput(trig.text, (v) => { trig.text = v; })), |
| 559 | field("nextStepId (blank=end)", textInput(trig.nextStepId ?? "", (v) => |
| 560 | { |
| 561 | trig.nextStepId = v.trim() || null; |
| 562 | })) |
| 563 | ); |
| 564 | tBox.append(renderConditionSet(trig.when, (next, { remount = true } = {}) => |
| 565 | { |
| 566 | trig.when = next; |
| 567 | if (remount) |
| 568 | { renderEditor(); } |
| 569 | }, conditionCtx())); |
| 570 | box.append(tBox); |
| 571 | }); |
| 572 | } |
| 573 | else if (step.exit?.kind === "branches") |
| 574 | { |
| 575 | const branchHead = sectionHeader("Branches", () => |
| 576 | { |
| 577 | step.exit.branches.push({ |
| 578 | when: emptyConditionSet(), |
| 579 | nextStepId: null, |
| 580 | effects: [] |
| 581 | }); |
| 582 | renderEditor(); |
| 583 | }); |
| 584 | box.append(branchHead); |
| 585 | step.exit.branches.forEach((branch, branchIndex) => |
| 586 | { |
| 587 | const bBox = document.createElement("div"); |
| 588 | bBox.className = "content-editor-section"; |
| 589 | bBox.append(sectionHeader(`Branch ${branchIndex + 1}`, null, () => |
| 590 | { |
| 591 | step.exit.branches.splice(branchIndex, 1); |
| 592 | renderEditor(); |
| 593 | })); |
| 594 | bBox.append( |
| 595 | field("nextStepId (blank=end)", textInput(branch.nextStepId ?? "", (v) => |
| 596 | { |
| 597 | branch.nextStepId = v.trim() || null; |
| 598 | })) |
| 599 | ); |
| 600 | bBox.append(renderConditionSet(branch.when, (next, { remount = true } = {}) => |
| 601 | { |
| 602 | branch.when = next; |
| 603 | if (remount) |
| 604 | { renderEditor(); } |
| 605 | }, conditionCtx())); |
| 606 | box.append(bBox); |
| 607 | }); |
| 608 | } |
| 609 | |
| 610 | editorEl.append(box); |
| 611 | }); |
| 612 | |
| 613 | const addStepBtn = document.createElement("button"); |
| 614 | addStepBtn.type = "button"; |
| 615 | addStepBtn.className = "content-editor-add-step"; |
| 616 | addStepBtn.textContent = "(add step)"; |
| 617 | addStepBtn.addEventListener("click", addStep); |
| 618 | editorEl.append(addStepBtn); |
| 619 | } |
| 620 | |
| 621 | function conditionCtx () |
| 622 | { |
| 623 | return { |
| 624 | quests: questCatalog, |
| 625 | dialogues, |
| 626 | describeWorldTile, |
| 627 | centerEditorOnTile, |
| 628 | getDimensionOptions |
| 629 | }; |
| 630 | } |
| 631 | |
| 632 | function render () |
| 633 | { |
| 634 | renderList(); |
| 635 | renderEditor(); |
| 636 | } |
| 637 | |
| 638 | document.getElementById("open-dialogues").addEventListener("click", () => |
| 639 | { |
| 640 | socket.send({ type: MessageType.editorLoadDialogues }); |
| 641 | socket.send({ type: MessageType.editorLoadQuests }); |
| 642 | show(true); |
| 643 | render(); |
| 644 | }); |
| 645 | document.getElementById("dialogues-close").addEventListener("click", () => show(false)); |
| 646 | backdrop.addEventListener("click", () => show(false)); |
| 647 | simulateBtn?.addEventListener("click", () => |
| 648 | { |
| 649 | if (!draft) |
| 650 | { return; } |
| 651 | simulate.start(); |
| 652 | }); |
| 653 | document.getElementById("dialogues-new").addEventListener("click", () => |
| 654 | { |
| 655 | const base = normalizeDialogueTree({ |
| 656 | dialogueId: `dialogue-${dialogues.length + 1}`, |
| 657 | label: "New dialogue", |
| 658 | unique: false |
| 659 | }); |
| 660 | draft = { ...structuredClone(base), assignedSpawnerCount: 0 }; |
| 661 | selectedId = null; |
| 662 | render(); |
| 663 | }); |
| 664 | saveBtn.addEventListener("click", () => |
| 665 | { |
| 666 | if (!draft) |
| 667 | { return; } |
| 668 | socket.send({ type: MessageType.editorSaveDialogue, dialogue: draft }); |
| 669 | }); |
| 670 | deleteBtn.addEventListener("click", () => |
| 671 | { |
| 672 | if (!draft?.dialogueId) |
| 673 | { return; } |
| 674 | if (!window.confirm(`Delete dialogue "${draft.dialogueId}"?`)) |
| 675 | { return; } |
| 676 | socket.send({ type: MessageType.editorDeleteDialogue, dialogueId: draft.dialogueId }); |
| 677 | }); |
| 678 | |
| 679 | return { |
| 680 | isOpen: () => open || simulate.isOpen(), |
| 681 | getDialogues: () => dialogues, |
| 682 | handleMessage (message) |
| 683 | { |
| 684 | if (message.type === MessageType.editorQuests) |
| 685 | { |
| 686 | questCatalog = (message.quests ?? []).map((q) => normalizeQuestDef(q)); |
| 687 | if (open) |
| 688 | { render(); } |
| 689 | return; |
| 690 | } |
| 691 | if (message.type !== MessageType.editorDialogues) |
| 692 | { return; } |
| 693 | dialogues = (message.dialogues ?? []).map((d) => |
| 694 | { |
| 695 | const tree = normalizeDialogueTree(d); |
| 696 | return { |
| 697 | ...tree, |
| 698 | assignedSpawnerCount: d.assignedSpawnerCount ?? 0 |
| 699 | }; |
| 700 | }); |
| 701 | if (draft) |
| 702 | { |
| 703 | const still = dialogues.find((d) => d.dialogueId === draft.dialogueId); |
| 704 | if (still) |
| 705 | { |
| 706 | selectedId = still.dialogueId; |
| 707 | draft = structuredClone(still); |
| 708 | } |
| 709 | else if (selectedId) |
| 710 | { |
| 711 | draft = null; |
| 712 | selectedId = null; |
| 713 | } |
| 714 | } |
| 715 | if (open) |
| 716 | { render(); } |
| 717 | } |
| 718 | }; |
| 719 | } |
| 720 | |
| 721 | function field (label, input) |
| 722 | { |
| 723 | const wrap = document.createElement("label"); |
| 724 | wrap.append(document.createTextNode(label), input); |
| 725 | return wrap; |
| 726 | } |
| 727 | |
| 728 | function textInput (value, onChange) |
| 729 | { |
| 730 | const input = document.createElement("input"); |
| 731 | input.type = "text"; |
| 732 | input.value = value ?? ""; |
| 733 | input.addEventListener("input", () => onChange(input.value)); |
| 734 | return input; |
| 735 | } |
| 736 | |
| 737 | function removeButton (onClick) |
| 738 | { |
| 739 | const remove = document.createElement("button"); |
| 740 | remove.type = "button"; |
| 741 | remove.className = "content-editor-remove"; |
| 742 | remove.textContent = "×"; |
| 743 | remove.title = "Remove"; |
| 744 | remove.addEventListener("click", onClick); |
| 745 | return remove; |
| 746 | } |
| 747 | |
| 748 | function sectionHeader (title, onAdd, onRemove) |
| 749 | { |
| 750 | const head = document.createElement("div"); |
| 751 | head.className = "content-editor-section-header"; |
| 752 | const strong = document.createElement("strong"); |
| 753 | strong.textContent = title; |
| 754 | head.append(strong); |
| 755 | const actions = document.createElement("div"); |
| 756 | actions.className = "content-editor-row"; |
| 757 | if (onAdd) |
| 758 | { |
| 759 | const add = document.createElement("button"); |
| 760 | add.type = "button"; |
| 761 | add.textContent = "+"; |
| 762 | add.title = "Add"; |
| 763 | add.addEventListener("click", onAdd); |
| 764 | actions.append(add); |
| 765 | } |
| 766 | if (onRemove) |
| 767 | { actions.append(removeButton(onRemove)); } |
| 768 | head.append(actions); |
| 769 | return head; |
| 770 | } |
| 771 | |