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