diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 392c9d5..819e56b 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -756,6 +756,11 @@ struct Player { /// equalizer's drag, for the same reason: a 4 px bar is left by the pointer /// on the first move and its own listeners then stop firing. color_dragging: bool, + /// HSV wheel on CLIP: same 4 px reason as `color_dragging` — the pointer + /// leaves the disk on the first move, so samples live on the root. + viz_hs_dragging: bool, + /// The wheel's box, recorded at prepaint for a drag's window position. + viz_wheel: Rc>>, /// Each slider's box, recorded at prepaint: a mouse listener is handed the /// window position only, so this is what a press and a drag are read against /// ([`frac_along`]). One per band, because the press picks the row it landed @@ -1064,6 +1069,8 @@ fn main() { color_open: None, color_band: 0, color_dragging: false, + viz_hs_dragging: false, + viz_wheel: Rc::default(), color_bars: std::array::from_fn(|_| Rc::default()), pending_color: None, transform_open: None, diff --git a/crates/app/src/player/library.rs b/crates/app/src/player/library.rs index 5acf26f..32d0857 100644 --- a/crates/app/src/player/library.rs +++ b/crates/app/src/player/library.rs @@ -242,6 +242,21 @@ impl Player { }); } + pub(crate) fn drag_viz_hs(&mut self, at: gpui::Point, cx: &mut Context) { + let b = self.viz_wheel.get(); + let w = f32::from(b.size.width).max(1.); + let h = f32::from(b.size.height).max(1.); + let dx = f32::from(at.x - b.origin.x) - w / 2.; + let dy = h / 2. - f32::from(at.y - b.origin.y); + let radius = w.min(h) / 2.; + let r = (dx * dx + dy * dy).sqrt() / radius; + let sat = (r.clamp(0., 1.) * 255.).round() as u8; + let ang = dx.atan2(dy).to_degrees(); + let hue_deg = (ang + 360.) % 360.; + let hue = (hue_deg / 360. * 256.).round() as u16 as u8; + self.set_viz_hs(hue, sat, cx); + } + pub(crate) fn set_viz_strands(&mut self, n: u8, cx: &mut Context) { self.set_viz_paint_with(cx, |p| engine::decode::with_viz_paint_strands(p, n)); } @@ -484,6 +499,7 @@ impl Player { self.eq_dragging = false; self.speed_dragging = false; self.color_dragging = false; + self.viz_hs_dragging = false; self.pending_color = None; self.transform_dragging = false; self.pending_transform = None; diff --git a/crates/app/src/player/timeline_edit.rs b/crates/app/src/player/timeline_edit.rs index 834dfd3..8ecad0b 100644 --- a/crates/app/src/player/timeline_edit.rs +++ b/crates/app/src/player/timeline_edit.rs @@ -2103,6 +2103,14 @@ impl Player { } return; } + if self.viz_hs_dragging { + if event.pressed_button == Some(MouseButton::Left) { + self.drag_viz_hs(event.position, cx); + } else { + self.viz_hs_dragging = false; + } + return; + } // The transform card's sliders, [`Self::color_dragging`]'s own reason. if self.transform_dragging { if event.pressed_button == Some(MouseButton::Left) { @@ -2219,6 +2227,10 @@ impl Player { self.flush_drag(cx); return; } + if std::mem::take(&mut self.viz_hs_dragging) { + self.drag_viz_hs(event.position, cx); + return; + } if std::mem::take(&mut self.transform_dragging) { self.drag_transform(event.position.x, false, cx); self.flush_drag(cx); diff --git a/crates/app/src/ui/dock_stance.rs b/crates/app/src/ui/dock_stance.rs index 4eca6fb..3590bf2 100644 --- a/crates/app/src/ui/dock_stance.rs +++ b/crates/app/src/ui/dock_stance.rs @@ -1282,7 +1282,7 @@ fn viz_style_verbs(player: &Player, cx: &mut Context) -> impl IntoElemen engine::decode::VIZ_FAST, flags & engine::decode::VIZ_FAST != 0, )) - .child(viz_paint_knobs(paint, cx)) + .child(viz_paint_knobs(player, paint, cx)) } fn viz_step( @@ -1306,9 +1306,7 @@ fn viz_step( .child(glyph) } -fn viz_color_wheel(paint: u32, cx: &mut Context) -> impl IntoElement { - let hit = std::rc::Rc::new(std::cell::Cell::new(Bounds::default())); - let hit_click = hit.clone(); +fn viz_color_wheel(player: &Player, paint: u32, cx: &mut Context) -> impl IntoElement { div() .id("dock-viz-hs") .w(px(112.)) @@ -1316,7 +1314,7 @@ fn viz_color_wheel(paint: u32, cx: &mut Context) -> impl IntoElement { .relative() .cursor_pointer() .children(hitmap::control("dock-viz-hs", "Colour wheel", true)) - .child(bounds_probe(hit)) + .child(bounds_probe(player.viz_wheel.clone())) .child( canvas( |_, _, _| (), @@ -1375,26 +1373,13 @@ fn viz_color_wheel(paint: u32, cx: &mut Context) -> impl IntoElement { .on_mouse_down( MouseButton::Left, cx.listener(move |this, event: &MouseDownEvent, _, cx| { - let b = hit_click.get(); - let w = f32::from(b.size.width).max(1.); - let h = f32::from(b.size.height).max(1.); - let dx = f32::from(event.position.x - b.origin.x) - w / 2.; - let dy = h / 2. - f32::from(event.position.y - b.origin.y); - let radius = w.min(h) / 2.; - let r = (dx * dx + dy * dy).sqrt() / radius; - if r > 1.08 { - return; - } - let sat = (r.clamp(0., 1.) * 255.).round() as u8; - let ang = dx.atan2(dy).to_degrees(); - let hue_deg = (ang + 360.) % 360.; - let hue = (hue_deg / 360. * 256.).round() as u16 as u8; - this.set_viz_hs(hue, sat, cx); + this.viz_hs_dragging = true; + this.drag_viz_hs(event.position, cx); }), ) } -fn viz_paint_knobs(paint: u32, cx: &mut Context) -> impl IntoElement { +fn viz_paint_knobs(player: &Player, paint: u32, cx: &mut Context) -> impl IntoElement { let strands = engine::decode::viz_strands_of(paint); let glow = engine::decode::viz_glow_of(paint); let style = label(type_scale::LABEL_ROW_PX, FontWeight::MEDIUM); @@ -1416,7 +1401,7 @@ fn viz_paint_knobs(paint: u32, cx: &mut Context) -> impl IntoElement { .text_color(rgb(INK2())) .child("Colour"), ) - .child(viz_color_wheel(paint, cx)), + .child(viz_color_wheel(player, paint, cx)), ) .child( div()