import { useRef, useEffect, useState } from 'react'; import { useCutsceneStore, useSelectedCutscene } from '../../store/cutsceneStore'; import { computeSegmentState, poseToStyle } from '../../utils/rendering'; import styles from './Preview.module.css'; const LOGICAL_W = 1280; const LOGICAL_H = 720; function computeSubtitle(cutscene: ReturnType, currentMs: number) { if (!cutscene) return null; let elapsed = 0; for (const line of cutscene.lines) { const dur = line.durationMs > 0 ? line.durationMs : Math.max(1500, Math.round((line.text.length / 17) * 1000)); if (currentMs >= elapsed && currentMs < elapsed + dur) return line; elapsed += dur; } return null; } export default function Preview() { const containerRef = useRef(null); const [containerSize, setContainerSize] = useState({ w: LOGICAL_W, h: LOGICAL_H }); const currentTimeMs = useCutsceneStore(s => s.currentTimeMs); const cutscene = useSelectedCutscene(); useEffect(() => { const el = containerRef.current; if (!el) return; const ro = new ResizeObserver(entries => { const e = entries[0]; if (e) setContainerSize({ w: e.contentRect.width, h: e.contentRect.height }); }); ro.observe(el); return () => ro.disconnect(); }, []); const subtitle = computeSubtitle(cutscene, currentTimeMs); return (
{!cutscene ? (
Select or create a cutscene
) : ( <> {cutscene.imageSegments.map((seg, i) => { const state = computeSegmentState(seg, currentTimeMs); if (!state) return null; const imgStyle = poseToStyle( state.pose, seg.width || LOGICAL_W, seg.height || LOGICAL_H, containerSize.w, containerSize.h, ); return ( ); })} {subtitle && (
{subtitle.speaker && (
{subtitle.speaker}
)}
{subtitle.text}
)} )}
); }