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 speed card: one continuous rate on one clip. |
| 2 | |
| 3 | use super::common::*; |
| 4 | use crate::player::cards::speed_fraction; |
| 5 | use super::tools::slider_track; |
| 6 | use crate::ui::icons::Icon; |
| 7 | use crate::layout::redesign as r; |
| 8 | use crate::ui::kit::{self, Segment}; |
| 9 | use crate::ui::widgets::{SliderTyped, typed_value_pill}; |
| 10 | use crate::ui::type_scale::{self, Typeset}; |
| 11 | use crate::*; |
| 12 | |
| 13 | /// A rate as the card reads it: "1.5×", "0.25×", "1×" -- as many decimals as |
| 14 | /// the rate needs, so the readout is the number a person would have typed. |
| 15 | pub(crate) fn speed_text(speed: Speed) -> String { |
| 16 | let s = format!("{:.2}", speed.as_f64()); |
| 17 | format!("{}×", s.trim_end_matches('0').trim_end_matches('.')) |
| 18 | } |
| 19 | |
| 20 | /// A preset chip's face: the bare multiplier, the way the chips are named. |
| 21 | fn preset_text(permille: u16) -> String { |
| 22 | format!("{}", f64::from(permille) / 1000.) |
| 23 | } |
| 24 | |
| 25 | impl Player { |
| 26 | /// The speed card: one log-scaled bar from a quarter speed to four times it |
| 27 | /// (equal travel per doubling), the rates people name as buttons under it, |
| 28 | /// and the clip's new length in frames -- which is the number a person is |
| 29 | /// actually choosing. Built like the colour card down to the scrim, because |
| 30 | /// it is the same kind of card: one continuous value on one clip, live at |
| 31 | /// the clip as it moves. |
| 32 | /// |
| 33 | /// The engine time-stretches a sped-up or slowed clip, so the pitch stays |
| 34 | /// where it was recorded; the card says so under the bar. |
| 35 | pub(crate) fn speed_card( |
| 36 | &self, |
| 37 | viewport: Size<Pixels>, |
| 38 | cx: &mut Context<Self>, |
| 39 | ) -> Option<impl IntoElement> { |
| 40 | let (lane, idx) = self.speed_open?; |
| 41 | let speed = self.card_speed(); |
| 42 | let session = self.session.as_ref()?; |
| 43 | let clip = session.lane_clips(lane).get(idx).cloned()?; |
| 44 | let ramp_mode = self.speed_ramp_mode; |
| 45 | let presets = kit::segmented_equal( |
| 46 | "speed-preset", |
| 47 | &SPEED_PRESETS.map(|permille| Segment::text(format!("{}\u{d7}", preset_text(permille)))), |
| 48 | SPEED_PRESETS.iter().position(|p| Speed::from_permille(*p) == speed).unwrap_or(usize::MAX), |
| 49 | true, |
| 50 | None, |
| 51 | { |
| 52 | let me = cx.entity(); |
| 53 | move |i, _, app| me.update(app, |this, cx| this.set_speed(Speed::from_permille(SPEED_PRESETS[i]), cx)) |
| 54 | }, |
| 55 | ); |
| 56 | // The one value on the card: ink1 once it is off real time, and the |
| 57 | // row's own Reset puts it back. In the ramp mode it is the speed of the |
| 58 | // next key instead. |
| 59 | let slider = div() |
| 60 | .flex() |
| 61 | .flex_col() |
| 62 | .child( |
| 63 | div() |
| 64 | .flex() |
| 65 | .items_center() |
| 66 | .justify_between() |
| 67 | .min_h(px(CONTROL_H)) |
| 68 | .child(dark_row_label(if ramp_mode { "Key speed" } else { "Rate" }, false)) |
| 69 | .child(typed_value_pill( |
| 70 | "speed-value", |
| 71 | &speed_text(speed), |
| 72 | None, |
| 73 | 64., |
| 74 | speed != Speed::NORMAL, |
| 75 | // Typed in times, "1.5" for 1.5x, on the bar's own grid. |
| 76 | Some(SliderTyped { |
| 77 | lo: f64::from(Speed::MIN.permille()) / 1000., |
| 78 | hi: f64::from(Speed::MAX.permille()) / 1000., |
| 79 | set: Rc::new(|this: &mut Player, v: f64, cx: &mut Context<Player>| { |
| 80 | let step = f64::from(crate::audio_cards::SPEED_STEP); |
| 81 | let permille = ((v * 1000. / step).round() * step) as u16; |
| 82 | this.set_speed(Speed::from_permille(permille), cx); |
| 83 | }), |
| 84 | }), |
| 85 | { |
| 86 | let me = cx.entity(); |
| 87 | move |_, app| me.update(app, |this, cx| this.set_speed(Speed::NORMAL, cx)) |
| 88 | }, |
| 89 | cx, |
| 90 | )), |
| 91 | ) |
| 92 | .child(slider_track( |
| 93 | "speed-bar", |
| 94 | speed_fraction(f32::from(speed.permille())), |
| 95 | self.speed_bar.clone(), |
| 96 | |this: &mut Player, x: Pixels, cx: &mut Context<Player>| { |
| 97 | this.speed_dragging = true; |
| 98 | this.drag_speed(x, true, cx); |
| 99 | }, |
| 100 | cx, |
| 101 | )) |
| 102 | .into_any_element(); |
| 103 | // The direction, beside the rate: a toggle that plays the clip and its |
| 104 | // group last frame first, composing with whatever the bar says. |
| 105 | let reversed = session.reversed_of(lane, idx); |
| 106 | let reverse_row = kit::switch_row("speed-reverse", "Reverse", Some("Play the clip backwards".into()), reversed, None, { |
| 107 | let me = cx.entity(); |
| 108 | move |_, app| me.update(app, |this, cx| this.write_reverse(lane, idx, !reversed, cx)) |
| 109 | }); |
| 110 | let help_text = match ramp_mode { |
| 111 | true => "Set the bar, then Add key puts that speed at the playhead. Plus and minus step a key, the cross removes it.", |
| 112 | false => "Drag the bar, or use the left and right arrows for 0.05× a step. R puts it back to 1×.", |
| 113 | }; |
| 114 | let tail_text: SharedString = format!( |
| 115 | "Pitch kept · {:.1} s of footage in {:.1} s", |
| 116 | f64::from(clip.len()) / self.fps.max(1.), |
| 117 | f64::from(clip.frames()) / self.fps.max(1.) |
| 118 | ) |
| 119 | .into(); |
| 120 | // The two modes of the card: one rate for the whole clip, or a ramp of keys. |
| 121 | // Constant takes a ramp off, which is what pressing it means. |
| 122 | let tabs = kit::segmented_equal( |
| 123 | "speed-mode", |
| 124 | &[Segment::text("Constant"), Segment::text("Ramp")], |
| 125 | usize::from(ramp_mode), |
| 126 | true, |
| 127 | None, |
| 128 | { |
| 129 | let me = cx.entity(); |
| 130 | move |i, _, app| { |
| 131 | me.update(app, |this, cx| { |
| 132 | if i == 0 { |
| 133 | this.clear_ramp(cx); |
| 134 | this.speed_ramp_mode = false; |
| 135 | } else { |
| 136 | this.speed_ramp_mode = true; |
| 137 | this.ramp_key_speed = None; |
| 138 | } |
| 139 | cx.notify(); |
| 140 | }) |
| 141 | } |
| 142 | }, |
| 143 | ); |
| 144 | let ramp = ramp_mode.then(|| self.ramp_editor(&clip, cx)); |
| 145 | Some( |
| 146 | self.card_outer(viewport, cx) |
| 147 | .child( |
| 148 | div() |
| 149 | .w_full() |
| 150 | .max_w(px(card_max_w( |
| 151 | COLOR_W, |
| 152 | self.card_maximized, |
| 153 | f32::from(viewport.width), |
| 154 | ))) |
| 155 | .on_mouse_down(MouseButton::Left, swallow) |
| 156 | .flex() |
| 157 | .flex_col() |
| 158 | .gap(px(CARD_GAP)) |
| 159 | .map(|d| self.card_surface(d)) |
| 160 | .map(|d| { |
| 161 | d.child(div().w_full().flex().justify_end().child(dark_card_head( |
| 162 | "Speed", |
| 163 | None, |
| 164 | Some(help_text.into()), |
| 165 | Some(self.card_maximized), |
| 166 | cx, |
| 167 | ))) |
| 168 | }) |
| 169 | .child(tabs) |
| 170 | .children(ramp) |
| 171 | .child(slider) |
| 172 | .when(!ramp_mode, |d| { |
| 173 | d.child(presets) |
| 174 | }) |
| 175 | .child(reverse_row) |
| 176 | // One line, never wrapped: this caption wrapped to two lines here, and the |
| 177 | // dock measured it far too tall, leaving a screenful of gap under the card. |
| 178 | .map(|d| d.child(div().flex_none().type_style(type_scale::caption()).text_color(rgb(tokens().ink3)).child(tail_text.clone()))), |
| 179 | ), |
| 180 | ) |
| 181 | } |
| 182 | |
| 183 | /// The ramp mode's body: the speed curve over the clip, the key list under it, |
| 184 | /// the shapes people name and the two verbs on the keys. Every control is a |
| 185 | /// button, so a ramp can be built without a keyboard. |
| 186 | fn ramp_editor(&self, clip: &Clip, cx: &mut Context<Self>) -> impl IntoElement + use<> { |
| 187 | let keys = clip.speed_keys.clone(); |
| 188 | let frames = clip.frames(); |
| 189 | let curve = ramp_curve(&keys, clip.speed, frames, 64); |
| 190 | let key_xs: Vec<(f32, f32)> = keys |
| 191 | .iter() |
| 192 | .map(|k| { |
| 193 | ( |
| 194 | (k.at as f32 / frames.max(1) as f32).clamp(0., 1.), |
| 195 | speed_fraction(f32::from(k.speed.permille())), |
| 196 | ) |
| 197 | }) |
| 198 | .collect(); |
| 199 | // Where the playhead is on the clip, as a fraction of its length. |
| 200 | let playhead = self.session.as_ref().and_then(|s| { |
| 201 | let at = crate::subs::frame_at(s.now(), self.fps); |
| 202 | (at >= clip.start && at < clip.end()) |
| 203 | .then(|| (at - clip.start) as f32 / frames.max(1) as f32) |
| 204 | }); |
| 205 | let plot = div() |
| 206 | .id("speed-ramp-graph") |
| 207 | .relative() |
| 208 | .flex_1() |
| 209 | .h(px(RAMP_GRAPH_H)) |
| 210 | .rounded(px(3.)) |
| 211 | .bg(rgb(DARK_RAISED())) |
| 212 | .child( |
| 213 | canvas( |
| 214 | |_, _, _| (), |
| 215 | move |bounds, _, window, _| { |
| 216 | let (o, s) = (bounds.origin, bounds.size); |
| 217 | let (w, h) = (f32::from(s.width), f32::from(s.height)); |
| 218 | if w <= 0. || h <= 0. { |
| 219 | return; |
| 220 | } |
| 221 | // Inset by the key diamond's radius, so an end key sits whole on the plot. |
| 222 | let at = |x: f32, y: f32| point(o.x + px(RAMP_KEY_R + x * (w - 2. * RAMP_KEY_R)), o.y + px((1. - y) * h)); |
| 223 | // Real time, as a quiet rule across the middle. |
| 224 | let mid = speed_fraction(1000.); |
| 225 | let mut rule = PathBuilder::stroke(px(1.)); |
| 226 | rule.move_to(at(0., mid)); |
| 227 | rule.line_to(at(1., mid)); |
| 228 | if let Ok(rule) = rule.build() { |
| 229 | window.paint_path(rule, rgba(DARK_SEAM())); |
| 230 | } |
| 231 | if let Some(x) = playhead { |
| 232 | let mut line = PathBuilder::stroke(px(1.)); |
| 233 | line.move_to(at(x, 0.)); |
| 234 | line.line_to(at(x, 1.)); |
| 235 | if let Ok(line) = line.build() { |
| 236 | window.paint_path(line, rgb(INK3())); |
| 237 | } |
| 238 | } |
| 239 | let mut path = PathBuilder::stroke(px(1.5)); |
| 240 | for (i, &(x, y)) in curve.iter().enumerate() { |
| 241 | match i { |
| 242 | 0 => path.move_to(at(x, y)), |
| 243 | _ => path.line_to(at(x, y)), |
| 244 | } |
| 245 | } |
| 246 | if let Ok(path) = path.build() { |
| 247 | window.paint_path(path, rgb(INK1())); |
| 248 | } |
| 249 | // A key is a small diamond on the curve. |
| 250 | for &(x, y) in &key_xs { |
| 251 | let c = at(x, y); |
| 252 | let r = px(RAMP_KEY_R); |
| 253 | let mut dot = PathBuilder::fill(); |
| 254 | dot.add_polygon( |
| 255 | &[ |
| 256 | point(c.x, c.y - r), |
| 257 | point(c.x + r, c.y), |
| 258 | point(c.x, c.y + r), |
| 259 | point(c.x - r, c.y), |
| 260 | ], |
| 261 | true, |
| 262 | ); |
| 263 | if let Ok(dot) = dot.build() { |
| 264 | window.paint_path(dot, rgb(INK1())); |
| 265 | } |
| 266 | } |
| 267 | }, |
| 268 | ) |
| 269 | .size_full(), |
| 270 | ); |
| 271 | // The speed labels live in a gutter left of the plot, so no label sits on the curve. |
| 272 | let gutter = div() |
| 273 | .relative() |
| 274 | .flex_none() |
| 275 | .w(px(RAMP_GUTTER_W)) |
| 276 | .h(px(RAMP_GRAPH_H)) |
| 277 | .children([("4\u{d7}", 0.), ("1\u{d7}", 1. - speed_fraction(1000.)), ("0.25\u{d7}", 1.)].map(|(label, at)| { |
| 278 | div() |
| 279 | .absolute() |
| 280 | .right(px(4.)) |
| 281 | .top(relative(at)) |
| 282 | .mt(px(if at >= 1. { -16. } else if at <= 0. { 0. } else { -8. })) |
| 283 | .type_style(type_scale::caption()) |
| 284 | .text_color(rgb(INK3())) |
| 285 | .child(label) |
| 286 | })); |
| 287 | let graph = div() |
| 288 | .flex() |
| 289 | .w_full() |
| 290 | .gap(px(6.)) |
| 291 | .child(gutter) |
| 292 | .child(plot); |
| 293 | // The caption sits under the plot in a reserved row, so the hint is never crossed |
| 294 | // by the 1x rule and adding the first key does not move the list below. |
| 295 | let caption = div() |
| 296 | .flex_none() |
| 297 | .h(px(RAMP_CAPTION_H)) |
| 298 | .w_full() |
| 299 | .flex() |
| 300 | .items_center() |
| 301 | .justify_center() |
| 302 | .type_style(type_scale::caption()) |
| 303 | .text_color(rgb(INK3())) |
| 304 | .child(if keys.is_empty() { "Add keys to shape the speed over the clip" } else { "Speed over the clip, left to right" }); |
| 305 | let fps = self.fps; |
| 306 | let rows: Vec<_> = keys |
| 307 | .iter() |
| 308 | .enumerate() |
| 309 | .map(|(i, k)| { |
| 310 | div() |
| 311 | .id(("speed-ramp-key", i)) |
| 312 | .flex() |
| 313 | .items_center() |
| 314 | .gap(px(4.)) |
| 315 | .h(px(r::CONTROL_M)) |
| 316 | .child(div().flex_1().child(dark_row_label(format!("{:.1} s", f64::from(k.at) / fps.max(1.)), false))) |
| 317 | .child(kit::icon_ghost("ramp-key-step", Icon::Minus, "Slower", None, { |
| 318 | let me = cx.entity(); |
| 319 | move |_, app| me.update(app, |this, cx| this.step_ramp_key(i, -1, cx)) |
| 320 | })) |
| 321 | .child(div().w(px(56.)).flex().justify_center().child(dark_row_value(speed_text(k.speed), true))) |
| 322 | .child(kit::icon_ghost("ramp-key-step-up", Icon::Plus, "Faster", None, { |
| 323 | let me = cx.entity(); |
| 324 | move |_, app| me.update(app, |this, cx| this.step_ramp_key(i, 1, cx)) |
| 325 | })) |
| 326 | .child(kit::icon_ghost("ramp-key-remove", Icon::X, "Remove key", None, { |
| 327 | let me = cx.entity(); |
| 328 | move |_, app| me.update(app, |this, cx| this.remove_ramp_key(i, cx)) |
| 329 | })) |
| 330 | }) |
| 331 | .collect(); |
| 332 | let shape = |preset: RampPreset, cx: &mut Context<Self>| { |
| 333 | let name = preset.label(); |
| 334 | dark_ghost_button( |
| 335 | ("ramp-preset", preset as usize), |
| 336 | name, |
| 337 | "", |
| 338 | false, |
| 339 | cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 340 | this.set_ramp_preset(preset, cx); |
| 341 | }), |
| 342 | ) |
| 343 | .into_any_element() |
| 344 | }; |
| 345 | let shapes = div() |
| 346 | .flex() |
| 347 | .flex_col() |
| 348 | .gap(px(4.)) |
| 349 | .child(div().flex().gap(px(4.)).children(RampPreset::ALL[..2].iter().map(|p| shape(*p, cx)))) |
| 350 | .child(div().flex().gap(px(4.)).children(RampPreset::ALL[2..].iter().map(|p| shape(*p, cx)))); |
| 351 | let verbs = div() |
| 352 | .flex() |
| 353 | .gap(px(4.)) |
| 354 | .child(dark_ghost_button( |
| 355 | "ramp-add-key", |
| 356 | "Add key at playhead", |
| 357 | "", |
| 358 | false, |
| 359 | cx.listener(|this, _: &ClickEvent, _, cx| { |
| 360 | this.add_ramp_key(cx); |
| 361 | }), |
| 362 | )) |
| 363 | .child(dark_ghost_button( |
| 364 | "ramp-clear", |
| 365 | "Clear ramp", |
| 366 | "", |
| 367 | false, |
| 368 | cx.listener(|this, _: &ClickEvent, _, cx| { |
| 369 | this.clear_ramp(cx); |
| 370 | }), |
| 371 | )); |
| 372 | div() |
| 373 | .flex() |
| 374 | .flex_col() |
| 375 | .gap(px(6.)) |
| 376 | .child(graph) |
| 377 | .child(caption) |
| 378 | .children(rows) |
| 379 | .child(shapes) |
| 380 | .child(verbs) |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /// The curve's height on the card. |
| 385 | const RAMP_GRAPH_H: f32 = 120.; |
| 386 | /// A key's diamond radius: the plot is inset this far from each side, so an end key is drawn whole. |
| 387 | const RAMP_KEY_R: f32 = 4.; |
| 388 | /// The left gutter that holds the speed labels, outside the plot. |
| 389 | const RAMP_GUTTER_W: f32 = 40.; |
| 390 | /// The caption row under the plot, reserved so it never moves the list. |
| 391 | const RAMP_CAPTION_H: f32 = 18.; |
| 392 | |
| 393 | #[cfg(test)] |
| 394 | mod tests { |
| 395 | use super::*; |
| 396 | |
| 397 | #[test] |
| 398 | fn the_readout_is_the_number_a_person_would_type() { |
| 399 | assert_eq!(speed_text(Speed::NORMAL), "1×"); |
| 400 | assert_eq!(speed_text(Speed::from_permille(1500)), "1.5×"); |
| 401 | assert_eq!(speed_text(Speed::from_permille(250)), "0.25×"); |
| 402 | assert_eq!(speed_text(Speed::from_permille(4000)), "4×"); |
| 403 | assert_eq!(speed_text(Speed::from_permille(1050)), "1.05×"); |
| 404 | } |
| 405 | |
| 406 | #[test] |
| 407 | fn the_chips_are_named_bare() { |
| 408 | let names: Vec<String> = SPEED_PRESETS.into_iter().map(preset_text).collect(); |
| 409 | assert_eq!(names, ["0.25", "0.5", "1", "1.5", "2", "4"]); |
| 410 | } |
| 411 | } |