From f7843db6be02e98174f46965d97d8665f81dfef0 Mon Sep 17 00:00:00 2001 From: bubulux Date: Wed, 25 Nov 2026 16:08:39 +0100 Subject: [PATCH] Flows: size boxes in the bar; the device gets the View options, keyframe sizes and level drag (#426) - Studio and device: Size leaves the View menu; two grouped boxes in the bar, Width (slider, Fit width) and Height (slider, Fit height). - Device: View has the Studio's switches (intensity graph, light colour, value labels, effect and power badges) and the keyframe size (small for a pen, large for a finger); with the graph on, a light's keyframe is dragged up or down for its intensity. Kept per device. - The drawing rules (graph line, light colour, labels) move to packages/ext-flows (look.ts). - Camera buttons: an unknown stored size falls back to medium. - Studio's Vite watches every shared package's folder (an extension package's new export was not seen until a restart). Co-Authored-By: Claude Opus 5.5 --- apps/mobile/src/ext/flows/ui/Editor.tsx | 48 +++++++++-- apps/mobile/src/ext/flows/ui/Lanes.tsx | 108 ++++++++++++++++-------- apps/mobile/src/settings.ts | 2 +- apps/studio/src/ext/flows/FlowsApp.tsx | 19 ++--- apps/studio/src/ext/flows/flows.css | 4 +- apps/studio/src/ext/flows/view.ts | 74 +--------------- apps/studio/vite.config.ts | 10 ++- docs/features/flows.md | 4 +- packages/ext-flows/src/index.ts | 1 + packages/ext-flows/src/look.ts | 78 +++++++++++++++++ 10 files changed, 213 insertions(+), 135 deletions(-) create mode 100644 packages/ext-flows/src/look.ts diff --git a/apps/mobile/src/ext/flows/ui/Editor.tsx b/apps/mobile/src/ext/flows/ui/Editor.tsx index e747506..3bda003 100644 --- a/apps/mobile/src/ext/flows/ui/Editor.tsx +++ b/apps/mobile/src/ext/flows/ui/Editor.tsx @@ -1,10 +1,11 @@ import { useEffect, useMemo, useRef, useState } from "react"; import { Text, View } from "react-native"; +import { persisted } from "../../../store"; import { randomUUID } from "expo-crypto"; import { CATALOGUE, modelOf } from "@openclapper/ext-hardware"; import { addKeyframe, addMarker, addTrack, fmtSeconds, FlowPlayer, patchMarker, patchTrack, removeKeys, removeTrack, setDuration, setKeyframe, - START_KEYFRAME, type Flow, type FlowBody, type Keyframe, + DEFAULT_VIEW, START_KEYFRAME, VIEW_LABELS, type Flow, type FlowBody, type FlowView, type Keyframe, type KeyframeSize, } from "@openclapper/ext-flows"; import { ApiError, canCall, lockFile, unlockFile } from "../../../api"; import { log } from "../../../log"; @@ -15,10 +16,14 @@ import { useDevices } from "../../hardware/library"; import { proxyRunning, startProxy, useHardware } from "../../hardware/proxy"; import { loadFlows, saveFlow, usePendingOf, type LocalFlow } from "../store"; import { CameraKeyframe, FadeInspect, LightKeyframe, MarkerInspect, TrackInspect } from "./Inspect"; -import { Lanes, type Pick } from "./Lanes"; +import { Lanes, MARKS_H, RULER_H, type Pick } from "./Lanes"; const RENEW_MS = 30_000; -const ZOOM_MAX = 32; +const ZOOM_MAX = 32, HEIGHT_MIN = 48, HEIGHT_MAX = 240; +/** What the lanes show and how large, on this device (#426): the Studio's View options, keyframe size, width and height. */ +interface DeviceView { view: FlowView; kfSize: KeyframeSize; zoom: number; height: number } +const deviceView = persisted("flows.view.v1", { view: DEFAULT_VIEW, kfSize: "m", zoom: 1, height: 64 }, + (v) => typeof v === "object" && v !== null && typeof (v as DeviceView).zoom === "number" && typeof (v as DeviceView).height === "number"); type Lock = { state: "mine"; holder: string } | { state: "offline" } | { state: "theirs"; by: string } | { state: "asking" }; /** @@ -34,11 +39,15 @@ export function FlowEditor({ flow, back }: { flow: LocalFlow; back: () => void } const [name, setName] = useState(flow.name); const [pick, setPick] = useState(null); const [insert, setInsert] = useState(false); - const [zoom, setZoom] = useState(1); + const dv = deviceView.use(); + const setDv = (p: Partial) => deviceView.set((x) => ({ ...x, ...p })); + const zoom = dv.zoom; + const [room, setRoom] = useState(0); + const fitHeight = () => setDv({ height: Math.round(Math.min(HEIGHT_MAX, Math.max(HEIGHT_MIN, (room - RULER_H - MARKS_H - 56) / Math.max(1, body.tracks.length)))) }); const [playhead, setPlayhead] = useState(0); const [playing, setPlaying] = useState(false); const [live, setLive] = useState(true); - const [sheet, setSheet] = useState<"flow" | "track" | null>(null); + const [sheet, setSheet] = useState<"flow" | "track" | "view" | null>(null); const [lock, setLock] = useState({ state: "asking" }); const devices = useDevices(); const hw = useHardware(); @@ -142,22 +151,34 @@ export function FlowEditor({ flow, back }: { flow: LocalFlow; back: () => void } actions={[{ icon: "settings", label: "Flow settings", onPress: () => setSheet("flow") }]} /> setInsert(v === "insert")} options={[{ id: "select", icon: "cursor" }, { id: "insert", icon: "plus" }]} disabled={locked} /> - setZoom((z) => Math.max(1, z / 1.5))} /> - = ZOOM_MAX} onPress={() => setZoom((z) => Math.min(ZOOM_MAX, z * 1.5))} /> + setSheet("view")} /> {fmtSeconds(playhead)} { player.current?.stop(); setPlaying(false); setPlayhead(0); }} /> + {/* Size (#426): two grouped boxes, each a slider and its Fit. */} + + + (z === 1 ? "fit" : `${z.toFixed(1)}×`)} onChange={(z) => setDv({ zoom: z })} /> + - - + {/* Size (#426): two grouped boxes in the bar, each a slider and its Fit. */} +
+ + +
+
+ + +
Live {fmt(runsThis ? runAt : playhead)} / {fmt(flow.durationMs)} diff --git a/apps/studio/src/ext/flows/flows.css b/apps/studio/src/ext/flows/flows.css index bbc7891..65b1691 100644 --- a/apps/studio/src/ext/flows/flows.css +++ b/apps/studio/src/ext/flows/flows.css @@ -81,8 +81,8 @@ .fl-swatch.is-on { outline: var(--border-w-strong) solid var(--focus); outline-offset: 2px; } .fl-swatch.is-none { background: repeating-linear-gradient(135deg, var(--surface) 0 4px, var(--border-divider) 4px 6px); } .fl-menu { display: grid; gap: var(--space-3); padding: var(--space-3) var(--space-4); min-width: 220px; } -.fl-menu__head { margin-top: var(--space-2); padding-top: var(--space-3); border-top: var(--border-w) solid var(--border-divider); font: var(--weight-semibold) var(--font-size-2xs)/1 var(--font-sans); letter-spacing: .06em; text-transform: uppercase; color: var(--text-secondary); } -.fl-menu__row { display: flex; align-items: end; gap: var(--space-2); } +.fl-size { display: inline-flex; align-items: center; gap: var(--space-3); padding: 2px var(--space-3); border: var(--border-w) solid var(--border-divider); border-radius: var(--radius-control); background: var(--surface-sunken); } +.fl-size .hw-ctl--range { width: 120px; } .fl-row > :first-child { position: sticky; left: 0; z-index: 3; background: var(--surface); } .fl-list { display: grid; } .fl-props { display: grid; gap: var(--space-3); padding: var(--space-3) var(--space-5) var(--space-5); border-bottom: var(--border-w) solid var(--border-divider); } diff --git a/apps/studio/src/ext/flows/view.ts b/apps/studio/src/ext/flows/view.ts index 27a0e44..d598563 100644 --- a/apps/studio/src/ext/flows/view.ts +++ b/apps/studio/src/ext/flows/view.ts @@ -1,12 +1,8 @@ // What a flow's lanes show (docs/features/flows.md, "In the Studio"): a light's colour, the intensity graph, short // labels of what a keyframe sets; and the view switches and keyframe size, kept per browser. import { useState } from "preact/hooks"; -import { effectName, modelOf } from "@openclapper/ext-hardware"; -import { sampleTrack, type Keyframe, type SequenceSample } from "@openclapper/ext-flows"; - -export interface FlowView { graph: boolean; fill: boolean; labels: boolean; badges: boolean } -export const VIEW_LABELS: Record = { graph: "Intensity graph", fill: "Light colour", labels: "Value labels", badges: "Effect and power badges" }; -const DEFAULT_VIEW: FlowView = { graph: false, fill: true, labels: false, badges: true }; +import { DEFAULT_VIEW, type FlowView } from "@openclapper/ext-flows"; +export { graphPoints, keyframeLevel, lightColour, shortLabel, VIEW_LABELS, type FlowView } from "@openclapper/ext-flows"; /** A per-browser setting (a viewer's convenience, not the flow's). */ function useLocal(key: string, fallback: T, valid: (v: unknown) => boolean): [T, (v: T) => void] { @@ -22,69 +18,3 @@ export const ZOOM_MIN = 1, ZOOM_MAX = 32; export const useZoom = () => useLocal("flows.zoom.v1", 1, (v) => typeof v === "number" && v >= ZOOM_MIN && v <= ZOOM_MAX); export const useTrackHeight = () => useLocal("flows.trackHeight.v1", 64, (v) => typeof v === "number" && v >= 40 && v <= 240); -/** - * The colour of a white light at `k` kelvin, as sRGB (Tanner Helland's fit of the Planckian locus; close enough for - * a swatch). A light's real colour is content, like a photo: not a theme token. - */ -export function kelvinRgb(k: number): [number, number, number] { - const t = Math.min(40_000, Math.max(1000, k)) / 100; - const clamp = (x: number) => Math.round(Math.min(255, Math.max(0, x))); - const r = t <= 66 ? 255 : 329.698727446 * (t - 60) ** -0.1332047592; - const g = t <= 66 ? 99.4708025861 * Math.log(t) - 161.1195681661 : 288.1221695283 * (t - 60) ** -0.0755148492; - const b = t >= 66 ? 255 : t <= 19 ? 0 : 138.5177312231 * Math.log(t - 10) - 305.0447927307; - return [clamp(r), clamp(g), clamp(b)]; -} - -/** The CSS colour a light shows in this state, or null when nothing has set white or colour. */ -export function lightColour(s: SequenceSample): string | null { - if (s.mode === "colour") return `hsl(${Math.round(s.hue ?? 0)} ${Math.round((s.sat ?? 1) * 100)}% ${Math.round(100 - (s.sat ?? 1) * 50)}%)`; - if (s.mode === "white" && s.kelvin !== undefined) { const [r, g, b] = kelvinRgb(s.kelvin); return `rgb(${r} ${g} ${b})`; } - return null; -} - -/** The level the graph shows at t: off counts as 0. */ -const levelAt = (ks: readonly Keyframe[], t: number) => { const s = sampleTrack(ks, t); return s.power === false ? 0 : s.intensity ?? 0; }; -/** The level a keyframe sits at in the graph. */ -export const keyframeLevel = (ks: readonly Keyframe[], k: Keyframe) => levelAt(ks, k.at); - -/** The intensity line of a track as SVG points ("t,y", y = 1 − level): flat while held, a step at a jump, the eased curve across a fade. */ -export function graphPoints(keyframes: readonly Keyframe[], durationMs: number): string { - const ks = [...keyframes].sort((a, b) => a.at - b.at); - if (!ks.length) return ""; - const pts: [number, number][] = []; - const push = (t: number) => pts.push([t, 1 - levelAt(ks, t)]); - for (let i = 0; i < ks.length; i++) { - const k = ks[i], next = ks[i + 1]; - if (i > 0 && !k.fadeIn) pts.push([k.at, 1 - levelAt(ks, Math.max(0, k.at - 1))]); - push(k.at); - const end = next ? next.at : durationMs; - if (next?.fadeIn) for (let j = 1; j < 24; j++) push(k.at + ((end - k.at) * j) / 24); - else if (!next) push(end); - } - return pts.map(([t, y]) => `${t},${y.toFixed(4)}`).join(" "); -} - -/** What a camera keyframe sets, in the catalogue's words ("Record · ISO 1600"). */ -export function cameraLabel(model: string, k: Keyframe): string { - const m = modelOf(model); - return Object.entries(k.controls ?? {}).map(([id, v]) => { - const c = m?.controls.find((x) => x.id === id); - if (!c) return id; - if (id === "record") return v ? "Record" : "Stop"; - if (c.kind === "choice") return `${c.label} ${c.options.find((o) => o.value === v)?.label ?? v}`; - if (c.kind === "switch") return `${c.label} ${v ? "on" : "off"}`; - return `${c.label} ${typeof v === "number" && !Number.isInteger(v) ? v.toFixed(2) : v}`; - }).join(" · "); -} - -/** A short line of what a keyframe sets ("70 % · 3200 K", "Fire"); `effect` false when a badge shows it. */ -export function shortLabel(model: string, k: Keyframe, effect = true): string { - if (k.controls) return cameraLabel(model, k); - return [ - k.power === false ? "off" : null, - k.intensity !== undefined ? `${Math.round(k.intensity * 100)} %` : null, - k.kelvin !== undefined ? `${k.kelvin} K` : null, - k.hue !== undefined ? `${Math.round(k.hue)}°` : null, - k.effect !== undefined && (effect || !k.effect.id) ? (k.effect.id ? effectName(k.effect.id) : "fx off") : null, - ].filter(Boolean).join(" · "); -} diff --git a/apps/studio/vite.config.ts b/apps/studio/vite.config.ts index f3551f2..fb952b5 100644 --- a/apps/studio/vite.config.ts +++ b/apps/studio/vite.config.ts @@ -1,7 +1,7 @@ import { defineConfig } from "vite"; import preact from "@preact/preset-vite"; import { execFileSync } from "node:child_process"; -import { existsSync } from "node:fs"; +import { existsSync, readdirSync } from "node:fs"; import { fileURLToPath } from "node:url"; import { version } from "../../package.json"; @@ -9,9 +9,11 @@ import { version } from "../../package.json"; const commit = (() => { try { return execFileSync("git", ["rev-parse", "--short", "HEAD"], { encoding: "utf8" }).trim(); } catch { return "dev"; } })(); const build = { __OPENCLAPPER_VERSION__: JSON.stringify(version), __OPENCLAPPER_COMMIT__: JSON.stringify(commit) }; -// The design system and the shared model live outside this root, where Vite watches single files; a -// git branch switch replaces them and those watches are lost until a restart. Watching the folders survives that. -const shared = ["../../packages/design/src", "../../packages/model/src"].map((p) => fileURLToPath(new URL(p, import.meta.url))); +// The shared packages (design system, model, extensions, fov-math) live outside this root, where Vite watches single +// files; a git branch switch replaces them and those watches are lost until a restart. Watching every package's +// folder survives that (#426: an extension package's new export was not seen). +const packagesDir = fileURLToPath(new URL("../../packages", import.meta.url)); +const shared = readdirSync(packagesDir).map((p) => `${packagesDir}/${p}/src`).filter((p) => existsSync(p)); const watchShared = { name: "openclapper-watch-shared", configureServer(server: { watcher: { add(path: string): unknown } }) { for (const p of shared) server.watcher.add(p); } }; export default defineConfig(({ command }) => { diff --git a/docs/features/flows.md b/docs/features/flows.md index a14d3c9..9d09177 100644 --- a/docs/features/flows.md +++ b/docs/features/flows.md @@ -32,7 +32,7 @@ The app **Flows** (one window for the project's flows): the list of flows in a p - **Keyframes**: two modes, **Select** (V, default: a click on empty lane space clears) and **Insert** (I: a click adds a keyframe); **Alt+click** adds in Select too. Shift-click adds to or removes from the selection; a box drawn over the lanes selects across tracks; dragging a selected keyframe moves the whole selection (0.1 s steps; start keyframes stay); with the intensity graph shown, dragging a light keyframe up or down also sets its intensity. Delete removes, Mod+A selects all, Esc clears. A light keyframe's panel sets power, intensity, white or colour, an effect with its options, the dimming curve (each checked: set here; unchecked: as before). A camera keyframe's panel checks the camera controls it sets (record, ISO, shutter…). - **Fades**: a click on the strip between two keyframes makes it a fade and shows it: Jump or Fade, the curve (Linear, Expo, Log, S-curve), and on the MC whether the light runs it or it goes in steps every 0.1 s (a dashed tile). - **Markers**: M at the playhead, Shift+M twice for a range, or in Insert mode (or with Alt) a click (point) or drag (range) in the marker strip under the ruler or a lane's top strip. They select, move and delete like keyframes; a range's edges drag; a marker's panel sets name, in, out, range or point, and scope. -- **View** (kept per browser): intensity graph, light colour, value labels, effect and power badges; keyframe size S, M, L; and a **Size** section: the timeline's width (a slider from fit to 32×, zoom out, zoom in, **Fit width**; the lanes then scroll sideways with the track labels kept in place) and one height for every track (**Fit height** to share the room). No help text under the lanes and no Marker button (#426). +- **View** (kept per browser): intensity graph, light colour, value labels, effect and power badges; keyframe size S, M, L; . **Size** sits in the bar as two grouped boxes (#426): the timeline's width (a slider from fit to 32× and **Fit width**; the lanes then scroll sideways with the track labels kept in place) and one height for every track (a slider and **Fit height**, which shares the room). No help text under the lanes and no Marker button. - **Playing**: Space plays from the playhead and, again, stops and goes back to where play started; K stops where it is. **Live** (on by default) sends the state at the playhead while stopped, and every edit; a camera's recording is never started or stopped by Live, only by playing. While a flow plays from here, its devices are locked in Devices. A proxy that appears again gets everything sent anew. - **Run on device** (from the start, or the button beside it from the playhead): the editor saves first and the device plays exactly that revision; meanwhile the flow is locked, its playhead follows the phone, and Play and Live are off for every flow. Stop in the banner or Space stops it. @@ -42,7 +42,7 @@ The app **Flows** (a tab like any other while the project has the extension on), - **The project's flows** (`store.ts`): kept on the device (`flows.v1`), so they open, change and play offline. **New flow** works offline too. A row says when a change is not on the server yet; **Run** (online, saved flows) runs it on the device that holds the connections, as the Studio's Run on device. - **Saving** (`store.ts`): every change is kept on the device at once (`flowEdits.v1`) and sent about a second later, or with the next sync. While online the editor holds the **edit lock** (renewed every 30 s) and saves under it; when someone else holds it, the flow is read-only on the device and shows their saves. Offline it edits freely. When the server's flow changed meanwhile, or was deleted, the device's version is saved **beside it as a copy** ("… (from this device)"): nothing made on the device is lost, nothing made elsewhere is overwritten. A change the server refuses stays on the device with the reason, and is tried again after the next change. -- **The editor** (`ui/Editor.tsx`, `ui/Lanes.tsx`, `ui/Inspect.tsx`; the editing rules shared with the Studio in `packages/ext-flows/src/edit.ts`): the ruler (tap: the playhead), the marker strip and one lane per track, with the track labels beside them; zoom in and out (1× to 32×, the lanes scroll sideways). **Select** or **Insert** mode: in Insert a tap on a lane adds a keyframe (with the intensity the track has there) and a tap on the strip a marker. A keyframe is tapped to choose it and dragged sideways to move it; the strip between two keyframes chooses the fade. What is chosen opens in a sheet: a light's keyframe (power, intensity, white or colour, effect, dimming curve), a camera's keyframe (the catalogue controls), the fade (jump or fade, curve, run by the light or in steps), a marker (name, where, a range), a track (name, the device it drives, muted, colour, delete). **Add a track** lists the light and camera models. The flow's name, length and loop are in its settings sheet. +- **The editor** (`ui/Editor.tsx`, `ui/Lanes.tsx`, `ui/Inspect.tsx`; the editing rules shared with the Studio in `packages/ext-flows/src/edit.ts`): the ruler (tap: the playhead), the marker strip and one lane per track, with the track labels beside them. Under the bar, the same two **Size** boxes as in the Studio (width 1× to 32× with Fit; track height with Fit). **View** (an eye in the bar) has the Studio's switches: intensity graph (a light's keyframes sit at their level on the line, and dragging one up or down sets its intensity, shown while dragging), light colour (a dot filled with what the light shows there), value labels, effect and power badges; and the keyframe size, small for a pen, large for a finger. All of it is kept on this device (`flows.view.v1`). The drawing rules are shared with the Studio (`packages/ext-flows/src/look.ts`). **Select** or **Insert** mode: in Insert a tap on a lane adds a keyframe (with the intensity the track has there) and a tap on the strip a marker. A keyframe is tapped to choose it and dragged sideways to move it; the strip between two keyframes chooses the fade. What is chosen opens in a sheet: a light's keyframe (power, intensity, white or colour, effect, dimming curve), a camera's keyframe (the catalogue controls), the fade (jump or fade, curve, run by the light or in steps), a marker (name, where, a range), a track (name, the device it drives, muted, colour, delete). **Add a track** lists the light and camera models. The flow's name, length and loop are in its settings sheet. - **Play and Live on this device**: the shared player through this device's own connections; **Hold the connections here** switches the proxy on when it is off. Live (on by default) shows the flow at the playhead on the lights while stopped and follows every edit; recording on a camera starts only while playing. - **The run** (`run.ts`, kept from the first app's `sequenceRun.ts`): the proxy phone fetches the named revision (asked again a few times while the server still has the previous one), plays it with the shared player through its own connections, reports its state, and keeps playing when the Studio closes. A banner shows it with **Stop**, here and in the Studio. It stops when the proxy lets go of its connections: the proxy switched off, or a pairing taking the radio. diff --git a/packages/ext-flows/src/index.ts b/packages/ext-flows/src/index.ts index 963f7f6..13fd056 100644 --- a/packages/ext-flows/src/index.ts +++ b/packages/ext-flows/src/index.ts @@ -49,3 +49,4 @@ export * from "./flow.ts"; export * from "./player.ts"; export * from "./protocol.ts"; export * from "./edit.ts"; +export * from "./look.ts"; diff --git a/packages/ext-flows/src/look.ts b/packages/ext-flows/src/look.ts new file mode 100644 index 0000000..ba66d21 --- /dev/null +++ b/packages/ext-flows/src/look.ts @@ -0,0 +1,78 @@ +// What a flow's lanes show (docs/features/flows.md): a light's colour, the intensity graph, short labels of what a +// keyframe sets, and the view switches. Shared by the Studio's and the device's editors. +import { effectName, modelOf } from "@openclapper/ext-hardware"; +import { sampleTrack, type Keyframe, type SequenceSample } from "./flow.ts"; + +export interface FlowView { graph: boolean; fill: boolean; labels: boolean; badges: boolean } +export const VIEW_LABELS: Record = { graph: "Intensity graph", fill: "Light colour", labels: "Value labels", badges: "Effect and power badges" }; +export const DEFAULT_VIEW: FlowView = { graph: false, fill: true, labels: false, badges: true }; +export type KeyframeSize = "s" | "m" | "l"; + +/** + * The colour of a white light at `k` kelvin, as sRGB (Tanner Helland's fit of the Planckian locus; close enough for + * a swatch). A light's real colour is content, like a photo: not a theme token. + */ +export function kelvinRgb(k: number): [number, number, number] { + const t = Math.min(40_000, Math.max(1000, k)) / 100; + const clamp = (x: number) => Math.round(Math.min(255, Math.max(0, x))); + const r = t <= 66 ? 255 : 329.698727446 * (t - 60) ** -0.1332047592; + const g = t <= 66 ? 99.4708025861 * Math.log(t) - 161.1195681661 : 288.1221695283 * (t - 60) ** -0.0755148492; + const b = t >= 66 ? 255 : t <= 19 ? 0 : 138.5177312231 * Math.log(t - 10) - 305.0447927307; + return [clamp(r), clamp(g), clamp(b)]; +} + +/** The colour a light shows (CSS and React Native) in this state, or null when nothing has set white or colour. */ +export function lightColour(s: SequenceSample): string | null { + if (s.mode === "colour") return `hsl(${Math.round(s.hue ?? 0)}, ${Math.round((s.sat ?? 1) * 100)}%, ${Math.round(100 - (s.sat ?? 1) * 50)}%)`; + if (s.mode === "white" && s.kelvin !== undefined) { const [r, g, b] = kelvinRgb(s.kelvin); return `rgb(${r}, ${g}, ${b})`; } + return null; +} + +/** The level the graph shows at t: off counts as 0. */ +const levelAt = (ks: readonly Keyframe[], t: number) => { const s = sampleTrack(ks, t); return s.power === false ? 0 : s.intensity ?? 0; }; +/** The level a keyframe sits at in the graph. */ +export const keyframeLevel = (ks: readonly Keyframe[], k: Keyframe) => levelAt(ks, k.at); + +/** The intensity line of a track as points [t, y] (y = 1 − level): flat while held, a step at a jump, the eased curve across a fade. */ +export function graphLine(keyframes: readonly Keyframe[], durationMs: number): [number, number][] { + const ks = [...keyframes].sort((a, b) => a.at - b.at); + if (!ks.length) return []; + const pts: [number, number][] = []; + const push = (t: number) => pts.push([t, 1 - levelAt(ks, t)]); + for (let i = 0; i < ks.length; i++) { + const k = ks[i], next = ks[i + 1]; + if (i > 0 && !k.fadeIn) pts.push([k.at, 1 - levelAt(ks, Math.max(0, k.at - 1))]); + push(k.at); + const end = next ? next.at : durationMs; + if (next?.fadeIn) for (let j = 1; j < 24; j++) push(k.at + ((end - k.at) * j) / 24); + else if (!next) push(end); + } + return pts; +} +/** The same as SVG points ("t,y …"). */ +export const graphPoints = (keyframes: readonly Keyframe[], durationMs: number): string => graphLine(keyframes, durationMs).map(([t, y]) => `${t},${y.toFixed(4)}`).join(" "); + +/** What a camera keyframe sets, in the catalogue's words ("Record · ISO 1600"). */ +export function cameraLabel(model: string, k: Keyframe): string { + const m = modelOf(model); + return Object.entries(k.controls ?? {}).map(([id, v]) => { + const c = m?.controls.find((x) => x.id === id); + if (!c) return id; + if (id === "record") return v ? "Record" : "Stop"; + if (c.kind === "choice") return `${c.label} ${c.options.find((o) => o.value === v)?.label ?? v}`; + if (c.kind === "switch") return `${c.label} ${v ? "on" : "off"}`; + return `${c.label} ${typeof v === "number" && !Number.isInteger(v) ? v.toFixed(2) : v}`; + }).join(" · "); +} + +/** A short line of what a keyframe sets ("70 % · 3200 K", "Fire"); `effect` false when a badge shows it. */ +export function shortLabel(model: string, k: Keyframe, effect = true): string { + if (k.controls) return cameraLabel(model, k); + return [ + k.power === false ? "off" : null, + k.intensity !== undefined ? `${Math.round(k.intensity * 100)} %` : null, + k.kelvin !== undefined ? `${k.kelvin} K` : null, + k.hue !== undefined ? `${Math.round(k.hue)}°` : null, + k.effect !== undefined && (effect || !k.effect.id) ? (k.effect.id ? effectName(k.effect.id) : "fx off") : null, + ].filter(Boolean).join(" · "); +}