86 lines
2.6 KiB
TypeScript
86 lines
2.6 KiB
TypeScript
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<typeof useSelectedCutscene>, 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<HTMLDivElement>(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 (
|
|
<div className={styles.wrapper}>
|
|
<div className={styles.viewport} ref={containerRef}>
|
|
{!cutscene ? (
|
|
<div className={styles.empty}>Select or create a cutscene</div>
|
|
) : (
|
|
<>
|
|
{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 (
|
|
<img
|
|
key={i}
|
|
src={`/${seg.path}`}
|
|
alt=""
|
|
style={{ ...imgStyle, opacity: state.alpha }}
|
|
draggable={false}
|
|
/>
|
|
);
|
|
})}
|
|
|
|
{subtitle && (
|
|
<div className={styles.subtitleBar}>
|
|
{subtitle.speaker && (
|
|
<div className={styles.speaker}>{subtitle.speaker}</div>
|
|
)}
|
|
<div className={styles.text}>{subtitle.text}</div>
|
|
</div>
|
|
)}
|
|
</>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|