From d1444454fe7b735179f69a0b8c2aa28da1ccfdcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 6 Nov 2026 18:10:06 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Regroup=20keeps=20the=20right=20?= =?UTF-8?q?half's=20keys,=20ramp=20and=20end=20edge;=20MCP=20regroup=20nam?= =?UTF-8?q?es=20its=20lanes=20and=20copy=5Fpaste=20says=20a=20paste=20past?= =?UTF-8?q?=20the=20end=20appends?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MCP.md | 2 +- crates/engine/src/project.rs | 103 ++++++++++++++++++++++++++++++----- crates/mcp/src/tools/edit.rs | 40 ++++++++++++-- 3 files changed, 126 insertions(+), 19 deletions(-) diff --git a/MCP.md b/MCP.md index 0537801..ffdcf23 100644 --- a/MCP.md +++ b/MCP.md @@ -159,7 +159,7 @@ in the GUI. Ops take `scope: "group" | "clip"` where it matters. | `lift`, `fade`, `dissolve`, `transition`, `crossfade`, `gain`, `color`, `transform`, `fit`, `eq`, `sub_trim`, `sub_delete`, `cue_text` | clip | act on the one clip named | | `split` | all lanes (or `lanes`) | a cut, not a ripple: nothing moves. `lanes` limits which lanes are cut; a linked partner follows its clip | | `regroup` | all lanes | `regroup {at}` rejoins the clips that meet at `at` and groups them again: the inverse of `split`, metadata only | -| `copy_paste` | the copied clips | `copy_paste {clips:[addr…], at, lane?}`: one clip pastes as the GUI paste does (splits and ripples the room open); several, or any with `lane`, land as one set and are refused whole if they would overlap. Copies that were one take (a picture and its sound) come back as one new take; a copy whose partner was not copied comes back unlinked. A `lane` override keeps a take only where its members land on different lanes | +| `copy_paste` | the copied clips | `copy_paste {clips:[addr…], at, lane?}`: one clip pastes as the GUI paste does (splits and ripples the room open); several, or any with `lane`, land as one set and are refused whole if they would overlap. A paste past the end appends at the end, and the reply says where the copies landed. Copies that were one take (a picture and its sound) come back as one new take; a copy whose partner was not copied comes back unlinked. A `lane` override keeps a take only where its members land on different lanes | | `close_gap` | the gap's take | `close_gap {lane, at}` closes the one gap on `lane` that `at` stands in (the GUI's Close gap); a linked take's other lane closes with it. Captions use `close_gaps` | | `solo` | — | `solo {lane, on}` for an audio lane: monitoring only, not project state, not saved, never exported, no undo step | | `close_gaps` | one lane | closes the gaps on that lane only | diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 5449f3d..4a4cc65 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -7971,7 +7971,9 @@ impl Project { /// so the clip list comes back exactly as it was and traversal with it. /// `false` when no lane has such a pair. The rejoined placement keeps the /// **left** half's id, which is the take the split left on it: rejoining a - /// cut take puts the take back, id and all. + /// cut take puts the take back, id and all. Its animation is both halves' + /// keys merged, so the right half's keys after the cut come back with it; a + /// lane whose merged animation would not fit is left as it is. pub fn regroup(&mut self, timeline_frame: u32) -> bool { // What each lane would end up covering, if it joins here at all. let joined: Vec> = self @@ -8010,10 +8012,10 @@ impl Project { let Some(idx) = joinable(&data.clips, timeline_frame) else { continue; }; - match data.clips[idx].reversed { - true => data.clips[idx].in_frame = data.clips[idx + 1].in_frame, - false => data.clips[idx].out_frame = data.clips[idx + 1].out_frame, - } + let Some(joined) = joined_clip(&data.clips[idx], &data.clips[idx + 1]) else { + continue; + }; + data.clips[idx] = joined; data.clips.remove(idx + 1); } true @@ -11549,24 +11551,97 @@ pub fn ramp_keys_until(keys: &[SpeedKey], cut: u32) -> Vec { fn joinable(clips: &[Clip], frame: u32) -> Option { let idx = clips.iter().position(|c| c.end() == frame)?; let next = clips.get(idx + 1)?; - let joined = clips[idx].len() + next.len(); + let left = &clips[idx]; (next.start == frame - && next.source == clips[idx].source - && next.reversed == clips[idx].reversed - && match clips[idx].reversed { + && next.source == left.source + && next.reversed == left.reversed + && match left.reversed { // The halves of a reversed cut swap sides: the head holds the window's // upper part. - true => clips[idx].in_frame == next.out_frame, - false => next.in_frame == clips[idx].out_frame, + true => left.in_frame == next.out_frame, + false => next.in_frame == left.out_frame, } // ...at one rate, and one that puts the rejoined clip back in exactly // the room the two took up: what a split could have produced, which is - // all this undoes. - && next.speed == clips[idx].speed - && clips[idx].speed.frames(joined) == clips[idx].frames() + next.frames()) + // all this undoes. A ramp is held to the same test, on its own keys. + && next.speed == left.speed + && joined_clip(left, next).is_some_and(|j| j.frames() == left.frames() + next.frames())) .then_some(idx) } +/// The clip that two halves of one cut rejoin into: `left` extended to the end of +/// `right`, with the animation both halves hold merged ([`merge_keys`]) and the +/// right half's end edge kept (its `fade_out` and dissolve), which a split takes +/// away from the left half. The speed ramp is [`joined_speed_keys`]. `None` when +/// the merged animation would not fit or is not a ramp the engine plays. +fn joined_clip(left: &Clip, right: &Clip) -> Option { + let mut out = left.clone(); + out.speed_keys = joined_speed_keys(left, right)?; + out.transform_keys = + merge_keys(&left.transform_keys, &right.transform_keys, |k| k.at, MAX_TRANSFORM_KEYS)?; + out.opacity_keys = + merge_keys(&left.opacity_keys, &right.opacity_keys, |k| k.at, MAX_TRANSFORM_KEYS)?; + out.color_keys = merge_keys(&left.color_keys, &right.color_keys, |k| k.at, MAX_COLOR_KEYS)?; + out.gain_keys = merge_keys(&left.gain_keys, &right.gain_keys, |k| k.at, MAX_GAIN_KEYS)?; + // Effects are matched by stack slot, and only when both halves carry the same + // stack; otherwise the left half's effects stand as they are. + let same_stack = left.effects.len() == right.effects.len() + && left.effects.iter().zip(&right.effects).all(|(l, r)| l.kind == r.kind); + if same_stack { + for (e, r) in out.effects.iter_mut().zip(&right.effects) { + e.keys = merge_keys(&e.keys, &r.keys, |k| k.at, MAX_TRANSFORM_KEYS)?; + } + } + out.fade_out = right.fade_out; + out.transition_out = right.transition_out; + out.transition_kind = right.transition_kind; + match left.reversed { + true => out.in_frame = right.in_frame, + false => out.out_frame = right.out_frame, + } + Some(out) +} + +/// The keys of one animation, both halves of a cut joined: the left half's keys, +/// then the right half's keys whose source frame the left does not already hold +/// (the cut is a key on both halves, so it is kept once), sorted by frame. Keys +/// are source frames on both sides, so the union is the same whichever half +/// comes first. `None` when the union holds more than `cap` keys. +fn merge_keys( + left: &[K], + right: &[K], + at: impl Fn(&K) -> u32, + cap: usize, +) -> Option> { + let mut out = left.to_vec(); + out.extend(right.iter().filter(|r| left.iter().all(|l| at(l) != at(r))).cloned()); + out.sort_by_key(|k| at(k)); + (out.len() <= cap).then_some(out) +} + +/// The speed ramp of two rejoined halves ([`Clip::speed_keys`]). Ramp keys count +/// clip-relative timeline frames, so the right half's keys move on by the left +/// half's timeline length and the left's last key and the right's first are the +/// same cut, kept once. Two constant halves have no ramp; one ramped half and one +/// constant half, or a reversed ramp (which no split makes), do not join. `None` +/// when the joined keys are not a ramp the engine plays. +fn joined_speed_keys(left: &Clip, right: &Clip) -> Option> { + match (left.speed_keys.is_empty(), right.speed_keys.is_empty()) { + (true, true) => return Some(Vec::new()), + (false, false) if !left.reversed => {} + _ => return None, + } + let off = left.frames(); + let mut keys = left.speed_keys.clone(); + for k in &right.speed_keys { + let at = k.at + off; + if keys.last().map_or(true, |l| l.at < at) { + keys.push(SpeedKey { at, speed: k.speed }); + } + } + valid_speed_keys(&keys).then_some(keys) +} + /// The lane invariant: sorted by `start`, no two placements overlapping, no /// empty placement. Checked at every constructor and asserted after every /// mutation -- the offset model is only tractable while it holds. diff --git a/crates/mcp/src/tools/edit.rs b/crates/mcp/src/tools/edit.rs index 31b5e1b..67a28f0 100644 --- a/crates/mcp/src/tools/edit.rs +++ b/crates/mcp/src/tools/edit.rs @@ -98,7 +98,7 @@ TIME speed {clip, x, ripple?, lanes?} — rate 0.25..4. [group] Its length changes, so a neighbour in its way refuses; ripple:true moves the later items in the RIPPLE RULE scope (titles too) by the length change: slower pushes them later, faster closes the gap. A clip on another lane that spans the change is never cut: outside the scope it is left in place and the reply names it; a scope lane (named in lanes) that holds one refuses, naming it. One undo step. speed_ramp {clip, keys:[{at, speed}...], ripple?, lanes?} [group] — a speed that changes over the clip: each key sets a speed 0.25–4 at a clip-relative time `at` ("0s", "2s", "end" or frames), linear between keys and held before the first and after the last. Keys must be in increasing `at`, at most 8, and keys:[] clears the ramp back to the clip's constant speed. The clip's length follows the ramp, and ripple:true moves the later items as speed does. preset:"speed_up"|"slow_down"|"fast_middle"|"slow_middle" lays one of the speed card's shapes across the clip's length instead of keys (speed_up: 1x to 2x; slow_down: 2x to 1x; fast_middle: slow–fast–slow; slow_middle: fast–slow–fast); not with keys. regroup {at} — rejoins the clips that meet at `at` in every lane and puts them back in one group: the inverse of split (metadata only, one undo step; refused when no cut sits there). -copy_paste {clips:[addr,...], at, lane?} — pastes copies of the named clips at `at`, with their effects, keys and volume, keeping their spacing. One clip splits and ripples the room open as the GUI's paste does; several (or any with `lane`) land as one set on their own lanes, refused whole when a landing would overlap. `lane` puts every copy on that lane (the same kind, V or A, as each copy). Links come back among the copies: copied clips that were one take (a picture and its sound, say) land as one new take, while a clip whose partner was not copied comes back unlinked. A set with a `lane` override keeps a take only where its members land on different lanes. +copy_paste {clips:[addr,...], at, lane?} — pastes copies of the named clips at `at`, with their effects, keys and volume, keeping their spacing. A paste past the end appends at the end, and the reply says where the copies landed. One clip splits and ripples the room open as the GUI's paste does; several (or any with `lane`) land as one set on their own lanes, refused whole when a landing would overlap. `lane` puts every copy on that lane (the same kind, V or A, as each copy). Links come back among the copies: copied clips that were one take (a picture and its sound, say) land as one new take, while a clip whose partner was not copied comes back unlinked. A set with a `lane` override keeps a take only where its members land on different lanes. close_gap {lane, at} — closes the one gap on `lane` that `at` stands in (the GUI's Close gap): a gap that borders a linked take closes with the take's other lane too; nothing else moves. Refused for a clip at `at`, the time past the lane's last clip, and the empty lead-in before its first clip. Captions use close_gaps. solo {lane, on} — solos an audio lane for listening: while any is soloed, the other audio lanes are silent in this session's playback. It is monitoring only: not project state, not saved, never exported, and it leaves no undo step. reverse {clip, on?} — the clip plays backward, picture and sound: on true/false, or omit `on` to toggle. [group] Its length and place do not change, so nothing ripples, and it composes with speed and a speed ramp. Fades stay on the timeline's ends and gain and transform keys keep their time on the timeline. A trim, split or join follows the footage (a reversed clip's start edge trims its source's end). A still refuses. One undo step. clip_info and timeline say "reversed". Limits: a reversed clip decodes at 8 bits (a 10-bit source loses its low 2 bits on a 10-bit export; banding is possible on HDR), hardware decode is off for it, a dissolve's B side plays forward, and waveforms and thumbnails draw forward. @@ -4809,17 +4809,38 @@ fn op_close_gap(s: &mut PlaybackSession, op: &Value, fps: f64, end: u32) -> Resu fn op_regroup(s: &mut PlaybackSession, op: &Value, fps: f64, end: u32) -> Result { let at = need(op, "at", fps, end)?; let shown = shown_time(op.get("at"), at); + // A rejoin takes one placement out of each lane it joins, and the joined one no longer starts at + // `at`, so the lanes are the ones that lost a placement, counted before and after. + let before: Vec<(Lane, usize)> = s.lanes().into_iter().map(|l| (l, placements(s, l))).collect(); if !s.regroup_at(parse::secs(at, fps)) { return Err(format!( "regroup {shown}: nothing to rejoin there; no cut sits at that frame, or the clips on it were never split apart" )); } - let lanes: Vec = s.lanes().into_iter().filter(|&l| s.lane_clips(l).iter().any(|c| c.start == at)).collect(); - let touched = lanes.iter().map(|&l| (l, at)).collect(); + let mut lanes: Vec = Vec::new(); + let mut touched: Vec<(Lane, u32)> = Vec::new(); + for (l, n) in before { + if placements(s, l) >= n { + continue; + } + // The rejoined placement is the one that now covers the cut, and its new address is what a reply names. + let start = if l.kind == LaneKind::Subtitle { + s.sub_lane(l).iter().find(|c| c.start < at && at < c.start + c.frames).map(|c| c.start) + } else { + s.lane_clips(l).iter().find(|c| c.start < at && at < c.end()).map(|c| c.start) + }; + lanes.push(l); + touched.extend(start.map(|st| (l, st))); + } let names: Vec = lanes.iter().map(|&l| parse::lane_name(l)).collect(); Ok(Done { text: format!("regroup {shown}: the cut is rejoined on {}", names.join(", ")), touched, lanes }) } +/// How many placements a lane holds: its clips, or its captions for a subtitle lane. +fn placements(s: &PlaybackSession, lane: Lane) -> usize { + if lane.kind == LaneKind::Subtitle { s.sub_lane(lane).len() } else { s.lane_clips(lane).len() } +} + /// `solo {lane, on}`: solos an audio lane for listening, so only the soloed lanes are heard while this /// session plays. Solo is monitoring only: it is not project state, it is not saved, and an export never /// reads it, so it leaves no undo step and marks nothing changed. @@ -4937,7 +4958,18 @@ fn op_copy_paste(s: &mut PlaybackSession, op: &Value, fps: f64, end: u32) -> Res } let addrs: Vec = landed.iter().map(|&(l, start)| parse::addr(l, start)).collect(); let effects = side_effects(&shape_before, &shape(s), "copy_paste"); - let mut text = format!("copy_paste {} clip(s) at {shown}: pasted {}", items.len(), addrs.join(", ")); + // A paste past the end is clamped to the end, so the copies append there; say so rather than + // print an asked frame the copies did not land on. + let mut text = if at > end { + format!( + "copy_paste {} clip(s) at {shown} is past the end, so the copies append at {:.3}s ({end}f): pasted {}", + items.len(), + parse::secs(end, fps), + addrs.join(", ") + ) + } else { + format!("copy_paste {} clip(s) at {shown}: pasted {}", items.len(), addrs.join(", ")) + }; if !effects.is_empty() { text.push_str(&format!("; effects: {}", effects.join("; "))); }