edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching·0 forks·Rust·18.3 MiB·ff99ad6·Tahinli·6 days ago·1055 commits
| 1 | //! The Generator card: the parameters of a generated clip (solid colour, gradient, countdown), |
| 2 | //! in the Clip tab beside the other cards. It is in the dock only while the selected clip is a |
| 3 | //! generator with something to set; every control writes one undo step. |
| 4 | |
| 5 | use super::common::*; |
| 6 | use crate::*; |
| 7 | use crate::player::generators::GenPaint; |
| 8 | use super::tools::drag_catcher; |
| 9 | use crate::ui::kit::{self, NumberEdit, Segment}; |
| 10 | use engine::generate::{Form, Gen, Kind, Shape}; |
| 11 | |
| 12 | /// The colours the chips offer: the palette of a title card, plain enough to find by eye. |
| 13 | const SWATCHES: [u32; 12] = [ |
| 14 | 0x000000, 0xffffff, 0x808080, 0xe53935, 0xfb8c00, 0xfdd835, 0x43a047, 0x00acc1, 0x1e88e5, 0x5e35b1, 0xd81b60, 0x6d4c41, |
| 15 | ]; |
| 16 | |
| 17 | /// The chips a shape's rows offer: ten that fit one line beside None. |
| 18 | const SHAPE_SWATCHES: [u32; 10] = [0x000000, 0xffffff, 0xe53935, 0xfb8c00, 0xfdd835, 0x43a047, 0x00acc1, 0x1e88e5, 0x5e35b1, 0xd81b60]; |
| 19 | |
| 20 | impl Player { |
| 21 | /// The generator under the selection's anchor, with where it sits. |
| 22 | fn generator_target(&self) -> Option<(Lane, usize, Gen)> { |
| 23 | let session = self.session.as_ref()?; |
| 24 | let (lane, idx) = self.selected.anchor()?; |
| 25 | session.generator_of(lane, idx).map(|g| (lane, idx, g)) |
| 26 | } |
| 27 | |
| 28 | /// A labelled number: the field at the right, which drags to scrub and types on a click. |
| 29 | /// `why` greys the row out and says so on hover. |
| 30 | fn num_row(&self, id: &'static str, label: &'static str, paint: GenPaint, unit: &'static str, why: Option<&'static str>, cx: &mut Context<Self>) -> Div { |
| 31 | let value = self.gen_num_value(paint).unwrap_or(0.); |
| 32 | let scrub = self.gen_scrub.filter(|s| s.paint == paint); |
| 33 | let typing = self.gen_hex.as_ref().filter(|(p, _)| *p == paint).map(|(_, f)| NumberEdit::begin(&f.text)); |
| 34 | let enabled = why.is_none(); |
| 35 | // Dimmed by the row, not the field, so the reason stays readable. |
| 36 | let field = kit::number_field(id, 76., format!("{:.0}", scrub.map_or(value, |s| s.cur)), Some(unit), typing.as_ref(), true) |
| 37 | .tooltip(crate::ui::widgets::overlay_tip_hover(label, "", Some(why.unwrap_or("Drag to change, click to type")))) |
| 38 | .when(enabled, |f| { |
| 39 | f.on_mouse_down( |
| 40 | MouseButton::Left, |
| 41 | cx.listener(move |this, event: &MouseDownEvent, _, cx| { |
| 42 | cx.stop_propagation(); |
| 43 | this.begin_gen_scrub(paint, f32::from(event.position.x), cx); |
| 44 | }), |
| 45 | ) |
| 46 | }); |
| 47 | div() |
| 48 | .relative() |
| 49 | .w_full() |
| 50 | .min_h(px(crate::layout::redesign::CONTROL_M)) |
| 51 | .flex() |
| 52 | .items_center() |
| 53 | .justify_between() |
| 54 | .gap(px(8.)) |
| 55 | .child(dark_row_label(label, false)) |
| 56 | .child(field) |
| 57 | .when(!enabled, |d| d.opacity(0.5)) |
| 58 | .child(drag_catcher( |
| 59 | cx.entity(), |
| 60 | scrub.is_some(), |
| 61 | |this, x, fine, cx| this.gen_scrub_move(x, fine, cx), |
| 62 | |this, cx| this.gen_scrub_end(cx), |
| 63 | )) |
| 64 | } |
| 65 | |
| 66 | /// A colour row: the label (and, where the paint may be absent, a None/Colour choice), then the |
| 67 | /// swatches with the current colour ringed, custom ones included, and the hex field. |
| 68 | #[allow(clippy::too_many_arguments)] |
| 69 | fn paint_row( |
| 70 | &self, |
| 71 | tag: &'static str, |
| 72 | label: &'static str, |
| 73 | current: Option<u32>, |
| 74 | paint: GenPaint, |
| 75 | can_none: bool, |
| 76 | colours: &'static [u32], |
| 77 | target: (Lane, usize, Gen), |
| 78 | cx: &mut Context<Self>, |
| 79 | ) -> Div { |
| 80 | let (lane, idx, spec) = target; |
| 81 | let editing = self.gen_hex.as_ref().filter(|(p, _)| *p == paint).map(|(_, f)| f.text.clone()); |
| 82 | let write = move |this: &mut Self, c: Option<u32>, cx: &mut Context<Self>| { |
| 83 | this.commit_gen_hex(cx); |
| 84 | let kind = match (paint, spec.kind) { |
| 85 | (GenPaint::Solid, Kind::Solid { .. }) => Kind::Solid { rgb: c.unwrap_or(0) }, |
| 86 | (GenPaint::From, Kind::Gradient { b, angle, .. }) => Kind::Gradient { a: c.unwrap_or(0), b, angle }, |
| 87 | (GenPaint::To, Kind::Gradient { a, angle, .. }) => Kind::Gradient { a, b: c.unwrap_or(0), angle }, |
| 88 | (_, Kind::Shape(mut sh)) => { |
| 89 | if paint == GenPaint::Fill { |
| 90 | sh.fill = c; |
| 91 | } else { |
| 92 | sh.stroke = c; |
| 93 | if c.is_some() && sh.width == 0 { |
| 94 | sh.width = 6; |
| 95 | } |
| 96 | } |
| 97 | Kind::Shape(sh) |
| 98 | } |
| 99 | (_, k) => k, |
| 100 | }; |
| 101 | this.write_generator(lane, idx, Gen { kind, ..spec }, cx); |
| 102 | }; |
| 103 | let mut col = div().w_full().flex().flex_col().gap(px(6.)); |
| 104 | let mut head = div().w_full().flex().items_center().justify_between().gap(px(8.)).child(dark_row_label(label, false)); |
| 105 | if can_none { |
| 106 | let me = cx.entity(); |
| 107 | head = head.child(kit::segmented( |
| 108 | tag, |
| 109 | &[Segment::text("None"), Segment::text("Colour")], |
| 110 | usize::from(current.is_some()), |
| 111 | true, |
| 112 | None, |
| 113 | move |pick, _, app| { |
| 114 | me.update(app, |this, cx| write(this, (pick == 1).then_some(0xe53935), cx)); |
| 115 | }, |
| 116 | )); |
| 117 | } |
| 118 | col = col.child(head); |
| 119 | if let Some(now) = current { |
| 120 | let (me, me2) = (cx.entity(), cx.entity()); |
| 121 | col = col.child(kit::swatch_row( |
| 122 | tag, |
| 123 | colours, |
| 124 | now, |
| 125 | editing.as_deref(), |
| 126 | move |c, _, app| me.update(app, |this, cx| write(this, Some(c), cx)), |
| 127 | move |_, app| me2.update(app, |this, cx| this.begin_gen_hex(paint, cx)), |
| 128 | )); |
| 129 | } |
| 130 | col |
| 131 | } |
| 132 | |
| 133 | /// The rows of a shape: its form, fill, stroke, width and corner radius. |
| 134 | fn shape_rows(&self, sh: Shape, target: (Lane, usize, Gen), rows: &mut Vec<AnyElement>, cx: &mut Context<Self>) { |
| 135 | let (lane, idx, spec) = target; |
| 136 | let me = cx.entity(); |
| 137 | let form = kit::segmented_equal( |
| 138 | "gen-form", |
| 139 | &Form::ALL.map(|f| Segment::text(f.title())), |
| 140 | Form::ALL.iter().position(|f| *f == sh.form).unwrap_or(0), |
| 141 | true, |
| 142 | None, |
| 143 | move |pick, _, app| { |
| 144 | me.update(app, |this, cx| { |
| 145 | let form = Form::ALL[pick]; |
| 146 | let mut next = sh; |
| 147 | next.form = form; |
| 148 | if !form.fills() { |
| 149 | next.stroke = next.stroke.or(Shape::of(form).stroke); |
| 150 | next.width = if next.width == 0 { Shape::of(form).width } else { next.width }; |
| 151 | } else if next.fill.is_none() && (next.stroke.is_none() || next.width == 0) { |
| 152 | next.fill = Shape::of(form).fill; |
| 153 | } |
| 154 | this.write_generator(lane, idx, Gen { kind: Kind::Shape(next), ..spec }, cx); |
| 155 | }) |
| 156 | }, |
| 157 | ); |
| 158 | rows.push(div().w_full().child(form).into_any_element()); |
| 159 | if sh.form.fills() { |
| 160 | let can_none = sh.stroke.is_some() && sh.width > 0; |
| 161 | rows.push(self.paint_row("gen-fill", "Fill", sh.fill, GenPaint::Fill, can_none, &SHAPE_SWATCHES, target, cx).into_any_element()); |
| 162 | } |
| 163 | let can_none = sh.form.fills() && sh.fill.is_some(); |
| 164 | rows.push(self.paint_row("gen-stroke", "Stroke", sh.stroke, GenPaint::Stroke, can_none, &SHAPE_SWATCHES, target, cx).into_any_element()); |
| 165 | let why = sh.stroke.is_none().then_some("Turn on a stroke to set its width"); |
| 166 | rows.push(self.num_row("gen-width", "Width", GenPaint::Width, "px", why, cx).into_any_element()); |
| 167 | if sh.form == Form::Rectangle { |
| 168 | rows.push(self.num_row("gen-radius", "Corners", GenPaint::Radius, "px", None, cx).into_any_element()); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /// The Generator card, or nothing when the selected clip is not a generator with a setting. |
| 173 | pub(crate) fn generator_card(&self, _room: Size<Pixels>, cx: &mut Context<Self>) -> Option<impl IntoElement> { |
| 174 | let target = self.generator_target()?; |
| 175 | let spec = target.2; |
| 176 | let note = match spec.kind { |
| 177 | Kind::Solid { .. } => None, |
| 178 | Kind::Gradient { .. } => None, |
| 179 | Kind::Countdown { .. } => None, |
| 180 | Kind::Bars => Some("Nothing to set — the bars are fixed"), |
| 181 | Kind::Shape(_) => None, |
| 182 | Kind::Adjust => Some("Colour, Effects and Transform act on every layer below, for as long as this clip lasts"), |
| 183 | }; |
| 184 | let mut rows: Vec<AnyElement> = Vec::new(); |
| 185 | match spec.kind { |
| 186 | Kind::Solid { rgb } => { |
| 187 | rows.push(self.paint_row("gen-solid", "Colour", Some(rgb), GenPaint::Solid, false, &SWATCHES, target, cx).into_any_element()) |
| 188 | } |
| 189 | Kind::Gradient { a, b, .. } => { |
| 190 | rows.push(self.paint_row("gen-from", "From", Some(a), GenPaint::From, false, &SWATCHES, target, cx).into_any_element()); |
| 191 | rows.push(self.paint_row("gen-to", "To", Some(b), GenPaint::To, false, &SWATCHES, target, cx).into_any_element()); |
| 192 | rows.push(self.num_row("gen-angle", "Angle", GenPaint::Angle, "°", None, cx).into_any_element()); |
| 193 | } |
| 194 | Kind::Countdown { .. } => rows.push(self.num_row("gen-from-s", "From", GenPaint::Count, "s", None, cx).into_any_element()), |
| 195 | Kind::Shape(sh) => self.shape_rows(sh, target, &mut rows, cx), |
| 196 | Kind::Bars | Kind::Adjust => {} |
| 197 | } |
| 198 | Some( |
| 199 | self.card_outer(_room, cx).child( |
| 200 | self.card_surface(div().w_full().flex().flex_col().gap(px(CARD_GAP)).px(px(6.)).py(px(6.))) |
| 201 | .id("gen-card") |
| 202 | .children(rows) |
| 203 | .children(note.map(dark_help)), |
| 204 | ), |
| 205 | ) |
| 206 | } |
| 207 | } |