Примеры UI-экранов
Предоставляет примеры UI-экранов для Pixi’VN, показывая компоненты React и Vue, а также демонстрируя хуки (например, useQueryDialogue, useQueryChoiceMenuOptions, useQueryCanGoNext) и интеграцию с повествованием для выбора, вперёд/назад, запросов ввода и истории.
ИИ
Вы можете использовать кнопку выше, чтобы создать собственный 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>
);
}