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.

TL4a: in progress: a dragged main-track clip swaps by the edge it leads with (past its neighbour's middle), as CapCut's does; 37 tests

shinyobjectzc78ded29e8f0
diff --git a/sdk/timeline/src/ops.ts b/sdk/timeline/src/ops.ts
index 1f9c69a4..4febe0b4 100644
--- a/sdk/timeline/src/ops.ts
+++ b/sdk/timeline/src/ops.ts
@@ -283,7 +283,7 @@ function halves(c: Clip, at: Frames, id: string): [Clip, Clip] {
function settle(was: Timeline, next: Timeline, op: Op): Timeline {
const named = new Set("ids" in op && op.ids ? op.ids : "id" in op && typeof op.id === "string" ? [op.id] : [])
let t = next
- if (t.magnet) t = withTracks(t, (k) => (isMain(k) ? { ...k, clips: close(k.clips) } : k))
+ if (t.magnet) t = withTracks(t, (k) => (isMain(k) ? { ...k, clips: close(k.clips, op.op === "move" ? was : undefined, named) } : k))
if (was.linkage && t.linkage && op.op !== "option") t = follow(was, t, op, named)
t = { ...t, tracks: t.tracks.filter((k) => k.clips.length || was.tracks.some((o) => o.id === k.id)).map((k) => ({ ...k, clips: [...k.clips].sort((a, b) => a.start - b.start) })) }
const cut = (x: Transition) => {
@@ -293,11 +293,15 @@ function settle(was: Timeline, next: Timeline, op: Op): Timeline {
return t.transitions.every(cut) ? t : { ...t, transitions: t.transitions.filter(cut) }
}
-/** The main track's clips with no gap, from frame 0. A clip the operation moved takes its place by its middle, so
- * it swaps with a neighbour once it is dragged past that neighbour's middle (TL4a). */
-function close(clips: Clip[]): Clip[] {
- // (clips that don't overlap are in the same order by their middles as by their starts)
- const key = (c: Clip) => c.start + c.length / 2
+/** The main track's clips with no gap, from frame 0, in the order of their middles. A clip being moved takes its
+ * place by the edge it leads with, so it swaps with a neighbour once that edge is dragged past the neighbour's
+ * middle (TL4a), as CapCut's main track does. */
+function close(clips: Clip[], was: Timeline | undefined, named: Set<string>): Clip[] {
+ const key = (c: Clip) => {
+ const from = was && named.has(c.id) ? find(was, c.id)?.clip.start : undefined
+ if (from === undefined || from === c.start) return c.start + c.length / 2
+ return c.start > from ? end(c) : c.start
+ }
let at = 0
return [...clips]
.sort((a, b) => key(a) - key(b))
diff --git a/sdk/timeline/test/ops.test.ts b/sdk/timeline/test/ops.test.ts
index 3740d31f..a60c52cb 100644
--- a/sdk/timeline/test/ops.test.ts
+++ b/sdk/timeline/test/ops.test.ts
@@ -117,9 +117,12 @@ describe("TL4: the magnet and linkage", () => {
})
test("a clip dragged past its neighbour's middle swaps places with it", () => {
- // a (0-30) dragged 40 later: its middle (55) is short of b's (60), so nothing swaps; 50 later it is past
- expect(spans(done(film(), { op: "move", ids: ["a"], by: 40 }), "main")).toEqual(["a 0-30", "b 30-90", "c 90-120"])
- expect(spans(done(film(), { op: "move", ids: ["a"], by: 50 }), "main")).toEqual(["b 0-60", "a 60-90", "c 90-120"])
+ // a (0-30) dragged 25 later: its leading edge (55) is short of b's middle (60), so nothing swaps; at 35 it is past
+ expect(spans(done(film(), { op: "move", ids: ["a"], by: 25 }), "main")).toEqual(["a 0-30", "b 30-90", "c 90-120"])
+ expect(spans(done(film(), { op: "move", ids: ["a"], by: 35 }), "main")).toEqual(["b 0-60", "a 60-90", "c 90-120"])
+ // and back: c (90-120) dragged earlier swaps once its start passes b's middle
+ expect(spans(done(film(), { op: "move", ids: ["c"], by: -25 }), "main")).toEqual(["a 0-30", "b 30-90", "c 90-120"])
+ expect(spans(done(film(), { op: "move", ids: ["c"], by: -35 }), "main")).toEqual(["a 0-30", "c 30-60", "b 60-120"])
})
test("with the magnet off a gap is allowed, and a clip dropped over another goes to a track above", () => {