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 | //! Generated clips, colour labels and the clip on/off switch on the window's side: the |
| 2 | //! File ▸ New rows that put a solid, a gradient, a countdown, colour bars or an adjustment layer on |
| 3 | //! the timeline, the Generator card's writes, Label and Enabled. The model is the engine's |
| 4 | //! ([`engine::generate`], [`engine::project::ClipMark`]); every change here is one undo step. |
| 5 | |
| 6 | use crate::*; |
| 7 | use crate::player::text_field::{FieldKey, TextField}; |
| 8 | use engine::generate::{Gen, Kind}; |
| 9 | |
| 10 | /// Which field of the cards the shared typing buffer ([`Player::gen_hex`]) is in: a paint's hex, |
| 11 | /// the green screen's key colour, or one of the numbers typed in the Generator and Title cards. |
| 12 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 13 | pub(crate) enum GenPaint { |
| 14 | Fill, |
| 15 | Stroke, |
| 16 | Solid, |
| 17 | From, |
| 18 | To, |
| 19 | /// The key colour of the green-screen effect at this place in the stack. |
| 20 | Key(usize), |
| 21 | /// A shape's stroke width, in pixels. |
| 22 | Width, |
| 23 | /// A rectangle's corner radius, in pixels. |
| 24 | Radius, |
| 25 | /// A gradient's angle, in degrees. |
| 26 | Angle, |
| 27 | /// A countdown's start, in seconds. |
| 28 | Count, |
| 29 | /// One of the selected title's numbers, as the Title card shows it. |
| 30 | Title(crate::player::titling::ScrubField), |
| 31 | /// The caption style's highlight colour, typed on the Subtitle style card. |
| 32 | SubHighlight, |
| 33 | } |
| 34 | |
| 35 | /// A Generator card number being scrubbed: where the press was and the value it had. A press |
| 36 | /// that never moves opens the field for typing instead. |
| 37 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 38 | pub(crate) struct GenScrub { |
| 39 | pub(crate) paint: GenPaint, |
| 40 | pub(crate) x0: f32, |
| 41 | pub(crate) v0: f64, |
| 42 | pub(crate) cur: f64, |
| 43 | pub(crate) moved: bool, |
| 44 | } |
| 45 | |
| 46 | impl GenPaint { |
| 47 | /// The range and the units a pixel of drag moves a Generator number; `None` for the rest. |
| 48 | pub(crate) fn num_range(self) -> Option<(f64, f64, f64)> { |
| 49 | match self { |
| 50 | GenPaint::Width => Some((0., 400., 1.)), |
| 51 | GenPaint::Radius => Some((0., 2000., 4.)), |
| 52 | GenPaint::Angle => Some((0., 359., 1.)), |
| 53 | GenPaint::Count => Some((1., 99., 0.25)), |
| 54 | _ => None, |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// The kind a New action makes, by the word [`engine::generate::KINDS`] uses. |
| 60 | /// The folder a tone of a project at `project` is kept in: beside the project file once it is |
| 61 | /// saved (it exists on disk), else the data folder. |
| 62 | pub(crate) fn tone_dir(project: &std::path::Path) -> Option<std::path::PathBuf> { |
| 63 | match project.parent() { |
| 64 | Some(parent) if project.is_file() && !parent.as_os_str().is_empty() => Some(parent.to_path_buf()), |
| 65 | _ => engine::generate::Tone::dir(), |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub(crate) fn new_kind(action: ActionId) -> Option<&'static str> { |
| 70 | match action { |
| 71 | ActionId::NewSolid => Some("solid"), |
| 72 | ActionId::NewGradient => Some("gradient"), |
| 73 | ActionId::NewCountdown => Some("countdown"), |
| 74 | ActionId::NewBars => Some("bars"), |
| 75 | ActionId::NewAdjustment => Some("adjust"), |
| 76 | ActionId::NewRectangle => Some("rectangle"), |
| 77 | ActionId::NewEllipse => Some("ellipse"), |
| 78 | ActionId::NewLine => Some("line"), |
| 79 | ActionId::NewArrow => Some("arrow"), |
| 80 | _ => None, |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | impl Player { |
| 85 | /// The clip a Label or Enabled stroke acts on: the selection's anchor, else the picture |
| 86 | /// under the playhead, as Reverse reads it. |
| 87 | pub(crate) fn label_target(&self) -> Option<(Lane, usize)> { |
| 88 | let session = self.session.as_ref()?; |
| 89 | self.selected |
| 90 | .anchor() |
| 91 | .or_else(|| session.video_clip_at(session.now())) |
| 92 | .filter(|(lane, idx)| lane.kind != LaneKind::Subtitle && session.lane_clips(*lane).get(*idx).is_some()) |
| 93 | } |
| 94 | |
| 95 | /// Every clip the switch acts on: the whole selection, else the one the stroke reads. |
| 96 | fn switch_targets(&self) -> Vec<(Lane, usize)> { |
| 97 | let (picks, _) = self.marks(); |
| 98 | let picks: Vec<_> = picks.into_iter().filter(|(lane, _)| lane.kind != LaneKind::Subtitle).collect(); |
| 99 | match picks.is_empty() { |
| 100 | true => self.label_target().into_iter().collect(), |
| 101 | false => picks, |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /// Enabled: switches the selected clips off, or back on when they are all off. One undo step |
| 106 | /// for the lot; the toast says which way it went. |
| 107 | pub(crate) fn toggle_clip_enabled(&mut self, cx: &mut Context<Self>) { |
| 108 | let targets = self.switch_targets(); |
| 109 | let Some(session) = &mut self.session else { |
| 110 | self.notify_user("Nothing to switch — no file open".into()); |
| 111 | cx.notify(); |
| 112 | return; |
| 113 | }; |
| 114 | if targets.is_empty() { |
| 115 | self.notify_user("Nothing to switch — select a clip".into()); |
| 116 | cx.notify(); |
| 117 | return; |
| 118 | } |
| 119 | let enable = targets.iter().all(|&(lane, idx)| session.mark_of(lane, idx).off); |
| 120 | session.begin_batch(); |
| 121 | let done = targets.iter().filter(|&&(lane, idx)| session.set_enabled(lane, idx, enable)).count(); |
| 122 | session.end_batch(); |
| 123 | if done > 0 { |
| 124 | self.mark_dirty(); |
| 125 | self.reset_after_reseek(); |
| 126 | let what = if enable { "enabled" } else { "disabled" }; |
| 127 | let undo = self.keymap.display(ActionId::Undo); |
| 128 | self.notify_user(format!("{done} clip{} {what} · undo {undo}", if done == 1 { "" } else { "s" }).into()); |
| 129 | } |
| 130 | cx.notify(); |
| 131 | } |
| 132 | |
| 133 | /// Labels the clip (0 clears it). One undo step. |
| 134 | pub(crate) fn apply_label(&mut self, lane: Lane, idx: usize, label: u8, cx: &mut Context<Self>) { |
| 135 | let Some(session) = &mut self.session else { |
| 136 | return; |
| 137 | }; |
| 138 | if session.set_label(lane, idx, label) { |
| 139 | self.mark_dirty(); |
| 140 | } |
| 141 | cx.notify(); |
| 142 | } |
| 143 | |
| 144 | /// The Label list for the clip a stroke or the palette names, hung from the clip readout. |
| 145 | pub(crate) fn open_label_list(&mut self, cx: &mut Context<Self>) { |
| 146 | match self.label_target() { |
| 147 | Some((lane, idx)) => { |
| 148 | let at = self.ledger_anchor(LedgerItem::Clip); |
| 149 | self.open_picker(Pick::Label(lane, idx), at, cx); |
| 150 | } |
| 151 | None => { |
| 152 | self.notify_user("Nothing to label — select a clip".into()); |
| 153 | cx.notify(); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /// File ▸ New: puts a generated clip on the timeline at the playhead. A solid, gradient, |
| 159 | /// countdown or bars lands on the lowest video lane that is free for its stretch, an |
| 160 | /// adjustment layer on the highest one (the layers below are what it acts on); when no lane |
| 161 | /// is free a lane is added above. Nothing is overwritten. One undo step, and the clip is |
| 162 | /// selected. |
| 163 | pub(crate) fn new_generator(&mut self, action: ActionId, cx: &mut Context<Self>) { |
| 164 | let Some(name) = new_kind(action) else { |
| 165 | return; |
| 166 | }; |
| 167 | let Some(session) = &mut self.session else { |
| 168 | self.notify_user("Nothing to add to — no file open".into()); |
| 169 | cx.notify(); |
| 170 | return; |
| 171 | }; |
| 172 | let Some(spec) = session.generator_named(name) else { |
| 173 | return; |
| 174 | }; |
| 175 | let fps = session.meta().frame_rate; |
| 176 | let at = session.now(); |
| 177 | let start = frame_at(at, fps); |
| 178 | let secs = match spec.kind { |
| 179 | Kind::Countdown { from, .. } => f64::from(from), |
| 180 | _ => 5.0, |
| 181 | }; |
| 182 | let end = start + (secs * fps).round() as u32; |
| 183 | let free = |session: &engine::PlaybackSession, lane: Lane| { |
| 184 | !session.lane_clips(lane).iter().any(|c| c.start < end && c.end() > start) |
| 185 | }; |
| 186 | let mut video: Vec<Lane> = session.lanes().into_iter().filter(|l| l.kind == LaneKind::Video).collect(); |
| 187 | let adjust = spec.is_adjust(); |
| 188 | if adjust { |
| 189 | video.reverse(); |
| 190 | } |
| 191 | // A shape goes above whatever the playhead is on, so it lands over the picture. |
| 192 | if spec.has_alpha() { |
| 193 | let top = video.iter().rposition(|&l| session.lane_clips(l).iter().any(|c| c.start <= start && c.end() > start)); |
| 194 | video.drain(..top.map_or(0, |t| t + 1)); |
| 195 | } |
| 196 | session.begin_batch(); |
| 197 | let lane = match video.into_iter().find(|&l| free(session, l)) { |
| 198 | Some(lane) => lane, |
| 199 | None => session.add_lane(LaneKind::Video), |
| 200 | }; |
| 201 | let placed = session.place_generated(spec, at, None, Some(lane)); |
| 202 | if let (Ok(true), Some(scale)) = (&placed, spec.default_scale()) |
| 203 | && let Some(i) = session.lane_clips(lane).iter().position(|c| c.start == start) |
| 204 | { |
| 205 | session.set_transform(lane, i, Some(engine::transform::TransformParams { scale, ..Default::default() })); |
| 206 | } |
| 207 | session.end_batch(); |
| 208 | match placed { |
| 209 | Ok(true) => { |
| 210 | let idx = session |
| 211 | .lane_clips(lane) |
| 212 | .iter() |
| 213 | .position(|c| c.start <= start && c.end() > start) |
| 214 | .unwrap_or(0); |
| 215 | self.selected.set_one((lane, idx)); |
| 216 | self.mark_dirty(); |
| 217 | self.reset_after_reseek(); |
| 218 | let undo = self.keymap.display(ActionId::Undo); |
| 219 | self.notify_user(format!("{} added on {} · undo {undo}", action.label(), lane.label()).into()); |
| 220 | } |
| 221 | Ok(false) => self.notify_user("Nothing added — the place is refused".into()), |
| 222 | Err(e) => self.notify_user(e.to_string().into()), |
| 223 | } |
| 224 | cx.notify(); |
| 225 | } |
| 226 | |
| 227 | /// File > New > Tone, and Bars and tone: a 1 kHz test tone at -20 dBFS (RMS), ten seconds, on the |
| 228 | /// first free audio lane at the playhead (a new lane when none is free). Bars and tone adds |
| 229 | /// colour bars of the same length on a free video lane and links the two. One undo step. |
| 230 | pub(crate) fn new_tone(&mut self, action: ActionId, cx: &mut Context<Self>) { |
| 231 | // A saved project keeps its tone files beside it; an untitled one keeps them in the data folder. |
| 232 | let tones = tone_dir(&self.project_path); |
| 233 | let Some(session) = &mut self.session else { |
| 234 | self.notify_user("Nothing to add to — no file open".into()); |
| 235 | cx.notify(); |
| 236 | return; |
| 237 | }; |
| 238 | let both = action == ActionId::NewBarsAndTone; |
| 239 | let fps = session.meta().frame_rate; |
| 240 | let at = session.now(); |
| 241 | let start = frame_at(at, fps); |
| 242 | let secs = engine::generate::TONE_SECS; |
| 243 | let end = start + (f64::from(secs) * fps).round() as u32; |
| 244 | let free = |session: &engine::PlaybackSession, lane: Lane| !session.lane_clips(lane).iter().any(|c| c.start < end && c.end() > start); |
| 245 | session.begin_batch(); |
| 246 | let audio = match session.lanes().into_iter().filter(|l| l.kind == LaneKind::Audio).find(|&l| free(session, l)) { |
| 247 | Some(lane) => lane, |
| 248 | None => session.add_lane(LaneKind::Audio), |
| 249 | }; |
| 250 | let video = both.then(|| match session.lanes().into_iter().filter(|l| l.kind == LaneKind::Video).find(|&l| free(session, l)) { |
| 251 | Some(lane) => lane, |
| 252 | None => session.add_lane(LaneKind::Video), |
| 253 | }); |
| 254 | let bars = match (video, session.generator_named("bars")) { |
| 255 | (Some(lane), Some(spec)) => session.place_generated(spec, at, Some(f64::from(secs)), Some(lane)), |
| 256 | _ => Ok(true), |
| 257 | }; |
| 258 | let tone = match &tones { |
| 259 | Some(dir) => session.place_tone(engine::generate::Tone::DEFAULT, at, secs, Some(audio), dir), |
| 260 | None => Err("no data folder to keep the tone in".into()), |
| 261 | }; |
| 262 | if let (Some(v), Ok(true), Ok(true)) = (video, &bars, &tone) |
| 263 | && let (Some(vi), Some(ai)) = ( |
| 264 | session.lane_clips(v).iter().position(|c| c.start == start), |
| 265 | session.lane_clips(audio).iter().position(|c| c.start == start), |
| 266 | ) |
| 267 | { |
| 268 | let _ = session.group(v, vi, audio, ai); |
| 269 | } |
| 270 | session.end_batch(); |
| 271 | let picked = match (&bars, &tone) { |
| 272 | (Ok(true), Ok(true)) => session.lane_clips(audio).iter().position(|c| c.start == start).map(|i| (audio, i)), |
| 273 | _ => None, |
| 274 | }; |
| 275 | match (bars, tone) { |
| 276 | (Ok(true), Ok(true)) => { |
| 277 | if let Some(pick) = picked { |
| 278 | self.selected.set_one(pick); |
| 279 | } |
| 280 | self.mark_dirty(); |
| 281 | self.reset_after_reseek(); |
| 282 | let undo = self.keymap.display(ActionId::Undo); |
| 283 | let what = match video { |
| 284 | Some(v) => format!("{} and tone added on {} and {}", "Colour bars", v.label(), audio.label()), |
| 285 | None => format!("Tone added on {}", audio.label()), |
| 286 | }; |
| 287 | self.notify_user(format!("{what} · undo {undo}").into()); |
| 288 | } |
| 289 | (Err(e), _) | (_, Err(e)) => self.notify_user(e.to_string().into()), |
| 290 | _ => self.notify_user("Nothing added — the place is refused".into()), |
| 291 | } |
| 292 | cx.notify(); |
| 293 | } |
| 294 | |
| 295 | /// A press on a shape's hex field: it opens for typing with all of it selected. |
| 296 | pub(crate) fn begin_gen_hex(&mut self, paint: GenPaint, cx: &mut Context<Self>) { |
| 297 | self.commit_gen_hex(cx); |
| 298 | let Some(session) = &self.session else { return }; |
| 299 | let Some((lane, idx)) = self.selected.anchor() else { return }; |
| 300 | let Some(kind) = session.generator_of(lane, idx).map(|g| g.kind) else { return }; |
| 301 | let now = match (paint, kind) { |
| 302 | (GenPaint::Fill, Kind::Shape(sh)) => sh.fill, |
| 303 | (GenPaint::Stroke, Kind::Shape(sh)) => sh.stroke, |
| 304 | (GenPaint::Solid, Kind::Solid { rgb }) => Some(rgb), |
| 305 | (GenPaint::From, Kind::Gradient { a, .. }) => Some(a), |
| 306 | (GenPaint::To, Kind::Gradient { b, .. }) => Some(b), |
| 307 | _ => return, |
| 308 | }; |
| 309 | self.gen_hex = Some((paint, TextField::selected(now.map_or("none".to_string(), |c| format!("{c:06x}"))))); |
| 310 | cx.notify(); |
| 311 | } |
| 312 | |
| 313 | /// Writes the typed colour (six hex digits, or none) as the shape's paint, one undo step. |
| 314 | /// Anything else keeps the old colour and says so. |
| 315 | pub(crate) fn commit_gen_hex(&mut self, cx: &mut Context<Self>) { |
| 316 | let Some((paint, field)) = self.gen_hex.take() else { |
| 317 | return; |
| 318 | }; |
| 319 | match paint { |
| 320 | GenPaint::Key(i) => return self.commit_key_hex(i, &field.text, cx), |
| 321 | GenPaint::Title(f) => return self.commit_title_num(f, &field.text, cx), |
| 322 | GenPaint::SubHighlight => return self.commit_sub_highlight_hex(&field.text, cx), |
| 323 | GenPaint::Width | GenPaint::Radius | GenPaint::Angle | GenPaint::Count => { |
| 324 | return match typed_number(&field.text) { |
| 325 | Some(v) => self.write_gen_num(paint, v, cx), |
| 326 | None => { |
| 327 | self.notify_user("That is not a number — kept the old one".into()); |
| 328 | cx.notify(); |
| 329 | } |
| 330 | }; |
| 331 | } |
| 332 | _ => {} |
| 333 | } |
| 334 | let text = field.text.trim().trim_start_matches('#').to_ascii_lowercase(); |
| 335 | let colour = match text.as_str() { |
| 336 | "none" | "" => Some(None), |
| 337 | t if t.len() == 6 => u32::from_str_radix(t, 16).ok().map(Some), |
| 338 | _ => None, |
| 339 | }; |
| 340 | let Some(colour) = colour else { |
| 341 | self.notify_user("A colour is six hex digits, such as ffcc00, or none — kept the old one".into()); |
| 342 | cx.notify(); |
| 343 | return; |
| 344 | }; |
| 345 | let Some(session) = &self.session else { return }; |
| 346 | let Some((lane, idx)) = self.selected.anchor() else { return }; |
| 347 | let Some(spec) = session.generator_of(lane, idx) else { return }; |
| 348 | let new_kind = match (paint, spec.kind) { |
| 349 | (GenPaint::Solid, Kind::Solid { rgb }) => Kind::Solid { rgb: colour.unwrap_or(rgb) }, |
| 350 | (GenPaint::From, Kind::Gradient { a, b, angle }) => Kind::Gradient { a: colour.unwrap_or(a), b, angle }, |
| 351 | (GenPaint::To, Kind::Gradient { a, b, angle }) => Kind::Gradient { a, b: colour.unwrap_or(b), angle }, |
| 352 | (_, Kind::Shape(mut sh)) => { |
| 353 | match paint { |
| 354 | GenPaint::Fill => sh.fill = colour, |
| 355 | _ => { |
| 356 | sh.stroke = colour; |
| 357 | if colour.is_some() && sh.width == 0 { |
| 358 | sh.width = 6; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | // A shape with nothing to paint is no shape. |
| 363 | let visible = sh.fill.is_some() && sh.form.fills() || sh.stroke.is_some() && sh.width > 0; |
| 364 | if !visible { |
| 365 | self.notify_user("A shape needs a fill or a stroke — kept the old colour".into()); |
| 366 | cx.notify(); |
| 367 | return; |
| 368 | } |
| 369 | Kind::Shape(sh) |
| 370 | } |
| 371 | _ => return, |
| 372 | }; |
| 373 | self.write_generator(lane, idx, Gen { kind: new_kind, ..spec }, cx); |
| 374 | } |
| 375 | |
| 376 | /// The keyboard while a shape's hex is being typed: enter applies, escape leaves. |
| 377 | pub(crate) fn gen_key(&mut self, key: &str, ctrl: bool, alt: bool, key_char: Option<&str>, cx: &mut Context<Self>) { |
| 378 | let Some((paint, field)) = self.gen_hex.as_mut() else { return }; |
| 379 | let number = matches!(paint, GenPaint::Width | GenPaint::Radius | GenPaint::Angle | GenPaint::Count | GenPaint::Title(_)); |
| 380 | // A title's key shifts run both ways, so they may be typed negative. |
| 381 | let signed = matches!(paint, GenPaint::Title(crate::player::titling::ScrubField::KeyX | crate::player::titling::ScrubField::KeyY)); |
| 382 | let result = field.key(key, ctrl, alt, key_char, if number { 8 } else { 7 }); |
| 383 | match paint { |
| 384 | GenPaint::Key(_) => field.text.retain(|c| c.is_ascii_hexdigit() || c == '#'), |
| 385 | _ if number => field.text.retain(|c| c.is_ascii_digit() || c == '.' || c == ',' || (signed && c == '-')), |
| 386 | _ => field.text.retain(|c| c.is_ascii_alphanumeric() || c == '#'), |
| 387 | } |
| 388 | match result { |
| 389 | FieldKey::Commit => self.commit_gen_hex(cx), |
| 390 | FieldKey::Cancel => self.gen_hex = None, |
| 391 | FieldKey::Edited => {} |
| 392 | } |
| 393 | cx.notify(); |
| 394 | } |
| 395 | |
| 396 | /// A press on the green screen's key colour field: its hex opens for typing, all selected. |
| 397 | pub(crate) fn begin_key_hex(&mut self, i: usize, cx: &mut Context<Self>) { |
| 398 | self.commit_gen_hex(cx); |
| 399 | let Some(e) = self.effects_shown().get(i).cloned() else { return }; |
| 400 | let rgb = ((e.values[0] as u32) << 16) | ((e.values[1] as u32) << 8) | e.values[2] as u32; |
| 401 | self.gen_hex = Some((GenPaint::Key(i), TextField::selected(crate::ui::kit::hex_text(rgb)))); |
| 402 | cx.notify(); |
| 403 | } |
| 404 | |
| 405 | /// Writes the typed key colour as one undo step; anything that is not a colour keeps the old one. |
| 406 | fn commit_key_hex(&mut self, i: usize, text: &str, cx: &mut Context<Self>) { |
| 407 | match crate::ui::kit::parse_hex(text) { |
| 408 | Some(rgb) => self.set_effect_colour(i, rgb, cx), |
| 409 | None => { |
| 410 | self.notify_user("A colour is six hex digits, such as 00ff00 — kept the old one".into()); |
| 411 | cx.notify(); |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /// The value of a Generator number on the selected clip. |
| 417 | pub(crate) fn gen_num_value(&self, paint: GenPaint) -> Option<f64> { |
| 418 | let (lane, idx) = self.selected.anchor()?; |
| 419 | match (paint, self.session.as_ref()?.generator_of(lane, idx)?.kind) { |
| 420 | (GenPaint::Width, Kind::Shape(s)) => Some(f64::from(s.width)), |
| 421 | (GenPaint::Radius, Kind::Shape(s)) => Some(f64::from(s.radius)), |
| 422 | (GenPaint::Angle, Kind::Gradient { angle, .. }) => Some(f64::from(angle)), |
| 423 | (GenPaint::Count, Kind::Countdown { from, .. }) => Some(f64::from(from)), |
| 424 | _ => None, |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | /// Writes a Generator number (clamped and rounded to whole units) as one undo step. |
| 429 | fn write_gen_num(&mut self, paint: GenPaint, v: f64, cx: &mut Context<Self>) { |
| 430 | let Some((lo, hi, _)) = paint.num_range() else { return }; |
| 431 | let Some((lane, idx)) = self.selected.anchor() else { return }; |
| 432 | let Some(spec) = self.session.as_ref().and_then(|s| s.generator_of(lane, idx)) else { return }; |
| 433 | let v = v.round().clamp(lo, hi) as u16; |
| 434 | let kind = match (paint, spec.kind) { |
| 435 | (GenPaint::Width, Kind::Shape(s)) => Kind::Shape(engine::generate::Shape { width: v, ..s }), |
| 436 | (GenPaint::Radius, Kind::Shape(s)) => Kind::Shape(engine::generate::Shape { radius: v, ..s }), |
| 437 | (GenPaint::Angle, Kind::Gradient { a, b, .. }) => Kind::Gradient { a, b, angle: v }, |
| 438 | (GenPaint::Count, Kind::Countdown { milli_fps, .. }) => Kind::Countdown { from: v, milli_fps }, |
| 439 | _ => return, |
| 440 | }; |
| 441 | if kind != spec.kind { |
| 442 | self.write_generator(lane, idx, Gen { kind, ..spec }, cx); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /// A press on a Generator number: a drag that follows scrubs it, a click that does not |
| 447 | /// move types into it. |
| 448 | pub(crate) fn begin_gen_scrub(&mut self, paint: GenPaint, x: f32, cx: &mut Context<Self>) { |
| 449 | self.commit_gen_hex(cx); |
| 450 | if let Some(v0) = self.gen_num_value(paint) { |
| 451 | self.gen_scrub = Some(GenScrub { paint, x0: x, v0, cur: v0, moved: false }); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | /// The pointer moved with a Generator number pressed: the number follows, shown live. |
| 456 | pub(crate) fn gen_scrub_move(&mut self, x: f32, fine: bool, cx: &mut Context<Self>) { |
| 457 | let Some(s) = self.gen_scrub else { return }; |
| 458 | let Some((lo, hi, per_px)) = s.paint.num_range() else { return }; |
| 459 | let dx = x - s.x0; |
| 460 | if !s.moved && dx.abs() < 3. { |
| 461 | return; |
| 462 | } |
| 463 | let cur = crate::ui::kit::scrub_value(s.v0, dx, per_px, fine, lo, hi).round(); |
| 464 | self.gen_scrub = Some(GenScrub { cur, moved: true, ..s }); |
| 465 | cx.notify(); |
| 466 | } |
| 467 | |
| 468 | /// The button came up: a scrubbed number is written (one undo step); a press that never |
| 469 | /// moved opens the field for typing. |
| 470 | pub(crate) fn gen_scrub_end(&mut self, cx: &mut Context<Self>) { |
| 471 | let Some(s) = self.gen_scrub.take() else { return }; |
| 472 | if s.moved { |
| 473 | self.write_gen_num(s.paint, s.cur, cx); |
| 474 | } else { |
| 475 | self.gen_hex = Some((s.paint, TextField::selected(format!("{}", s.v0)))); |
| 476 | } |
| 477 | cx.notify(); |
| 478 | } |
| 479 | |
| 480 | /// Writes a typed Title number as one undo step. Seconds become whole frames; a number out |
| 481 | /// of range is held to it. |
| 482 | fn commit_title_num(&mut self, field: crate::player::titling::ScrubField, text: &str, cx: &mut Context<Self>) { |
| 483 | use crate::player::titling::ScrubField as F; |
| 484 | let Some(v) = typed_number(text) else { |
| 485 | self.notify_user("That is not a number — kept the old one".into()); |
| 486 | cx.notify(); |
| 487 | return; |
| 488 | }; |
| 489 | if field.is_key() { |
| 490 | return self.set_title_key_percent(field, v, cx); |
| 491 | } |
| 492 | let fps = self.session.as_ref().map_or(25., |s| s.meta().frame_rate).max(1.); |
| 493 | let frames = (v.max(0.) * fps).round() as u32; |
| 494 | self.write_title( |
| 495 | |t| match field { |
| 496 | // The card shows the share of the picture height in percent; the engine keeps per mille. |
| 497 | F::Size => t.style.size = ((v * 10.).round().max(0.) as u32).clamp(engine::text::SIZE_RANGE.0, engine::text::SIZE_RANGE.1), |
| 498 | F::Start | F::Move => t.start = frames, |
| 499 | F::Length => { |
| 500 | t.frames = frames.max(1); |
| 501 | t.fade_in = t.fade_in.min(t.frames); |
| 502 | t.fade_out = t.fade_out.min(t.frames); |
| 503 | t.anim_frames = t.anim_frames.min(t.frames); |
| 504 | } |
| 505 | F::AnimLength => t.anim_frames = frames.clamp(1, t.frames.max(1)), |
| 506 | F::BoxOpacity => t.style.box_color[3] = ((v.round().clamp(0., 100.) as u32 * 255 + 50) / 100) as u8, |
| 507 | // The key fields were written by `set_title_key_percent` above. |
| 508 | F::KeyX | F::KeyY | F::KeyScale | F::KeyOpacity => {} |
| 509 | }, |
| 510 | cx, |
| 511 | ); |
| 512 | } |
| 513 | |
| 514 | /// Writes new parameters for the generator clip at `(lane, idx)`. One undo step. |
| 515 | pub(crate) fn write_generator(&mut self, lane: Lane, idx: usize, spec: Gen, cx: &mut Context<Self>) { |
| 516 | let Some(session) = &mut self.session else { |
| 517 | return; |
| 518 | }; |
| 519 | if session.set_generator(lane, idx, spec) { |
| 520 | self.mark_dirty(); |
| 521 | self.reset_after_reseek(); |
| 522 | } |
| 523 | cx.notify(); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /// The number a typed buffer holds (`,` reads as `.`), or `None` when it is not one. |
| 528 | fn typed_number(text: &str) -> Option<f64> { |
| 529 | text.trim().replace(',', ".").parse::<f64>().ok().filter(|v| v.is_finite()) |
| 530 | } |