edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching·0 forks·Rust·18.3 MiB·ff99ad6·Tahinli·6 days ago·1055 commits
| 1 | //! The effects card's side of the window: opening it on a picture clip and the writes |
| 2 | //! behind its rows. Every discrete act (add, switch, move, remove, a choice) is one |
| 3 | //! [`engine::PlaybackSession::set_effects`] call and so one undo step; a slider drag is |
| 4 | //! held as a draft the card shows and written once when the hand lets go. |
| 5 | |
| 6 | use crate::*; |
| 7 | use crate::player::cards::{bar_fraction, value_at}; |
| 8 | use engine::effects::{BlendMode, Effect, EffectKey, EffectKind, MAX_EFFECTS, MAX_PARAMS, ParamSpec}; |
| 9 | use engine::transform::KeyEase; |
| 10 | |
| 11 | /// How a parameter is drawn: a bar, an on/off switch (a step of 1 over 0..1), or the |
| 12 | /// blend mode's choice of words. |
| 13 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 14 | pub(crate) enum ParamShape { |
| 15 | Bar, |
| 16 | Switch, |
| 17 | Mode, |
| 18 | } |
| 19 | |
| 20 | pub(crate) fn param_shape(kind: EffectKind, spec: &ParamSpec) -> ParamShape { |
| 21 | if kind == EffectKind::Blend && spec.name == "mode" { |
| 22 | ParamShape::Mode |
| 23 | } else if spec.min == 0. && spec.max == 1. && spec.step == 1. { |
| 24 | ParamShape::Switch |
| 25 | } else { |
| 26 | ParamShape::Bar |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /// A value on its slider's grid: the step the spec names, clamped to its range. |
| 31 | pub(crate) fn snap_param(spec: &ParamSpec, value: f32) -> f32 { |
| 32 | let steps = ((value - spec.min) / spec.step).round(); |
| 33 | (spec.min + steps * spec.step).clamp(spec.min, spec.max) |
| 34 | } |
| 35 | |
| 36 | |
| 37 | /// The unit a bar's number reads in. |
| 38 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 39 | pub(crate) enum ParamUnit { |
| 40 | /// Whole pixels: blur radius, camera shake distance. |
| 41 | Px, |
| 42 | /// Whole percent of the stored fraction: amounts, softness, mask edges, glow. |
| 43 | Percent, |
| 44 | /// A multiplier, `1.00×` at the default: sharpen amount. |
| 45 | Times, |
| 46 | /// A rate: camera shake speed, a number of times a second. |
| 47 | PerSecond, |
| 48 | /// A bare whole number. |
| 49 | Plain, |
| 50 | } |
| 51 | |
| 52 | impl ParamUnit { |
| 53 | /// The factor from the stored value to the number the card reads and types (a percent reads |
| 54 | /// 100 for 1). |
| 55 | pub(crate) fn scale(self) -> f32 { |
| 56 | match self { |
| 57 | Self::Percent => 100., |
| 58 | _ => 1., |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /// The unit a parameter's number is shown in. |
| 64 | pub(crate) fn param_unit(kind: EffectKind, spec: &ParamSpec) -> ParamUnit { |
| 65 | match (kind, spec.name) { |
| 66 | (EffectKind::Blur, "radius") | (EffectKind::Shake, "amount") | (EffectKind::Pixelate, "size") => ParamUnit::Px, |
| 67 | (EffectKind::Shake, "speed") => ParamUnit::PerSecond, |
| 68 | (EffectKind::Sharpen, "amount") => ParamUnit::Times, |
| 69 | _ if spec.max <= 3. => ParamUnit::Percent, |
| 70 | _ => ParamUnit::Plain, |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// A value as the card shows it, with its unit (see [`ParamUnit`]). |
| 75 | pub(crate) fn param_display(kind: EffectKind, spec: &ParamSpec, value: f32) -> String { |
| 76 | match param_unit(kind, spec) { |
| 77 | ParamUnit::Px => format!("{} px", value.round()), |
| 78 | ParamUnit::Percent => format!("{}%", (value * 100.).round()), |
| 79 | ParamUnit::Times => format!("{value:.2}×"), |
| 80 | ParamUnit::PerSecond => format!("{value:.1} per second"), |
| 81 | ParamUnit::Plain => format!("{}", value.round()), |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// The plain word for a blend mode. |
| 86 | /// The key an effect holds at source frame `at`: every parameter as the effect shows it there, |
| 87 | /// as a key of its own. |
| 88 | fn effect_key_at(e: &Effect, at: u32) -> EffectKey { |
| 89 | let values = e.posed(f64::from(at)).values; |
| 90 | EffectKey { at, values, ease: KeyEase::Linear } |
| 91 | } |
| 92 | |
| 93 | /// Writes parameter `p` of `e`. A keyed effect takes the change on its key at source frame |
| 94 | /// `at` for a keyable parameter, the key made there from what the effect shows when none is, |
| 95 | /// so an edit at the playhead animates; a switch or a choice, an unkeyed effect, or no playhead |
| 96 | /// takes it as its static value. |
| 97 | fn write_param(e: &mut Effect, p: usize, v: f32, at: Option<u32>) { |
| 98 | let keyable = e.kind.params().get(p).is_some_and(|s| s.keyable()); |
| 99 | match at { |
| 100 | Some(at) if keyable && !e.keys.is_empty() => { |
| 101 | let mut key = match e.keys.iter().position(|k| k.at == at) { |
| 102 | Some(i) => e.keys[i], |
| 103 | None => effect_key_at(e, at), |
| 104 | }; |
| 105 | key.values[p] = v; |
| 106 | match e.keys.binary_search_by_key(&at, |k| k.at) { |
| 107 | Ok(i) => e.keys[i] = key, |
| 108 | Err(i) => e.keys.insert(i, key), |
| 109 | } |
| 110 | } |
| 111 | _ => e.values[p] = v, |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | pub(crate) fn mode_text(mode: BlendMode) -> &'static str { |
| 116 | match mode { |
| 117 | BlendMode::Normal => "Normal", |
| 118 | BlendMode::Multiply => "Multiply", |
| 119 | BlendMode::Screen => "Screen", |
| 120 | BlendMode::Overlay => "Overlay", |
| 121 | BlendMode::Add => "Add", |
| 122 | BlendMode::Darken => "Darken", |
| 123 | BlendMode::Lighten => "Lighten", |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | impl Player { |
| 128 | /// Opens the effects card on the clip whose stack is to change: the selected |
| 129 | /// picture clip, else the one under the playhead -- the transform card's rule. |
| 130 | pub(crate) fn open_effects(&mut self, cx: &mut Context<Self>) { |
| 131 | if self.exporting().is_some() { |
| 132 | return; |
| 133 | } |
| 134 | let Some(session) = &self.session else { |
| 135 | self.notify_user("Nothing to add effects to — no file open".into()); |
| 136 | cx.notify(); |
| 137 | return; |
| 138 | }; |
| 139 | let target = match self.selected.anchor() { |
| 140 | Some((lane, idx)) if lane.kind == LaneKind::Subtitle => { |
| 141 | match caption_media_half(session, (lane, idx), LaneKind::Video) { |
| 142 | Some(half) => Some(half), |
| 143 | None => { |
| 144 | self.notify_user("Nothing to add effects to — a caption has no picture".into()); |
| 145 | cx.notify(); |
| 146 | return; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | Some((lane, _)) if lane.kind == LaneKind::Audio => session.video_clip_at(session.now()), |
| 151 | other => other.or_else(|| session.video_clip_at(session.now())), |
| 152 | }; |
| 153 | match target { |
| 154 | Some(clip) => { |
| 155 | // One card at a time -- `close_card`'s own rule. |
| 156 | self.close_other_cards(); |
| 157 | self.effects_open = Some(clip); |
| 158 | self.effects_mode_open = None; |
| 159 | self.context_menu = None; |
| 160 | } |
| 161 | None => self.notify_user("Nothing to add effects to — no clip under the playhead".into()), |
| 162 | } |
| 163 | cx.notify(); |
| 164 | } |
| 165 | |
| 166 | /// The stack the card shows: the draft while a bar is held, else the clip's own. |
| 167 | pub(crate) fn effects_shown(&self) -> Vec<Effect> { |
| 168 | if let Some(draft) = &self.effects_draft { |
| 169 | return draft.clone(); |
| 170 | } |
| 171 | self.effects_open |
| 172 | .zip(self.session.as_ref()) |
| 173 | .map_or_else(Vec::new, |((lane, idx), s)| s.effects_of(lane, idx).to_vec()) |
| 174 | } |
| 175 | |
| 176 | /// The source frame the playhead shows on the effects card's clip, where a mask key goes; |
| 177 | /// `None` when the playhead is not on the clip. |
| 178 | pub(crate) fn effects_src(&self) -> Option<u32> { |
| 179 | let (lane, idx) = self.effects_open?; |
| 180 | let session = self.session.as_ref()?; |
| 181 | session.key_source_frame(lane, idx, self.key_frame()).or_else(|| { |
| 182 | // The playhead can stand on the clip's end frame, which is not one of its frames: |
| 183 | // the picture holds the last frame there, so the card shows the values of that one, |
| 184 | // as the renderer draws it. |
| 185 | let end = session.lane_clips(lane).get(idx)?.end(); |
| 186 | session.key_source_frame(lane, idx, end.checked_sub(1)?) |
| 187 | }) |
| 188 | } |
| 189 | |
| 190 | /// The keyframe diamond on an effect's row: a key for the effect's parameters at the |
| 191 | /// playhead, or that key taken off. Taking off the last key leaves the values it had there |
| 192 | /// as the static ones (the switches and choices keep theirs). |
| 193 | pub(crate) fn toggle_effect_key(&mut self, i: usize, cx: &mut Context<Self>) { |
| 194 | let Some(at) = self.effects_src() else { |
| 195 | self.notify_user("The playhead is not on this clip".into()); |
| 196 | cx.notify(); |
| 197 | return; |
| 198 | }; |
| 199 | let mut stack = self.effects_shown(); |
| 200 | if let Some(e) = stack.get_mut(i) { |
| 201 | match e.keys.binary_search_by_key(&at, |k| k.at) { |
| 202 | Ok(pos) => { |
| 203 | let key = e.keys.remove(pos); |
| 204 | if e.keys.is_empty() { |
| 205 | let specs = e.kind.params(); |
| 206 | for (j, _) in specs.iter().enumerate().filter(|(_, s)| s.keyable()) { |
| 207 | e.values[j] = key.values[j]; |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | Err(pos) => { |
| 212 | let key = effect_key_at(e, at); |
| 213 | e.keys.insert(pos, key); |
| 214 | } |
| 215 | } |
| 216 | self.write_effects(stack, cx); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /// The ease of the key the effect at stack position `slot` holds at the playhead, `None` with |
| 221 | /// no key there. |
| 222 | pub(crate) fn effect_key_ease_here(&self, slot: usize) -> Option<KeyEase> { |
| 223 | let at = self.effects_src()?; |
| 224 | let shown = self.effects_shown(); |
| 225 | let effect = shown.get(slot)?; |
| 226 | effect.keys.iter().find(|k| k.at == at).map(|k| k.ease) |
| 227 | } |
| 228 | |
| 229 | /// Sets the ease of the key the effect at stack position `slot` holds at the playhead, as one |
| 230 | /// undo step. Nothing changes with no key there, which the easing row shows as disabled. |
| 231 | pub(crate) fn set_effect_key_ease_here(&mut self, slot: usize, ease: KeyEase, cx: &mut Context<Self>) { |
| 232 | let Some(at) = self.effects_src() else { |
| 233 | return cx.notify(); |
| 234 | }; |
| 235 | let mut stack = self.effects_shown(); |
| 236 | if let Some(e) = stack.get_mut(slot) |
| 237 | && let Ok(pos) = e.keys.binary_search_by_key(&at, |k| k.at) |
| 238 | { |
| 239 | e.keys[pos].ease = ease; |
| 240 | self.write_effects(stack, cx); |
| 241 | } |
| 242 | cx.notify(); |
| 243 | } |
| 244 | |
| 245 | /// Follows the mask at stack position `slot` of the open clip from the playhead to the |
| 246 | /// clip's end, or back to its start ([`engine::PlaybackSession::track_mask_start`]). The |
| 247 | /// keys land when the track ends ([`Self::poll_tracks`]). |
| 248 | pub(crate) fn start_track(&mut self, slot: usize, backward: bool, cx: &mut Context<Self>) { |
| 249 | let Some((lane, idx)) = self.effects_open else { |
| 250 | return; |
| 251 | }; |
| 252 | match self.effects_src() { |
| 253 | Some(from) => self.begin_track(lane, idx, slot, from, backward, cx), |
| 254 | None => { |
| 255 | self.notify_user("The playhead is not on this clip".into()); |
| 256 | cx.notify(); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /// Track mask ([`ActionId::TrackMask`]): the selected clip's first rect or ellipse mask, |
| 262 | /// followed from the playhead to the clip's end. |
| 263 | pub(crate) fn track_selected_mask(&mut self, cx: &mut Context<Self>) { |
| 264 | let Some((lane, idx)) = self.selected.anchor() else { |
| 265 | self.notify_user("Track mask: select a video clip".into()); |
| 266 | cx.notify(); |
| 267 | return; |
| 268 | }; |
| 269 | let Some(session) = &self.session else { |
| 270 | return; |
| 271 | }; |
| 272 | let slot = session |
| 273 | .lane_clips(lane) |
| 274 | .get(idx) |
| 275 | .and_then(|c| c.effects.iter().position(|e| matches!(e.kind, EffectKind::RectMask | EffectKind::EllipseMask))); |
| 276 | let from = session.key_source_frame(lane, idx, self.key_frame()); |
| 277 | match (slot, from) { |
| 278 | (Some(slot), Some(from)) => self.begin_track(lane, idx, slot, from, false, cx), |
| 279 | (None, _) => { |
| 280 | self.notify_user("Track mask: the clip has no rect or ellipse mask".into()); |
| 281 | cx.notify(); |
| 282 | } |
| 283 | (_, None) => { |
| 284 | self.notify_user("Track mask: the playhead is not on the clip".into()); |
| 285 | cx.notify(); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /// Starts the track of the mask at `slot` of `lane`/`idx` from source frame `from`. |
| 291 | fn begin_track(&mut self, lane: Lane, idx: usize, slot: usize, from: u32, backward: bool, cx: &mut Context<Self>) { |
| 292 | let Some(session) = &self.session else { |
| 293 | return; |
| 294 | }; |
| 295 | let Some((_, lo, hi)) = session.stabilise_range(lane, idx) else { |
| 296 | return; |
| 297 | }; |
| 298 | let to = if backward { lo } else { hi - 1 }; |
| 299 | match session.track_mask_start(lane, idx, slot, from, to) { |
| 300 | Ok(job) => self.track_jobs.push(job), |
| 301 | Err(e) => self.notify_user(format!("Track: {e}").into()), |
| 302 | } |
| 303 | cx.notify(); |
| 304 | } |
| 305 | |
| 306 | /// Stops the running track of the mask at `slot` on the open clip. |
| 307 | pub(crate) fn cancel_track(&mut self, slot: usize, cx: &mut Context<Self>) { |
| 308 | if let Some((lane, idx)) = self.effects_open { |
| 309 | for job in self.track_jobs.iter().filter(|j| j.lane == lane && j.idx == idx && j.slot == slot) { |
| 310 | engine::track::cancel(job.id); |
| 311 | } |
| 312 | } |
| 313 | cx.notify(); |
| 314 | } |
| 315 | |
| 316 | /// Writes every finished mask track onto its mask, one undo step each, and says how each |
| 317 | /// one ended. |
| 318 | pub(crate) fn poll_tracks(&mut self) { |
| 319 | if self.track_jobs.is_empty() { |
| 320 | return; |
| 321 | } |
| 322 | let done = match &mut self.session { |
| 323 | Some(session) => session.track_settle(&mut self.track_jobs), |
| 324 | None => return, |
| 325 | }; |
| 326 | if done.is_empty() { |
| 327 | return; |
| 328 | } |
| 329 | self.mark_dirty(); |
| 330 | self.reset_after_reseek(); |
| 331 | for d in done { |
| 332 | let text = match d.result { |
| 333 | Ok(o) => match o.lost_at { |
| 334 | Some(f) => format!("Lost {} · {} keys", lost_at_text(f, self.fps), o.keys), |
| 335 | None => format!("Tracked · {} keys", o.keys), |
| 336 | }, |
| 337 | Err(e) if e.contains("cancelled") => "Track cancelled".to_owned(), |
| 338 | Err(_) => "Track failed".to_owned(), |
| 339 | }; |
| 340 | self.notify_user(text.into()); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /// Writes a whole stack at the card's clip: one undo step, or none when it is the |
| 345 | /// stack already there. |
| 346 | pub(crate) fn write_effects(&mut self, stack: Vec<Effect>, cx: &mut Context<Self>) { |
| 347 | self.effects_draft = None; |
| 348 | let Some((lane, idx)) = self.effects_open else { |
| 349 | return; |
| 350 | }; |
| 351 | if let Some(session) = &mut self.session |
| 352 | && session.set_effects(lane, idx, stack) |
| 353 | { |
| 354 | self.mark_dirty(); |
| 355 | self.reset_after_reseek(); |
| 356 | } |
| 357 | cx.notify(); |
| 358 | } |
| 359 | |
| 360 | /// Puts a new effect at the end of the stack. |
| 361 | pub(crate) fn add_effect(&mut self, kind: EffectKind, cx: &mut Context<Self>) { |
| 362 | let mut stack = self.effects_shown(); |
| 363 | if stack.len() >= MAX_EFFECTS { |
| 364 | self.notify_user(format!("A clip holds at most {MAX_EFFECTS} effects").into()); |
| 365 | cx.notify(); |
| 366 | return; |
| 367 | } |
| 368 | stack.push(Effect::new(kind)); |
| 369 | self.effects_adding = false; |
| 370 | self.write_effects(stack, cx); |
| 371 | } |
| 372 | |
| 373 | /// Switches effect `i` on or off, keeping its settings. |
| 374 | pub(crate) fn toggle_effect(&mut self, i: usize, cx: &mut Context<Self>) { |
| 375 | let mut stack = self.effects_shown(); |
| 376 | if let Some(e) = stack.get_mut(i) { |
| 377 | e.on = !e.on; |
| 378 | self.write_effects(stack, cx); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | /// Moves effect `i` one place toward the end (`down`) or the start of the stack. |
| 383 | pub(crate) fn move_effect(&mut self, i: usize, down: bool, cx: &mut Context<Self>) { |
| 384 | // The stack's rows change under an open list, so it closes. |
| 385 | self.effects_mode_open = None; |
| 386 | let mut stack = self.effects_shown(); |
| 387 | let to = if down { i + 1 } else { i.wrapping_sub(1) }; |
| 388 | if i < stack.len() && to < stack.len() { |
| 389 | stack.swap(i, to); |
| 390 | self.write_effects(stack, cx); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /// Takes effect `i` out of the stack. |
| 395 | pub(crate) fn remove_effect(&mut self, i: usize, cx: &mut Context<Self>) { |
| 396 | // The stack's rows change under an open list, so it closes. |
| 397 | self.effects_mode_open = None; |
| 398 | let mut stack = self.effects_shown(); |
| 399 | if i < stack.len() { |
| 400 | stack.remove(i); |
| 401 | self.write_effects(stack, cx); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | /// Sets parameter `p` of effect `i` in one step: a switch, a mode, a reset. |
| 406 | pub(crate) fn set_effect_param(&mut self, i: usize, p: usize, value: f32, cx: &mut Context<Self>) { |
| 407 | let at = self.effects_src(); |
| 408 | let mut stack = self.effects_shown(); |
| 409 | if let Some(e) = stack.get_mut(i) |
| 410 | && let Some(spec) = e.kind.params().get(p) |
| 411 | { |
| 412 | write_param(e, p, snap_param(spec, value), at); |
| 413 | self.write_effects(stack, cx); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /// Sets the key colour of effect `i` (the green screen's three channels) in one step. |
| 418 | pub(crate) fn set_effect_colour(&mut self, i: usize, rgb: u32, cx: &mut Context<Self>) { |
| 419 | let mut stack = self.effects_shown(); |
| 420 | if let Some(e) = stack.get_mut(i) { |
| 421 | e.values[0] = ((rgb >> 16) & 0xff) as f32; |
| 422 | e.values[1] = ((rgb >> 8) & 0xff) as f32; |
| 423 | e.values[2] = (rgb & 0xff) as f32; |
| 424 | self.write_effects(stack, cx); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | /// The bar parameter `p` of effect `i` is read against, as the card last drew it. |
| 429 | pub(crate) fn effect_bar(&self, i: usize, p: usize) -> Rc<Cell<Bounds<Pixels>>> { |
| 430 | let at = i * MAX_PARAMS + p; |
| 431 | let mut bars = self.effects_bars.borrow_mut(); |
| 432 | if bars.len() <= at { |
| 433 | bars.resize_with(at + 1, Default::default); |
| 434 | } |
| 435 | bars[at].clone() |
| 436 | } |
| 437 | |
| 438 | /// A bar was pressed: the drag it starts, and the value under the press. |
| 439 | pub(crate) fn press_effect(&mut self, i: usize, p: usize, x: Pixels, cx: &mut Context<Self>) { |
| 440 | if self.exporting().is_some() { |
| 441 | return; |
| 442 | } |
| 443 | self.effects_drag = Some((i, p)); |
| 444 | self.drag_effect(x, cx); |
| 445 | } |
| 446 | |
| 447 | /// The held bar follows the pointer into the draft; nothing is written yet. |
| 448 | pub(crate) fn drag_effect(&mut self, x: Pixels, cx: &mut Context<Self>) { |
| 449 | let Some((i, p)) = self.effects_drag else { |
| 450 | return; |
| 451 | }; |
| 452 | let at = self.effects_src(); |
| 453 | let mut stack = self.effects_shown(); |
| 454 | if let Some(e) = stack.get_mut(i) |
| 455 | && let Some(spec) = e.kind.params().get(p) |
| 456 | && let Some(fraction) = bar_fraction(self.effect_bar(i, p).get(), x) |
| 457 | { |
| 458 | write_param(e, p, snap_param(spec, value_at(fraction, spec.min, spec.max)), at); |
| 459 | self.effects_draft = Some(stack); |
| 460 | } |
| 461 | cx.notify(); |
| 462 | } |
| 463 | |
| 464 | /// The hand lets go: the draft is written as the one undo step of the whole drag. |
| 465 | pub(crate) fn release_effect(&mut self, cx: &mut Context<Self>) { |
| 466 | if self.effects_drag.take().is_some() |
| 467 | && let Some(draft) = self.effects_draft.take() |
| 468 | { |
| 469 | self.write_effects(draft, cx); |
| 470 | } |
| 471 | cx.notify(); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | /// Where a lost feature stands, as `m:ss.t` (`0:01.2`), for the notice. |
| 476 | fn lost_at_text(frame: u32, fps: f64) -> String { |
| 477 | let s = f64::from(frame) / fps.max(1.0); |
| 478 | format!("{}:{:04.1}", (s / 60.0) as u64, s % 60.0) |
| 479 | } |