diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index d5ad00b..92fb52d 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -1304,6 +1304,11 @@ struct Player { opacity_drag: bool, /// The opacity row's bar, recorded at prepaint like the transform bars. opacity_bar: Rc>>, + /// The easing curve editor's box, recorded at prepaint: a press and a drag on it are read + /// against it. One editor is on screen at a time, since one card is open at a time. + curve_bounds: Rc>>, + /// 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 /// reason, one per band. transform_bars: [Rc>>; TRANSFORM_BANDS.len()], @@ -1807,6 +1812,8 @@ fn main() { transform_dragging: false, opacity_drag: false, opacity_bar: Rc::default(), + curve_bounds: Rc::default(), + curve_drag: None, transform_bars: std::array::from_fn(|_| Rc::default()), pending_transform: None, // Empty until the first frame is pumped, which draws as a diff --git a/crates/app/src/player/library.rs b/crates/app/src/player/library.rs index c427947..fe4fb19 100644 --- a/crates/app/src/player/library.rs +++ b/crates/app/src/player/library.rs @@ -1063,6 +1063,7 @@ impl Player { self.pending_viz = None; self.transform_dragging = false; self.pending_transform = None; + self.curve_drag = None; self.pending_speed = None; self.displayed = 0; self.dropped = 0; diff --git a/crates/app/src/player/timeline_edit.rs b/crates/app/src/player/timeline_edit.rs index d6ec765..413ed1b 100644 --- a/crates/app/src/player/timeline_edit.rs +++ b/crates/app/src/player/timeline_edit.rs @@ -2988,6 +2988,15 @@ impl Player { } return; } + // An easing curve's handle follows the pointer over the whole window, the way the + // sliders do; a button-up move ends it with its write. + if self.curve_drag.is_some() { + match event.pressed_button { + Some(MouseButton::Left) => self.drag_curve(event.position, cx), + _ => self.end_curve_drag(event.position, cx), + } + return; + } // A colour wheel's puck or master bar: the same live writes. if let Some((wheel, master)) = self.color_wheel_drag { if event.pressed_button == Some(MouseButton::Left) { @@ -3121,6 +3130,11 @@ impl Player { self.gain_release(cx); return; } + // The curve's handle lands where the hand let go, as one undo step. + if self.curve_drag.is_some() { + self.end_curve_drag(event.position, cx); + return; + } if self.pan_scrub_end(cx) || self.title_scrub_end(cx) || self.cue_num_scrub_end(cx) { return; } diff --git a/crates/app/src/ui/cards/curve.rs b/crates/app/src/ui/cards/curve.rs new file mode 100644 index 0000000..ad7a00f --- /dev/null +++ b/crates/app/src/ui/cards/curve.rs @@ -0,0 +1,182 @@ +//! The easing curve editor: a square showing a key's cubic bezier, with one handle per +//! control point. A press takes the handle nearer the pointer, and a drag moves it. +//! +//! The drag keeps a draft curve on the player and writes the key once, when the button comes +//! up, so the whole gesture is one undo step. The root's move and release handlers carry the +//! drag past the editor's own box, the way the sliders' drags are carried. + +use std::cell::Cell; +use std::rc::Rc; + +use crate::*; +use engine::project::KeyKind; +use gpui::Rgba; +use engine::transform::{Bezier, KeyEase, ease_shape}; + +/// The editor's side in pixels. +pub(crate) const CURVE_PX: f32 = 140.; +/// The straight segments the curve is drawn with. +const CURVE_STEPS: usize = 48; +/// A handle's radius in pixels. +const HANDLE_R: f32 = 5.; + +/// 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, + /// `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, +} + +/// The curve a key takes the first time it is set to custom: CSS `ease`. +pub(crate) fn css_ease() -> Option { + Bezier::new(0.25, 0.1, 0.25, 1.0) +} + +/// `at` (window pixels) as a point of the editor's unit square, with y pointing up, clamped to +/// `0..=1` on both axes. `None` while the editor has not been measured yet. +fn unit_point(at: Point, bounds: Bounds) -> Option<(f32, f32)> { + let (w, h) = (f32::from(bounds.size.width), f32::from(bounds.size.height)); + if w <= 0. || h <= 0. { + return None; + } + let x = (f32::from(at.x - bounds.left()) / w).clamp(0., 1.); + let y = (1. - f32::from(at.y - bounds.top()) / h).clamp(0., 1.); + Some((x, y)) +} + +/// The handle of `b` nearer to the unit point `at`: `0` or `1`. +fn nearest_handle(b: Bezier, at: (f32, f32)) -> usize { + let [x1, y1, x2, y2] = b.points(); + let dist = |hx: f32, hy: f32| (hx - at.0).powi(2) + (hy - at.1).powi(2); + if dist(x1, y1) <= dist(x2, y2) { 0 } else { 1 } +} + +/// `b` with handle `handle` moved to the unit point `to`, `None` if the result is not a curve. +fn moved(b: Bezier, handle: usize, to: (f32, f32)) -> Option { + let [x1, y1, x2, y2] = b.points(); + match handle { + 0 => Bezier::new(to.0, to.1, x2, y2), + _ => Bezier::new(x1, y1, to.0, to.1), + } +} + +/// One straight stroke from `from` to `to`, `width` pixels thick. +fn paint_segment(window: &mut Window, from: Point, to: Point, width: f32, colour: Rgba) { + let mut path = PathBuilder::stroke(px(width)); + path.move_to(from); + path.line_to(to); + if let Ok(path) = path.build() { + window.paint_path(path, colour); + } +} + +/// A round handle of [`HANDLE_R`] pixels centred on `centre`. +fn paint_handle(window: &mut Window, centre: Point, colour: Rgba) { + let r = px(HANDLE_R); + let bounds = Bounds::new(point(centre.x - r, centre.y - r), size(r * 2., r * 2.)); + window.paint_quad(gpui::quad( + bounds, + gpui::Corners::all(r), + colour, + gpui::Edges::all(px(0.)), + colour, + gpui::BorderStyle::default(), + )); +} + +/// 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. +pub(super) fn curve_editor( + id: impl Into, + kind: KeyKind, + shown: Bezier, + bounds: Rc>>, + cx: &mut Context, +) -> AnyElement { + let t = crate::ui::theme::tokens(); + let (accent, ink3, well, line) = (t.accent, t.ink3, t.well, t.line); + let [x1, y1, x2, y2] = shown.points(); + let samples: Vec = (0..=CURVE_STEPS) + .map(|i| ease_shape(KeyEase::Bezier(shown), i as f32 / CURVE_STEPS as f32)) + .collect(); + let paint = canvas( + move |measured, _, _| bounds.set(measured), + move |b: Bounds, _, window, _| { + let at = |x: f32, y: f32| point(b.origin.x + b.size.width * x, b.origin.y + b.size.height * (1. - y)); + // The faint diagonal, the guides from each end to its handle, then the curve and the handles. + paint_segment(window, at(0., 0.), at(1., 1.), 1., rgba(line)); + paint_segment(window, at(0., 0.), at(x1, y1), 1., rgb(ink3)); + paint_segment(window, at(1., 1.), at(x2, y2), 1., rgb(ink3)); + let mut path = PathBuilder::stroke(px(2.)); + for (i, &y) in samples.iter().enumerate() { + let p = at(i as f32 / CURVE_STEPS as f32, y); + match i { + 0 => path.move_to(p), + _ => path.line_to(p), + } + } + if let Ok(path) = path.build() { + window.paint_path(path, rgb(accent)); + } + paint_handle(window, at(x1, y1), rgb(accent)); + paint_handle(window, at(x2, y2), rgb(accent)); + }, + ) + .absolute() + .size_full(); + div() + .id(id) + .relative() + .flex_none() + .size(px(CURVE_PX)) + .rounded(px(3.)) + .bg(rgb(well)) + .border_1() + .border_color(rgba(line)) + .cursor_pointer() + .on_mouse_down( + MouseButton::Left, + cx.listener(move |this, event: &MouseDownEvent, _, cx| this.press_curve(kind, event.position, cx)), + ) + .child(paint) + .into_any_element() +} + +impl Player { + /// 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 { + return; + }; + let Some(unit) = unit_point(at, self.curve_bounds.get()) else { + return; + }; + let handle = nearest_handle(shown, unit); + self.curve_drag = Some(CurveDrag { kind, 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 unit = unit_point(at, self.curve_bounds.get()); + if let (Some(unit), Some(drag)) = (unit, self.curve_drag.as_mut()) + && let Some(draft) = moved(drag.draft, drag.handle, unit) + { + drag.draft = draft; + } + cx.notify(); + } + + /// Ends the drag where the button came up: the draft is written to the key on the 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() { + self.set_key_ease_here(drag.kind, KeyEase::Bezier(drag.draft), cx); + } + cx.notify(); + } +} diff --git a/crates/app/src/ui/cards/keys.rs b/crates/app/src/ui/cards/keys.rs index e4e2568..a75d79c 100644 --- a/crates/app/src/ui/cards/keys.rs +++ b/crates/app/src/ui/cards/keys.rs @@ -6,9 +6,10 @@ 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 crate::*; use engine::project::KeyKind; -use engine::transform::KeyEase; +use engine::transform::{Bezier, KeyEase}; /// What a key of `kind` is called in a row and its tooltips. fn kind_label(kind: KeyKind) -> &'static str { @@ -140,17 +141,26 @@ impl Player { 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. - let eases = kit::segmented_equal( - "key-ease", - &KeyEase::ALL.map(|e| Segment::text(ease_text(e))), - here.and_then(|current| KeyEase::ALL.iter().position(|e| *e == current)).unwrap_or(usize::MAX), - here.is_some(), - None, - move |pick, _, app| { - me.update(app, |this, cx| this.set_key_ease_here(kind, KeyEase::ALL[pick], cx)); - }, - ); + // the first key never moves the rows under it. 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(); + let selected = match here { + Some(KeyEase::Bezier(_)) => custom_at, + 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| { + // 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), + (None, Some(KeyEase::Bezier(b))) => Some(KeyEase::Bezier(b)), + (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)); + } + }); // 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() @@ -169,6 +179,26 @@ impl Player { .tooltip(overlay_tip_hover("Add a key first", "", None)), ) }); + // 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(KeyEase::Bezier(b))) => Some(b), + _ => None, + }; + let editor = shown.map(|shown| { + div() + .w_full() + .flex() + .justify_center() + .child(curve::curve_editor( + ("key-curve", kind_id(kind)), + kind, + shown, + self.curve_bounds.clone(), + 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() @@ -202,6 +232,7 @@ impl Player { })), ) .child(eases) + .children(editor) .into_any_element() } diff --git a/crates/app/src/ui/cards/mod.rs b/crates/app/src/ui/cards/mod.rs index 44876ab..8cfe962 100644 --- a/crates/app/src/ui/cards/mod.rs +++ b/crates/app/src/ui/cards/mod.rs @@ -6,6 +6,7 @@ mod caption; mod color; mod common; +pub(crate) mod curve; mod effects; mod eq; mod export;