✨ Custom bezier curve editor on the keyframe ease row: Custom chip, draggable handles, one undo step per drag
Browse files at this commitc21f6c16a5cb0d42dda2bb625deffb2de4083d94Parent: b0e72a46 changed files +248 -12
- crates/app/src/main.rs +7 -0
- crates/app/src/player/library.rs +1 -0
- crates/app/src/player/timeline_edit.rs +14 -0
- crates/app/src/ui/cards/curve.rs +182 -0
- crates/app/src/ui/cards/keys.rs +43 -12
- crates/app/src/ui/cards/mod.rs +1 -0
crates/app/src/main.rsmodified+7 -0
| @@ -1304,6 +1304,11 @@ struct Player { | |||
| 1304 | opacity_drag: bool, | 1304 | opacity_drag: bool, |
| 1305 | /// The opacity row's bar, recorded at prepaint like the transform bars. | 1305 | /// The opacity row's bar, recorded at prepaint like the transform bars. |
| 1306 | opacity_bar: Rc<Cell<Bounds<Pixels>>>, | 1306 | opacity_bar: Rc<Cell<Bounds<Pixels>>>, |
| 1307 | + /// The easing curve editor's box, recorded at prepaint: a press and a drag on it are read | ||
| 1308 | + /// against it. One editor is on screen at a time, since one card is open at a time. | ||
| 1309 | + curve_bounds: Rc<Cell<Bounds<Pixels>>>, | ||
| 1310 | + /// A curve handle is held: its draft curve, written to the key when the button comes up. | ||
| 1311 | + curve_drag: Option<ui::cards::curve::CurveDrag>, | ||
| 1307 | /// Each slider's box, recorded at prepaint -- [`Self::color_bars`]' own | 1312 | /// Each slider's box, recorded at prepaint -- [`Self::color_bars`]' own |
| 1308 | /// reason, one per band. | 1313 | /// reason, one per band. |
| 1309 | transform_bars: [Rc<Cell<Bounds<Pixels>>>; TRANSFORM_BANDS.len()], | 1314 | transform_bars: [Rc<Cell<Bounds<Pixels>>>; TRANSFORM_BANDS.len()], |
| @@ -1807,6 +1812,8 @@ fn main() { | |||
| 1807 | transform_dragging: false, | 1812 | transform_dragging: false, |
| 1808 | opacity_drag: false, | 1813 | opacity_drag: false, |
| 1809 | opacity_bar: Rc::default(), | 1814 | opacity_bar: Rc::default(), |
| 1815 | + curve_bounds: Rc::default(), | ||
| 1816 | + curve_drag: None, | ||
| 1810 | transform_bars: std::array::from_fn(|_| Rc::default()), | 1817 | transform_bars: std::array::from_fn(|_| Rc::default()), |
| 1811 | pending_transform: None, | 1818 | pending_transform: None, |
| 1812 | // Empty until the first frame is pumped, which draws as a | 1819 | // Empty until the first frame is pumped, which draws as a |
crates/app/src/player/library.rsmodified+1 -0
| @@ -1063,6 +1063,7 @@ impl Player { | |||
| 1063 | self.pending_viz = None; | 1063 | self.pending_viz = None; |
| 1064 | self.transform_dragging = false; | 1064 | self.transform_dragging = false; |
| 1065 | self.pending_transform = None; | 1065 | self.pending_transform = None; |
| 1066 | + self.curve_drag = None; | ||
| 1066 | self.pending_speed = None; | 1067 | self.pending_speed = None; |
| 1067 | self.displayed = 0; | 1068 | self.displayed = 0; |
| 1068 | self.dropped = 0; | 1069 | self.dropped = 0; |
crates/app/src/player/timeline_edit.rsmodified+14 -0
| @@ -2988,6 +2988,15 @@ impl Player { | |||
| 2988 | } | 2988 | } |
| 2989 | return; | 2989 | return; |
| 2990 | } | 2990 | } |
| 2991 | + // An easing curve's handle follows the pointer over the whole window, the way the | ||
| 2992 | + // sliders do; a button-up move ends it with its write. | ||
| 2993 | + if self.curve_drag.is_some() { | ||
| 2994 | + match event.pressed_button { | ||
| 2995 | + Some(MouseButton::Left) => self.drag_curve(event.position, cx), | ||
| 2996 | + _ => self.end_curve_drag(event.position, cx), | ||
| 2997 | + } | ||
| 2998 | + return; | ||
| 2999 | + } | ||
| 2991 | // A colour wheel's puck or master bar: the same live writes. | 3000 | // A colour wheel's puck or master bar: the same live writes. |
| 2992 | if let Some((wheel, master)) = self.color_wheel_drag { | 3001 | if let Some((wheel, master)) = self.color_wheel_drag { |
| 2993 | if event.pressed_button == Some(MouseButton::Left) { | 3002 | if event.pressed_button == Some(MouseButton::Left) { |
| @@ -3121,6 +3130,11 @@ impl Player { | |||
| 3121 | self.gain_release(cx); | 3130 | self.gain_release(cx); |
| 3122 | return; | 3131 | return; |
| 3123 | } | 3132 | } |
| 3133 | + // The curve's handle lands where the hand let go, as one undo step. | ||
| 3134 | + if self.curve_drag.is_some() { | ||
| 3135 | + self.end_curve_drag(event.position, cx); | ||
| 3136 | + return; | ||
| 3137 | + } | ||
| 3124 | if self.pan_scrub_end(cx) || self.title_scrub_end(cx) || self.cue_num_scrub_end(cx) { | 3138 | if self.pan_scrub_end(cx) || self.title_scrub_end(cx) || self.cue_num_scrub_end(cx) { |
| 3125 | return; | 3139 | return; |
| 3126 | } | 3140 | } |
crates/app/src/ui/cards/curve.rsadded+182 -0
| @@ -0,0 +1,182 @@ | |||
| 1 | +//! The easing curve editor: a square showing a key's cubic bezier, with one handle per | ||
| 2 | +//! control point. A press takes the handle nearer the pointer, and a drag moves it. | ||
| 3 | +//! | ||
| 4 | +//! The drag keeps a draft curve on the player and writes the key once, when the button comes | ||
| 5 | +//! up, so the whole gesture is one undo step. The root's move and release handlers carry the | ||
| 6 | +//! drag past the editor's own box, the way the sliders' drags are carried. | ||
| 7 | + | ||
| 8 | +use std::cell::Cell; | ||
| 9 | +use std::rc::Rc; | ||
| 10 | + | ||
| 11 | +use crate::*; | ||
| 12 | +use engine::project::KeyKind; | ||
| 13 | +use gpui::Rgba; | ||
| 14 | +use engine::transform::{Bezier, KeyEase, ease_shape}; | ||
| 15 | + | ||
| 16 | +/// The editor's side in pixels. | ||
| 17 | +pub(crate) const CURVE_PX: f32 = 140.; | ||
| 18 | +/// The straight segments the curve is drawn with. | ||
| 19 | +const CURVE_STEPS: usize = 48; | ||
| 20 | +/// A handle's radius in pixels. | ||
| 21 | +const HANDLE_R: f32 = 5.; | ||
| 22 | + | ||
| 23 | +/// A handle being dragged: the key it belongs to, which control point it is, and the curve it | ||
| 24 | +/// has reached so far. | ||
| 25 | +pub(crate) struct CurveDrag { | ||
| 26 | + pub(crate) kind: KeyKind, | ||
| 27 | + /// `0` for the control point near the start of the curve, `1` for the one near its end. | ||
| 28 | + pub(crate) handle: usize, | ||
| 29 | + pub(crate) draft: Bezier, | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +/// The curve a key takes the first time it is set to custom: CSS `ease`. | ||
| 33 | +pub(crate) fn css_ease() -> Option<Bezier> { | ||
| 34 | + Bezier::new(0.25, 0.1, 0.25, 1.0) | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +/// `at` (window pixels) as a point of the editor's unit square, with y pointing up, clamped to | ||
| 38 | +/// `0..=1` on both axes. `None` while the editor has not been measured yet. | ||
| 39 | +fn unit_point(at: Point<Pixels>, bounds: Bounds<Pixels>) -> Option<(f32, f32)> { | ||
| 40 | + let (w, h) = (f32::from(bounds.size.width), f32::from(bounds.size.height)); | ||
| 41 | + if w <= 0. || h <= 0. { | ||
| 42 | + return None; | ||
| 43 | + } | ||
| 44 | + let x = (f32::from(at.x - bounds.left()) / w).clamp(0., 1.); | ||
| 45 | + let y = (1. - f32::from(at.y - bounds.top()) / h).clamp(0., 1.); | ||
| 46 | + Some((x, y)) | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +/// The handle of `b` nearer to the unit point `at`: `0` or `1`. | ||
| 50 | +fn nearest_handle(b: Bezier, at: (f32, f32)) -> usize { | ||
| 51 | + let [x1, y1, x2, y2] = b.points(); | ||
| 52 | + let dist = |hx: f32, hy: f32| (hx - at.0).powi(2) + (hy - at.1).powi(2); | ||
| 53 | + if dist(x1, y1) <= dist(x2, y2) { 0 } else { 1 } | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +/// `b` with handle `handle` moved to the unit point `to`, `None` if the result is not a curve. | ||
| 57 | +fn moved(b: Bezier, handle: usize, to: (f32, f32)) -> Option<Bezier> { | ||
| 58 | + let [x1, y1, x2, y2] = b.points(); | ||
| 59 | + match handle { | ||
| 60 | + 0 => Bezier::new(to.0, to.1, x2, y2), | ||
| 61 | + _ => Bezier::new(x1, y1, to.0, to.1), | ||
| 62 | + } | ||
| 63 | +} | ||
| 64 | + | ||
| 65 | +/// One straight stroke from `from` to `to`, `width` pixels thick. | ||
| 66 | +fn paint_segment(window: &mut Window, from: Point<Pixels>, to: Point<Pixels>, width: f32, colour: Rgba) { | ||
| 67 | + let mut path = PathBuilder::stroke(px(width)); | ||
| 68 | + path.move_to(from); | ||
| 69 | + path.line_to(to); | ||
| 70 | + if let Ok(path) = path.build() { | ||
| 71 | + window.paint_path(path, colour); | ||
| 72 | + } | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +/// A round handle of [`HANDLE_R`] pixels centred on `centre`. | ||
| 76 | +fn paint_handle(window: &mut Window, centre: Point<Pixels>, colour: Rgba) { | ||
| 77 | + let r = px(HANDLE_R); | ||
| 78 | + let bounds = Bounds::new(point(centre.x - r, centre.y - r), size(r * 2., r * 2.)); | ||
| 79 | + window.paint_quad(gpui::quad( | ||
| 80 | + bounds, | ||
| 81 | + gpui::Corners::all(r), | ||
| 82 | + colour, | ||
| 83 | + gpui::Edges::all(px(0.)), | ||
| 84 | + colour, | ||
| 85 | + gpui::BorderStyle::default(), | ||
| 86 | + )); | ||
| 87 | +} | ||
| 88 | + | ||
| 89 | +/// The curve editor for the key of `kind` whose ease is `shown`. `id` names it, and `bounds` | ||
| 90 | +/// receives its box at prepaint, which the press and the drag are read against. | ||
| 91 | +pub(super) fn curve_editor( | ||
| 92 | + id: impl Into<gpui::ElementId>, | ||
| 93 | + kind: KeyKind, | ||
| 94 | + shown: Bezier, | ||
| 95 | + bounds: Rc<Cell<Bounds<Pixels>>>, | ||
| 96 | + cx: &mut Context<Player>, | ||
| 97 | +) -> AnyElement { | ||
| 98 | + let t = crate::ui::theme::tokens(); | ||
| 99 | + let (accent, ink3, well, line) = (t.accent, t.ink3, t.well, t.line); | ||
| 100 | + let [x1, y1, x2, y2] = shown.points(); | ||
| 101 | + let samples: Vec<f32> = (0..=CURVE_STEPS) | ||
| 102 | + .map(|i| ease_shape(KeyEase::Bezier(shown), i as f32 / CURVE_STEPS as f32)) | ||
| 103 | + .collect(); | ||
| 104 | + let paint = canvas( | ||
| 105 | + move |measured, _, _| bounds.set(measured), | ||
| 106 | + move |b: Bounds<Pixels>, _, window, _| { | ||
| 107 | + let at = |x: f32, y: f32| point(b.origin.x + b.size.width * x, b.origin.y + b.size.height * (1. - y)); | ||
| 108 | + // The faint diagonal, the guides from each end to its handle, then the curve and the handles. | ||
| 109 | + paint_segment(window, at(0., 0.), at(1., 1.), 1., rgba(line)); | ||
| 110 | + paint_segment(window, at(0., 0.), at(x1, y1), 1., rgb(ink3)); | ||
| 111 | + paint_segment(window, at(1., 1.), at(x2, y2), 1., rgb(ink3)); | ||
| 112 | + let mut path = PathBuilder::stroke(px(2.)); | ||
| 113 | + for (i, &y) in samples.iter().enumerate() { | ||
| 114 | + let p = at(i as f32 / CURVE_STEPS as f32, y); | ||
| 115 | + match i { | ||
| 116 | + 0 => path.move_to(p), | ||
| 117 | + _ => path.line_to(p), | ||
| 118 | + } | ||
| 119 | + } | ||
| 120 | + if let Ok(path) = path.build() { | ||
| 121 | + window.paint_path(path, rgb(accent)); | ||
| 122 | + } | ||
| 123 | + paint_handle(window, at(x1, y1), rgb(accent)); | ||
| 124 | + paint_handle(window, at(x2, y2), rgb(accent)); | ||
| 125 | + }, | ||
| 126 | + ) | ||
| 127 | + .absolute() | ||
| 128 | + .size_full(); | ||
| 129 | + div() | ||
| 130 | + .id(id) | ||
| 131 | + .relative() | ||
| 132 | + .flex_none() | ||
| 133 | + .size(px(CURVE_PX)) | ||
| 134 | + .rounded(px(3.)) | ||
| 135 | + .bg(rgb(well)) | ||
| 136 | + .border_1() | ||
| 137 | + .border_color(rgba(line)) | ||
| 138 | + .cursor_pointer() | ||
| 139 | + .on_mouse_down( | ||
| 140 | + MouseButton::Left, | ||
| 141 | + cx.listener(move |this, event: &MouseDownEvent, _, cx| this.press_curve(kind, event.position, cx)), | ||
| 142 | + ) | ||
| 143 | + .child(paint) | ||
| 144 | + .into_any_element() | ||
| 145 | +} | ||
| 146 | + | ||
| 147 | +impl Player { | ||
| 148 | + /// A press on the editor takes the handle nearer to it and moves that handle to the press. | ||
| 149 | + /// Nothing starts unless the key on the playhead has a custom ease. | ||
| 150 | + pub(crate) fn press_curve(&mut self, kind: KeyKind, at: Point<Pixels>, cx: &mut Context<Self>) { | ||
| 151 | + let Some(KeyEase::Bezier(shown)) = self.key_here(kind) else { | ||
| 152 | + return; | ||
| 153 | + }; | ||
| 154 | + let Some(unit) = unit_point(at, self.curve_bounds.get()) else { | ||
| 155 | + return; | ||
| 156 | + }; | ||
| 157 | + let handle = nearest_handle(shown, unit); | ||
| 158 | + self.curve_drag = Some(CurveDrag { kind, handle, draft: shown }); | ||
| 159 | + self.drag_curve(at, cx); | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + /// Moves the held handle to the pointer. The draft is drawn until the button comes up. | ||
| 163 | + pub(crate) fn drag_curve(&mut self, at: Point<Pixels>, cx: &mut Context<Self>) { | ||
| 164 | + let unit = unit_point(at, self.curve_bounds.get()); | ||
| 165 | + if let (Some(unit), Some(drag)) = (unit, self.curve_drag.as_mut()) | ||
| 166 | + && let Some(draft) = moved(drag.draft, drag.handle, unit) | ||
| 167 | + { | ||
| 168 | + drag.draft = draft; | ||
| 169 | + } | ||
| 170 | + cx.notify(); | ||
| 171 | + } | ||
| 172 | + | ||
| 173 | + /// Ends the drag where the button came up: the draft is written to the key on the playhead | ||
| 174 | + /// as one undo step. | ||
| 175 | + pub(crate) fn end_curve_drag(&mut self, at: Point<Pixels>, cx: &mut Context<Self>) { | ||
| 176 | + self.drag_curve(at, cx); | ||
| 177 | + if let Some(drag) = self.curve_drag.take() { | ||
| 178 | + self.set_key_ease_here(drag.kind, KeyEase::Bezier(drag.draft), cx); | ||
| 179 | + } | ||
| 180 | + cx.notify(); | ||
| 181 | + } | ||
| 182 | +} | ||
crates/app/src/ui/cards/keys.rsmodified+43 -12
| @@ -6,9 +6,10 @@ use crate::ui::icons::Icon; | |||
| 6 | use crate::ui::kit::{self, Segment}; | 6 | use crate::ui::kit::{self, Segment}; |
| 7 | use crate::ui::type_scale::{self, Typeset}; | 7 | use crate::ui::type_scale::{self, Typeset}; |
| 8 | use crate::ui::widgets::*; | 8 | use crate::ui::widgets::*; |
| 9 | +use super::curve; | ||
| 9 | use crate::*; | 10 | use crate::*; |
| 10 | use engine::project::KeyKind; | 11 | use engine::project::KeyKind; |
| 11 | -use engine::transform::KeyEase; | 12 | +use engine::transform::{Bezier, KeyEase}; |
| 12 | 13 | ||
| 13 | /// What a key of `kind` is called in a row and its tooltips. | 14 | /// What a key of `kind` is called in a row and its tooltips. |
| 14 | fn kind_label(kind: KeyKind) -> &'static str { | 15 | fn kind_label(kind: KeyKind) -> &'static str { |
| @@ -140,17 +141,26 @@ impl Player { | |||
| 140 | let me = cx.entity(); | 141 | let me = cx.entity(); |
| 141 | // The ease of the key on the playhead gets a line of its own, whole words. The line is | 142 | // The ease of the key on the playhead gets a line of its own, whole words. The line is |
| 142 | // always there: without a key on the playhead it is disabled (and says why), so setting | 143 | // always there: without a key on the playhead it is disabled (and says why), so setting |
| 143 | - // the first key never moves the rows under it. | 144 | + // the first key never moves the rows under it. The sixth chip is the custom curve. |
| 144 | - let eases = kit::segmented_equal( | 145 | + let mut labels: Vec<Segment> = KeyEase::ALL.iter().map(|e| Segment::text(ease_text(*e))).collect(); |
| 145 | - "key-ease", | 146 | + labels.push(Segment::text("Custom")); |
| 146 | - &KeyEase::ALL.map(|e| Segment::text(ease_text(e))), | 147 | + let custom_at = KeyEase::ALL.len(); |
| 147 | - here.and_then(|current| KeyEase::ALL.iter().position(|e| *e == current)).unwrap_or(usize::MAX), | 148 | + let selected = match here { |
| 148 | - here.is_some(), | 149 | + Some(KeyEase::Bezier(_)) => custom_at, |
| 149 | - None, | 150 | + Some(current) => KeyEase::ALL.iter().position(|e| *e == current).unwrap_or(usize::MAX), |
| 150 | - move |pick, _, app| { | 151 | + None => usize::MAX, |
| 151 | - me.update(app, |this, cx| this.set_key_ease_here(kind, KeyEase::ALL[pick], cx)); | 152 | + }; |
| 152 | - }, | 153 | + let eases = kit::segmented_equal("key-ease", &labels, selected, here.is_some(), None, move |pick, _, app| { |
| 153 | - ); | 154 | + // A custom pick keeps the curve the key already has, or starts from CSS `ease`. |
| 155 | + let ease = match (KeyEase::ALL.get(pick), here) { | ||
| 156 | + (Some(preset), _) => Some(*preset), | ||
| 157 | + (None, Some(KeyEase::Bezier(b))) => Some(KeyEase::Bezier(b)), | ||
| 158 | + (None, _) => curve::css_ease().map(KeyEase::Bezier), | ||
| 159 | + }; | ||
| 160 | + if let Some(ease) = ease { | ||
| 161 | + me.update(app, |this, cx| this.set_key_ease_here(kind, ease, cx)); | ||
| 162 | + } | ||
| 163 | + }); | ||
| 154 | // With no key on the playhead the row is disabled, and its reason rides a clear layer | 164 | // With no key on the playhead the row is disabled, and its reason rides a clear layer |
| 155 | // over the chips: a tooltip on the row itself did not reach the pointer past the chips. | 165 | // over the chips: a tooltip on the row itself did not reach the pointer past the chips. |
| 156 | let eases = div() | 166 | let eases = div() |
| @@ -169,6 +179,26 @@ impl Player { | |||
| 169 | .tooltip(overlay_tip_hover("Add a key first", "", None)), | 179 | .tooltip(overlay_tip_hover("Add a key first", "", None)), |
| 170 | ) | 180 | ) |
| 171 | }); | 181 | }); |
| 182 | + // The curve shows the draft while a handle is held, else the key's own custom ease. | ||
| 183 | + let shown: Option<Bezier> = match (self.curve_drag.as_ref(), here) { | ||
| 184 | + (Some(drag), _) if drag.kind == kind => Some(drag.draft), | ||
| 185 | + (_, Some(KeyEase::Bezier(b))) => Some(b), | ||
| 186 | + _ => None, | ||
| 187 | + }; | ||
| 188 | + let editor = shown.map(|shown| { | ||
| 189 | + div() | ||
| 190 | + .w_full() | ||
| 191 | + .flex() | ||
| 192 | + .justify_center() | ||
| 193 | + .child(curve::curve_editor( | ||
| 194 | + ("key-curve", kind_id(kind)), | ||
| 195 | + kind, | ||
| 196 | + shown, | ||
| 197 | + self.curve_bounds.clone(), | ||
| 198 | + cx, | ||
| 199 | + )) | ||
| 200 | + .into_any_element() | ||
| 201 | + }); | ||
| 172 | // The colour card's rows run to its edge; the transform card's sit 6px in. | 202 | // The colour card's rows run to its edge; the transform card's sit 6px in. |
| 173 | let inset = if kind == KeyKind::Color { 0. } else { 6. }; | 203 | let inset = if kind == KeyKind::Color { 0. } else { 6. }; |
| 174 | div() | 204 | div() |
| @@ -202,6 +232,7 @@ impl Player { | |||
| 202 | })), | 232 | })), |
| 203 | ) | 233 | ) |
| 204 | .child(eases) | 234 | .child(eases) |
| 235 | + .children(editor) | ||
| 205 | .into_any_element() | 236 | .into_any_element() |
| 206 | } | 237 | } |
| 207 | 238 | ||
crates/app/src/ui/cards/mod.rsmodified+1 -0
| @@ -6,6 +6,7 @@ | |||
| 6 | mod caption; | 6 | mod caption; |
| 7 | mod color; | 7 | mod color; |
| 8 | mod common; | 8 | mod common; |
| 9 | +pub(crate) mod curve; | ||
| 9 | mod effects; | 10 | mod effects; |
| 10 | mod eq; | 11 | mod eq; |
| 11 | mod export; | 12 | mod export; |