Conditions become a discriminated union: the existing flag leaf plus a
single-level group combining leaves with AND / OR / NOR. The evaluator
recurses one level, and every flat-assuming site (choice gating, graph
validation, editor, canvas previews) is group-aware; DB entry gates stay
flat by design. The node editor gains a shared condition builder with a
combinator dropdown and repeatable rows for both branch nodes and gated
choices.
Display strings also gain ternaries: {{ key ? yes : no }}, {{ !key ? .. }},
and numeric compares like {{ rep >= 5 ? .. }}. A brace-depth scanner
rewrites ternaries over bounded passes so branches may nest flag tokens
and even nested ternaries, then plain interpolation runs its single
never-rescanned pass. Ternary keys join collectNodeTokens so the server
reads exactly the flags a node needs.
239 lines
9 KiB
TypeScript
239 lines
9 KiB
TypeScript
"use client";
|
|
|
|
import { Handle, NodeProps, Position } from "@xyflow/react";
|
|
import {
|
|
FileTextIcon,
|
|
GitBranchIcon,
|
|
GitCompareIcon,
|
|
SparklesIcon,
|
|
CircleStopIcon,
|
|
TimerIcon,
|
|
} from "lucide-react";
|
|
|
|
import type { DialogueNodeData, NpcDialogueNodeType } from "@/lib/npc-dialogue/dialogue";
|
|
import { conditionSummary, hasEmptyConditionKey } from "@/lib/npc-dialogue/dialogue";
|
|
import { DialogueText } from "@/lib/npc-dialogue/DialogueText";
|
|
import { DIALOGUE_NODES } from "./types";
|
|
|
|
const position: Position = Position.Right;
|
|
const inputPosition: Position = Position.Left;
|
|
|
|
/** Single reusable canvas node for every dialogue node type. */
|
|
export function NpcDialogueNode({ data, type, selected }: NodeProps) {
|
|
const d = data as DialogueNodeData;
|
|
const nodeType = type as NpcDialogueNodeType;
|
|
const meta = DIALOGUE_NODES[nodeType];
|
|
// Only end nodes are truly terminal. A choice node keeps its generic source
|
|
// handle while it has zero choices, otherwise there would be no way to drag
|
|
// the first choice out of it; once choices exist each row gets its own
|
|
// handle (litegraph-style per-output wiring). Condition nodes always use
|
|
// dedicated true/false handles instead of the generic one.
|
|
const choiceCount = nodeType === "choice" ? (d.choices?.length ?? 0) : 1;
|
|
const terminal = nodeType === "end";
|
|
const genericSource = !terminal && nodeType !== "condition" && !(nodeType === "choice" && choiceCount > 0);
|
|
|
|
return (
|
|
<div
|
|
className={`w-[20rem] max-w-[20rem] rounded border bg-zinc-900 shadow-lg ${meta.border} ${
|
|
// Gentle glow so the selection reads without shouting over the
|
|
// per-type border colors.
|
|
selected ? "ring-2 ring-cyan-400/50" : ""
|
|
}`}
|
|
>
|
|
<div className="flex items-center gap-2 border-b border-white/10 px-3 py-1.5">
|
|
<NodeIcon type={nodeType} />
|
|
<span className="font-mono text-[10px] uppercase tracking-widest text-white/70">
|
|
{meta.label}
|
|
</span>
|
|
{d.isStart && (
|
|
<span className="ml-auto font-mono text-[8px] uppercase tracking-widest text-emerald-400">
|
|
start
|
|
</span>
|
|
)}
|
|
</div>
|
|
|
|
{/* Choice/condition cards must not clip their per-output handles, which
|
|
sit outside the card edge; other types keep scrollable content. */}
|
|
<div
|
|
className={`min-h-20 px-3 py-2 ${
|
|
nodeType === "choice" || nodeType === "condition"
|
|
? "overflow-visible"
|
|
: "max-h-32 overflow-y-auto"
|
|
}`}
|
|
>
|
|
{nodeType === "dialogue" && (
|
|
<div>
|
|
{d.speaker && (
|
|
<p className="text-[10px] uppercase tracking-wider text-white/40">{d.speaker}</p>
|
|
)}
|
|
<p className="line-clamp-3 text-sm text-white/90">
|
|
{d.body ? (
|
|
<DialogueText text={d.body} />
|
|
) : (
|
|
<span className="italic text-white/30">Empty line</span>
|
|
)}
|
|
</p>
|
|
</div>
|
|
)}
|
|
|
|
{nodeType === "choice" && (
|
|
<div>
|
|
{d.leadIn && (
|
|
<p className="mb-1.5 line-clamp-2 text-sm text-white/70">
|
|
<DialogueText text={d.leadIn} />
|
|
</p>
|
|
)}
|
|
<ul className="flex flex-col gap-1">
|
|
{(d.choices ?? []).map((choice, index) => (
|
|
<li key={index} className="relative flex items-center gap-1.5 text-xs text-white/70">
|
|
<span className="size-3 shrink-0 rounded border border-amber-500/50" />
|
|
<span className="truncate">{choice.label || "Unlabeled choice"}</span>
|
|
{choice.once && (
|
|
<span className="shrink-0 rounded-sm border border-amber-500/40 bg-amber-500/10 px-1 font-mono text-[8px] uppercase tracking-wider text-amber-300">
|
|
once
|
|
</span>
|
|
)}
|
|
{choice.condition && (
|
|
<span
|
|
className="shrink-0 rounded-sm border border-sky-500/40 bg-sky-500/10 px-1 font-mono text-[8px] uppercase tracking-wider text-sky-300"
|
|
title={`Only shown when ${conditionSummary(choice.condition)}`}
|
|
>
|
|
gated
|
|
</span>
|
|
)}
|
|
{/* Per-choice source handle: drag this row's dot to wire
|
|
exactly this choice (litegraph-style per-output). */}
|
|
<Handle
|
|
id={`c${index}`}
|
|
type="source"
|
|
position={position}
|
|
className="!size-3 !bg-amber-400/80"
|
|
style={{ right: -16 }}
|
|
/>
|
|
</li>
|
|
))}
|
|
{(d.choices?.length ?? 0) === 0 && (
|
|
<li className="text-white/30">Add a choice below.</li>
|
|
)}
|
|
</ul>
|
|
</div>
|
|
)}
|
|
|
|
{nodeType === "event" && (
|
|
<div className="text-sm">
|
|
{d.body && (
|
|
<p className="line-clamp-3 italic text-amber-100/80">
|
|
<DialogueText text={d.body} />
|
|
</p>
|
|
)}
|
|
{d.effect ? (
|
|
<span className={d.body ? "text-xs text-white/50" : "text-white/80"}>
|
|
{d.effect.type === "notify"
|
|
? "Notify user"
|
|
: d.effect.type === "emit"
|
|
? "Log game event"
|
|
: d.effect.type === "flag"
|
|
? `Set flag "${d.effect.flagKey || "?"}"`
|
|
: "Reward (unconfigured)"}
|
|
{d.effect.message ? `: "${d.effect.message}"` : ""}
|
|
</span>
|
|
) : (
|
|
!d.body && (
|
|
<span className="italic text-white/30">
|
|
No narration or effect. Leave the effect on “None” for pure narration.
|
|
</span>
|
|
)
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{nodeType === "delay" && (
|
|
<div className="text-sm text-white/70">
|
|
{d.delaySeconds ? (
|
|
<span>
|
|
Pause <span className="font-mono text-violet-300">{d.delaySeconds}s</span> before
|
|
continuing.
|
|
</span>
|
|
) : (
|
|
<span className="italic text-white/30">Set a pause length.</span>
|
|
)}
|
|
</div>
|
|
)}
|
|
|
|
{nodeType === "condition" && (
|
|
<div className="flex flex-col gap-1.5 text-sm text-white/70">
|
|
<div>
|
|
{d.condition && !hasEmptyConditionKey(d.condition) ? (
|
|
<span>
|
|
Flag <span className="font-mono text-sky-300">{conditionSummary(d.condition)}</span>
|
|
</span>
|
|
) : (
|
|
<span className="italic text-white/30">Set a flag key to branch on.</span>
|
|
)}
|
|
</div>
|
|
{/* Dedicated true/false source handles, one per branch. */}
|
|
<div className="relative flex items-center gap-1.5 text-[10px] uppercase tracking-wider text-emerald-300/80">
|
|
<span className="size-2 shrink-0 rounded-full border border-emerald-400/60" />
|
|
true
|
|
</div>
|
|
<Handle
|
|
id="true"
|
|
type="source"
|
|
position={position}
|
|
className="!size-3 !bg-emerald-400/80"
|
|
style={{ top: "auto", bottom: 26 }}
|
|
/>
|
|
<div className="relative flex items-center gap-1.5 text-[10px] uppercase tracking-wider text-rose-300/80">
|
|
<span className="size-2 shrink-0 rounded-full border border-rose-400/60" />
|
|
false
|
|
</div>
|
|
<Handle
|
|
id="false"
|
|
type="source"
|
|
position={position}
|
|
className="!size-3 !bg-rose-400/80"
|
|
style={{ bottom: 4 }}
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
{nodeType === "end" && (
|
|
<div className="text-sm text-white/70">
|
|
{d.closing ? (
|
|
<span className="line-clamp-3">
|
|
<DialogueText text={d.closing} />
|
|
</span>
|
|
) : (
|
|
<span className="italic text-white/30">No closing line</span>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
{genericSource && (
|
|
<Handle type="source" position={position} className="!size-3 !bg-white/60" />
|
|
)}
|
|
{!d.isStart && (
|
|
<Handle type="target" position={inputPosition} className="!size-3 !bg-white/60" />
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function NodeIcon({ type }: { type: NpcDialogueNodeType }) {
|
|
const className = "size-3.5 text-white/50";
|
|
switch (type) {
|
|
case "choice":
|
|
return <GitBranchIcon className={className} />;
|
|
case "event":
|
|
return <SparklesIcon className={className} />;
|
|
case "delay":
|
|
return <TimerIcon className={className} />;
|
|
case "condition":
|
|
return <GitCompareIcon className={className} />;
|
|
case "end":
|
|
return <CircleStopIcon className={className} />;
|
|
default:
|
|
return <FileTextIcon className={className} />;
|
|
}
|
|
}
|