Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

TL3g: in progress: compound clips in sdk/timeline: the picked clips made one clip that holds them (each with the kind of track it came from), moved and trimmed as one, and put back where the compound is; 55 tests

shinyobjectz661adee3962d
diff --git a/sdk/timeline/src/model.ts b/sdk/timeline/src/model.ts
index 2fc3da28..d6f0a76f 100644
--- a/sdk/timeline/src/model.ts
+++ b/sdk/timeline/src/model.ts
@@ -67,6 +67,9 @@ export interface Clip {
at?: string
/** markers on the clip itself, in frames from its start (TL2e) */
markers?: Marker[]
+ /** a compound clip (TL3g): the clips it holds, each with the kind of track it came from, their starts in frames
+ * from this clip's own start */
+ inside?: { kind: TrackKind; clip: Clip }[]
}
export interface Track {
diff --git a/sdk/timeline/src/ops.ts b/sdk/timeline/src/ops.ts
index be6bde43..c149e07b 100644
--- a/sdk/timeline/src/ops.ts
+++ b/sdk/timeline/src/ops.ts
@@ -19,6 +19,10 @@ export type Op =
/** what is left (Q) or right (W) of `at` on a clip removed, the gap closed (TL3e) */
| { op: "cut"; id: string; side: "left" | "right"; at: Frames }
| { op: "duplicate"; ids: string[] }
+ /** the clips made one compound clip, where the first of them on the main track (else the first) was (TL3g) */
+ | { op: "compound"; ids: string[]; id?: string; name?: string }
+ /** a compound clip put back as the clips it holds */
+ | { op: "uncompound"; id: string }
/** a clip put on the timeline: on `track`, else the first of `kind` with room, else a new track of that kind */
| { op: "add"; kind: TrackKind; clip: Clip; track?: string }
/** a clip's own fields; `props` are merged into its props; a speed or a curve changes its length (TL6) */
@@ -187,6 +191,25 @@ function run(t: Timeline, op: Exclude<Op, { op: "restore" }>): Timeline {
}
return next
}
+ case "compound": {
+ const members = op.ids.map((id) => open(t, id))
+ if (members.length < 2) no("a compound clip is made of two clips or more")
+ const from = Math.min(...members.map((m) => m.clip.start)), to = Math.max(...members.map((m) => end(m.clip)))
+ const home = (members.find((m) => isMain(m.track)) ?? members[0]).track
+ const ids = new Set(op.ids)
+ const id = op.id ?? fresh(t, `compound-${members[0].clip.id}`)
+ if (find(t, id)) no(`there is already a clip ${id}`)
+ const made: Clip = { id, kind: "compound", name: op.name ?? "Compound clip", start: from, length: to - from, inside: members.map((m) => ({ kind: m.track.kind, clip: { ...m.clip, start: m.clip.start - from } })) }
+ return place(withTracks(t, (k) => ({ ...k, clips: k.clips.filter((c) => !ids.has(c.id)) })), home.kind, made, home.id)
+ }
+ case "uncompound": {
+ const { clip } = open(t, op.id)
+ if (!clip.inside?.length) no(`${clip.name ?? clip.id} isn't a compound clip`)
+ // each clip back where it sat in the compound, on a track of the kind it came from
+ let next = withTracks(t, (k) => ({ ...k, clips: k.clips.filter((c) => c.id !== clip.id) }))
+ for (const m of clip.inside!) next = place(next, m.kind, { ...m.clip, start: clip.start + m.clip.start })
+ return next
+ }
case "add": {
if (find(t, op.clip.id)) no(`there is already a clip ${op.clip.id}`)
if (!(op.clip.length >= 1) || op.clip.start < 0) no("a clip is at least one frame long and starts in the film")
diff --git a/sdk/timeline/test/ops.test.ts b/sdk/timeline/test/ops.test.ts
index f105b46e..6bfea367 100644
--- a/sdk/timeline/test/ops.test.ts
+++ b/sdk/timeline/test/ops.test.ts
@@ -28,6 +28,7 @@ describe("TL0b: an operation is a pure function with an inverse", () => {
{ op: "remove", ids: ["a"] },
{ op: "cut", id: "b", side: "left", at: 50 },
{ op: "duplicate", ids: ["c"] },
+ { op: "compound", ids: ["a", "title"] },
{ op: "set", id: "b", set: { speed: 2 } },
{ op: "key", id: "b", prop: "scale", at: 10, value: 1.2 },
{ op: "mark", marker: { id: "m1", at: 45 } },
@@ -109,6 +110,24 @@ describe("TL3: clips", () => {
})
})
+describe("TL3g: compound clips", () => {
+ test("clips made one compound clip, and put back as they were", () => {
+ const t = film()
+ const one = done(t, { op: "compound", ids: ["b", "title"], id: "open", name: "Opening" })
+ expect(spans(one, "main")).toEqual(["a 0-30", "open 30-90", "c 90-120"])
+ expect(spans(one, "text-1")).toEqual([])
+ expect(at(one, "open").inside!.map((m) => `${m.kind} ${m.clip.id} ${m.clip.start}+${m.clip.length}`)).toEqual(["main b 0+60", "text title 10+30"])
+ // it moves as one clip; unpacked there, what it held is where the compound is
+ const moved = done(one, { op: "move", ids: ["open"], by: -50 })
+ expect(spans(moved, "main")).toEqual(["open 0-60", "a 60-90", "c 90-120"])
+ const back = done(moved, { op: "uncompound", id: "open" })
+ expect(spans(back, "main")).toEqual(["b 0-60", "a 60-90", "c 90-120"])
+ expect(spans(back, "text-1")).toEqual(["title 10-40"])
+ expect(apply(t, { op: "compound", ids: ["a"] })).toEqual({ ok: false, why: "a compound clip is made of two clips or more" })
+ expect(apply(t, { op: "uncompound", id: "a" })).toEqual({ ok: false, why: "a isn't a compound clip" })
+ })
+})
+
describe("TL4: the magnet and linkage", () => {
test("with the magnet on the main track has no gap: a removed clip's gap closes", () => {
const t = done(film(), { op: "remove", ids: ["a"] })