diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 02f4a10..69e40fe 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -1304,10 +1304,10 @@ struct Player { opacity_drag: bool, /// The opacity row's bar, recorded at prepaint like the transform bars. opacity_bar: Rc>>, - /// Each easing curve editor's box, by key kind (placement, opacity, colour), recorded at - /// prepaint: a press and a drag on an editor are read against its own box. The transform - /// card can show two editors at once, one per key row. - curve_bounds: [Rc>>; 3], + /// Each easing curve editor's box, by the target it edits, recorded at prepaint: a press and + /// a drag on an editor are read against its own box. The transform card can show two editors + /// at once, one per key row, and an effect's editor sits beside them. + curve_bounds: std::cell::RefCell>>>>, /// A curve handle is held: its draft curve, written to the key when the button comes up. curve_drag: Option, /// Each slider's box, recorded at prepaint -- [`Self::color_bars`]' own diff --git a/crates/app/src/player/effects.rs b/crates/app/src/player/effects.rs index 1fbf2ae..d14a29d 100644 --- a/crates/app/src/player/effects.rs +++ b/crates/app/src/player/effects.rs @@ -217,6 +217,31 @@ impl Player { } } + /// The ease of the key the effect at stack position `slot` holds at the playhead, `None` with + /// no key there. + pub(crate) fn effect_key_ease_here(&self, slot: usize) -> Option { + let at = self.effects_src()?; + let shown = self.effects_shown(); + let effect = shown.get(slot)?; + effect.keys.iter().find(|k| k.at == at).map(|k| k.ease) + } + + /// Sets the ease of the key the effect at stack position `slot` holds at the playhead, as one + /// undo step. Nothing changes with no key there, which the easing row shows as disabled. + pub(crate) fn set_effect_key_ease_here(&mut self, slot: usize, ease: KeyEase, cx: &mut Context) { + let Some(at) = self.effects_src() else { + return cx.notify(); + }; + let mut stack = self.effects_shown(); + if let Some(e) = stack.get_mut(slot) + && let Ok(pos) = e.keys.binary_search_by_key(&at, |k| k.at) + { + e.keys[pos].ease = ease; + self.write_effects(stack, cx); + } + cx.notify(); + } + /// Follows the mask at stack position `slot` of the open clip from the playhead to the /// clip's end, or back to its start ([`engine::PlaybackSession::track_mask_start`]). The /// keys land when the track ends ([`Self::poll_tracks`]). diff --git a/crates/app/src/ui/cards/curve.rs b/crates/app/src/ui/cards/curve.rs index 92ec1e8..3f8a40f 100644 --- a/crates/app/src/ui/cards/curve.rs +++ b/crates/app/src/ui/cards/curve.rs @@ -20,10 +20,24 @@ const CURVE_STEPS: usize = 48; /// A handle's radius in pixels. const HANDLE_R: f32 = 5.; +/// Which key's ease a curve editor reads and writes. Each editor on screen has its own target, +/// so each reads its own box and its own drag. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub(crate) enum CurveTarget { + /// The key of this kind on the playhead of the open card's clip. + 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)] + Title(usize), +} + /// A handle being dragged: the key it belongs to, which control point it is, and the curve it /// has reached so far. pub(crate) struct CurveDrag { - pub(crate) kind: KeyKind, + pub(crate) target: CurveTarget, /// `0` for the control point near the start of the curve, `1` for the one near its end. pub(crate) handle: usize, pub(crate) draft: Bezier, @@ -86,11 +100,11 @@ fn paint_handle(window: &mut Window, centre: Point, colour: Rgba) { )); } -/// The curve editor for the key of `kind` whose ease is `shown`. `id` names it, and `bounds` -/// receives its box at prepaint, which the press and the drag are read against. +/// The curve editor for the ease of `target` as `shown`. `id` names it, and `bounds` receives its +/// box at prepaint, which the press and the drag are read against. pub(super) fn curve_editor( id: impl Into, - kind: KeyKind, + target: CurveTarget, shown: Bezier, bounds: Rc>>, cx: &mut Context, @@ -138,31 +152,46 @@ pub(super) fn curve_editor( .cursor_pointer() .on_mouse_down( MouseButton::Left, - cx.listener(move |this, event: &MouseDownEvent, _, cx| this.press_curve(kind, event.position, cx)), + cx.listener(move |this, event: &MouseDownEvent, _, cx| this.press_curve(target, event.position, cx)), ) .child(paint) .into_any_element() } 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. + 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, + } + } + + /// The box of the editor for `target`, made on first use and recorded at prepaint. + pub(crate) fn curve_bounds_for(&self, target: CurveTarget) -> Rc>> { + self.curve_bounds.borrow_mut().entry(target).or_default().clone() + } + /// A press on the editor takes the handle nearer to it and moves that handle to the press. /// Nothing starts unless the key on the playhead has a custom ease. - pub(crate) fn press_curve(&mut self, kind: KeyKind, at: Point, cx: &mut Context) { - let Some(KeyEase::Bezier(shown)) = self.key_here(kind) else { + pub(crate) fn press_curve(&mut self, target: CurveTarget, at: Point, cx: &mut Context) { + let Some(KeyEase::Bezier(shown)) = self.ease_here(target) else { return; }; - let Some(unit) = unit_point(at, self.curve_bounds[super::keys::kind_id(kind)].get()) else { + let Some(unit) = unit_point(at, self.curve_bounds_for(target).get()) else { return; }; let handle = nearest_handle(shown, unit); - self.curve_drag = Some(CurveDrag { kind, handle, draft: shown }); + self.curve_drag = Some(CurveDrag { target, handle, draft: shown }); self.drag_curve(at, cx); } /// Moves the held handle to the pointer. The draft is drawn until the button comes up. pub(crate) fn drag_curve(&mut self, at: Point, cx: &mut Context) { - let Some(kind) = self.curve_drag.as_ref().map(|d| d.kind) else { return cx.notify() }; - let unit = unit_point(at, self.curve_bounds[super::keys::kind_id(kind)].get()); + let Some(target) = self.curve_drag.as_ref().map(|d| d.target) else { return cx.notify() }; + let unit = unit_point(at, self.curve_bounds_for(target).get()); if let (Some(unit), Some(drag)) = (unit, self.curve_drag.as_mut()) && let Some(draft) = moved(drag.draft, drag.handle, unit) { @@ -171,12 +200,17 @@ impl Player { cx.notify(); } - /// Ends the drag where the button came up: the draft is written to the key on the playhead - /// as one undo step. + /// 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. 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() { - self.set_key_ease_here(drag.kind, KeyEase::Bezier(drag.draft), cx); + let ease = KeyEase::Bezier(drag.draft); + 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(_) => {} + } } cx.notify(); } diff --git a/crates/app/src/ui/cards/effects.rs b/crates/app/src/ui/cards/effects.rs index 05532e2..7cdc685 100644 --- a/crates/app/src/ui/cards/effects.rs +++ b/crates/app/src/ui/cards/effects.rs @@ -9,6 +9,7 @@ use crate::ui::icons::{Icon, IconSize}; use crate::ui::type_scale::{self, Typeset}; use crate::ui::kit::{self, Segment}; use crate::ui::widgets::{SliderRow, SliderTyped, slider_row}; +use super::curve::CurveTarget; use crate::*; use engine::effects::{BlendMode, Effect, EffectKind, MAX_EFFECTS}; @@ -438,6 +439,23 @@ impl Player { controls.push(control); } } + // An effect with keys shows the easing of the one on the playhead under its settings, with + // the same chips and curve as the transform card; with no key there the row is disabled. + if !e.keys.is_empty() { + controls.push( + div() + .flex() + .flex_col() + .gap(px(4.)) + .child(dark_row_label("Easing", false)) + .child(self.ease_picker( + CurveTarget::Effect(i), + move |this, ease, cx| this.set_effect_key_ease_here(i, ease, cx), + cx, + )) + .into_any_element(), + ); + } // A mask follows the picture under it: the Track row sits under its controls. if matches!(e.kind, EffectKind::RectMask | EffectKind::EllipseMask) { controls.push(self.track_row(i, cx)); diff --git a/crates/app/src/ui/cards/keys.rs b/crates/app/src/ui/cards/keys.rs index 0fe4760..00bf4c0 100644 --- a/crates/app/src/ui/cards/keys.rs +++ b/crates/app/src/ui/cards/keys.rs @@ -6,7 +6,7 @@ use crate::ui::icons::Icon; use crate::ui::kit::{self, Segment}; use crate::ui::type_scale::{self, Typeset}; use crate::ui::widgets::*; -use super::curve; +use super::curve::{self, CurveTarget}; use crate::*; use engine::project::KeyKind; use engine::transform::{Bezier, KeyEase}; @@ -138,10 +138,62 @@ impl Player { .child(diamond(12., ink, here.is_some())); let (m1, m2) = (cx.entity(), cx.entity()); let none = (!keyed).then_some("No keys yet"); + let eases = self.ease_picker( + CurveTarget::Key(kind), + move |this, ease, cx| this.set_key_ease_here(kind, ease, cx), + cx, + ); + // The colour card's rows run to its edge; the transform card's sit 6px in. + let inset = if kind == KeyKind::Color { 0. } else { 6. }; + div() + .id(("key-row", kind_id(kind))) + .w_full() + .px(px(inset)) + .flex() + .flex_col() + .gap(px(4.)) + .child( + div() + .w_full() + .flex() + .items_center() + .gap(px(2.)) + .child( + div() + .flex_1() + .min_w(px(0.)) + .truncate() + .type_style(type_scale::body()) + .text_color(rgb(if keyed { t.ink1 } else { t.ink2 })) + .child(row_label(kind)), + ) + .child(kit::icon_ghost("key-prev", Icon::ChevronLeft, "Previous key", none, move |_, app| { + m1.update(app, |this, cx| this.jump_key(kind, false, cx)); + })) + .child(diamond) + .child(kit::icon_ghost("key-next", Icon::ChevronRight, "Next key", none, move |_, app| { + m2.update(app, |this, cx| this.jump_key(kind, true, cx)); + })), + ) + .child(eases) + .into_any_element() + } + + /// The easing row of one target: the five presets and a sixth chip for the custom curve, then, + /// under it, the curve editor while the key on the playhead is custom or a handle of this + /// target is held. The row is always there: without a key on the playhead it is disabled, and + /// its reason rides a clear layer over the chips (a tooltip on the row itself did not reach the + /// pointer past the chips). `on_pick` writes the ease the row picked. + pub(super) fn ease_picker( + &self, + target: CurveTarget, + on_pick: impl Fn(&mut Player, KeyEase, &mut Context) + 'static, + cx: &mut Context, + ) -> AnyElement { + let here = self.ease_here(target); let me = cx.entity(); - // The ease of the key on the playhead gets a line of its own, whole words. The line is - // always there: without a key on the playhead it is disabled (and says why), so setting - // the first key never moves the rows under it. The sixth chip is the custom curve. + // The ease of the key on the playhead gets a line of its own, whole words. The sixth chip + // is the custom curve. let mut labels: Vec = KeyEase::ALL.iter().map(|e| Segment::text(ease_text(*e))).collect(); labels.push(Segment::text("Custom")); let custom_at = KeyEase::ALL.len(); @@ -150,7 +202,8 @@ impl Player { Some(current) => KeyEase::ALL.iter().position(|e| *e == current).unwrap_or(usize::MAX), None => usize::MAX, }; - let eases = kit::segmented_equal("key-ease", &labels, selected, here.is_some(), None, move |pick, _, app| { + let on_pick = Rc::new(on_pick); + let chips = kit::segmented_equal("key-ease", &labels, selected, here.is_some(), None, move |pick, _, app| { // A custom pick keeps the curve the key already has, or starts from CSS `ease`. let ease = match (KeyEase::ALL.get(pick), here) { (Some(preset), _) => Some(*preset), @@ -158,20 +211,18 @@ impl Player { (None, _) => curve::css_ease().map(KeyEase::Bezier), }; if let Some(ease) = ease { - me.update(app, |this, cx| this.set_key_ease_here(kind, ease, cx)); + me.update(app, |this, cx| on_pick(this, ease, cx)); } }); - // With no key on the playhead the row is disabled, and its reason rides a clear layer - // over the chips: a tooltip on the row itself did not reach the pointer past the chips. - let eases = div() - .id(("key-ease-row", kind_id(kind))) + let chips = div() + .id("key-ease-row") .relative() .w_full() - .child(eases) + .child(chips) .when(here.is_none(), |d| { d.child( div() - .id(("key-ease-why", kind_id(kind))) + .id("key-ease-why") .absolute() .top(px(0.)) .left(px(0.)) @@ -181,7 +232,7 @@ impl Player { }); // The curve shows the draft while a handle is held, else the key's own custom ease. let shown: Option = match (self.curve_drag.as_ref(), here) { - (Some(drag), _) if drag.kind == kind => Some(drag.draft), + (Some(drag), _) if drag.target == target => Some(drag.draft), (_, Some(KeyEase::Bezier(b))) => Some(b), _ => None, }; @@ -190,48 +241,16 @@ impl Player { .w_full() .flex() .justify_center() - .child(curve::curve_editor( - ("key-curve", kind_id(kind)), - kind, - shown, - self.curve_bounds[kind_id(kind)].clone(), - cx, - )) + .child(curve::curve_editor("curve", target, shown, self.curve_bounds_for(target), cx)) .into_any_element() }); - // The colour card's rows run to its edge; the transform card's sit 6px in. - let inset = if kind == KeyKind::Color { 0. } else { 6. }; div() - .id(("key-row", kind_id(kind))) + .id("ease-picker") .w_full() - .px(px(inset)) .flex() .flex_col() .gap(px(4.)) - .child( - div() - .w_full() - .flex() - .items_center() - .gap(px(2.)) - .child( - div() - .flex_1() - .min_w(px(0.)) - .truncate() - .type_style(type_scale::body()) - .text_color(rgb(if keyed { t.ink1 } else { t.ink2 })) - .child(row_label(kind)), - ) - .child(kit::icon_ghost("key-prev", Icon::ChevronLeft, "Previous key", none, move |_, app| { - m1.update(app, |this, cx| this.jump_key(kind, false, cx)); - })) - .child(diamond) - .child(kit::icon_ghost("key-next", Icon::ChevronRight, "Next key", none, move |_, app| { - m2.update(app, |this, cx| this.jump_key(kind, true, cx)); - })), - ) - .child(eases) + .child(chips) .children(editor) .into_any_element() } diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 684480a..11481f0 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -1104,7 +1104,7 @@ pub struct GainKey { /// Which animation a keyframe button or action is about: the placement /// ([`Clip::transform_keys`]), the opacity ([`Clip::opacity_keys`]) or the grade /// ([`Clip::color_keys`]). -#[derive(Clone, Copy, PartialEq, Eq, Debug)] +#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub enum KeyKind { Placement, Opacity,