forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

effects.rs500 lines, 24.5 KiBHistoryRawDownload
1//! The effects card: the selected picture clip's stack of effects, top to bottom in the
2//! order they are applied. Each row switches, moves or removes its effect and carries one
3//! control per setting; a button at the foot adds one.
4
5use super::common::*;
6use crate::player::generators::GenPaint;
7use crate::player::effects::{ParamShape, mode_text, param_display, param_shape, param_unit};
8use crate::ui::icons::{Icon, IconSize};
9use crate::ui::type_scale::{self, Typeset};
10use crate::ui::kit::{self, Segment};
11use crate::ui::widgets::{SliderRow, SliderTyped, slider_row};
12use super::curve::CurveTarget;
13use crate::*;
14use engine::effects::{BlendMode, Effect, EffectKind, MAX_EFFECTS};
15
16/// What an effect is called in the add list and its row: plain words, not the engine's name.
17fn kind_name(kind: EffectKind) -> &'static str {
18 match kind {
19 EffectKind::Blend => "Blend mode",
20 other => other.label(),
21 }
22}
23
24/// The one-line description a list row carries as its tooltip ("" for none).
25fn kind_hint(kind: EffectKind) -> &'static str {
26 match kind {
27 EffectKind::Blend => "Mix this picture with the lanes under it",
28 EffectKind::Pixelate => "Blocks of colour; a mask below it limits them to its shape",
29 _ => "",
30 }
31}
32
33/// What a blend mode does, in plain words: the one line its list row carries.
34fn blend_why(mode: BlendMode) -> &'static str {
35 match mode {
36 BlendMode::Normal => "Shows this picture as it is, covering the lanes under it",
37 BlendMode::Multiply => "Darkens: this picture and the lanes under it multiply, so both darken",
38 BlendMode::Screen => "Lightens: the dark parts of this picture drop out over the lanes under it",
39 BlendMode::Overlay => "More contrast: darks get darker and lights get lighter over the lanes under it",
40 BlendMode::Add => "Adds this picture's light to the lanes under it, so bright parts glow",
41 BlendMode::Darken => "Keeps the darker of this picture and the lanes under it, pixel by pixel",
42 BlendMode::Lighten => "Keeps the lighter of this picture and the lanes under it, pixel by pixel",
43 }
44}
45
46/// The icon an effect's row in the add list wears.
47fn kind_icon(kind: EffectKind) -> Icon {
48 match kind {
49 EffectKind::Blur => Icon::WandSparkles,
50 EffectKind::Sharpen => Icon::Scan,
51 EffectKind::Vignette => Icon::Eye,
52 EffectKind::Glow => Icon::Sparkles,
53 EffectKind::Shake => Icon::Move,
54 EffectKind::ChromaKey => Icon::Palette,
55 EffectKind::Blend => Icon::Layers,
56 EffectKind::RectMask => Icon::Square,
57 EffectKind::EllipseMask => Icon::Circle,
58 EffectKind::Pixelate => Icon::Grid3x3,
59 }
60}
61
62impl Player {
63 /// The effects card. The stack is the clip's own, or the draft of a bar being held;
64 /// for an audio clip or no clip it says so in words instead of showing rows.
65 pub(crate) fn effects_card(
66 &self,
67 viewport: Size<Pixels>,
68 cx: &mut Context<Self>,
69 ) -> Option<impl IntoElement> {
70 let (lane, idx) = self.effects_open?;
71 let session = self.session.as_ref()?;
72 let on_picture = lane.kind == LaneKind::Video && session.lane_clips(lane).get(idx).is_some();
73 // The rows show what each effect is at the playhead: a keyed effect's values there.
74 let at = self.effects_src().unwrap_or(0);
75 let stack: Vec<Effect> = self.effects_shown().iter().map(|e| e.posed(f64::from(at)).into_owned()).collect();
76 let count = stack.len();
77 let rows: Vec<AnyElement> = stack
78 .iter()
79 .enumerate()
80 .map(|(i, e)| self.effect_row(i, e, count, cx))
81 .collect();
82 let help_text: SharedString = "switch an effect on or off, move it up or down the stack, or take it away; drag a bar to set it — a click away or esc closes".into();
83 let tail_text: SharedString = match (on_picture, count) {
84 (false, _) => "effects belong to picture clips — select one".into(),
85 (true, n) if n >= MAX_EFFECTS => format!("All {MAX_EFFECTS} effects are in use — remove one to add another").into(),
86 (true, _) => "Effects run top to bottom".into(),
87 };
88 let adding = self.effects_adding;
89 let kinds: Vec<AnyElement> = EffectKind::ALL
90 .into_iter()
91 .enumerate()
92 .map(|(k, kind)| {
93 let row = crate::ui::popup::popup_row_icon(("effects-kind", k), kind_name(kind), "", Some(false), Some(kind_icon(kind)), None);
94 let hint = kind_hint(kind);
95 row.when(!hint.is_empty(), |r| r.tooltip(crate::ui::widgets::overlay_tip_hover(kind_name(kind), "", Some(hint))))
96 .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| this.add_effect(kind, cx)))
97 .into_any_element()
98 })
99 .collect();
100 let add_button = kit::Button::new("effects-add")
101 .label("Add effect")
102 .icon(Icon::Plus)
103 .style(kit::ButtonStyle::Secondary)
104 .on(adding)
105 .enabled(count < MAX_EFFECTS)
106 .build()
107 .on_click(cx.listener(|this, _: &ClickEvent, _, cx| {
108 this.effects_adding = !this.effects_adding;
109 cx.notify();
110 }));
111 let empty = (on_picture && count == 0).then(|| {
112 div().w_full().py(px(8.)).flex().justify_center().text_color(rgb(INK2())).child("No effects yet")
113 });
114 Some(
115 self.card_outer(viewport, cx)
116 .child(
117 div()
118 .w_full()
119 .max_w(px(card_max_w(COLOR_W, self.card_maximized, f32::from(viewport.width))))
120 .on_mouse_down(MouseButton::Left, swallow)
121 .flex()
122 .flex_col()
123 .gap(px(CARD_GAP))
124 .map(|d| self.card_surface(d))
125 .child(div().id("effects-reset").w_full().flex().justify_end().child(dark_card_head_reset("", None, Some(help_text), None, None, cx)))
126 .child(
127 div()
128 .id("effects-rows")
129 .flex()
130 .flex_col()
131 .gap(px(8.))
132 .overflow_y_scroll()
133 .children(rows),
134 )
135 .children(empty)
136 .when(on_picture, |d| {
137 d.child(
138 div()
139 .relative()
140 .flex()
141 .flex_none()
142 .child(add_button)
143 .when(adding, |d| {
144 d.child(gpui::deferred(
145 kit::plate(6.)
146 .id("effects-kinds")
147 .absolute()
148 .top(px(36.))
149 .left(px(0.))
150 .w(px(200.))
151 .p(px(crate::layout::MENU_PAD))
152 .flex()
153 .flex_col()
154 .occlude()
155 .children(kinds),
156 ).with_priority(1))
157 }),
158 )
159 })
160 .child(dark_help(tail_text)),
161 ),
162 )
163 }
164
165 /// One effect's row: its switch, name and the three buttons, then a control per setting.
166 /// The Track row under a mask: follows the picture under it from the playhead, forward or
167 /// backward, and shows how far that has got with a Cancel while it runs. The keys land
168 /// when it ends, and show as diamonds on the position rows.
169 fn track_row(&self, i: usize, cx: &mut Context<Self>) -> AnyElement {
170 let job = self
171 .effects_open
172 .and_then(|(lane, idx)| self.track_jobs.iter().find(|j| j.lane == lane && j.idx == idx && j.slot == i));
173 let progress = job.and_then(|j| match engine::track::status(j.id) {
174 engine::track::Status::Running(f) => Some(f),
175 engine::track::Status::Finished => Some(1.0),
176 engine::track::Status::Gone => None,
177 });
178 let (words, buttons): (String, Vec<AnyElement>) = match progress {
179 Some(f) => (
180 format!("Tracking {:.0}%", f * 100.),
181 vec![
182 dark_ghost_button(
183 "effects-track-cancel",
184 "Cancel",
185 "",
186 false,
187 cx.listener(move |this, _: &ClickEvent, _, cx| this.cancel_track(i, cx)),
188 )
189 .into_any_element(),
190 ],
191 ),
192 None => (
193 "Follows the picture under the mask from the playhead".to_owned(),
194 vec![
195 dark_ghost_button(
196 "effects-track-back",
197 "Track backward",
198 "",
199 false,
200 cx.listener(move |this, _: &ClickEvent, _, cx| this.start_track(i, true, cx)),
201 )
202 .into_any_element(),
203 dark_ghost_button(
204 "effects-track-forward",
205 "Track forward",
206 "",
207 false,
208 cx.listener(move |this, _: &ClickEvent, _, cx| this.start_track(i, false, cx)),
209 )
210 .into_any_element(),
211 ],
212 ),
213 };
214 // The words take the full width, the buttons sit under them, so neither is squeezed.
215 div()
216 .flex()
217 .flex_col()
218 .gap(px(6.))
219 .child(dark_help(words).w_full())
220 .child(div().flex().justify_end().gap(px(6.)).min_h(px(CONTROL_H)).children(buttons))
221 .into_any_element()
222 }
223
224 fn effect_row(&self, i: usize, e: &Effect, count: usize, cx: &mut Context<Self>) -> AnyElement {
225 let on = e.on;
226 let me = cx.entity();
227 let mut controls: Vec<AnyElement> = Vec::new();
228 for (p, spec) in e.kind.params().iter().enumerate() {
229 let spec = *spec;
230 if e.kind == EffectKind::ChromaKey && (1..=2).contains(&p) {
231 continue;
232 }
233 if e.kind == EffectKind::ChromaKey && p == 0 {
234 let current = ((e.values[0] as u32) << 16) | ((e.values[1] as u32) << 8) | e.values[2] as u32;
235 let (m, m2) = (me.clone(), me.clone());
236 let typing = self.gen_hex.as_ref().filter(|(p, _)| *p == GenPaint::Key(i)).map(|(_, f)| f.text.clone());
237 controls.push(
238 div()
239 .flex()
240 .flex_col()
241 .gap(px(4.))
242 .child(dark_row_label("Key colour", false))
243 .child(kit::swatch_row(
244 "effects-key",
245 &[0x00ff00, 0x0000ff, 0xff00ff, 0x00ffff],
246 current,
247 typing.as_deref(),
248 move |c, _, app| {
249 m.update(app, |this, cx| {
250 this.gen_hex = None;
251 this.set_effect_colour(i, c, cx);
252 });
253 },
254 move |_, app| m2.update(app, |this, cx| this.begin_key_hex(i, cx)),
255 ))
256 .into_any_element(),
257 );
258 continue;
259 }
260 let m = me.clone();
261 let control = match param_shape(e.kind, &spec) {
262 ParamShape::Bar if spec.name == "seed" => {
263 let seed = e.values[p] as usize;
264 div()
265 .flex()
266 .flex_col()
267 .gap(px(4.))
268 .child(dark_row_label(spec.label, false))
269 .child(kit::segmented_equal(
270 "effects-pattern",
271 &["1", "2", "3", "4", "5"].map(Segment::text),
272 if (1..=5).contains(&seed) { seed - 1 } else { usize::MAX },
273 on,
274 None,
275 move |pick, _, app| m.update(app, |this, cx| this.set_effect_param(i, p, (pick + 1) as f32, cx)),
276 ))
277 .into_any_element()
278 }
279 ParamShape::Bar => {
280 let value = e.values[p];
281 let scale = param_unit(e.kind, &spec).scale();
282 // Wrapped so the shared bar id is unique under this row and setting.
283 div()
284 .id(("effects-param", i * 8 + p))
285 .child(slider_row(
286 SliderRow {
287 // Unique per bar: a typed value keeps its typing under the id.
288 id: super::mix::interned_id(format!("effects-bar-{i}-{p}")),
289 label: spec.label.into(),
290 value: param_display(e.kind, &spec, value).into(),
291 fraction: (value - spec.min) / (spec.max - spec.min),
292 changed: value != spec.default,
293 bar: self.effect_bar(i, p),
294 on_press: Box::new(move |this: &mut Player, x: Pixels, cx: &mut Context<Player>| {
295 this.press_effect(i, p, x, cx);
296 }),
297 on_reset: Some(Rc::new(move |this: &mut Player, cx: &mut Context<Player>| {
298 this.set_effect_param(i, p, spec.default, cx);
299 })),
300 // Typed in the unit the value reads: a percent types 40 for 0.4.
301 typed: Some(SliderTyped {
302 lo: f64::from(spec.min) * f64::from(scale),
303 hi: f64::from(spec.max) * f64::from(scale),
304 set: Rc::new(move |this: &mut Player, v: f64, cx: &mut Context<Player>| {
305 this.set_effect_param(i, p, (v / f64::from(scale)) as f32, cx);
306 }),
307 }),
308 },
309 cx,
310 ))
311 .into_any_element()
312 }
313 ParamShape::Switch => {
314 let set = e.values[p] >= 0.5;
315 div()
316 .id(("effects-switch", i * 8 + p))
317 .child(kit::switch_row(
318 "effects-switch-row",
319 spec.label,
320 None,
321 set,
322 None,
323 move |_, app| m.update(app, |this, cx| this.set_effect_param(i, p, if set { 0. } else { 1. }, cx)),
324 ))
325 .into_any_element()
326 }
327 ParamShape::Mode => {
328 let current = BlendMode::from_value(e.values[p]);
329 let at = BlendMode::ALL.iter().position(|&x| x == current).unwrap_or(0);
330 let open = on && self.effects_mode_open == Some(i);
331 let field = kit::dropdown_field("effects-mode", mode_text(current), on)
332 .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
333 this.effects_mode_open = if this.effects_mode_open == Some(i) { None } else { Some(i) };
334 cx.notify();
335 }));
336 let field = div()
337 .id(("effects-mode-field", i))
338 .w_full()
339 .child(field)
340 .when(!on, |d| d.tooltip(overlay_tip_hover("Switch this effect on to change its mode", "", None)));
341 let list = open.then(|| {
342 let t = crate::ui::theme::tokens();
343 let backdrop = div().absolute().top(px(-4000.)).left(px(-4000.)).size(px(8000.)).on_mouse_down(
344 MouseButton::Left,
345 cx.listener(|this, _: &MouseDownEvent, _, cx| {
346 this.effects_mode_open = None;
347 cx.stop_propagation();
348 cx.notify();
349 }),
350 );
351 let mut plate = kit::plate(6.).id(("effects-mode-list", i)).w_full().max_h(px(240.)).overflow_y_scroll().p(px(crate::layout::MENU_PAD)).flex().flex_col().occlude();
352 for (k, &mode) in BlendMode::ALL.iter().enumerate() {
353 plate = plate.child(
354 div()
355 .id(("effects-mode-row", i * 8 + k))
356 .flex_none()
357 .px(px(8.))
358 .py(px(4.))
359 .flex()
360 .items_start()
361 .gap(px(8.))
362 .rounded(px(4.))
363 .cursor_pointer()
364 .hover(|s| s.bg(rgba(crate::ui::theme::WASH_HOVER)))
365 .child(div().flex_none().size(px(16.)).children((k == at).then(|| Icon::Check.el(IconSize::Inline, t.accent))))
366 .child(
367 div()
368 .flex_1()
369 .min_w(px(0.))
370 .flex()
371 .flex_col()
372 .child(div().type_style(type_scale::body_strong()).text_color(rgb(t.ink1)).child(mode_text(mode)))
373 .child(dark_help(blend_why(mode))),
374 )
375 .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
376 this.effects_mode_open = None;
377 this.set_effect_param(i, p, k as f32, cx);
378 })),
379 );
380 }
381 gpui::deferred(
382 div()
383 .absolute()
384 .top(px(0.))
385 .left(px(0.))
386 .child(backdrop)
387 .child(div().absolute().top(px(crate::layout::redesign::CONTROL_M + 2.)).left(px(0.)).w(px(280.)).child(plate)),
388 )
389 });
390 div()
391 .flex()
392 .flex_col()
393 .gap(px(4.))
394 .child(dark_row_label(spec.label, false))
395 .child(dark_help(kind_hint(e.kind)))
396 .child(div().relative().w_full().child(field).children(list))
397 .into_any_element()
398 }
399 };
400 // An effect's keyframe diamond sits on its first keyable row (a mask's position and size
401 // rows) and keys the whole effect at the playhead. It sits above the slider whether or
402 // not a key is there, so nothing moves.
403 let mask = matches!(e.kind, EffectKind::RectMask | EffectKind::EllipseMask);
404 let key_row = match mask {
405 true => p == 0 || p == 2,
406 false => spec.keyable() && e.kind.params()[..p].iter().all(|s| !s.keyable()),
407 };
408 if key_row {
409 let here = self.effects_src();
410 let filled = here.is_some_and(|at| e.keys.iter().any(|k| k.at == at));
411 let t = crate::ui::theme::tokens();
412 let key = div()
413 .id(("effects-mask-key", i * 8 + p))
414 .flex_none()
415 .size(px(crate::layout::redesign::CONTROL_M))
416 .flex()
417 .items_center()
418 .justify_center()
419 .rounded(px(crate::layout::redesign::R_CONTROL))
420 .cursor_pointer()
421 .hover(|s| s.bg(rgb(t.e3)))
422 .tooltip(overlay_tip_hover(
423 if filled { "Remove the mask key at the playhead" } else { "Key the mask at the playhead" },
424 "",
425 None,
426 ))
427 .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| this.toggle_effect_key(i, cx)))
428 .child(super::keys::diamond(12., if filled { t.accent } else { t.ink3 }, filled));
429 controls.push(
430 div()
431 .flex()
432 .flex_col()
433 .gap(px(2.))
434 .child(div().w_full().flex().justify_end().child(key))
435 .child(control)
436 .into_any_element(),
437 );
438 } else {
439 controls.push(control);
440 }
441 }
442 // An effect with keys shows the easing of the one on the playhead under its settings, with
443 // the same chips and curve as the transform card; with no key there the row is disabled.
444 if !e.keys.is_empty() {
445 controls.push(
446 div()
447 .flex()
448 .flex_col()
449 .gap(px(4.))
450 .child(dark_row_label("Easing", false))
451 .child(self.ease_picker(
452 CurveTarget::Effect(i),
453 move |this, ease, cx| this.set_effect_key_ease_here(i, ease, cx),
454 cx,
455 ))
456 .into_any_element(),
457 );
458 }
459 // A mask follows the picture under it: the Track row sits under its controls.
460 if matches!(e.kind, EffectKind::RectMask | EffectKind::EllipseMask) {
461 controls.push(self.track_row(i, cx));
462 }
463 let (m1, m2, m3, m4) = (me.clone(), me.clone(), me.clone(), me);
464 div()
465 .id(("effects-row", i))
466 .flex_none()
467 .flex()
468 .flex_col()
469 .gap(px(4.))
470 .child(
471 div()
472 .flex()
473 .items_center()
474 .gap(px(8.))
475 .child(kit::switch(("effects-on", i), on, true, None).on_click(move |_, _, app| {
476 m1.update(app, |this, cx| this.toggle_effect(i, cx));
477 }))
478 .child(div().flex_1().min_w(px(0.)).child(dark_row_label(kind_name(e.kind), on)))
479 .child(kit::icon_ghost(
480 "effects-up",
481 Icon::ArrowUp,
482 "Move up",
483 (i == 0).then_some("Already first"),
484 move |_, app| m2.update(app, |this, cx| this.move_effect(i, false, cx)),
485 ))
486 .child(kit::icon_ghost(
487 "effects-down",
488 Icon::ArrowDown,
489 "Move down",
490 (i + 1 >= count).then_some("Already last"),
491 move |_, app| m3.update(app, |this, cx| this.move_effect(i, true, cx)),
492 ))
493 .child(kit::icon_ghost("effects-remove", Icon::X, "Remove effect", None, move |_, app| {
494 m4.update(app, |this, cx| this.remove_effect(i, cx));
495 })),
496 )
497 .children(controls)
498 .into_any_element()
499 }
500}