| 1 | /** |
| 2 | * Desktop node-graph canvas for dialogue steps. |
| 3 | * Pan empty space, drag cards, wheel zoom, wire outputs → inputs. |
| 4 | */ |
| 5 | |
| 6 | const CARD_W = 168; |
| 7 | const CARD_H = 72; |
| 8 | const PORT_R = 7; |
| 9 | const EDGE_PAD = 36; |
| 10 | const MIN_ZOOM = 0.35; |
| 11 | const MAX_ZOOM = 2.25; |
| 12 | const AUTO_PAN = 14; |
| 13 | |
| 14 | /** |
| 15 | * @param {{ |
| 16 | * getDraft: () => object | null, |
| 17 | * onOpenStep: (stepId: string) => void, |
| 18 | * onCreateStep: (worldX: number, worldY: number) => void, |
| 19 | * onChange: () => void |
| 20 | * }} options |
| 21 | */ |
| 22 | export function createDialogueCanvas ({ getDraft, onOpenStep, onCreateStep, onChange }) |
| 23 | { |
| 24 | const wrap = document.createElement("div"); |
| 25 | wrap.className = "dialogue-canvas-wrap"; |
| 26 | |
| 27 | const viewport = document.createElement("div"); |
| 28 | viewport.className = "dialogue-canvas-viewport"; |
| 29 | viewport.tabIndex = 0; |
| 30 | |
| 31 | const world = document.createElement("div"); |
| 32 | world.className = "dialogue-canvas-world"; |
| 33 | |
| 34 | const edgesSvg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); |
| 35 | edgesSvg.classList.add("dialogue-canvas-edges"); |
| 36 | edgesSvg.setAttribute("overflow", "visible"); |
| 37 | |
| 38 | const cardsLayer = document.createElement("div"); |
| 39 | cardsLayer.className = "dialogue-canvas-cards"; |
| 40 | |
| 41 | const rubberPath = document.createElementNS("http://www.w3.org/2000/svg", "path"); |
| 42 | rubberPath.classList.add("dialogue-canvas-rubber"); |
| 43 | rubberPath.setAttribute("fill", "none"); |
| 44 | |
| 45 | const ctxMenu = document.createElement("div"); |
| 46 | ctxMenu.className = "dialogue-canvas-context"; |
| 47 | ctxMenu.hidden = true; |
| 48 | const createBtn = document.createElement("button"); |
| 49 | createBtn.type = "button"; |
| 50 | createBtn.textContent = "Create new step"; |
| 51 | ctxMenu.append(createBtn); |
| 52 | |
| 53 | const tooltip = document.createElement("div"); |
| 54 | tooltip.className = "dialogue-canvas-tooltip"; |
| 55 | tooltip.hidden = true; |
| 56 | |
| 57 | world.append(edgesSvg, cardsLayer); |
| 58 | edgesSvg.append(rubberPath); |
| 59 | viewport.append(world); |
| 60 | wrap.append(viewport, ctxMenu, tooltip); |
| 61 | |
| 62 | /** @type {{ x: number, y: number, zoom: number }} */ |
| 63 | let view = { x: 40, y: 40, zoom: 1 }; |
| 64 | /** @type {null | { |
| 65 | * mode: "pan" | "card" | "wire", |
| 66 | * startClientX: number, |
| 67 | * startClientY: number, |
| 68 | * originViewX?: number, |
| 69 | * originViewY?: number, |
| 70 | * step?: object, |
| 71 | * originEditorX?: number, |
| 72 | * originEditorY?: number, |
| 73 | * moved: boolean, |
| 74 | * portKey?: string, |
| 75 | * fromStepId?: string, |
| 76 | * portIndex?: number |
| 77 | * }} */ |
| 78 | let drag = null; |
| 79 | /** @type {null | { stepId: string, portIndex: number, portKey: string }} */ |
| 80 | let wiring = null; |
| 81 | let ctxWorld = { x: 0, y: 0 }; |
| 82 | /** @type {number | null} */ |
| 83 | let autoPanRaf = null; |
| 84 | let lastPointer = { x: 0, y: 0 }; |
| 85 | |
| 86 | function applyTransform () |
| 87 | { |
| 88 | world.style.transform = `translate(${view.x}px, ${view.y}px) scale(${view.zoom})`; |
| 89 | } |
| 90 | |
| 91 | function clientToWorld (clientX, clientY) |
| 92 | { |
| 93 | const rect = viewport.getBoundingClientRect(); |
| 94 | return { |
| 95 | x: (clientX - rect.left - view.x) / view.zoom, |
| 96 | y: (clientY - rect.top - view.y) / view.zoom |
| 97 | }; |
| 98 | } |
| 99 | |
| 100 | function hideContext () |
| 101 | { |
| 102 | ctxMenu.hidden = true; |
| 103 | } |
| 104 | |
| 105 | function hideTooltip () |
| 106 | { |
| 107 | tooltip.hidden = true; |
| 108 | } |
| 109 | |
| 110 | function showTooltip (text, clientX, clientY) |
| 111 | { |
| 112 | if (!text) |
| 113 | { |
| 114 | hideTooltip(); |
| 115 | return; |
| 116 | } |
| 117 | tooltip.hidden = false; |
| 118 | tooltip.textContent = text; |
| 119 | const rect = wrap.getBoundingClientRect(); |
| 120 | tooltip.style.left = `${clientX - rect.left + 12}px`; |
| 121 | tooltip.style.top = `${clientY - rect.top + 12}px`; |
| 122 | } |
| 123 | |
| 124 | function stepPosition (step, index) |
| 125 | { |
| 126 | const x = Number.isFinite(step.editorX) ? step.editorX : 40 + (index % 4) * (CARD_W + 48); |
| 127 | const y = Number.isFinite(step.editorY) ? step.editorY : 40 + Math.floor(index / 4) * (CARD_H + 64); |
| 128 | return { x, y }; |
| 129 | } |
| 130 | |
| 131 | function outputPorts (step) |
| 132 | { |
| 133 | const exit = step?.exit ?? { kind: "end" }; |
| 134 | if (exit.kind === "continue") |
| 135 | { |
| 136 | return [{ |
| 137 | key: "continue", |
| 138 | label: "Continue", |
| 139 | nextStepId: exit.nextStepId ?? null, |
| 140 | setNext (id) |
| 141 | { |
| 142 | exit.nextStepId = id; |
| 143 | } |
| 144 | }]; |
| 145 | } |
| 146 | if (exit.kind === "triggers") |
| 147 | { |
| 148 | return (exit.triggers ?? []).map((trig, i) => ({ |
| 149 | key: `trigger:${i}`, |
| 150 | /** Player-facing button text (not the internal trigger id / legacy label). */ |
| 151 | label: (trig.text ?? "").trim() || trig.trigger || `Trigger ${i + 1}`, |
| 152 | nextStepId: trig.nextStepId ?? null, |
| 153 | setNext (id) |
| 154 | { |
| 155 | trig.nextStepId = id; |
| 156 | } |
| 157 | })); |
| 158 | } |
| 159 | if (exit.kind === "branches") |
| 160 | { |
| 161 | return (exit.branches ?? []).map((branch, i) => ({ |
| 162 | key: `branch:${i}`, |
| 163 | label: branch.label?.trim() ? branch.label : `Branch ${i + 1}`, |
| 164 | nextStepId: branch.nextStepId ?? null, |
| 165 | setNext (id) |
| 166 | { |
| 167 | branch.nextStepId = id; |
| 168 | } |
| 169 | })); |
| 170 | } |
| 171 | return []; |
| 172 | } |
| 173 | |
| 174 | function portWorldPos (step, index, portIndex, kind) |
| 175 | { |
| 176 | const pos = stepPosition(step, index); |
| 177 | if (kind === "in") |
| 178 | { |
| 179 | return { x: pos.x + CARD_W / 2, y: pos.y }; |
| 180 | } |
| 181 | const ports = outputPorts(step); |
| 182 | const n = Math.max(1, ports.length); |
| 183 | const spacing = CARD_W / (n + 1); |
| 184 | return { |
| 185 | x: pos.x + spacing * (portIndex + 1), |
| 186 | y: pos.y + CARD_H |
| 187 | }; |
| 188 | } |
| 189 | |
| 190 | function bezier (x1, y1, x2, y2) |
| 191 | { |
| 192 | const dy = Math.max(40, Math.abs(y2 - y1) * 0.45); |
| 193 | return `M ${x1} ${y1} C ${x1} ${y1 + dy}, ${x2} ${y2 - dy}, ${x2} ${y2}`; |
| 194 | } |
| 195 | |
| 196 | function clearWiring () |
| 197 | { |
| 198 | wiring = null; |
| 199 | rubberPath.setAttribute("d", ""); |
| 200 | rubberPath.classList.remove("active"); |
| 201 | wrap.classList.remove("dialogue-canvas-wiring"); |
| 202 | for (const el of cardsLayer.querySelectorAll(".dialogue-canvas-input.show")) |
| 203 | { el.classList.remove("show"); } |
| 204 | } |
| 205 | |
| 206 | function setWiring (stepId, portIndex, portKey) |
| 207 | { |
| 208 | wiring = { stepId, portIndex, portKey }; |
| 209 | wrap.classList.add("dialogue-canvas-wiring"); |
| 210 | for (const el of cardsLayer.querySelectorAll(".dialogue-canvas-input")) |
| 211 | { |
| 212 | if (el.getAttribute("data-step-id") !== stepId) |
| 213 | { el.classList.add("show"); } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | function stopAutoPan () |
| 218 | { |
| 219 | if (autoPanRaf != null) |
| 220 | { |
| 221 | cancelAnimationFrame(autoPanRaf); |
| 222 | autoPanRaf = null; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | function tickAutoPan () |
| 227 | { |
| 228 | autoPanRaf = null; |
| 229 | if (!drag || drag.mode !== "card") |
| 230 | { return; } |
| 231 | const rect = viewport.getBoundingClientRect(); |
| 232 | let dx = 0; |
| 233 | let dy = 0; |
| 234 | if (lastPointer.x < rect.left + EDGE_PAD) |
| 235 | { dx = AUTO_PAN; } |
| 236 | else if (lastPointer.x > rect.right - EDGE_PAD) |
| 237 | { dx = -AUTO_PAN; } |
| 238 | if (lastPointer.y < rect.top + EDGE_PAD) |
| 239 | { dy = AUTO_PAN; } |
| 240 | else if (lastPointer.y > rect.bottom - EDGE_PAD) |
| 241 | { dy = -AUTO_PAN; } |
| 242 | if (dx || dy) |
| 243 | { |
| 244 | view.x += dx; |
| 245 | view.y += dy; |
| 246 | applyTransform(); |
| 247 | const worldPt = clientToWorld(lastPointer.x, lastPointer.y); |
| 248 | drag.step.editorX = worldPt.x - CARD_W / 2; |
| 249 | drag.step.editorY = worldPt.y - CARD_H / 2; |
| 250 | refreshCardsOnly(); |
| 251 | redrawEdges(); |
| 252 | } |
| 253 | autoPanRaf = requestAnimationFrame(tickAutoPan); |
| 254 | } |
| 255 | |
| 256 | function redrawEdges () |
| 257 | { |
| 258 | const draft = getDraft(); |
| 259 | for (const el of [...edgesSvg.children]) |
| 260 | { |
| 261 | if (el !== rubberPath) |
| 262 | { el.remove(); } |
| 263 | } |
| 264 | if (rubberPath.parentNode !== edgesSvg) |
| 265 | { edgesSvg.append(rubberPath); } |
| 266 | if (!draft?.steps?.length) |
| 267 | { return; } |
| 268 | draft.steps.forEach((step, index) => |
| 269 | { |
| 270 | const ports = outputPorts(step); |
| 271 | ports.forEach((port, portIndex) => |
| 272 | { |
| 273 | if (!port.nextStepId) |
| 274 | { return; } |
| 275 | const targetIndex = draft.steps.findIndex((s) => s.stepId === port.nextStepId); |
| 276 | if (targetIndex < 0) |
| 277 | { return; } |
| 278 | const from = portWorldPos(step, index, portIndex, "out"); |
| 279 | const to = portWorldPos(draft.steps[targetIndex], targetIndex, 0, "in"); |
| 280 | const path = document.createElementNS("http://www.w3.org/2000/svg", "path"); |
| 281 | path.classList.add("dialogue-canvas-edge"); |
| 282 | const rewiring = !!wiring |
| 283 | && wiring.stepId === step.stepId |
| 284 | && wiring.portIndex === portIndex; |
| 285 | if (rewiring) |
| 286 | { path.classList.add("is-rewiring"); } |
| 287 | path.setAttribute("d", bezier(from.x, from.y, to.x, to.y)); |
| 288 | path.setAttribute("fill", "none"); |
| 289 | edgesSvg.insertBefore(path, rubberPath); |
| 290 | }); |
| 291 | }); |
| 292 | } |
| 293 | |
| 294 | function refreshCardsOnly () |
| 295 | { |
| 296 | const draft = getDraft(); |
| 297 | if (!draft) |
| 298 | { |
| 299 | cardsLayer.replaceChildren(); |
| 300 | return; |
| 301 | } |
| 302 | const existing = new Map( |
| 303 | [...cardsLayer.querySelectorAll(".dialogue-canvas-card")] |
| 304 | .map((el) => [el.getAttribute("data-step-id"), el]) |
| 305 | ); |
| 306 | const keep = new Set(); |
| 307 | draft.steps.forEach((step, index) => |
| 308 | { |
| 309 | keep.add(step.stepId); |
| 310 | let card = existing.get(step.stepId); |
| 311 | if (!card) |
| 312 | { |
| 313 | card = buildCard(step, index); |
| 314 | cardsLayer.append(card); |
| 315 | } |
| 316 | else |
| 317 | { updateCard(card, step, index); } |
| 318 | const pos = stepPosition(step, index); |
| 319 | card.style.left = `${pos.x}px`; |
| 320 | card.style.top = `${pos.y}px`; |
| 321 | }); |
| 322 | for (const [id, el] of existing) |
| 323 | { |
| 324 | if (!keep.has(id)) |
| 325 | { el.remove(); } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | function buildCard (step, index) |
| 330 | { |
| 331 | const card = document.createElement("div"); |
| 332 | card.className = "dialogue-canvas-card"; |
| 333 | card.setAttribute("data-step-id", step.stepId); |
| 334 | |
| 335 | const input = document.createElement("button"); |
| 336 | input.type = "button"; |
| 337 | input.className = "dialogue-canvas-input"; |
| 338 | input.title = "Connect here"; |
| 339 | input.setAttribute("data-step-id", step.stepId); |
| 340 | input.addEventListener("pointerdown", (event) => |
| 341 | { |
| 342 | event.stopPropagation(); |
| 343 | event.preventDefault(); |
| 344 | if (!wiring) |
| 345 | { return; } |
| 346 | if (wiring.stepId === step.stepId) |
| 347 | { return; } |
| 348 | const draft = getDraft(); |
| 349 | const from = draft?.steps?.find((s) => s.stepId === wiring.stepId); |
| 350 | if (!from) |
| 351 | { return; } |
| 352 | const ports = outputPorts(from); |
| 353 | const port = ports[wiring.portIndex]; |
| 354 | if (!port) |
| 355 | { return; } |
| 356 | port.setNext(step.stepId); |
| 357 | clearWiring(); |
| 358 | onChange(); |
| 359 | refresh(); |
| 360 | }); |
| 361 | |
| 362 | const label = document.createElement("div"); |
| 363 | label.className = "dialogue-canvas-card-label"; |
| 364 | |
| 365 | const meta = document.createElement("div"); |
| 366 | meta.className = "dialogue-canvas-card-meta"; |
| 367 | |
| 368 | const portsRow = document.createElement("div"); |
| 369 | portsRow.className = "dialogue-canvas-ports"; |
| 370 | |
| 371 | card.append(input, label, meta, portsRow); |
| 372 | |
| 373 | card.addEventListener("pointerdown", (event) => |
| 374 | { |
| 375 | if (event.button !== 0) |
| 376 | { return; } |
| 377 | if (event.target.closest(".dialogue-canvas-port, .dialogue-canvas-input")) |
| 378 | { return; } |
| 379 | event.preventDefault(); |
| 380 | hideContext(); |
| 381 | const draft = getDraft(); |
| 382 | const live = draft?.steps?.find((s) => s.stepId === step.stepId); |
| 383 | if (!live) |
| 384 | { return; } |
| 385 | const pos = stepPosition(live, draft.steps.indexOf(live)); |
| 386 | if (!Number.isFinite(live.editorX)) |
| 387 | { live.editorX = pos.x; } |
| 388 | if (!Number.isFinite(live.editorY)) |
| 389 | { live.editorY = pos.y; } |
| 390 | drag = { |
| 391 | mode: "card", |
| 392 | startClientX: event.clientX, |
| 393 | startClientY: event.clientY, |
| 394 | step: live, |
| 395 | originEditorX: live.editorX, |
| 396 | originEditorY: live.editorY, |
| 397 | moved: false |
| 398 | }; |
| 399 | lastPointer = { x: event.clientX, y: event.clientY }; |
| 400 | card.classList.add("dragging"); |
| 401 | card.setPointerCapture(event.pointerId); |
| 402 | stopAutoPan(); |
| 403 | autoPanRaf = requestAnimationFrame(tickAutoPan); |
| 404 | }); |
| 405 | |
| 406 | updateCard(card, step, index); |
| 407 | return card; |
| 408 | } |
| 409 | |
| 410 | function updateCard (card, step, index) |
| 411 | { |
| 412 | card.setAttribute("data-step-id", step.stepId); |
| 413 | const draft = getDraft(); |
| 414 | const isEntry = draft && (draft.entryStepId === step.stepId |
| 415 | || (!draft.entryStepId && draft.steps[0] === step)); |
| 416 | card.classList.toggle("is-entry", !!isEntry); |
| 417 | const label = card.querySelector(".dialogue-canvas-card-label"); |
| 418 | if (label) |
| 419 | { label.textContent = step.label || step.stepId; } |
| 420 | const meta = card.querySelector(".dialogue-canvas-card-meta"); |
| 421 | if (meta) |
| 422 | { |
| 423 | const kind = step.exit?.kind ?? "end"; |
| 424 | meta.textContent = isEntry ? `entry · ${kind}` : kind; |
| 425 | } |
| 426 | const input = card.querySelector(".dialogue-canvas-input"); |
| 427 | if (input) |
| 428 | { |
| 429 | input.setAttribute("data-step-id", step.stepId); |
| 430 | input.classList.toggle("show", !!wiring && wiring.stepId !== step.stepId); |
| 431 | } |
| 432 | const portsRow = card.querySelector(".dialogue-canvas-ports"); |
| 433 | if (!portsRow) |
| 434 | { return; } |
| 435 | portsRow.replaceChildren(); |
| 436 | const ports = outputPorts(step); |
| 437 | ports.forEach((port, portIndex) => |
| 438 | { |
| 439 | const btn = document.createElement("button"); |
| 440 | btn.type = "button"; |
| 441 | btn.className = "dialogue-canvas-port"; |
| 442 | btn.title = port.label; |
| 443 | btn.classList.toggle("linked", !!port.nextStepId); |
| 444 | btn.classList.toggle("selected", |
| 445 | !!wiring && wiring.stepId === step.stepId && wiring.portIndex === portIndex); |
| 446 | btn.addEventListener("pointerdown", (event) => |
| 447 | { |
| 448 | event.stopPropagation(); |
| 449 | event.preventDefault(); |
| 450 | hideContext(); |
| 451 | // Clicking an output always starts/continues wire mode (existing link stays until |
| 452 | // a new input is chosen; that edge is drawn red via is-rewiring). |
| 453 | setWiring(step.stepId, portIndex, port.key); |
| 454 | drag = { |
| 455 | mode: "wire", |
| 456 | startClientX: event.clientX, |
| 457 | startClientY: event.clientY, |
| 458 | moved: false, |
| 459 | fromStepId: step.stepId, |
| 460 | portIndex, |
| 461 | portKey: port.key |
| 462 | }; |
| 463 | const from = portWorldPos(step, index, portIndex, "out"); |
| 464 | const worldPt = clientToWorld(event.clientX, event.clientY); |
| 465 | rubberPath.classList.add("active"); |
| 466 | rubberPath.setAttribute("d", bezier(from.x, from.y, worldPt.x, worldPt.y)); |
| 467 | refreshCardsOnly(); |
| 468 | redrawEdges(); |
| 469 | }); |
| 470 | btn.addEventListener("pointerenter", (event) => |
| 471 | { |
| 472 | showTooltip(port.label, event.clientX, event.clientY); |
| 473 | }); |
| 474 | btn.addEventListener("pointerleave", () => hideTooltip()); |
| 475 | btn.addEventListener("pointermove", (event) => |
| 476 | { |
| 477 | showTooltip(port.label, event.clientX, event.clientY); |
| 478 | }); |
| 479 | portsRow.append(btn); |
| 480 | }); |
| 481 | } |
| 482 | |
| 483 | function refresh () |
| 484 | { |
| 485 | applyTransform(); |
| 486 | refreshCardsOnly(); |
| 487 | redrawEdges(); |
| 488 | } |
| 489 | |
| 490 | function onPointerMove (event) |
| 491 | { |
| 492 | lastPointer = { x: event.clientX, y: event.clientY }; |
| 493 | if (!drag) |
| 494 | { |
| 495 | if (wiring) |
| 496 | { |
| 497 | const draft = getDraft(); |
| 498 | const fromStep = draft?.steps?.find((s) => s.stepId === wiring.stepId); |
| 499 | if (!fromStep) |
| 500 | { return; } |
| 501 | const index = draft.steps.indexOf(fromStep); |
| 502 | const from = portWorldPos(fromStep, index, wiring.portIndex, "out"); |
| 503 | const worldPt = clientToWorld(event.clientX, event.clientY); |
| 504 | rubberPath.setAttribute("d", bezier(from.x, from.y, worldPt.x, worldPt.y)); |
| 505 | } |
| 506 | return; |
| 507 | } |
| 508 | const dx = event.clientX - drag.startClientX; |
| 509 | const dy = event.clientY - drag.startClientY; |
| 510 | if (Math.hypot(dx, dy) > 4) |
| 511 | { drag.moved = true; } |
| 512 | if (drag.mode === "pan") |
| 513 | { |
| 514 | view.x = drag.originViewX + dx; |
| 515 | view.y = drag.originViewY + dy; |
| 516 | applyTransform(); |
| 517 | return; |
| 518 | } |
| 519 | if (drag.mode === "card") |
| 520 | { |
| 521 | drag.step.editorX = drag.originEditorX + dx / view.zoom; |
| 522 | drag.step.editorY = drag.originEditorY + dy / view.zoom; |
| 523 | refreshCardsOnly(); |
| 524 | redrawEdges(); |
| 525 | return; |
| 526 | } |
| 527 | if (drag.mode === "wire") |
| 528 | { |
| 529 | const draft = getDraft(); |
| 530 | const fromStep = draft?.steps?.find((s) => s.stepId === drag.fromStepId); |
| 531 | if (!fromStep) |
| 532 | { return; } |
| 533 | const index = draft.steps.indexOf(fromStep); |
| 534 | const from = portWorldPos(fromStep, index, drag.portIndex, "out"); |
| 535 | const worldPt = clientToWorld(event.clientX, event.clientY); |
| 536 | rubberPath.setAttribute("d", bezier(from.x, from.y, worldPt.x, worldPt.y)); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | function onPointerUp (event) |
| 541 | { |
| 542 | if (!drag) |
| 543 | { return; } |
| 544 | const finished = drag; |
| 545 | drag = null; |
| 546 | stopAutoPan(); |
| 547 | for (const el of cardsLayer.querySelectorAll(".dragging")) |
| 548 | { el.classList.remove("dragging"); } |
| 549 | if (finished.mode === "card") |
| 550 | { |
| 551 | if (!finished.moved) |
| 552 | { onOpenStep(finished.step.stepId); } |
| 553 | else |
| 554 | { onChange(); } |
| 555 | refresh(); |
| 556 | return; |
| 557 | } |
| 558 | if (finished.mode === "wire") |
| 559 | { |
| 560 | // Keep wiring selected until input click or cancel |
| 561 | if (!wiring) |
| 562 | { clearWiring(); } |
| 563 | refresh(); |
| 564 | return; |
| 565 | } |
| 566 | if (finished.mode === "pan" && !finished.moved && event.button === 0) |
| 567 | { |
| 568 | clearWiring(); |
| 569 | refresh(); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | viewport.addEventListener("pointerdown", (event) => |
| 574 | { |
| 575 | if (event.button === 2) |
| 576 | { return; } |
| 577 | if (event.button !== 0) |
| 578 | { return; } |
| 579 | if (event.target.closest(".dialogue-canvas-card, .dialogue-canvas-context")) |
| 580 | { return; } |
| 581 | hideContext(); |
| 582 | drag = { |
| 583 | mode: "pan", |
| 584 | startClientX: event.clientX, |
| 585 | startClientY: event.clientY, |
| 586 | originViewX: view.x, |
| 587 | originViewY: view.y, |
| 588 | moved: false |
| 589 | }; |
| 590 | viewport.setPointerCapture(event.pointerId); |
| 591 | }); |
| 592 | |
| 593 | viewport.addEventListener("contextmenu", (event) => |
| 594 | { |
| 595 | event.preventDefault(); |
| 596 | // RMB cancels an in-progress wire/rewire. |
| 597 | if (wiring) |
| 598 | { |
| 599 | clearWiring(); |
| 600 | hideContext(); |
| 601 | refresh(); |
| 602 | return; |
| 603 | } |
| 604 | if (event.target.closest(".dialogue-canvas-card")) |
| 605 | { return; } |
| 606 | const worldPt = clientToWorld(event.clientX, event.clientY); |
| 607 | ctxWorld = worldPt; |
| 608 | const rect = wrap.getBoundingClientRect(); |
| 609 | ctxMenu.hidden = false; |
| 610 | ctxMenu.style.left = `${event.clientX - rect.left}px`; |
| 611 | ctxMenu.style.top = `${event.clientY - rect.top}px`; |
| 612 | }); |
| 613 | |
| 614 | createBtn.addEventListener("click", () => |
| 615 | { |
| 616 | hideContext(); |
| 617 | onCreateStep(ctxWorld.x, ctxWorld.y); |
| 618 | }); |
| 619 | |
| 620 | viewport.addEventListener("wheel", (event) => |
| 621 | { |
| 622 | event.preventDefault(); |
| 623 | const rect = viewport.getBoundingClientRect(); |
| 624 | const mx = event.clientX - rect.left; |
| 625 | const my = event.clientY - rect.top; |
| 626 | const before = { |
| 627 | x: (mx - view.x) / view.zoom, |
| 628 | y: (my - view.y) / view.zoom |
| 629 | }; |
| 630 | const factor = event.deltaY < 0 ? 1.08 : 1 / 1.08; |
| 631 | view.zoom = Math.min(MAX_ZOOM, Math.max(MIN_ZOOM, view.zoom * factor)); |
| 632 | view.x = mx - before.x * view.zoom; |
| 633 | view.y = my - before.y * view.zoom; |
| 634 | applyTransform(); |
| 635 | }, { passive: false }); |
| 636 | |
| 637 | window.addEventListener("pointermove", onPointerMove); |
| 638 | window.addEventListener("pointerup", onPointerUp); |
| 639 | window.addEventListener("pointercancel", onPointerUp); |
| 640 | window.addEventListener("keydown", (event) => |
| 641 | { |
| 642 | if (event.key === "Escape") |
| 643 | { |
| 644 | clearWiring(); |
| 645 | hideContext(); |
| 646 | refresh(); |
| 647 | } |
| 648 | }); |
| 649 | |
| 650 | applyTransform(); |
| 651 | |
| 652 | return { |
| 653 | el: wrap, |
| 654 | refresh, |
| 655 | clearWiring: () => |
| 656 | { |
| 657 | clearWiring(); |
| 658 | refresh(); |
| 659 | }, |
| 660 | destroy () |
| 661 | { |
| 662 | stopAutoPan(); |
| 663 | window.removeEventListener("pointermove", onPointerMove); |
| 664 | window.removeEventListener("pointerup", onPointerUp); |
| 665 | window.removeEventListener("pointercancel", onPointerUp); |
| 666 | wrap.remove(); |
| 667 | } |
| 668 | }; |
| 669 | } |
| 670 | |
| 671 | export function blankDialogueStep (stepId, editorX, editorY) |
| 672 | { |
| 673 | const step = { |
| 674 | stepId, |
| 675 | label: stepId, |
| 676 | lines: [{ speaker: "npc", text: "…", effects: [] }], |
| 677 | itemRewards: [], |
| 678 | itemTakes: [], |
| 679 | questRewards: [], |
| 680 | exit: { kind: "end" } |
| 681 | }; |
| 682 | if (Number.isFinite(editorX)) |
| 683 | { step.editorX = editorX; } |
| 684 | if (Number.isFinite(editorY)) |
| 685 | { step.editorY = editorY; } |
| 686 | return step; |
| 687 | } |
| 688 | |
| 689 | /** Clear nextStepId references to a deleted step. */ |
| 690 | export function scrubDialogueStepLinks (draft, removedStepId) |
| 691 | { |
| 692 | if (!draft?.steps) |
| 693 | { return; } |
| 694 | for (const step of draft.steps) |
| 695 | { |
| 696 | const exit = step.exit; |
| 697 | if (!exit) |
| 698 | { continue; } |
| 699 | if (exit.kind === "continue" && exit.nextStepId === removedStepId) |
| 700 | { exit.nextStepId = null; } |
| 701 | if (exit.kind === "triggers") |
| 702 | { |
| 703 | for (const trig of exit.triggers ?? []) |
| 704 | { |
| 705 | if (trig.nextStepId === removedStepId) |
| 706 | { trig.nextStepId = null; } |
| 707 | } |
| 708 | } |
| 709 | if (exit.kind === "branches") |
| 710 | { |
| 711 | for (const branch of exit.branches ?? []) |
| 712 | { |
| 713 | if (branch.nextStepId === removedStepId) |
| 714 | { branch.nextStepId = null; } |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | export function stepHasEdges (draft, step) |
| 721 | { |
| 722 | const exit = step?.exit ?? { kind: "end" }; |
| 723 | let outgoing = false; |
| 724 | if (exit.kind === "continue") |
| 725 | { outgoing = !!exit.nextStepId; } |
| 726 | else if (exit.kind === "triggers") |
| 727 | { outgoing = (exit.triggers ?? []).some((t) => !!t.nextStepId); } |
| 728 | else if (exit.kind === "branches") |
| 729 | { outgoing = (exit.branches ?? []).some((b) => !!b.nextStepId); } |
| 730 | const incoming = (draft?.steps ?? []).some((other) => |
| 731 | { |
| 732 | if (other === step) |
| 733 | { return false; } |
| 734 | const e = other.exit; |
| 735 | if (!e) |
| 736 | { return false; } |
| 737 | if (e.kind === "continue") |
| 738 | { return e.nextStepId === step.stepId; } |
| 739 | if (e.kind === "triggers") |
| 740 | { return (e.triggers ?? []).some((t) => t.nextStepId === step.stepId); } |
| 741 | if (e.kind === "branches") |
| 742 | { return (e.branches ?? []).some((b) => b.nextStepId === step.stepId); } |
| 743 | return false; |
| 744 | }); |
| 745 | return incoming || outgoing; |
| 746 | } |