forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

keys.rs296 lines, 11.9 KiBHistoryRawDownload
1//! The keyframe rows the transform and colour cards share: the stopwatch
2//! diamond that puts a key on the playhead (or takes the one there off), and --
3//! with a key on the playhead -- the chips that pick its easing.
4
5use crate::ui::icons::Icon;
6use crate::ui::kit::{self, Segment};
7use crate::ui::type_scale::{self, Typeset};
8use crate::ui::widgets::*;
9use super::curve::{self, CurveTarget};
10use crate::*;
11use engine::project::KeyKind;
12use engine::transform::{Bezier, KeyEase};
13
14/// What a key of `kind` is called in a row and its tooltips.
15fn kind_label(kind: KeyKind) -> &'static str {
16 match kind {
17 KeyKind::Placement => "Position and size",
18 KeyKind::Opacity => "Opacity",
19 KeyKind::Color => "Colour",
20 }
21}
22
23/// The ease as the segmented row names it: whole words, the row has its own line.
24fn ease_text(ease: KeyEase) -> &'static str {
25 match ease {
26 KeyEase::Linear => "Linear",
27 KeyEase::EaseIn => "Ease in",
28 KeyEase::EaseOut => "Ease out",
29 KeyEase::Ease => "Smooth",
30 KeyEase::Hold => "Hold",
31 KeyEase::Bezier(_) => "Custom",
32 }
33}
34
35/// The row's own label: the colour card's row sits among sliders, so it says what it animates.
36fn row_label(kind: KeyKind) -> &'static str {
37 match kind {
38 KeyKind::Color => "Colour keyframes",
39 other => kind_label(other),
40 }
41}
42
43pub(super) fn kind_id(kind: KeyKind) -> usize {
44 match kind {
45 KeyKind::Placement => 0,
46 KeyKind::Opacity => 1,
47 KeyKind::Color => 2,
48 }
49}
50
51/// A diamond of `size` px: filled (a key is on the playhead) or an outline.
52pub(super) fn diamond(size: f32, colour: u32, filled: bool) -> impl IntoElement {
53 gpui::canvas(
54 |_, _, _| (),
55 move |bounds, _, window, _| {
56 let (o, s) = (bounds.origin, bounds.size);
57 let (cx_, cy_) = (o.x + s.width / 2., o.y + s.height / 2.);
58 let half = px(size / 2. - if filled { 0. } else { 0.75 });
59 let mut path = if filled { gpui::PathBuilder::fill() } else { gpui::PathBuilder::stroke(px(1.5)) };
60 path.move_to(gpui::point(cx_, cy_ - half));
61 path.line_to(gpui::point(cx_ + half, cy_));
62 path.line_to(gpui::point(cx_, cy_ + half));
63 path.line_to(gpui::point(cx_ - half, cy_));
64 path.close();
65 if let Ok(path) = path.build() {
66 window.paint_path(path, rgb(colour));
67 }
68 },
69 )
70 .size(px(size))
71}
72
73impl Player {
74 /// Puts the playhead on the next (`forward`) or the previous key of `kind` of the open
75 /// card's clip, and says so when there is none that way.
76 pub(super) fn jump_key(&mut self, kind: KeyKind, forward: bool, cx: &mut Context<Self>) {
77 let Some((lane, idx)) = self.key_clip(kind) else { return };
78 let Some(session) = self.session.as_ref() else { return };
79 let ats: Vec<u32> = match kind {
80 KeyKind::Placement => session.transform_keys_of(lane, idx).iter().map(|k| k.at).collect(),
81 KeyKind::Opacity => session.opacity_keys_of(lane, idx).iter().map(|k| k.at).collect(),
82 KeyKind::Color => session.color_keys_of(lane, idx).iter().map(|k| k.at).collect(),
83 };
84 let Some(clip) = session.lane_clips(lane).get(idx) else { return };
85 let (start, end, now) = (clip.start, clip.end(), self.key_frame());
86 let on_key = |t: &u32| session.key_source_frame(lane, idx, *t).is_some_and(|s| ats.contains(&s));
87 let hit = match forward {
88 true => (now.saturating_add(1)..end).find(on_key),
89 false => (start..now.min(end)).rev().find(on_key),
90 };
91 match hit {
92 Some(t) => {
93 let at = f64::from(t) / self.active_fps();
94 self.seek(at, cx);
95 }
96 None => {
97 self.notify_user(if forward { "No later key on this clip" } else { "No earlier key on this clip" }.into());
98 cx.notify();
99 }
100 }
101 }
102
103 /// One keyframe row: the name, then previous key, the diamond (filled with a key on the
104 /// playhead, an outline without; lit once the clip is keyed at all) and next key; under it,
105 /// the easing of the key on the playhead.
106 pub(super) fn key_row(&self, kind: KeyKind, cx: &mut Context<Self>) -> AnyElement {
107 let here = self.key_here(kind);
108 let keyed = self.key_active(kind);
109 let name = kind_label(kind);
110 let t = crate::ui::theme::tokens();
111 let ink = match (here.is_some(), keyed) {
112 (true, _) => t.accent,
113 (false, true) => t.ink1,
114 _ => t.ink3,
115 };
116 let diamond = div()
117 .id(("key-diamond", kind_id(kind)))
118 .flex_none()
119 .size(px(crate::layout::redesign::CONTROL_M))
120 .flex()
121 .items_center()
122 .justify_center()
123 .rounded(px(crate::layout::redesign::R_CONTROL))
124 .cursor_pointer()
125 .hover(|s| s.bg(rgb(crate::ui::theme::tokens().e3)))
126 .tooltip(overlay_tip_hover(
127 &if here.is_some() {
128 format!("Remove the {name} key at the playhead")
129 } else {
130 format!("Key {name} at the playhead")
131 },
132 "",
133 None,
134 ))
135 .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
136 this.toggle_key_here(kind, cx);
137 }))
138 .child(diamond(12., ink, here.is_some()));
139 let (m1, m2) = (cx.entity(), cx.entity());
140 let none = (!keyed).then_some("No keys yet");
141 let eases = self.ease_picker(
142 CurveTarget::Key(kind),
143 move |this, ease, cx| this.set_key_ease_here(kind, ease, cx),
144 cx,
145 );
146 // The colour card's rows run to its edge; the transform card's sit 6px in.
147 let inset = if kind == KeyKind::Color { 0. } else { 6. };
148 div()
149 .id(("key-row", kind_id(kind)))
150 .w_full()
151 .px(px(inset))
152 .flex()
153 .flex_col()
154 .gap(px(4.))
155 .child(
156 div()
157 .w_full()
158 .flex()
159 .items_center()
160 .gap(px(2.))
161 .child(
162 div()
163 .flex_1()
164 .min_w(px(0.))
165 .truncate()
166 .type_style(type_scale::body())
167 .text_color(rgb(if keyed { t.ink1 } else { t.ink2 }))
168 .child(row_label(kind)),
169 )
170 .child(kit::icon_ghost("key-prev", Icon::ChevronLeft, "Previous key", none, move |_, app| {
171 m1.update(app, |this, cx| this.jump_key(kind, false, cx));
172 }))
173 .child(diamond)
174 .child(kit::icon_ghost("key-next", Icon::ChevronRight, "Next key", none, move |_, app| {
175 m2.update(app, |this, cx| this.jump_key(kind, true, cx));
176 })),
177 )
178 .child(eases)
179 .into_any_element()
180 }
181
182 /// The easing row of one target: the five presets and a sixth chip for the custom curve, then,
183 /// under it, the curve editor while the key on the playhead is custom or a handle of this
184 /// target is held. The row is always there: without a key on the playhead it is disabled, and
185 /// its reason rides a clear layer over the chips (a tooltip on the row itself did not reach the
186 /// pointer past the chips). `on_pick` writes the ease the row picked.
187 pub(super) fn ease_picker(
188 &self,
189 target: CurveTarget,
190 on_pick: impl Fn(&mut Player, KeyEase, &mut Context<Player>) + 'static,
191 cx: &mut Context<Self>,
192 ) -> AnyElement {
193 let here = self.ease_here(target);
194 let me = cx.entity();
195 // The ease of the key on the playhead gets a line of its own, whole words. The sixth chip
196 // is the custom curve.
197 let mut labels: Vec<Segment> = KeyEase::ALL.iter().map(|e| Segment::text(ease_text(*e))).collect();
198 labels.push(Segment::text("Custom"));
199 let custom_at = KeyEase::ALL.len();
200 let selected = match here {
201 Some(KeyEase::Bezier(_)) => custom_at,
202 Some(current) => KeyEase::ALL.iter().position(|e| *e == current).unwrap_or(usize::MAX),
203 None => usize::MAX,
204 };
205 let reason = self.ease_reason(target);
206 let on_pick = Rc::new(on_pick);
207 let chips = kit::segmented_equal("key-ease", &labels, selected, here.is_some(), None, move |pick, _, app| {
208 // A custom pick keeps the curve the key already has, or starts from CSS `ease`.
209 let ease = match (KeyEase::ALL.get(pick), here) {
210 (Some(preset), _) => Some(*preset),
211 (None, Some(KeyEase::Bezier(b))) => Some(KeyEase::Bezier(b)),
212 (None, _) => curve::css_ease().map(KeyEase::Bezier),
213 };
214 if let Some(ease) = ease {
215 me.update(app, |this, cx| on_pick(this, ease, cx));
216 }
217 });
218 let chips = div()
219 .id("key-ease-row")
220 .relative()
221 .w_full()
222 .child(chips)
223 .when(here.is_none(), |d| {
224 d.child(
225 div()
226 .id("key-ease-why")
227 .absolute()
228 .top(px(0.))
229 .left(px(0.))
230 .size_full()
231 .tooltip(overlay_tip_hover(reason, "", None)),
232 )
233 });
234 // The curve shows the draft while a handle is held, else the key's own custom ease.
235 let shown: Option<Bezier> = match (self.curve_drag.as_ref(), here) {
236 (Some(drag), _) if drag.target == target => Some(drag.draft),
237 (_, Some(KeyEase::Bezier(b))) => Some(b),
238 _ => None,
239 };
240 let editor = shown.map(|shown| {
241 div()
242 .w_full()
243 .flex()
244 .justify_center()
245 .child(curve::curve_editor("curve", target, shown, self.curve_bounds_for(target), cx))
246 .into_any_element()
247 });
248 div()
249 .id("ease-picker")
250 .w_full()
251 .flex()
252 .flex_col()
253 .gap(px(4.))
254 .child(chips)
255 .children(editor)
256 .into_any_element()
257 }
258
259 /// The opacity row: the same slider and typed pill as Scale and Rotation, 0 to 100 percent.
260 pub(super) fn opacity_row(&self, opacity: f32, cx: &mut Context<Self>) -> AnyElement {
261 let row = SliderRow {
262 id: "transform-opacity-slider",
263 label: "Opacity".into(),
264 value: format!("{:.0}%", opacity * 100.).into(),
265 fraction: slider_fraction(opacity, 0., 1.),
266 changed: (opacity - 1.).abs() > 0.005,
267 bar: self.opacity_bar.clone(),
268 on_press: Box::new(move |this: &mut Player, x: Pixels, cx: &mut Context<Player>| {
269 this.opacity_drag = true;
270 this.transform_dragging = true;
271 this.drag_opacity(x, true, cx);
272 }),
273 on_reset: Some(Rc::new(move |this: &mut Player, cx: &mut Context<Player>| {
274 this.set_opacity(1., cx);
275 })),
276 // Typed in percent: "40" is 40% opacity.
277 typed: Some(SliderTyped {
278 lo: 0.,
279 hi: 100.,
280 set: Rc::new(|this: &mut Player, v: f64, cx: &mut Context<Player>| {
281 this.set_opacity((v / 100.) as f32, cx);
282 }),
283 }),
284 };
285 div()
286 .id("transform-opacity")
287 .w_full()
288 .flex()
289 .flex_col()
290 .px(px(6.))
291 .rounded(px(3.))
292 .tooltip(overlay_tip_hover("Opacity", "", None))
293 .child(slider_row(row, cx))
294 .into_any_element()
295 }
296}