diff --git a/crates/app/src/player/generators.rs b/crates/app/src/player/generators.rs index 97383b7..528cb52 100644 --- a/crates/app/src/player/generators.rs +++ b/crates/app/src/player/generators.rs @@ -377,10 +377,12 @@ impl Player { pub(crate) fn gen_key(&mut self, key: &str, ctrl: bool, alt: bool, key_char: Option<&str>, cx: &mut Context) { let Some((paint, field)) = self.gen_hex.as_mut() else { return }; let number = matches!(paint, GenPaint::Width | GenPaint::Radius | GenPaint::Angle | GenPaint::Count | GenPaint::Title(_)); + // A title's key shifts run both ways, so they may be typed negative. + let signed = matches!(paint, GenPaint::Title(crate::player::titling::ScrubField::KeyX | crate::player::titling::ScrubField::KeyY)); let result = field.key(key, ctrl, alt, key_char, if number { 8 } else { 7 }); match paint { GenPaint::Key(_) => field.text.retain(|c| c.is_ascii_hexdigit() || c == '#'), - _ if number => field.text.retain(|c| c.is_ascii_digit() || c == '.' || c == ','), + _ if number => field.text.retain(|c| c.is_ascii_digit() || c == '.' || c == ',' || (signed && c == '-')), _ => field.text.retain(|c| c.is_ascii_alphanumeric() || c == '#'), } match result { @@ -484,6 +486,9 @@ impl Player { cx.notify(); return; }; + if field.is_key() { + return self.set_title_key_percent(field, v, cx); + } let fps = self.session.as_ref().map_or(25., |s| s.meta().frame_rate).max(1.); let frames = (v.max(0.) * fps).round() as u32; self.write_title( @@ -499,6 +504,8 @@ impl Player { } F::AnimLength => t.anim_frames = frames.clamp(1, t.frames.max(1)), F::BoxOpacity => t.style.box_color[3] = ((v.round().clamp(0., 100.) as u32 * 255 + 50) / 100) as u8, + // The key fields were written by `set_title_key_percent` above. + F::KeyX | F::KeyY | F::KeyScale | F::KeyOpacity => {} }, cx, ); diff --git a/crates/app/src/player/titling.rs b/crates/app/src/player/titling.rs index 8886e83..b334302 100644 --- a/crates/app/src/player/titling.rs +++ b/crates/app/src/player/titling.rs @@ -9,6 +9,8 @@ use crate::*; use engine::Title; use engine::project::{MAX_TITLE_CHARS, TitlePreset as Preset}; use engine::text::{DEFAULT_SIZE, SIZE_RANGE, TextStyle}; +use engine::titleanim::{KEY_SCALE_RANGE, TitleKey}; +use engine::transform::{KeyEase, sample}; /// How long a new title is up, in seconds. pub(crate) const NEW_SECS: f64 = 5.0; @@ -28,15 +30,32 @@ pub(crate) enum ScrubField { BoxOpacity, /// The title's block dragged along the timeline: `Start` in timeline pixels. Move, + /// The title's own key at the playhead: its horizontal shift, in percent of the width. + KeyX, + /// The key's vertical shift, in percent of the height, down positive. + KeyY, + /// The key's size multiplier, in percent. + KeyScale, + /// The key's opacity multiplier, in percent. + KeyOpacity, } -/// A title number being scrubbed: where the press was, the value it had, and the value now. +impl ScrubField { + /// Whether the field is one of the title's key numbers, which write the key at the playhead. + pub(crate) fn is_key(self) -> bool { + matches!(self, ScrubField::KeyX | ScrubField::KeyY | ScrubField::KeyScale | ScrubField::KeyOpacity) + } +} + +/// A title number being scrubbed: where the press was, the value it had, the value now, and the +/// title frame a key field writes its key at. #[derive(Clone, Copy, Debug, PartialEq)] pub(crate) struct TitleScrub { pub(crate) field: ScrubField, pub(crate) x0: f32, pub(crate) v0: i64, pub(crate) cur: i64, + pub(crate) at: u32, } /// Which colour of the selected title a hex field is typing: the letters' or the box's. @@ -82,6 +101,10 @@ pub(crate) fn scrub_value(field: ScrubField, v0: i64, dx: f32, fps: f64, pps: f6 ScrubField::Length => (v0 + by(4.)).clamp(1, max_end), ScrubField::AnimLength => (v0 + by(4.)).clamp(1, i64::from(frames.max(1))), ScrubField::BoxOpacity => (v0 + by(2.)).clamp(0, 100), + ScrubField::KeyX | ScrubField::KeyY | ScrubField::KeyScale | ScrubField::KeyOpacity => { + let (lo, hi) = key_range(field); + (v0 + by(2.)).clamp(lo, hi) + } ScrubField::Move => { let moved = if pps > 0. { (f64::from(dx) / pps * fps).round() as i64 } else { 0 }; (v0 + moved).clamp(0, max_end - i64::from(frames)) @@ -123,7 +146,7 @@ impl Player { t.text = field.text.clone(); } if let Some(s) = self.title_scrub { - set_field(&mut t, s.field, s.cur); + set_field(&mut t, s.field, s.cur, s.at); } t } @@ -263,6 +286,11 @@ impl Player { self.named_step("Title text", |this| this.write_title_now(edit, cx)) } + /// The same write as [`Self::write_title`], named as a keyframe change in the undo history. + pub(crate) fn write_title_key(&mut self, edit: impl FnOnce(&mut Title), cx: &mut Context) -> bool { + self.named_step("Title keyframe", |this| this.write_title_now(edit, cx)) + } + fn write_title_now(&mut self, edit: impl FnOnce(&mut Title), cx: &mut Context) -> bool { let Some(idx) = self.title_sel else { return false; @@ -352,8 +380,9 @@ impl Player { let Some((_, t)) = self.title_target() else { return; }; - let v0 = get_field(&t, field); - self.title_scrub = Some(TitleScrub { field, x0: x, v0, cur: v0 }); + let at = self.title_key_frame(&t).unwrap_or(0); + let v0 = get_field(&t, field, at); + self.title_scrub = Some(TitleScrub { field, x0: x, v0, cur: v0, at }); } /// The pointer moved with a title scrub in flight: the number follows, shown live. @@ -377,36 +406,196 @@ impl Player { return false; }; if s.cur != s.v0 { - self.write_title(|t| set_field(t, s.field, s.cur), cx); + match s.field.is_key() { + true => self.write_title_key(|t| set_field(t, s.field, s.cur, s.at), cx), + false => self.write_title(|t| set_field(t, s.field, s.cur, s.at), cx), + }; } cx.notify(); true } + + /// The frame of the title's own keys the playhead is on, counted from the title's first frame. + /// `None` when the playhead is outside the title, where no key can be written. + pub(crate) fn title_key_frame(&self, t: &Title) -> Option { + let now = self.key_frame(); + (t.start <= now && now < t.end()).then(|| now - t.start) + } + + /// The ease of the title's key on the playhead, `None` with no key there. + pub(crate) fn title_key_here(&self, idx: usize) -> Option { + let t = self.session.as_ref()?.titles().get(idx)?; + let at = self.title_key_frame(t)?; + t.keys.iter().find(|k| k.at == at).map(|k| k.ease) + } + + /// The diamond: adds a key at the playhead holding what the title's keys give there, or takes + /// the key that is there off. One undo step. + pub(crate) fn toggle_title_key(&mut self, cx: &mut Context) { + let Some((_, stored)) = self.title_target() else { + return; + }; + let Some(at) = self.title_key_frame(&stored) else { + self.notify_user("The playhead is outside this title".into()); + cx.notify(); + return; + }; + let there = stored.keys.iter().any(|k| k.at == at); + self.write_title_key( + |t| match there { + true => t.keys.retain(|k| k.at != at), + false => { + key_index_at(t, at); + } + }, + cx, + ); + } + + /// Sets the ease of the title's key on the playhead to the one the ease row picked, one undo + /// step. Nothing changes with no key there, where the row is disabled. + pub(crate) fn set_title_key_ease_here(&mut self, idx: usize, ease: KeyEase, cx: &mut Context) { + if self.title_sel != Some(idx) { + return; + } + let Some((_, stored)) = self.title_target() else { + return; + }; + let Some(at) = self.title_key_frame(&stored) else { + return; + }; + self.write_title_key( + |t| { + if let Some(k) = t.keys.iter_mut().find(|k| k.at == at) { + k.ease = ease; + } + }, + cx, + ); + } + + /// Seeks the playhead to the next (`forward`) or the previous key of the selected title, and + /// says so when there is none that way. + pub(crate) fn jump_title_key(&mut self, forward: bool, cx: &mut Context) { + let Some((_, t)) = self.title_target() else { + return; + }; + let now = self.key_frame(); + let frames: Vec = t.keys.iter().filter(|k| k.at < t.frames).map(|k| t.start + k.at).collect(); + let hit = match forward { + true => frames.iter().copied().find(|&f| f > now), + false => frames.iter().copied().rev().find(|&f| f < now), + }; + match hit { + Some(f) => { + let at = f64::from(f) / self.active_fps(); + self.seek(at, cx); + } + None => { + self.notify_user(if forward { "No later key on this title" } else { "No earlier key on this title" }.into()); + cx.notify(); + } + } + } + + /// Writes a typed percent as the title's key field at the playhead, one undo step. The key is + /// added there if the title has none. Outside the title there is no key, so it says so. + pub(crate) fn set_title_key_percent(&mut self, field: ScrubField, v: f64, cx: &mut Context) { + let Some((_, stored)) = self.title_target() else { + return; + }; + let Some(at) = self.title_key_frame(&stored) else { + self.notify_user("The playhead is outside this title".into()); + cx.notify(); + return; + }; + self.write_title_key(|t| set_field(t, field, v.round() as i64, at), cx); + } } -/// One scrubbable number of a title. -fn get_field(t: &Title, field: ScrubField) -> i64 { +/// One scrubbable number of a title. A key field reads the title's key at `at`, a frame counted +/// from the title's first frame, or the rest a title with no keys holds. +pub(crate) fn get_field(t: &Title, field: ScrubField, at: u32) -> i64 { match field { ScrubField::Size => i64::from(t.style.size), ScrubField::Start | ScrubField::Move => i64::from(t.start), ScrubField::Length => i64::from(t.frames), ScrubField::AnimLength => i64::from(t.anim_frames), ScrubField::BoxOpacity => (i64::from(t.style.box_color[3]) * 100 + 127) / 255, + ScrubField::KeyX | ScrubField::KeyY | ScrubField::KeyScale | ScrubField::KeyOpacity => { + (f64::from(key_number(&held_at(&t.keys, at), field)) * 100.).round() as i64 + } } } -fn set_field(t: &mut Title, field: ScrubField, v: i64) { - let v = v.max(0) as u32; +fn set_field(t: &mut Title, field: ScrubField, v: i64, at: u32) { + let u = v.max(0) as u32; match field { - ScrubField::Size => t.style.size = v, - ScrubField::Start | ScrubField::Move => t.start = v, + ScrubField::KeyX | ScrubField::KeyY | ScrubField::KeyScale | ScrubField::KeyOpacity => set_key_field(t, field, v, at), + ScrubField::Size => t.style.size = u, + ScrubField::Start | ScrubField::Move => t.start = u, ScrubField::Length => { - t.frames = v.max(1); + t.frames = u.max(1); t.fade_in = t.fade_in.min(t.frames); t.fade_out = t.fade_out.min(t.frames); } - ScrubField::AnimLength => t.anim_frames = v.clamp(1, t.frames.max(1)), - ScrubField::BoxOpacity => t.style.box_color[3] = ((v.min(100) * 255 + 50) / 100) as u8, + ScrubField::AnimLength => t.anim_frames = u.clamp(1, t.frames.max(1)), + ScrubField::BoxOpacity => t.style.box_color[3] = ((u.min(100) * 255 + 50) / 100) as u8, + } +} + +/// The least and the most a key field may read, in percent: the engine's limits as the card shows +/// them. A shift runs both ways, the scale and the opacity do not. +fn key_range(field: ScrubField) -> (i64, i64) { + match field { + ScrubField::KeyScale => ((KEY_SCALE_RANGE.0 * 100.).round() as i64, (KEY_SCALE_RANGE.1 * 100.).round() as i64), + ScrubField::KeyOpacity => (0, 100), + _ => (-100, 100), + } +} + +/// The number a key holds for a key field: a share of the width or height for the shifts, a +/// multiple for the scale and the opacity. +fn key_number(k: &TitleKey, field: ScrubField) -> f32 { + match field { + ScrubField::KeyX => k.x, + ScrubField::KeyY => k.y, + ScrubField::KeyScale => k.scale, + _ => k.opacity, + } +} + +/// The key the title's keys give at `at`: the interpolated one, or the rest a title with no keys +/// holds (no shift, full size, full opacity). +fn held_at(keys: &[TitleKey], at: u32) -> TitleKey { + sample(keys, f64::from(at)).unwrap_or(TitleKey { at, x: 0., y: 0., scale: 1., opacity: 1., ease: KeyEase::Linear }) +} + +/// The index of the key at `at`, adding one first when there is none. A new key holds what the +/// title's keys give there, so adding it moves nothing. +fn key_index_at(t: &mut Title, at: u32) -> usize { + match t.keys.binary_search_by_key(&at, |k| k.at) { + Ok(i) => i, + Err(i) => { + let held = held_at(&t.keys, at); + t.keys.insert(i, TitleKey { at, ease: KeyEase::Linear, ..held }); + i + } + } +} + +/// Writes a key field's percent into the title's key at `at`, adding that key when there is none. +/// The value is held to the engine's limits. +fn set_key_field(t: &mut Title, field: ScrubField, v: i64, at: u32) { + let (lo, hi) = key_range(field); + let share = v.clamp(lo, hi) as f32 / 100.; + let i = key_index_at(t, at); + let k = &mut t.keys[i]; + match field { + ScrubField::KeyX => k.x = share.clamp(-1., 1.), + ScrubField::KeyY => k.y = share.clamp(-1., 1.), + ScrubField::KeyScale => k.scale = share.clamp(KEY_SCALE_RANGE.0, KEY_SCALE_RANGE.1), + _ => k.opacity = share.clamp(0., 1.), } } @@ -468,10 +657,20 @@ mod tests { let mut t = title(); t.fade_in = 40; t.fade_out = 60; - set_field(&mut t, ScrubField::Length, 30); + set_field(&mut t, ScrubField::Length, 30, 0); assert_eq!((t.frames, t.fade_in, t.fade_out), (30, 30, 30)); } + #[test] + fn a_key_field_adds_its_key_at_the_playhead_and_holds_the_limits() { + let mut t = title(); + set_field(&mut t, ScrubField::KeyX, 150, 10); + assert_eq!((t.keys.len(), t.keys[0].at, t.keys[0].x), (1, 10, 1.)); + assert_eq!(get_field(&t, ScrubField::KeyX, 10), 100); + set_field(&mut t, ScrubField::KeyScale, 0, 10); + assert_eq!(t.keys[0].scale, KEY_SCALE_RANGE.0); + } + #[test] fn the_lower_third_is_bold_bottom_left_and_a_hex_colour_parses_or_is_refused() { let mut t = title(); @@ -484,9 +683,9 @@ mod tests { assert_eq!(parse_hex(bad), None, "{bad}"); } let mut t = title(); - set_field(&mut t, ScrubField::BoxOpacity, 50); + set_field(&mut t, ScrubField::BoxOpacity, 50, 0); assert_eq!(t.style.box_color[3], 128); - assert_eq!(get_field(&t, ScrubField::BoxOpacity), 50); + assert_eq!(get_field(&t, ScrubField::BoxOpacity, 0), 50); } #[test] diff --git a/crates/app/src/ui/cards/curve.rs b/crates/app/src/ui/cards/curve.rs index 3f8a40f..ee58708 100644 --- a/crates/app/src/ui/cards/curve.rs +++ b/crates/app/src/ui/cards/curve.rs @@ -28,9 +28,7 @@ pub(crate) enum CurveTarget { Key(KeyKind), /// The key on the playhead of the effect at this stack slot of the open clip. Effect(usize), - /// A title's key, by title index. No title key takes an ease yet, so a drag on this target - /// is read but never written: a later change wires it up in `end_curve_drag`. - #[allow(dead_code)] + /// The title's key on the playhead, by title index. Title(usize), } @@ -159,13 +157,12 @@ pub(super) fn curve_editor( } impl Player { - /// The ease the target's key on the playhead holds, `None` with no key there. A title takes - /// no ease yet, so its target has none. + /// The ease the target's key on the playhead holds, `None` with no key there. pub(crate) fn ease_here(&self, target: CurveTarget) -> Option { match target { CurveTarget::Key(kind) => self.key_here(kind), CurveTarget::Effect(slot) => self.effect_key_ease_here(slot), - CurveTarget::Title(_) => None, + CurveTarget::Title(idx) => self.title_key_here(idx), } } @@ -201,7 +198,7 @@ impl Player { } /// Ends the drag where the button came up: the draft is written to the target's key on the - /// playhead as one undo step. A title's draft is dropped for now. + /// playhead as one undo step. pub(crate) fn end_curve_drag(&mut self, at: Point, cx: &mut Context) { self.drag_curve(at, cx); if let Some(drag) = self.curve_drag.take() { @@ -209,7 +206,7 @@ impl Player { match drag.target { CurveTarget::Key(kind) => self.set_key_ease_here(kind, ease, cx), CurveTarget::Effect(slot) => self.set_effect_key_ease_here(slot, ease, cx), - CurveTarget::Title(_) => {} + CurveTarget::Title(idx) => self.set_title_key_ease_here(idx, ease, cx), } } cx.notify(); diff --git a/crates/app/src/ui/cards/title.rs b/crates/app/src/ui/cards/title.rs index d1e4114..f2260d6 100644 --- a/crates/app/src/ui/cards/title.rs +++ b/crates/app/src/ui/cards/title.rs @@ -4,7 +4,9 @@ //! step through [`Player::write_title`], and the viewer shows the words and numbers live. use super::common::*; -use crate::player::titling::{HexTarget, ScrubField, anim_default_frames, hex_of, preset_label}; +use super::curve::CurveTarget; +use super::keys::diamond; +use crate::player::titling::{HexTarget, ScrubField, anim_default_frames, get_field, hex_of, preset_label}; use engine::titleanim::TitleAnim; use engine::project::TitlePreset as Preset; use crate::ui::icons::Icon; @@ -276,7 +278,7 @@ impl Player { /// The Title section, or nothing while no title is selected. pub(crate) fn title_card(&self, room: Size, cx: &mut Context) -> Option { - let (_, stored) = self.title_target()?; + let (idx, stored) = self.title_target()?; let t = self.shown_title(&stored); let fps = self.session.as_ref().map_or(25., |s| s.meta().frame_rate).max(1.); // Every time reads in seconds with one decimal and its unit. @@ -440,6 +442,59 @@ impl Player { this.write_title(|t| t.style.boxed = !t.style.boxed, cx); })); + // The title's keys: the section reads and writes the key at the playhead, so the playhead has + // to be inside the title. Outside it the section dims and says why on hover. + let at = self.title_key_frame(&t); + let why_out: Option<&'static str> = at.is_none().then_some("Move the playhead into the title"); + let keyed = self.title_key_here(idx).is_some(); + let ink = match (keyed, !t.keys.is_empty()) { + (true, _) => tk.accent, + (false, true) => tk.ink1, + _ => tk.ink3, + }; + let tip: &str = match (at.is_some(), keyed) { + (false, _) => "Move the playhead into the title", + (true, true) => "Remove the title's key at the playhead", + (true, false) => "Key the title at the playhead", + }; + let key_dot = div() + .id("title-key-diamond") + .flex_none() + .size(px(crate::layout::redesign::CONTROL_M)) + .flex() + .items_center() + .justify_center() + .rounded(px(crate::layout::redesign::R_CONTROL)) + .cursor_pointer() + .hover(|s| s.bg(rgb(tk.e3))) + .tooltip(crate::ui::widgets::overlay_tip_hover(tip, "", None)) + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| this.toggle_title_key(cx))) + .child(diamond(12., ink, keyed)); + let (m_prev, m_next) = (cx.entity(), cx.entity()); + let nav = div() + .flex() + .items_center() + .gap(px(2.)) + .child(kit::icon_ghost("title-key-prev", Icon::ChevronLeft, "Previous key", why_out, move |_, app| { + m_prev.update(app, |this, cx| this.jump_title_key(false, cx)); + })) + .child(key_dot) + .child(kit::icon_ghost("title-key-next", Icon::ChevronRight, "Next key", why_out, move |_, app| { + m_next.update(app, |this, cx| this.jump_title_key(true, cx)); + })) + .when(at.is_none(), |d| d.opacity(0.5)); + let keys_row = row("Keyframes", nav); + let eases = div().w_full().when(at.is_none(), |d| d.opacity(0.5)).child(self.ease_picker( + CurveTarget::Title(idx), + move |this, ease, cx| this.set_title_key_ease_here(idx, ease, cx), + cx, + )); + // The four numbers read the key at the playhead, or the rest a title holds with no key there. + let key_val = |field: ScrubField| match at { + Some(at) => get_field(&t, field, at).to_string(), + None => "-".to_string(), + }; + // The header (icon, words, length) is the dock's, like a clip's; the remove verb ends the card. let remove = kit::Button::new("title-remove") .label("Remove title") @@ -491,6 +546,12 @@ impl Player { DRAG_HINT, cx, )) + .child(keys_row) + .child(eases) + .child(self.title_scrub_row("title-key-x", "Move across", key_val(ScrubField::KeyX), Some("%"), ScrubField::KeyX, why_out, DRAG_HINT, cx)) + .child(self.title_scrub_row("title-key-y", "Move down", key_val(ScrubField::KeyY), Some("%"), ScrubField::KeyY, why_out, DRAG_HINT, cx)) + .child(self.title_scrub_row("title-key-scale", "Scale", key_val(ScrubField::KeyScale), Some("%"), ScrubField::KeyScale, why_out, DRAG_HINT, cx)) + .child(self.title_scrub_row("title-key-opacity", "Opacity", key_val(ScrubField::KeyOpacity), Some("%"), ScrubField::KeyOpacity, why_out, DRAG_HINT, cx)) .child(div().flex().justify_start().child(remove)), ), )