UI 界面示例
为 Pixi’VN 提供 UI 界面示例,展示 React 和 Vue 组件,并演示 Hook(例如 useQueryDialogue、useQueryChoiceMenuOptions、useQueryCanGoNext)以及与叙事的集成,涵盖选项、前进/后退、输入提示和历史记录。
AI
你可以使用上方的按钮,用 AI 创建你自己的 UI 界面。
这些示例使用了简单的 UI 基础组件(Button、Card、Avatar、Dialog、Input 等)它们从 components/ui 文件夹导入,类似于 shadcn/ui(React)或 shadcn-vue 提供的组件。请将它们替换为你自己 UI 组件库中的等效组件。
import { Markdown } from "@tanstack/markdown/react";
import { Avatar } from "@/components/ui/avatar";
import { Card, CardContent } from "@/components/ui/card";
import { useQueryDialogue } from "@/hooks/useQueryInterface";
import ChoiceMenu from "@/components/narration/ChoiceMenu";
export default function NarrationScreen() {
const { data: { text, character } = {} } = useQueryDialogue();
return (
<div
style={{
position: "absolute",
inset: 0,
display: "flex",
flexDirection: "column",
}}
>
<div style={{ flex: 1, minHeight: 0 }}>
<ChoiceMenu />
</div>
{text && (
<Card
style={{
flex: "0 0 auto",
height: "35%",
minHeight: 0,
pointerEvents: "auto",
}}
>
<CardContent
style={{
display: "flex",
gap: 12,
height: "100%",
minHeight: 0,
overflow: "hidden",
}}
>
{character?.icon && (
<Avatar size="lg" style={{ flexShrink: 0 }}>
<img
src={character.icon}
loading="lazy"
alt=""
style={{
width: "100%",
height: "100%",
objectFit: "cover",
}}
/>
</Avatar>
)}
<div style={{ flex: 1, minHeight: 0, overflow: "auto" }}>
{character?.name && (
<b>{`${character.name} ${character.surname || ""}`}</b>
)}
<Markdown allowHtml>{text}</Markdown>
</div>
</CardContent>
</Card>
)}
</div>
);
}import { Button } from "@/components/ui/button";
import useNarrationFunctions from "@/hooks/useNarrationFunctions";
import { useQueryChoiceMenuOptions } from "@/hooks/useQueryInterface";
export default function ChoiceMenu() {
const { data: menu = [] } = useQueryChoiceMenuOptions();
const { selectChoice } = useNarrationFunctions();
if (menu.length === 0) {
return null;
}
return (
<div
onClick={(e) => e.stopPropagation()}
style={{
display: "flex",
flexDirection: "column",
justifyContent: "center",
alignItems: "center",
width: "100%",
height: "100%",
overflow: "auto",
gap: 8,
pointerEvents: "auto",
}}
>
{menu.map((item) => (
<Button
key={item.choiceIndex}
variant="secondary"
onClick={() => selectChoice(item)}
>
{item.text}
</Button>
))}
</div>
);
}不使用专门的“下一步”按钮,而是将叙事界面包裹在一个覆盖层中,玩家点击该覆盖层的任意位置都会推进叙事。
import { useState } from "react";
import useNarrationFunctions from "@/hooks/useNarrationFunctions";
import { useQueryCanGoNext } from "@/hooks/useQueryInterface";
export default function ContinueOverlay({
children,
}: {
children: React.ReactNode;
}) {
const { data: canGoNext = false } = useQueryCanGoNext();
const [loading, setLoading] = useState(false);
const { goNext } = useNarrationFunctions();
return (
<div
onClick={() => {
if (!canGoNext || loading) {
return;
}
setLoading(true);
goNext().finally(() => setLoading(false));
}}
style={{
position: "relative",
width: "100%",
height: "100%",
pointerEvents: "auto",
}}
>
{children}
</div>
);
}用它包裹叙事对话界面,这样点击屏幕上的任意位置都会继续叙事(选项菜单、返回按钮和输入对话框等元素会调用 stopPropagation(),因此与它们交互不会同时触发“下一步”):
<ContinueOverlay>
<NarrationScreen />
</ContinueOverlay>import { useState } from "react";
import { Button } from "@/components/ui/button";
import useNarrationFunctions from "@/hooks/useNarrationFunctions";
import { useQueryCanGoBack } from "@/hooks/useQueryInterface";
export default function BackButton() {
const { data: canGoBack = false } = useQueryCanGoBack();
const [loading, setLoading] = useState(false);
const { goBack } = useNarrationFunctions();
if (!canGoBack) {
return null;
}
return (
<Button
variant="secondary"
disabled={loading}
onClick={(e) => {
e.stopPropagation();
setLoading(true);
goBack().finally(() => setLoading(false));
}}
>
Back
</Button>
);
}import { narration } from "@drincs/pixi-vn";
import { useQueryClient } from "@tanstack/react-query";
import { useEffect, useState } from "react";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import {
INTERFACE_DATA_QUERY_KEY,
useQueryDialogue,
useQueryInputValue,
} from "@/hooks/useQueryInterface";
export default function TextInputDialog() {
const { data: { text } = {} } = useQueryDialogue();
const {
data: { isRequired: open, currentValue, type } = {
isRequired: false,
currentValue: undefined,
type: undefined,
},
} = useQueryInputValue<string | number>();
const [value, setValue] = useState<string | number>("");
const queryClient = useQueryClient();
useEffect(() => {
if (open) {
setValue(currentValue ?? "");
}
}, [open]);
const confirm = () => {
narration.input.value = value;
queryClient.invalidateQueries({ queryKey: [INTERFACE_DATA_QUERY_KEY] });
};
return (
<Dialog open={open}>
<DialogContent onClick={(e) => e.stopPropagation()}>
<DialogHeader>
<DialogTitle>Your input is needed</DialogTitle>
{text && <DialogDescription>{text}</DialogDescription>}
</DialogHeader>
<Input
type={type === "number" ? "number" : "text"}
value={value}
onChange={(e) => {
setValue(
type === "number"
? e.target.valueAsNumber
: e.target.value,
);
}}
onKeyDown={(e) => {
if (e.key === "Enter") {
confirm();
}
}}
/>
<DialogFooter>
<Button onClick={confirm}>Confirm</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}