SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Move/Copy: show a handle for the selected rotation axis (#6822) (#6832)
Colin Faulkinghamabdf9d79cff6
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 2dca08d4f6..ad08c8ed77 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -276,6 +276,12 @@ pub(crate) fn owns_pointer(app: &SolveApp, proj: &Proj, p: Pos2) -> bool {// The triad only registers an egui hit widget after a handle is under the pointer.// Test its geometry before navigation, including the first frame entering a handle.app.dialog.as_ref().and_then(|d| crate::dialogs_move::triad(app, d)).is_some_and(|t| crate::gizmo::contains_pointer(proj, &t, p))+ || app+ .dialog+ .as_ref()+ .and_then(|d| move_rotation(app, d, proj))+ .and_then(|r| proj.to_screen(r.point(r.angle)))+ .is_some_and(|end| egui::Rect::from_center_size(end, vec2(20.0, 20.0)).contains(p))|| (app.dialog.is_none()&& app.tool.is_none()&& crate::browser::occurrence_triad_model(app).is_some_and(|t| crate::gizmo::contains_pointer(proj, &t, p)))@@ -318,9 +324,11 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, projdragging = true;}// Read the triad again after the drag, so the box follows it in the same frame. Move- // types with no triad (Rotate, Point to Point, Point to Position) put the box on the- // picked bodies instead.+ // types with no triad put the box on the picked bodies, or beside Rotate's handle+ // once an axis is selected.let triad = crate::dialogs_move::triad(app, &d);+ let rotation = draw_move_rotation(app, ui, painter, proj, &mut d);+ dragging |= rotation.is_some_and(|(_, changed)| changed);let at = triad.map(|t| t.center).or_else(|| anchor(app, &d).map(|(base, _)| base));// Tab and Shift+Tab in the box show the next or previous value (Y after X; #4776),// keeping the keyboard in the box.@@ -340,7 +348,8 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, proj// Clear of the triad: its arrows reach LEN_PX and its rings RING of that, so a box// beside the centre would cover the handles the user is reaching for.let off = if triad.is_some() { vec2(crate::gizmo::LEN_PX as f32 + 16.0, -12.0) } else { vec2(16.0, -30.0) };- value_box_with(ui, label, value, bs + off, focus || dragging, true);+ let box_at = rotation.map_or(bs + off, |(end, _)| end + vec2(16.0, -12.0));+ value_box_with(ui, label, value, box_at, focus || dragging, true);}app.dialog = Some(d);return;@@ -355,6 +364,59 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, projcrate::hole_place::show(app, ui, painter, proj);}+struct MoveRotation {+ centre: Vec3,+ axis: Vec3,+ radius: f64,+ angle: f64,+}++impl MoveRotation {+ fn point(&self, angle: f64) -> Vec3 {+ let e1 = self.axis.any_perp();+ let e2 = self.axis.cross(e1);+ self.centre + (e1 * angle.cos() + e2 * angle.sin()) * self.radius+ }+}++fn move_rotation(app: &SolveApp, d: &Dialog, proj: &Proj) -> Option<MoveRotation> {+ let Kind::Move { angle, .. } = &d.kind else { return None };+ let (centre, axis) = crate::dialogs_move::rotation_axis(app, d)?;+ let angle = crate::length_input::eval(&app.session.doc, angle, ValueKind::Angle).ok().filter(|a| a.is_finite()).unwrap_or(0.0);+ Some(MoveRotation { centre, axis, radius: crate::gizmo::ring_radius(proj, centre)?, angle })+}++/// Reuse the angular handle's snapping and edge-on projection for Move's selected axis.+fn draw_move_rotation(app: &SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, proj: &Proj, d: &mut Dialog) -> Option<(Pos2, bool)> {+ let r = move_rotation(app, d, proj)?;+ let points: Vec<_> = (0..=64).filter_map(|i| proj.to_screen(r.point(i as f64 / 64.0 * std::f64::consts::TAU))).collect();+ painter.add(egui::Shape::line(points, Stroke::new(1.8, Tokens::get().manipulator)));+ let Kind::Move { angle, .. } = &mut d.kind else { return None };+ let before = angle.clone();+ let e1 = r.axis.any_perp();+ // Keep full turns in the expression while the shared handle works near zero.+ let boundary = r.angle % std::f64::consts::TAU;+ let offset = r.angle - boundary;+ let span = 2.0 * std::f64::consts::TAU;+ let end = rotation_handle(+ ui,+ painter,+ proj,+ r.centre,+ e1,+ r.axis.cross(e1),+ r.axis,+ r.radius,+ boundary,+ 1.0,+ offset,+ (offset - span, offset + span),+ "move_rotate",+ angle,+ )?;+ Some((end, *angle != before))+}+/// A round step for dragged values at this zoom (1, 2 or 5 times a power of ten).pub fn snap_step(half_height: f64) -> f64 {let raw = (half_height / 40.0).max(1e-4);diff --git a/crates/ui-egui/src/dialogs_move.rs b/crates/ui-egui/src/dialogs_move.rsindex 83b718abdc..c19f22d6f5 100644--- a/crates/ui-egui/src/dialogs_move.rs+++ b/crates/ui-egui/src/dialogs_move.rs@@ -252,6 +252,17 @@ pub fn triad(app: &SolveApp, d: &Dialog) -> Option<gizmo::Triad> {Some(gizmo::Triad { center: pivot(app, d)? + t, translate: true, rotate: *mode == FREE })}+/// Rotate's ring lies on the selected axis, level with the picked bodies' centre.+pub(crate) fn rotation_axis(app: &SolveApp, d: &Dialog) -> Option<(Vec3, Vec3)> {+ if !matches!(d.kind, Kind::Move { mode: ROTATE, .. }) {+ return None;+ }+ let centre = bodies_center(app, d)?;+ let (origin, axis) = axis_line(app, items(d, 1).first()?)?;+ let axis = axis.normalized()?;+ Some((origin + axis * (centre - origin).dot(axis), axis))+}+/// Apply a triad drag to the dialog's values (added to the values when the drag began)./// Which value the canvas box shows and types into, for the types that translate: X, Y or Z/// (0 to 2), or, in Free Move, the angle about X, Y or Z (3 to 5). Kept in `extra` asdiff --git a/crates/ui-egui/src/dialogs_move_tests.rs b/crates/ui-egui/src/dialogs_move_tests.rsindex ba5895360f..042bb76217 100644--- a/crates/ui-egui/src/dialogs_move_tests.rs+++ b/crates/ui-egui/src/dialogs_move_tests.rs@@ -54,8 +54,105 @@ fn the_main_value_writes_through_to_the_command() {assert_eq!((x.as_str(), y.as_str(), z.as_str()), ("30 mm", "0 mm", "0 mm"), "the box edits X alone");}-/// Free Move and Translate carry the triad. The other three have no manipulator yet, so their-/// value box is the only way to drive them: it must still have somewhere to sit.+/// #6822: Rotate must offer a draggable handle after the body and axis are picked.+#[test]+fn rotate_handle_updates_the_angle_without_moving_the_camera() {+ let mut h = crate::scenario::Harness::new();+ h.app.preferences.nav = "openscad".into();+ h.app.run("solid.box", json!({"length": 20, "width": 30, "height": 10})).unwrap();+ h.step(&json!({"call": "ui.view", "params": {"view": "top"}})).unwrap();+ h.app.cam.distance = 180.0;+ h.app.start("solid.move");+ {+ let d = h.app.dialog.as_mut().unwrap();+ d.kind = kind(ROTATE, &h.app.session.doc);+ d.inputs = inputs(ROTATE);+ d.inputs[0].items = vec![Sel::Body { name: "Body1".into() }];+ d.inputs[1].items = vec![Sel::Axis { name: "Z".into() }];+ }+ h.frames(5);+ let before = serde_json::to_value(&*h.app.session.doc).unwrap();+ let camera = h.app.cam;+ let start = h.at(&json!({"handle": "move_rotate"})).unwrap();+ let centre = h.at(&json!({"world": [0, 0, 5]})).unwrap();+ // A quarter turn clockwise in the top view is -90 degrees about +Z.+ let end = [centre[0] - (start[1] - centre[1]), centre[1] + (start[0] - centre[0])];+ h.step(&json!({"drag": [start, end], "steps": 12})).unwrap();+ let d = h.app.dialog.as_ref().unwrap();+ let p = params(&h.app, d).unwrap();+ assert_eq!(p["angle"], "-90 deg");+ assert_eq!(p["axis"], json!([0.0, 0.0, 1.0]));+ assert_eq!(h.app.cam, camera, "the handle owns the drag");+ assert_eq!(serde_json::to_value(&*h.app.session.doc).unwrap(), before, "a preview does not commit the move");+ h.step(&json!({"key": "Enter"})).unwrap();+ h.settle();+ assert!(h.app.dialog.is_none());+ let bounds = h.app.session.model.state().body("Body1").unwrap().mesh().bounds();+ assert!((bounds.min - Vec3::new(0.0, -20.0, 0.0)).len() < 1e-6, "{bounds:?}");+ assert!((bounds.max - Vec3::new(30.0, 0.0, 10.0)).len() < 1e-6, "{bounds:?}");+ h.app.run("edit.undo", json!({})).unwrap();+ assert_eq!(serde_json::to_value(&*h.app.session.doc).unwrap(), before);+}++#[test]+fn rotate_handle_follows_an_offset_axis_and_preserves_full_turns() {+ for (direction, initial, expected, free) in [(Vec3::new(1.0, 1.0, 1.0), "720 deg", 755.0, false), (-Vec3::Z, "", 37.3, true)] {+ let mut h = crate::scenario::Harness::new();+ h.app.run("solid.box", json!({"length": 20, "width": 30, "height": 10})).unwrap();+ let origin = Vec3::new(5.0, 7.0, 9.0);+ let end = origin + direction;+ h.app+ .run(+ "construct.axis.two_points",+ json!({"a": {"point": [origin.x, origin.y, origin.z]}, "b": {"point": [end.x, end.y, end.z]}, "name": "TurnAxis"}),+ )+ .unwrap();+ h.app.cam = h.app.cam.looking_from(direction);+ h.app.cam.distance = 180.0;+ h.app.start("solid.move");+ {+ let d = h.app.dialog.as_mut().unwrap();+ d.kind = kind(ROTATE, &h.app.session.doc);+ d.inputs = inputs(ROTATE);+ d.inputs[0].items = vec![Sel::Body { name: "Body1".into() }];+ }+ h.frames(3);+ assert!(crate::scenario::handle_at("move_rotate").is_none(), "an axis must be selected first");+ {+ let d = h.app.dialog.as_mut().unwrap();+ d.inputs[1].items = vec![Sel::Axis { name: "TurnAxis".into() }];+ if let Kind::Move { angle, .. } = &mut d.kind {+ *angle = initial.into();+ }+ }+ h.frames(3);+ let before = serde_json::to_value(&*h.app.session.doc).unwrap();+ let d = h.app.dialog.as_ref().unwrap();+ let (centre, axis) = rotation_axis(&h.app, d).unwrap();+ assert!((centre - origin).cross(direction).len() < 1e-9);+ assert!((axis - direction.normalized().unwrap()).len() < 1e-9);+ let proj = crate::viewport::projection(&h.app, h.app.viewport.rect.unwrap());+ let radius = crate::gizmo::ring_radius(&proj, centre).unwrap();+ let e1 = axis.any_perp();+ let e2 = axis.cross(e1);+ let th = 37.3_f64.to_radians();+ let target = centre + (e1 * th.cos() + e2 * th.sin()) * radius;+ h.step(&json!({"drag": [{"handle": "move_rotate"}, {"world": [target.x, target.y, target.z]}], "ctrl": free, "steps": 12})).unwrap();+ let d = h.app.dialog.as_ref().unwrap();+ let p = params(&h.app, d).unwrap();+ let actual = ang(&h.app, p["angle"].as_str().unwrap()).unwrap();+ assert!((actual.to_degrees() - expected).abs() < 0.11, "{p}");+ let translation: [String; 3] = serde_json::from_value(p["translate"].clone()).unwrap();+ let translation = translation.map(|v| len(&h.app, &v).unwrap());+ let moved_origin = gizmo::apply(&gizmo::rotation(axis, actual), origin) + Vec3::new(translation[0], translation[1], translation[2]);+ assert!((moved_origin - origin).len() < 1e-5, "the selected axis stays fixed");+ h.step(&json!({"widget": "Cancel"})).unwrap();+ assert_eq!(serde_json::to_value(&*h.app.session.doc).unwrap(), before);+ }+}++/// Free Move and Translate carry the triad. The other modes still need a place for their value+/// box before enough geometry is picked to draw a handle.#[test]fn every_move_type_has_somewhere_to_put_its_value_box() {let mut s = Session::default();diff --git a/crates/ui-egui/src/gizmo.rs b/crates/ui-egui/src/gizmo.rsindex b02e442868..c809e2e150 100644--- a/crates/ui-egui/src/gizmo.rs+++ b/crates/ui-egui/src/gizmo.rs@@ -228,6 +228,11 @@ fn mm_per_px(proj: &Proj, c: Vec3) -> Option<f64> {(px > 1e-9).then(|| 1.0 / px)}+/// Radius shared by the triad rings and the selected-axis Rotate handle.+pub(crate) fn ring_radius(proj: &Proj, centre: Vec3) -> Option<f64> {+ Some(mm_per_px(proj, centre)? * LEN_PX * RING)+}+fn snap(v: f64, step: f64, free: bool) -> f64 {if free || step <= 0.0 { v } else { (v / step).round() * step }}