Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Free Transform: rotation cursor and live angle readout (#1948)

Nigel Ihza Firdausy765495e53b64
diff --git a/crates/ui-egui/src/transform_tool.rs b/crates/ui-egui/src/transform_tool.rs
index a81646cb3..83af1f897 100644
--- a/crates/ui-egui/src/transform_tool.rs
+++ b/crates/ui-egui/src/transform_tool.rs
@@ -1270,6 +1270,22 @@ pub fn split(app: &mut PhotocraftApp, id: &str, at: Option<[f64; 2]>) -> Result<
edit_session_warp(app, id, &p)
}
+/// True just outside a corner: rotate handle feedback lives here, not over the entire
+/// empty canvas. The actual rotation gesture continues to work farther away.
+fn near_rotate_corner(t: &TransformSession, p: [f64; 2], tol: f64) -> bool {
+ t.warp.is_none()
+ && t.mode != TransformMode::Distort
+ && hit(t, p, tol) == Hit::Outside
+ && t.quad.iter().any(|q| (p[0] - q[0]).hypot(p[1] - q[1]) <= tol * 3.0)
+}
+
+/// Clockwise degrees in canvas coordinates (positive Y points down).
+fn rotation_degrees(quad: [[f64; 2]; 4]) -> f64 {
+ let dx = quad[1][0] - quad[0][0];
+ let dy = quad[1][1] - quad[0][1];
+ if dx.is_finite() && dy.is_finite() && dx.hypot(dy) > 1e-9 { dy.atan2(dx).to_degrees() } else { 0.0 }
+}
+
/// Cursor for hovering a document point while transforming. `alt` is Option, which arms a quick
/// split while a warp is active.
pub fn cursor(app: &PhotocraftApp, p: [f64; 2], alt: bool) -> Option<CursorIcon> {
@@ -1288,6 +1304,12 @@ pub fn cursor(app: &PhotocraftApp, p: [f64; 2], alt: bool) -> Option<CursorIcon>
if !distort_allows(t.mode, h) {
return Some(CursorIcon::Default);
}
+ if (near_rotate_corner(t, p, tol) || app.transform_preview.as_ref().is_some_and(|pv| pv.gesture.is_some_and(|g| g.hit == Hit::Outside)))
+ && t.mode != TransformMode::Distort
+ {
+ // The overlay paints the rotating arrow and pointer marker, including during a drag.
+ return Some(CursorIcon::None);
+ }
Some(match h {
Hit::Corner(0 | 2) => CursorIcon::ResizeNwSe,
Hit::Corner(_) => CursorIcon::ResizeNeSw,
@@ -1362,6 +1384,37 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform
painter.circle_stroke(c, 5.0, Stroke::new(0.5, Color32::BLACK));
painter.line_segment([c - vec2(8.0, 0.0), c + vec2(8.0, 0.0)], Stroke::new(1.0, Color32::WHITE));
painter.line_segment([c - vec2(0.0, 8.0), c + vec2(0.0, 8.0)], Stroke::new(1.0, Color32::WHITE));
+ let rotating = pv.gesture.is_some_and(|g| g.hit == Hit::Outside);
+ if let Some(at) = painter.ctx().pointer_hover_pos().filter(|p| xf.rect.contains(*p)) {
+ let p = xf.to_doc(at);
+ let tol = HANDLE_PX / f64::from(xf.zoom.max(0.01));
+ if t.mode != TransformMode::Distort && (rotating || near_rotate_corner(t, p, tol)) {
+ draw_rotate_feedback(painter, at, rotating.then(|| rotation_degrees(t.quad)), xf.rect);
+ }
+ }
+}
+
+/// The transform's rotation handle is an arc, not the generic Alias pointer.
+fn draw_rotate_feedback(painter: &egui::Painter, at: Pos2, angle: Option<f64>, bounds: egui::Rect) {
+ let center = at + vec2(14.0, 11.0);
+ // Keep the visible cursor anchored to the actual pointer hotspot.
+ painter.circle_filled(at, 2.0, Color32::BLACK);
+ painter.circle_filled(at, 1.0, Color32::WHITE);
+ let points: Vec<Pos2> = (0..=18)
+ .map(|i| {
+ let rad = 0.5 + i as f32 * 4.6 / 18.0;
+ center + vec2(rad.cos(), rad.sin()) * 7.0
+ })
+ .collect();
+ painter.add(egui::Shape::line(points.clone(), Stroke::new(3.0, Color32::BLACK)));
+ painter.add(egui::Shape::line(points, Stroke::new(1.5, Color32::WHITE)));
+ let tip = center + vec2(4.1, -5.7);
+ painter.line_segment([tip, tip + vec2(-5.0, -1.0)], Stroke::new(2.0, Color32::WHITE));
+ painter.line_segment([tip, tip + vec2(-1.0, 5.0)], Stroke::new(2.0, Color32::WHITE));
+ if let Some(deg) = angle {
+ let pos = egui::pos2((at.x + 27.0).min(bounds.right() - 48.0).max(bounds.left()), (at.y - 14.0).max(bounds.top() + 17.0).min(bounds.bottom()));
+ painter.text(pos, egui::Align2::LEFT_BOTTOM, format!("{deg:.1}°"), egui::FontId::proportional(12.0), Color32::WHITE);
+ }
}
/// Warp preview (the moving pixels on a fine textured mesh) plus the control mesh. Patch
@@ -1803,6 +1856,28 @@ mod tests {
}
}
+ #[test]
+ fn rotate_feedback_only_appears_just_outside_corners() {
+ let mut t = session();
+ assert!(near_rotate_corner(&t, [-18.0, -18.0], 12.0));
+ assert!(!near_rotate_corner(&t, [50.0, 25.0], 12.0), "inside moves the box");
+ assert!(!near_rotate_corner(&t, [500.0, 500.0], 12.0), "far outside stays generic");
+ assert!(!near_rotate_corner(&t, [2.0, 2.0], 12.0), "corner handle resizes");
+ t.mode = TransformMode::Distort;
+ assert!(!near_rotate_corner(&t, [-18.0, -18.0], 12.0));
+ }
+
+ #[test]
+ fn angle_readout_tracks_rotated_quad_and_handles_degenerate_edges() {
+ let mut t = session();
+ assert_eq!(rotation_degrees(t.quad), 0.0);
+ let pivot = t.pivot;
+ t.quad = t.quad.map(|q| [pivot[0] - (q[1] - pivot[1]), pivot[1] + (q[0] - pivot[0])]);
+ assert!((rotation_degrees(t.quad) - 90.0).abs() < 1e-9);
+ t.quad[1] = t.quad[0];
+ assert_eq!(rotation_degrees(t.quad), 0.0);
+ }
+
#[test]
fn transform_bar_pins_warp_cancel_and_commit() {
use egui_kittest::Harness;