UI画面の例
Pixi’VN向けのUI画面の例を提供し、ReactおよびVueコンポーネントを示すとともに、フック(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>
);
}