[246] Kamerafahrt: durch die Kameras fahren und als Video aufnehmen
Knopf in der Kamera-Gruppe fährt im 3D in Listenreihenfolge durch die Kameras (Catmull-Rom oder geglättet, Bildwinkel linear, Dauer je Abschnitt, Esc/Klick bricht ab). Dialog Video …: WebM/MP4 über MediaRecorder auf dem Canvas, 30 Bilder/s, Fortschritt, Abbruch, Speichern über saveBinaryFile. Unter Tauri ohne MediaRecorder meldet notify. Folge aus [213].
This commit is contained in:
20 files changed
+1327
-3
No files matched your search
@@ -29,6 +29,7 @@ import { ImportDialog } from "./ImportDialog";
|
||||
import { LockConflictDialog } from "./LockConflictDialog";
|
||||
import { PresentationDialogHost } from "./PresentationDialog";
|
||||
import { RenderDialogHost } from "./RenderDialog";
|
||||
import { CameraTourDialogHost } from "./CameraTourDialog";
|
||||
import { ImageExportDialogHost } from "./ImageExportDialog";
|
||||
import { ShortcutOverviewDialogHost } from "./ShortcutOverviewDialog";
|
||||
import { PromptDialog } from "./PromptDialog";
|
||||
@@ -164,6 +165,8 @@ export function AppDialogs(props: {
|
||||
<RenderDialogHost />
|
||||
{/* [223] 3D-Bild exportieren (Befehl `bild3d`, Menü Ansicht) */}
|
||||
<ImageExportDialogHost />
|
||||
{/* [246] Kamerafahrt abspielen und als Video aufnehmen (Panel Ausschnitte, Gruppe Kameras) */}
|
||||
<CameraTourDialogHost />
|
||||
{/* [210] Kürzel-Übersicht (Menü Hilfe) */}
|
||||
<ShortcutOverviewDialogHost />
|
||||
{importState && (
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
// [246] Dialog „Kamerafahrt": Dauer je Abschnitt, Interpolation, Videoformat (WebM, MP4 wenn der Browser es kann), Abspielen
|
||||
// im 3D-Viewport und Aufnahme als Video (`MediaRecorder` auf dem Canvas, Auflösung = Viewport, 30 Bilder/s). Während
|
||||
// Abspielen/Aufnahme schrumpft der Dialog zu einer Leiste mit Fortschritt und „Abbrechen" (der Viewport bleibt sichtbar;
|
||||
// Esc oder Klick in den Viewport brechen ebenfalls ab). Zustand: `cameraTourDialogState.ts`; Öffner `state/cameraTourDialogHook`.
|
||||
|
||||
import { memo, useCallback, useEffect, useMemo, useReducer, useRef } from "react";
|
||||
import { t } from "../i18n";
|
||||
import { getState } from "../state/appStore";
|
||||
import { cancelCameraTour, currentTourKeys, getTourSettings, playCameraTour, recordCameraTour, setTourSettings } from "../state/cameraTourActions";
|
||||
import { setCameraTourDialogOpener } from "../state/cameraTourDialogHook";
|
||||
import { saveBinaryFile } from "../native/saveFile";
|
||||
import { getViewportCanvas } from "../viewport/cameraBridge";
|
||||
import { availableVideoFormats, videoFileName } from "../viewport/videoRecorder";
|
||||
import type { TourMode } from "../viewport/cameraTour";
|
||||
import {
|
||||
TOUR_FPS,
|
||||
canRecord,
|
||||
canStart,
|
||||
initialTourDialogState,
|
||||
parseSeconds,
|
||||
totalSeconds,
|
||||
tourDialogReducer,
|
||||
} from "./cameraTourDialogState";
|
||||
|
||||
export const CameraTourDialogHost = memo(function CameraTourDialogHost() {
|
||||
const [s, dispatch] = useReducer(tourDialogReducer, initialTourDialogState);
|
||||
const resultRef = useRef<{ blob: Blob; ext: string } | null>(null);
|
||||
const formats = useMemo(() => (s.open ? availableVideoFormats() : []), [s.open]);
|
||||
|
||||
useEffect(() => {
|
||||
setCameraTourDialogOpener(() => {
|
||||
resultRef.current = null;
|
||||
const cur = getTourSettings();
|
||||
dispatch({ type: "open", cameraCount: currentTourKeys().length, seconds: cur.seconds, mode: cur.mode, formats: availableVideoFormats().map((f) => f.id) });
|
||||
});
|
||||
return () => setCameraTourDialogOpener(null);
|
||||
}, []);
|
||||
|
||||
const close = useCallback(() => dispatch({ type: "close" }), []);
|
||||
useEffect(() => {
|
||||
if (!s.open) return;
|
||||
const onKey = (e: KeyboardEvent) => {
|
||||
if (e.key === "Escape" && s.phase !== "running") close();
|
||||
};
|
||||
window.addEventListener("keydown", onKey);
|
||||
return () => window.removeEventListener("keydown", onKey);
|
||||
}, [s.open, s.phase, close]);
|
||||
|
||||
if (!s.open) return null;
|
||||
|
||||
const commitSettings = () => {
|
||||
const seconds = parseSeconds(s.secondsText);
|
||||
if (seconds !== null) setTourSettings({ seconds, mode: s.mode });
|
||||
};
|
||||
|
||||
const play = async () => {
|
||||
if (!canStart(s)) return;
|
||||
commitSettings();
|
||||
dispatch({ type: "start", recording: false });
|
||||
const r = await playCameraTour((f) => dispatch({ type: "progress", fraction: f }));
|
||||
if (r.status === "unavailable" || r.status === "failed") dispatch({ type: "cancel" });
|
||||
else dispatch({ type: "finish", recording: false });
|
||||
};
|
||||
|
||||
const record = async () => {
|
||||
const format = availableVideoFormats().find((f) => f.id === s.format);
|
||||
if (!canRecord(s) || !format) return;
|
||||
commitSettings();
|
||||
resultRef.current = null;
|
||||
dispatch({ type: "start", recording: true });
|
||||
const r = await recordCameraTour(format, TOUR_FPS, (f) => dispatch({ type: "progress", fraction: f }));
|
||||
if (r.status === "done" && r.blob) {
|
||||
resultRef.current = { blob: r.blob, ext: format.ext };
|
||||
dispatch({ type: "finish", recording: true });
|
||||
} else if (r.status === "cancelled") {
|
||||
dispatch({ type: "cancel" });
|
||||
getState().notify(t("tour.cancelled"), "info");
|
||||
} else {
|
||||
dispatch({ type: "cancel" });
|
||||
}
|
||||
};
|
||||
|
||||
const save = async () => {
|
||||
const res = resultRef.current;
|
||||
if (!res) return;
|
||||
try {
|
||||
const bytes = new Uint8Array(await res.blob.arrayBuffer());
|
||||
if (await saveBinaryFile(bytes, videoFileName(getState().project.name, res.ext), res.blob.type || "video/webm")) getState().notify(t("tour.saved"), "info");
|
||||
} catch (err) {
|
||||
getState().notify(t("tour.recordFailed", { reason: err instanceof Error ? err.message : String(err) }), "warning");
|
||||
}
|
||||
};
|
||||
|
||||
if (s.phase === "running") {
|
||||
return (
|
||||
<div
|
||||
data-tour-ui
|
||||
data-testid="tour-bar"
|
||||
role="status"
|
||||
style={{ position: "fixed", left: "50%", bottom: 24, transform: "translateX(-50%)", zIndex: 1000, background: "var(--panel, #fff)", color: "var(--text, #111)", border: "1px solid var(--border, #888)", borderRadius: 6, padding: "8px 12px", display: "flex", gap: 10, alignItems: "center", boxShadow: "0 4px 16px rgba(0,0,0,.25)", maxWidth: "calc(100vw - 32px)" }}
|
||||
>
|
||||
<span style={{ fontSize: 12 }}>{s.recording ? t("tour.recording") : t("tour.playing")}</span>
|
||||
<progress style={{ width: 160 }} max={1} value={s.progress} data-testid="tour-progress" />
|
||||
<span style={{ fontSize: 12, minWidth: 34 }}>{Math.round(s.progress * 100)} %</span>
|
||||
<button className="imp-btn" data-testid="tour-cancel" onClick={() => cancelCameraTour()}>
|
||||
{t("tour.cancel")}
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const canvas = getViewportCanvas();
|
||||
const total = totalSeconds(s);
|
||||
const secondsOk = parseSeconds(s.secondsText) !== null;
|
||||
const res = resultRef.current;
|
||||
return (
|
||||
<div className="imp-overlay" data-tour-ui onClick={close}>
|
||||
<div className="imp-dialog render-dialog" role="dialog" aria-modal="true" aria-label={t("tour.title")} data-testid="tour-dialog" onClick={(e) => e.stopPropagation()}>
|
||||
<header className="imp-head">
|
||||
<span className="imp-head-title">{t("tour.title")}</span>
|
||||
<button className="imp-close" onClick={close} aria-label={t("export.close")}>
|
||||
×
|
||||
</button>
|
||||
</header>
|
||||
<div className="imp-body">
|
||||
{s.phase === "done" && res ? (
|
||||
<section className="imp-section" data-testid="tour-result">
|
||||
<div className="imp-note">{t("tour.done", { size: (res.blob.size / 1_048_576).toFixed(1), ext: res.ext.toUpperCase() })}</div>
|
||||
</section>
|
||||
) : (
|
||||
<>
|
||||
<p className="imp-note">{t("tour.intro", { count: s.cameraCount })}</p>
|
||||
<section className="imp-section">
|
||||
<div style={{ display: "flex", gap: 12, flexWrap: "wrap", alignItems: "center" }}>
|
||||
<label className="imp-field-label">
|
||||
{t("tour.seconds")}{" "}
|
||||
<input
|
||||
className="imp-input imp-input-flush"
|
||||
style={{ width: 70 }}
|
||||
inputMode="decimal"
|
||||
data-testid="tour-seconds"
|
||||
aria-invalid={!secondsOk}
|
||||
value={s.secondsText}
|
||||
onChange={(e) => dispatch({ type: "seconds", text: e.target.value })}
|
||||
/>
|
||||
</label>
|
||||
<label className="imp-field-label">
|
||||
{t("tour.mode")}{" "}
|
||||
<select className="imp-select" data-testid="tour-mode" value={s.mode} onChange={(e) => dispatch({ type: "mode", mode: e.target.value as TourMode })}>
|
||||
<option value="catmull">{t("tour.mode.catmull")}</option>
|
||||
<option value="smooth">{t("tour.mode.smooth")}</option>
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
<div className="imp-note" data-testid="tour-total">
|
||||
{secondsOk ? t("tour.total", { seconds: total.toFixed(1) }) : t("tour.secondsBad")}
|
||||
</div>
|
||||
</section>
|
||||
<section className="imp-section">
|
||||
<div className="imp-section-title">{t("tour.video")}</div>
|
||||
{formats.length === 0 ? (
|
||||
<div className="imp-note" data-testid="tour-noformat">
|
||||
{t("tour.noRecorder")}
|
||||
</div>
|
||||
) : (
|
||||
<label className="imp-field-label">
|
||||
{t("tour.format")}{" "}
|
||||
<select className="imp-select" data-testid="tour-format" value={s.format ?? ""} onChange={(e) => dispatch({ type: "format", format: e.target.value as "mp4" | "webm" })}>
|
||||
{formats.map((f) => (
|
||||
<option key={f.id} value={f.id}>
|
||||
{f.ext.toUpperCase()}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
)}
|
||||
<div className="imp-note" data-testid="tour-resolution">
|
||||
{canvas ? t("tour.resolution", { width: canvas.width, height: canvas.height, fps: TOUR_FPS }) : t("tour.resolutionUnknown", { fps: TOUR_FPS })}
|
||||
</div>
|
||||
{s.cameraCount < 2 && <div className="imp-note">{t("tour.needTwo")}</div>}
|
||||
{getState().renderMode === "pathtrace" && <div className="imp-note" data-testid="tour-pathtrace">{t("tour.pathtrace")}</div>}
|
||||
</section>
|
||||
</>
|
||||
)}
|
||||
{s.message && (
|
||||
<p className="imp-note" role="status">
|
||||
{s.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<footer className="imp-foot">
|
||||
<button className="imp-btn" onClick={close}>
|
||||
{t("export.close")}
|
||||
</button>
|
||||
{s.phase === "done" && res ? (
|
||||
<>
|
||||
<button className="imp-btn" onClick={() => dispatch({ type: "again" })}>
|
||||
{t("tour.again")}
|
||||
</button>
|
||||
<button className="imp-btn primary" data-testid="tour-save" onClick={() => void save()}>
|
||||
{t("tour.save")}
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<button className="imp-btn" data-testid="tour-play" disabled={!canStart(s)} onClick={() => void play()}>
|
||||
{t("tour.play")}
|
||||
</button>
|
||||
<button className="imp-btn primary" data-testid="tour-record" disabled={!canRecord(s)} onClick={() => void record()}>
|
||||
{t("tour.record")}
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</footer>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,61 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { TOUR_FPS, canRecord, canStart, effectiveSeconds, initialTourDialogState, parseSeconds, totalSeconds, tourDialogReducer, type TourDialogAction, type TourDialogState } from "./cameraTourDialogState";
|
||||
|
||||
const run = (actions: TourDialogAction[], from: TourDialogState = initialTourDialogState) => actions.reduce(tourDialogReducer, from);
|
||||
const open = (n = 3, formats: ("mp4" | "webm")[] = ["webm"]): TourDialogAction => ({ type: "open", cameraCount: n, formats });
|
||||
|
||||
describe("[246] Kamerafahrt-Dialog: Zustand", () => {
|
||||
it("Vorgabe: 4 s je Abschnitt, flüssig, 30 fps, erstes verfügbares Format", () => {
|
||||
const s = run([open(3, ["mp4", "webm"])]);
|
||||
expect(s).toMatchObject({ open: true, phase: "setup", secondsText: "4", mode: "catmull", format: "mp4", progress: 0 });
|
||||
expect(TOUR_FPS).toBe(30);
|
||||
expect(totalSeconds(s)).toBe(8);
|
||||
});
|
||||
|
||||
it("Dauer: Komma, Bereichsprüfung, Start nur bei gültiger Eingabe", () => {
|
||||
expect(parseSeconds("2,5")).toBe(2.5);
|
||||
expect(parseSeconds("0.4")).toBeNull();
|
||||
expect(parseSeconds("61")).toBeNull();
|
||||
expect(parseSeconds("abc")).toBeNull();
|
||||
expect(parseSeconds("")).toBeNull();
|
||||
const bad = run([open(), { type: "seconds", text: "x" }]);
|
||||
expect(canStart(bad)).toBe(false);
|
||||
expect(effectiveSeconds(bad)).toBe(4);
|
||||
expect(run([{ type: "start", recording: false }], bad).phase).toBe("setup");
|
||||
const ok = run([open(), { type: "seconds", text: "6" }]);
|
||||
expect(totalSeconds(ok)).toBe(12);
|
||||
});
|
||||
|
||||
it("Aufnehmen braucht zwei Kameras und ein Format, Abspielen reicht eine Kamera", () => {
|
||||
expect(canStart(run([open(0)]))).toBe(false);
|
||||
expect(canStart(run([open(1)]))).toBe(true);
|
||||
expect(canRecord(run([open(1)]))).toBe(false);
|
||||
expect(canRecord(run([open(2, [])]))).toBe(false);
|
||||
expect(canRecord(run([open(2)]))).toBe(true);
|
||||
});
|
||||
|
||||
it("Ablauf Aufnahme: start → Fortschritt → finish → done; Fortschritt geklemmt", () => {
|
||||
let s = run([open(), { type: "start", recording: true }]);
|
||||
expect(s).toMatchObject({ phase: "running", recording: true });
|
||||
s = run([{ type: "progress", fraction: 0.4 }], s);
|
||||
expect(s.progress).toBe(0.4);
|
||||
expect(run([{ type: "progress", fraction: 7 }], s).progress).toBe(1);
|
||||
expect(run([{ type: "progress", fraction: Number.NaN }], s).progress).toBe(0);
|
||||
s = run([{ type: "finish", recording: true }], s);
|
||||
expect(s).toMatchObject({ phase: "done", progress: 1, recording: false });
|
||||
expect(run([{ type: "again" }], s).phase).toBe("setup");
|
||||
});
|
||||
|
||||
it("Vorschau endet wieder im Setup; Abbruch und Fehler", () => {
|
||||
const running = run([open(), { type: "start", recording: false }]);
|
||||
expect(run([{ type: "finish", recording: false }], running).phase).toBe("setup");
|
||||
expect(run([{ type: "cancel" }], running)).toMatchObject({ phase: "setup", progress: 0 });
|
||||
expect(run([{ type: "fail", message: "kaputt" }], running)).toMatchObject({ phase: "error", message: "kaputt" });
|
||||
});
|
||||
|
||||
it("Schliessen ist während des Laufs gesperrt", () => {
|
||||
const running = run([open(), { type: "start", recording: true }]);
|
||||
expect(run([{ type: "close" }], running).open).toBe(true);
|
||||
expect(run([{ type: "close" }], run([open()])).open).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,112 @@
|
||||
// [246] Zustand des Dialogs „Kamerafahrt": reiner Reducer (Phasen, Eingaben, Fortschritt), getrennt von React und Viewport,
|
||||
// damit er sich testen lässt. Phasen: setup → running (Abspielen oder Aufnehmen) → done (nur nach Aufnahme) bzw. error.
|
||||
|
||||
import { TOUR_DEFAULT_SECONDS, clampSeconds, tourDuration, type TourMode } from "../viewport/cameraTour";
|
||||
import type { VideoFormatId } from "../viewport/videoRecorder";
|
||||
|
||||
export type TourPhase = "setup" | "running" | "done" | "error";
|
||||
|
||||
/** Bildrate der Aufnahme (fest, Vorgabe laut [246]). */
|
||||
export const TOUR_FPS = 30;
|
||||
|
||||
export interface TourDialogState {
|
||||
open: boolean;
|
||||
phase: TourPhase;
|
||||
/** Eingabe in Sekunden je Abschnitt (kann ungültig sein, solange getippt wird; `seconds` ist die gültige Fassung). */
|
||||
secondsText: string;
|
||||
mode: TourMode;
|
||||
format: VideoFormatId | null;
|
||||
/** Läuft gerade eine Aufnahme (statt nur Vorschau)? */
|
||||
recording: boolean;
|
||||
/** 0…1. */
|
||||
progress: number;
|
||||
/** Meldung (Fehler, Abbruch). */
|
||||
message: string;
|
||||
cameraCount: number;
|
||||
}
|
||||
|
||||
export type TourDialogAction =
|
||||
| { type: "open"; cameraCount: number; seconds?: number; mode?: TourMode; formats: VideoFormatId[] }
|
||||
| { type: "close" }
|
||||
| { type: "seconds"; text: string }
|
||||
| { type: "mode"; mode: TourMode }
|
||||
| { type: "format"; format: VideoFormatId }
|
||||
| { type: "start"; recording: boolean }
|
||||
| { type: "progress"; fraction: number }
|
||||
| { type: "finish"; recording: boolean }
|
||||
| { type: "cancel" }
|
||||
| { type: "fail"; message: string }
|
||||
| { type: "again" };
|
||||
|
||||
export const initialTourDialogState: TourDialogState = {
|
||||
open: false,
|
||||
phase: "setup",
|
||||
secondsText: String(TOUR_DEFAULT_SECONDS),
|
||||
mode: "catmull",
|
||||
format: null,
|
||||
recording: false,
|
||||
progress: 0,
|
||||
message: "",
|
||||
cameraCount: 0,
|
||||
};
|
||||
|
||||
/** Eingegebene Sekunden als Zahl (Komma erlaubt), `null` bei Unsinn oder ausserhalb 0,5 … 60. */
|
||||
export function parseSeconds(text: string): number | null {
|
||||
const v = Number(text.trim().replace(",", "."));
|
||||
if (!text.trim() || !Number.isFinite(v)) return null;
|
||||
return v >= 0.5 && v <= 60 ? v : null;
|
||||
}
|
||||
|
||||
/** Gültige Sekunden (geklemmt), auch bei halb getipptem Text. */
|
||||
export function effectiveSeconds(s: TourDialogState): number {
|
||||
return parseSeconds(s.secondsText) ?? clampSeconds(Number(s.secondsText.replace(",", ".")));
|
||||
}
|
||||
|
||||
/** Gesamtdauer der Fahrt in Sekunden. */
|
||||
export function totalSeconds(s: TourDialogState): number {
|
||||
return tourDuration(s.cameraCount, effectiveSeconds(s));
|
||||
}
|
||||
|
||||
/** Darf gestartet werden (mindestens eine Kamera, gültige Dauer, nicht schon laufend)? */
|
||||
export function canStart(s: TourDialogState): boolean {
|
||||
return s.phase !== "running" && s.cameraCount >= 1 && parseSeconds(s.secondsText) !== null;
|
||||
}
|
||||
|
||||
/** Darf aufgenommen werden (zusätzlich: ein Format gewählt, mindestens zwei Kameras)? */
|
||||
export function canRecord(s: TourDialogState): boolean {
|
||||
return canStart(s) && s.format !== null && s.cameraCount >= 2;
|
||||
}
|
||||
|
||||
export function tourDialogReducer(s: TourDialogState, a: TourDialogAction): TourDialogState {
|
||||
switch (a.type) {
|
||||
case "open":
|
||||
return {
|
||||
...initialTourDialogState,
|
||||
open: true,
|
||||
cameraCount: a.cameraCount,
|
||||
secondsText: String(a.seconds ?? TOUR_DEFAULT_SECONDS),
|
||||
mode: a.mode ?? "catmull",
|
||||
format: a.formats[0] ?? null,
|
||||
};
|
||||
case "close":
|
||||
return s.phase === "running" ? s : { ...s, open: false, phase: "setup", message: "" };
|
||||
case "seconds":
|
||||
return { ...s, secondsText: a.text };
|
||||
case "mode":
|
||||
return { ...s, mode: a.mode };
|
||||
case "format":
|
||||
return { ...s, format: a.format };
|
||||
case "start":
|
||||
return canStart(s) ? { ...s, phase: "running", recording: a.recording, progress: 0, message: "" } : s;
|
||||
case "progress":
|
||||
return s.phase === "running" ? { ...s, progress: Math.min(1, Math.max(0, Number.isFinite(a.fraction) ? a.fraction : 0)) } : s;
|
||||
case "finish":
|
||||
return a.recording ? { ...s, phase: "done", recording: false, progress: 1 } : { ...s, phase: "setup", recording: false, progress: 0 };
|
||||
case "cancel":
|
||||
return { ...s, phase: "setup", recording: false, progress: 0 };
|
||||
case "fail":
|
||||
return { ...s, phase: "error", recording: false, progress: 0, message: a.message };
|
||||
case "again":
|
||||
return { ...s, phase: "setup", message: "", progress: 0 };
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user