feat(npc-dialogue): rich hover tooltips on player choices
This commit is contained in:
parent
1e0df82725
commit
59fe616024
4 changed files with 74 additions and 13 deletions
|
|
@ -193,7 +193,9 @@ export function NodeEditDialog({ node, allNodes, open, onOpenChange, onSave }: N
|
|||
Line breaks are kept. **bold**, *italic*, {`{red}color{/}`} and {'`code`'} render
|
||||
in the conversation. Colors take a friendly name (red, amber, cyan, emerald,
|
||||
violet, pink, slate, ...) or a hex code like {`{#ff00ff}`}. Insert flag values
|
||||
with {'{{flag_key}}'}; text flags may carry their own color markup.
|
||||
with {'{{flag_key}}'}; text flags may carry their own color markup. Inline
|
||||
images use {''} (e.g. {``}) and
|
||||
render at text height.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
|
@ -237,6 +239,20 @@ export function NodeEditDialog({ node, allNodes, open, onOpenChange, onSave }: N
|
|||
className="bg-zinc-800 border-white/20 text-white placeholder:text-white/30"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label className="text-[9px] uppercase tracking-wider text-white/40">
|
||||
Tooltip (optional)
|
||||
</Label>
|
||||
<Textarea
|
||||
value={choice.tooltip ?? ""}
|
||||
onChange={(e) =>
|
||||
setChoice(index, { ...choice, tooltip: e.target.value || undefined })
|
||||
}
|
||||
placeholder="Hover card. Supports markup, {{flags}} and  images."
|
||||
rows={2}
|
||||
className="bg-zinc-800 border-white/20 text-white placeholder:text-white/30"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<Label className="text-[9px] uppercase tracking-wider text-white/40">Goes to</Label>
|
||||
<select
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
"use client";
|
||||
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useEffect, useMemo, useRef, useState, Fragment } from "react";
|
||||
import { MessageCircleIcon, XIcon } from "lucide-react";
|
||||
|
||||
import type { GameNpc } from "@/payload-types";
|
||||
|
|
@ -16,6 +16,12 @@ import {
|
|||
DialogContent,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
TooltipProvider,
|
||||
TooltipTrigger,
|
||||
} from "@/components/ui/tooltip";
|
||||
import {
|
||||
evaluateNpcDialogueChoices,
|
||||
evaluateNpcDialogueCondition,
|
||||
|
|
@ -265,6 +271,15 @@ export function NpcConversation({ npc, dialogue }: NpcConversationProps) {
|
|||
hop.label) ||
|
||||
"…";
|
||||
|
||||
/** Rich hover tooltip for a choice (resolved like labels); undefined when
|
||||
* the author left it empty. */
|
||||
const choiceTooltip = (hop: DialogueNext, index: number): string | undefined => {
|
||||
const raw =
|
||||
(lastNode?.kind === "node" ? lastNode.resolved?.tooltips?.[index] : undefined) ??
|
||||
hop.tooltip;
|
||||
return raw?.trim() ? raw : undefined;
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Button
|
||||
|
|
@ -321,16 +336,33 @@ export function NpcConversation({ npc, dialogue }: NpcConversationProps) {
|
|||
(lastNode.node.type === "choice" || lastNode.node.type === "end") && (
|
||||
<div className="flex flex-col gap-2 border-t border-white/15 bg-black/50 p-4">
|
||||
{lastNode.node.type === "choice" && visibleChoices.length > 0 ? (
|
||||
visibleChoices.map(({ hop, index }) => (
|
||||
<button
|
||||
key={index}
|
||||
type="button"
|
||||
onClick={() => pick(hop, index, choiceLabel(hop, index))}
|
||||
className="w-full rounded-none border border-white/20 bg-black/40 px-3 py-2 text-left text-sm text-white/80 transition-colors hover:border-cyan-500/60 hover:text-white"
|
||||
>
|
||||
<DialogueText text={choiceLabel(hop, index)} />
|
||||
</button>
|
||||
))
|
||||
<TooltipProvider delayDuration={150}>
|
||||
{visibleChoices.map(({ hop, index }) => {
|
||||
const button = (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => pick(hop, index, choiceLabel(hop, index))}
|
||||
className="w-full rounded-none border border-white/20 bg-black/40 px-3 py-2 text-left text-sm text-white/80 transition-colors hover:border-cyan-500/60 hover:text-white"
|
||||
>
|
||||
<DialogueText text={choiceLabel(hop, index)} />
|
||||
</button>
|
||||
);
|
||||
const tip = choiceTooltip(hop, index);
|
||||
if (!tip) return <Fragment key={index}>{button}</Fragment>;
|
||||
return (
|
||||
<Tooltip key={index}>
|
||||
<TooltipTrigger asChild>{button}</TooltipTrigger>
|
||||
<TooltipContent
|
||||
side="top"
|
||||
sideOffset={8}
|
||||
className="max-w-xs rounded-none border border-white/20 bg-zinc-900 p-3 text-left text-sm font-normal leading-relaxed text-white/85"
|
||||
>
|
||||
<DialogueText text={tip} />
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
);
|
||||
})}
|
||||
</TooltipProvider>
|
||||
) : (
|
||||
<Button
|
||||
onClick={close}
|
||||
|
|
|
|||
|
|
@ -27,6 +27,12 @@ export interface DialogueNext {
|
|||
* `dlg-once:` player flag).
|
||||
*/
|
||||
once?: boolean;
|
||||
/**
|
||||
* Choice hops only: optional rich tooltip shown when the player hovers or
|
||||
* focuses the choice button. Supports the same inline markup as dialogue
|
||||
* lines (colors, bold, images) plus `{{flag}}` interpolation.
|
||||
*/
|
||||
tooltip?: string;
|
||||
/**
|
||||
* Choice hops only: when set, the choice button is only offered to players
|
||||
* whose live flags satisfy this condition (evaluated server-side, same
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@ export interface ResolvedNodeTexts {
|
|||
message?: string;
|
||||
/** Choice button labels, index-aligned with the node's choices. */
|
||||
labels?: string[];
|
||||
/** Choice tooltips, index-aligned with the node's choices. */
|
||||
tooltips?: string[];
|
||||
}
|
||||
|
||||
/** Every distinct flag key referenced by `text`, lowercased. */
|
||||
|
|
@ -85,7 +87,7 @@ export function collectNodeTokens(node: DialogueNode): string[] {
|
|||
node.data.leadIn,
|
||||
node.data.closing,
|
||||
node.data.effect?.message,
|
||||
...(node.data.choices?.map((c) => c.label) ?? []),
|
||||
...(node.data.choices?.flatMap((c) => [c.label, c.tooltip]) ?? []),
|
||||
];
|
||||
for (const text of texts) {
|
||||
for (const key of extractFlagTokens(text)) keys.add(key);
|
||||
|
|
@ -111,6 +113,11 @@ export function resolveNodeTexts(
|
|||
resolved.message = interpolateFlags(node.data.effect.message, values);
|
||||
if (node.data.choices) {
|
||||
resolved.labels = node.data.choices.map((choice) => interpolateFlags(choice.label, values));
|
||||
if (node.data.choices.some((choice) => choice.tooltip !== undefined)) {
|
||||
resolved.tooltips = node.data.choices.map((choice) =>
|
||||
choice.tooltip === undefined ? "" : interpolateFlags(choice.tooltip, values),
|
||||
);
|
||||
}
|
||||
}
|
||||
return resolved;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue