FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
Fix issue #2263 resize cursor directions
Max Mittelstädt896525e9df2a
diff --git a/crates/ui-egui/src/panels/graphics.rs b/crates/ui-egui/src/panels/graphics.rsindex 92b384be7..1b9e5438c 100644--- a/crates/ui-egui/src/panels/graphics.rs+++ b/crates/ui-egui/src/panels/graphics.rs@@ -190,6 +190,13 @@ pub(crate) fn handle_points(quad: &[Pos2; 4]) -> [Pos2; 8] {[quad[0], quad[1], quad[2], quad[3], mid(0), mid(1), mid(2), mid(3)]}+/// Resize cursor for a selected graphics-box handle, following the handle's screen-space axis.+fn resize_handle_cursor(v: &LayerView, at: Pos2) -> Option<egui::CursorIcon> {+ let quad = v.quad();+ let centre = quad[0] + (quad[2] - quad[0]) * 0.5;+ handle_points(&quad).into_iter().find(|point| (*point - at).length() <= 7.0).map(|point| super::transform_handles::resize_cursor(point - centre))+}+/// Dragging handle `n` of `quad` from `start` to `cur`: the box grows towards the pointer while/// the opposite corner / edge stays put. A corner scales each axis by the pointer's travel along/// it (both alike with `keep_ratio`, Shift); an edge scales only its own.@@ -594,15 +601,18 @@ pub fn monitor_overlay(app: &mut FilmcraftApp, ui: &mut egui::Ui, pic: Rect, fraTool::Type | Tool::VerticalType => ui.ctx().set_cursor_icon(egui::CursorIcon::Text),Tool::Rectangle | Tool::Ellipse | Tool::Pen => ui.ctx().set_cursor_icon(egui::CursorIcon::Crosshair),Tool::Selection => {- let corner = views- .iter()- .filter(|v| Some(v.clip) == sel_clip && sel_layers.contains(&v.layer))- .find_map(|v| corner_handles(v).and_then(|(_, pts)| pts.iter().position(|c| (*c - p).length() <= 6.0)));+ let selected = || views.iter().filter(|v| Some(v.clip) == sel_clip && sel_layers.contains(&v.layer));+ let handle = selected().find_map(|v| resize_handle_cursor(v, p));+ let radius_handle = selected().find_map(|v| {+ let centre = v.quad()[0] + (v.quad()[2] - v.quad()[0]) * 0.5;+ corner_handles(v)?.1.iter().find(|c| (**c - p).length() <= 6.0).map(|c| super::transform_handles::resize_cursor(*c - centre))+ });if editing.as_ref().is_some_and(|e| views.iter().any(|v| v.clip.0 == e.clip && v.layer == e.layer && v.hit(p))) {ui.ctx().set_cursor_icon(egui::CursorIcon::Text);- } else if let Some(n) = corner {- // along the corner's diagonal, the way the drag works- ui.ctx().set_cursor_icon(if n % 2 == 0 { egui::CursorIcon::ResizeNwSe } else { egui::CursorIcon::ResizeNeSw });+ } else if let Some(icon) = handle {+ ui.ctx().set_cursor_icon(icon);+ } else if let Some(icon) = radius_handle {+ ui.ctx().set_cursor_icon(icon);} else if views.iter().any(|v| v.hit(p)) {ui.ctx().set_cursor_icon(egui::CursorIcon::Move);}diff --git a/crates/ui-egui/src/panels/transform_handles.rs b/crates/ui-egui/src/panels/transform_handles.rsindex c31d989c0..c767e3ed3 100644--- a/crates/ui-egui/src/panels/transform_handles.rs+++ b/crates/ui-egui/src/panels/transform_handles.rs@@ -277,7 +277,7 @@ fn changes(d: &Drag, effect: &str, cur: Pos2) -> Vec<(&'static str, Value)> {}/// The resize cursor for a handle in direction `v` from the box's centre.-fn resize_cursor(v: egui::Vec2) -> CursorIcon {+pub(crate) fn resize_cursor(v: egui::Vec2) -> CursorIcon {let deg = v.y.atan2(v.x).to_degrees().rem_euclid(180.0);if !(22.5..157.5).contains(°) {CursorIcon::ResizeHorizontaldiff --git a/crates/ui-egui/tests/graphics_handles_ui.rs b/crates/ui-egui/tests/graphics_handles_ui.rsindex 1f1827c67..5a7090fdd 100644--- a/crates/ui-egui/tests/graphics_handles_ui.rs+++ b/crates/ui-egui/tests/graphics_handles_ui.rs@@ -136,6 +136,15 @@ impl Driver {self.frames(4);}+ fn hover(&mut self, at: (f64, f64)) {+ self.harness.input_mut().events.push(egui::Event::PointerMoved(egui::pos2(at.0 as f32, at.1 as f32)));+ self.frames(2);+ }++ fn cursor(&self) -> egui::CursorIcon {+ self.harness.output().platform_output.cursor_icon+ }+/// Press or let go of Shift as a keyboard reports it. The drag fixture then explicitly/// forwards the held modifiers to the control gesture.fn hold_shift(&mut self, on: bool) {@@ -404,6 +413,39 @@ fn shape_corner_handles_scale_away_from_the_opposite_corner() {assert!(bl2.0 < bl.0 - 20.0 && bl2.1 > bl.1 + 5.0, "bottom-left follows the pointer: {bl:?} -> {bl2:?}");}+#[test]+fn graphic_resize_handles_show_their_screen_space_direction() {+ let mut d = Driver::new();+ let r = d.exec("graphics.newShape", json!({"shape": "rectangle", "position": [800, 500], "size": [500, 300]}));+ let clip = r["clip"].as_u64().unwrap();+ let pre = d.select(clip);++ for (handle, expected) in [+ (0, egui::CursorIcon::ResizeNwSe),+ (1, egui::CursorIcon::ResizeNeSw),+ (2, egui::CursorIcon::ResizeNwSe),+ (3, egui::CursorIcon::ResizeNeSw),+ (4, egui::CursorIcon::ResizeVertical),+ (5, egui::CursorIcon::ResizeHorizontal),+ (6, egui::CursorIcon::ResizeVertical),+ (7, egui::CursorIcon::ResizeHorizontal),+ ] {+ let at = d.handle(&pre, handle);+ d.hover(at);+ assert_eq!(d.cursor(), expected, "handle {handle}");+ }++ d.exec("graphics.set", json!({"clip": clip, "props": {"rotation": 45}}));+ d.frames(4);+ let at = d.handle(&pre, 4);+ d.hover(at);+ assert!(+ matches!(d.cursor(), egui::CursorIcon::ResizeNeSw | egui::CursorIcon::ResizeNwSe),+ "rotated edge handle should use a diagonal cursor, got {:?}",+ d.cursor()+ );+}+/// Width and height of a layer's box in its own pixels.fn box_size(l: &Value) -> (f64, f64) {let b = &l["localBounds"];