forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

pro_trim.rs183 lines, 6.9 KiBHistoryRawDownload
1//! Roll, slip and slide: the three trims that move *where* an edit falls
2//! without changing how long the timeline is. The pointer makes them with a
3//! shift-drag (an edge for a roll, the clip for a slip, shift+alt on the clip
4//! for a slide) and the keyboard with frame detents ([`Player::pro_nudge`]);
5//! both reach the engine through the same [`Player::pro_apply`], one undo step
6//! each.
7
8use crate::*;
9
10impl Player {
11 /// The clip a roll at `idx` is made on -- the one *before* the cut -- when
12 /// the subject clip is `idx`: the cut after it if one abuts, else the cut
13 /// before it.
14 fn roll_anchor(&self, lane: Lane, idx: usize) -> Option<usize> {
15 let session = self.session.as_ref()?;
16 match (session.roll_room(lane, idx), idx.checked_sub(1)) {
17 (Some(_), _) => Some(idx),
18 (None, Some(before)) => session.roll_room(lane, before).map(|_| before),
19 _ => None,
20 }
21 }
22
23 /// Makes one pro trim of `by` timeline frames on the clip at `idx`. A
24 /// roll's `idx` is the clip before its cut. Says why in the notices when the
25 /// engine refuses, and returns whether anything changed -- so a drag
26 /// let go where it began, or pressed against a media wall, takes no undo
27 /// step and leaves the project looking unedited.
28 pub(crate) fn pro_apply(&mut self, kind: ProKind, lane: Lane, idx: usize, by: i64) -> bool {
29 let Some(session) = self.session.as_mut() else {
30 return false;
31 };
32 let changed = match kind {
33 ProKind::Roll => {
34 let Some(cut) = session.lane_clips(lane).get(idx).map(|c| c.end()) else {
35 return false;
36 };
37 let to = (i64::from(cut) + by).max(0) as u32;
38 match session.roll_clip(lane, idx, to) {
39 Some(at) => at != cut,
40 None => {
41 self.notify_user("Not rolled — there is no cut here".into());
42 return false;
43 }
44 }
45 }
46 ProKind::Slide => {
47 let Some(start) = session.lane_clips(lane).get(idx).map(|c| c.start) else {
48 return false;
49 };
50 let to = (i64::from(start) + by).max(0) as u32;
51 match session.slide_clip(lane, idx, to) {
52 Some(at) => at != start,
53 None => {
54 self.notify_user("Not slid — it needs a clip on each side".into());
55 return false;
56 }
57 }
58 }
59 ProKind::Slip => {
60 let Some(source) = session.slip_source_delta(lane, idx, by) else {
61 self.notify_user("Not slipped — a still has no more footage".into());
62 return false;
63 };
64 match session.slip_clip(lane, idx, source) {
65 Some(moved) => moved != 0,
66 None => {
67 self.notify_user("Not slipped — a still has no more footage".into());
68 return false;
69 }
70 }
71 }
72 };
73 if changed {
74 self.mark_dirty();
75 self.reset_after_reseek();
76 }
77 changed
78 }
79
80 /// The keys: one frame of roll, slip or slide on the subject clip.
81 pub(crate) fn pro_nudge(&mut self, kind: ProKind, dir: i64, cx: &mut Context<Self>) {
82 let Some((lane, idx)) = self.selected.anchor() else {
83 return;
84 };
85 let at = match kind {
86 ProKind::Roll => self.roll_anchor(lane, idx),
87 _ => Some(idx),
88 };
89 let Some(at) = at else {
90 self.notify_user("Not rolled — there is no cut here".into());
91 cx.notify();
92 return;
93 };
94 self.pro_apply(kind, lane, at, dir);
95 cx.notify();
96 }
97
98 /// A shift-press on a clip: starts the drag. `edge` is `Some` on an edge
99 /// strip (a roll of the cut at that edge) and `None` on the body (a slip,
100 /// or a slide with alt). The lock is asked first, like every edit door.
101 pub(crate) fn start_pro(
102 &mut self,
103 lane: Lane,
104 idx: usize,
105 kind: ProKind,
106 edge: Option<Edge>,
107 x: Pixels,
108 cx: &mut Context<Self>,
109 ) {
110 if self.covers_room()
111 || self.exporting().is_some()
112 || lane.kind == LaneKind::Subtitle
113 || self.lane_locked_refusal(lane)
114 {
115 return;
116 }
117 // A roll names the clip before its cut: the strip's own clip at its
118 // out edge, the one ahead of it at its in edge.
119 let at = match (kind, edge) {
120 (ProKind::Roll, Some(Edge::Start)) => match idx.checked_sub(1) {
121 Some(before) => before,
122 None => return,
123 },
124 _ => idx,
125 };
126 if kind == ProKind::Roll
127 && self
128 .session
129 .as_ref()
130 .is_none_or(|session| session.roll_room(lane, at).is_none())
131 {
132 self.notify_user("Not rolled — there is no cut here".into());
133 cx.notify();
134 return;
135 }
136 self.pick((lane, idx), false, cx);
137 self.pro_drag = Some(ProDrag {
138 lane,
139 idx: at,
140 kind,
141 from: self.frame_under(x),
142 by: 0,
143 });
144 cx.notify();
145 }
146
147 /// Follows the pointer: the frames moved, held inside the room a roll or a
148 /// slide has (a slip is clamped by the engine at the release).
149 pub(crate) fn pro_to(&mut self, x: Pixels, cx: &mut Context<Self>) {
150 let Some(drag) = self.pro_drag else {
151 return;
152 };
153 let by = i64::from(self.frame_under(x)) - i64::from(drag.from);
154 let by = self.session.as_ref().map_or(0, |session| {
155 let clip = session.lane_clips(drag.lane).get(drag.idx);
156 match (drag.kind, clip) {
157 (ProKind::Roll, Some(c)) => session
158 .roll_room(drag.lane, drag.idx)
159 .map_or(0, |(lo, hi)| i64::from(pro_target(c.end(), by, lo, hi)) - i64::from(c.end())),
160 (ProKind::Slide, Some(c)) => session
161 .slide_room(drag.lane, drag.idx)
162 .map_or(0, |(lo, hi)| i64::from(pro_target(c.start, by, lo, hi)) - i64::from(c.start)),
163 _ => by,
164 }
165 });
166 if by != drag.by {
167 self.pro_drag = Some(ProDrag { by, ..drag });
168 cx.notify();
169 }
170 }
171
172 /// The release: the whole drag reaches the engine as one edit.
173 pub(crate) fn commit_pro(&mut self, cx: &mut Context<Self>) {
174 let Some(drag) = self.pro_drag.take() else {
175 return;
176 };
177 self.hover_hint = None;
178 if drag.by != 0 {
179 self.pro_apply(drag.kind, drag.lane, drag.idx, drag.by);
180 }
181 cx.notify();
182 }
183}