diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 007b38216..ac443d275 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -513,16 +513,20 @@ fn draw(app: &SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, proj: &Proj, } else { 1.0 }; - // Extrudes and moves go either way; radii and thicknesses stay positive. - let signed = matches!( - d.kind, + // Extrudes, moves, pattern spacings and one-sided sketch offsets go either way (#5497); + // radii, thicknesses, depths and heights stay positive. A symmetric offset takes the + // distance's size only, so a drag past its curve would just turn the arrow round. + let signed = match &d.kind { Kind::Extrude { .. } - | Kind::Move { .. } - | Kind::Section { .. } - | Kind::OffsetPlane { .. } - | Kind::OffsetFaces { .. } - | Kind::SketchModify(crate::sketch_modify_ui::Mod::Move { .. }) - ); + | Kind::Move { .. } + | Kind::Section { .. } + | Kind::OffsetPlane { .. } + | Kind::OffsetFaces { .. } + | Kind::PatternRect { .. } => true, + Kind::SketchModify(crate::sketch_modify_ui::Mod::Move { .. } | crate::sketch_modify_ui::Mod::PatternRect { .. }) => true, + Kind::SketchModify(crate::sketch_modify_ui::Mod::Offset { mode, .. }) => !crate::sketch_modify_ui::offset_symmetric(*mode), + _ => false, + }; let mut focus = std::mem::take(&mut d.focus); let half_height = app.cam.half_height(); let axis = revolve_axis(app, d); @@ -569,7 +573,7 @@ fn draw(app: &SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, proj: &Proj, if let Some(spacing2) = spacing2 && let Some(l2) = crate::length_input::eval(&app.session.doc, spacing2, ValueKind::Length).ok().filter(|v| v.is_finite()) { - drag_arrow(&app.session.doc, ui, painter, proj, base, bs, n2, l2, false, egui::Id::new("sc_manipulator2"), half_height, 0.0, spacing2); + drag_arrow(&app.session.doc, ui, painter, proj, base, bs, n2, l2, true, egui::Id::new("sc_manipulator2"), half_height, 0.0, spacing2); } } // An offset plane shows where it will be: at the distance, or through the picked point. diff --git a/crates/ui-egui/src/pattern_arrow_tests.rs b/crates/ui-egui/src/pattern_arrow_tests.rs index 26efe08cf..a6d8bc9b0 100644 --- a/crates/ui-egui/src/pattern_arrow_tests.rs +++ b/crates/ui-egui/src/pattern_arrow_tests.rs @@ -145,3 +145,37 @@ fn inch_pattern_arrows_follow_bare_primary_and_secondary_distances() { assert!(bare.0.distance(explicit.0) < 0.01, "primary: {bare:?} vs {explicit:?}"); assert!(bare.1.distance(explicit.1) < 0.01, "secondary: {bare:?} vs {explicit:?}"); } + +/// Dragging a solid pattern's Spacing arrow back past its start gives a negative spacing, as +/// typing one does (#5497): the arrow was floored at one snap step. +#[test] +fn a_solid_pattern_spacing_arrow_dragged_past_zero_goes_negative() { + let mut h = Harness::new(); + h.run(&json!([ + {"call": "engine.execute", "params": {"command": "solid.box", "params": {"length": 10, "width": 10, "height": 10}}}, + {"call": "ui.view", "params": {"view": "iso"}}, + {"start": "solid.pattern.rectangular"}, + {"click": {"world": [5, 5, 10]}}, + {"call": "ui.dialogInput", "params": {"index": 1}}, + {"click": {"axis": "X"}} + ])) + .unwrap(); + // Keep the arrow's tip clear of its value box, which the iso view pushes onto it at the + // viewport's edge. + h.call("ui.camera", json!({"target": [15, 5, 5]})); + h.settle(); + let (at, dir) = crate::canvas::anchor(&h.app, h.app.dialog.as_ref().unwrap()).expect("an anchor"); + let to = at - dir.expect("a direction") * 10.0; + h.step(&json!({"drag": [{"handle": "arrow"}, {"world": [to.x, to.y, to.z]}], "ctrl": true})).unwrap(); + let (spacing, _) = spacings(&h); + let v = h.app.session.doc.eval(&spacing, solidcraft_engine::doc::expr::Kind::Length).unwrap(); + assert!((v + 10.0).abs() < 0.5, "dragging 10 mm back from the start gave Spacing {spacing}"); + // Applied, the copies sit on the negative side, as a typed negative spacing puts them. + h.step(&json!({"widget": "OK"})).unwrap(); + assert!(h.app.dialog.is_none(), "{:?}", h.app.status); + let st = h.app.session.world_state(); + for (copy, k) in [("Body1 (2)", 1.0), ("Body1 (3)", 2.0)] { + let x = st.body(copy).unwrap_or_else(|| panic!("no {copy}: {:?}", h.app.status)).mesh().bounds().center().x; + assert!((x - (5.0 + k * v)).abs() < 1e-6, "{copy} centred at x = {x} for Spacing {spacing}"); + } +} diff --git a/crates/ui-egui/src/sketch_modify_ui_tests.rs b/crates/ui-egui/src/sketch_modify_ui_tests.rs index 3a04a73b5..551329a9e 100644 --- a/crates/ui-egui/src/sketch_modify_ui_tests.rs +++ b/crates/ui-egui/src/sketch_modify_ui_tests.rs @@ -669,6 +669,85 @@ fn the_spacing_gets_a_canvas_arrow_along_the_pattern_direction() { assert_eq!(dir, Some(solidcraft_engine::geom::Vec3::Y)); } +/// Dragging a spacing arrow past its start flips the direction, as typing a negative spacing does +/// (#5497): the first direction's arrow and the second's both read a negative value. +#[test] +fn dragging_a_spacing_arrow_past_zero_gives_a_negative_spacing() { + let (mut h, id) = open("sketch.pattern.rectangular"); + h.app.dialog.as_mut().unwrap().inputs[0].items = vec![Sel::SketchCurve { id, sketch: None }]; + h.app.dialog.as_mut().unwrap().inputs[1].items = vec![Sel::Axis { name: "X".into() }]; + h.app.dialog.as_mut().unwrap().inputs[2].items = vec![Sel::Axis { name: "Y".into() }]; + rect(&mut h, "3", "20", "2", "10"); + // From above, so both arrows lie across the screen. + h.step(&json!({"camera": {"yaw": 0.4, "pitch": 1.2}})).unwrap(); + h.frames(5); + let spacing = |h: &crate::scenario::Harness, second: bool| { + let Some(Kind::SketchModify(Mod::PatternRect { spacing, spacing2, .. })) = h.app.dialog.as_ref().map(|d| &d.kind) else { + panic!("the pattern window") + }; + h.app.session.doc.eval(if second { spacing2 } else { spacing }, solidcraft_engine::doc::expr::Kind::Length).unwrap() + }; + let (at, dir) = crate::canvas::anchor(&h.app, h.app.dialog.as_ref().unwrap()).expect("an anchor"); + let dir = dir.expect("a direction"); + let to = at - dir * 10.0; + h.step(&json!({"drag": [{"handle": "arrow"}, {"world": [to.x, to.y, to.z]}], "ctrl": true})).unwrap(); + let v = spacing(&h, false); + assert!((v + 10.0).abs() < 0.5, "dragging 10 mm back gave {v}"); + h.frames(3); + let (at, _) = crate::canvas::anchor(&h.app, h.app.dialog.as_ref().unwrap()).expect("an anchor"); + let dir2 = crate::sketch_modify_ui::arrow2(&h.app, h.app.dialog.as_ref().unwrap()).expect("a second direction"); + let to = at - dir2 * 6.0; + h.step(&json!({"drag": [{"handle": "arrow2"}, {"world": [to.x, to.y, to.z]}], "ctrl": true})).unwrap(); + assert!(spacing(&h, true) < 0.0, "the second arrow stayed positive: {}", spacing(&h, true)); +} + +/// Dragging a one-sided offset's arrow back across its curve offsets to the other side, the same +/// as typing a negative distance (#5497). A symmetric offset uses the distance's size only, so its +/// arrow still stops short of the curve. +#[test] +fn dragging_an_offset_arrow_across_its_curve_offsets_the_other_way() { + let made_radius = |drag: bool, symmetric: bool| { + let (mut h, id) = open("sketch.offset"); + let d = h.app.dialog.as_mut().unwrap(); + d.inputs[0].items = vec![Sel::SketchCurve { id, sketch: None }]; + let Kind::SketchModify(Mod::Offset { distance, mode, .. }) = &mut d.kind else { panic!("the offset window") }; + if symmetric { + *mode = 2; + } + if !drag { + *distance = "-1 mm".into(); + } + h.step(&json!({"camera": {"yaw": 0.4, "pitch": 1.2}})).unwrap(); + h.frames(5); + if drag { + let (at, dir) = crate::canvas::anchor(&h.app, h.app.dialog.as_ref().unwrap()).expect("an anchor"); + let to = at - dir.expect("a direction") * 1.0; + h.step(&json!({"drag": [{"handle": "arrow"}, {"world": [to.x, to.y, to.z]}], "ctrl": true})).unwrap(); + } + let Some(Kind::SketchModify(Mod::Offset { distance, .. })) = h.app.dialog.as_ref().map(|d| &d.kind) else { panic!("the offset window") }; + let value = h.app.session.doc.eval(distance, solidcraft_engine::doc::expr::Kind::Length).unwrap(); + h.step(&json!({"widget": "OK"})).unwrap(); + assert!(h.app.dialog.is_none(), "{:?}", h.app.status); + let st = h.app.session.model.state(); + let sk = &st.sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch; + let radii: Vec = sk + .curves + .iter() + .filter_map(|c| if let solidcraft_engine::sketch::CurveKind::Circle { r, .. } = c.kind { Some(r) } else { None }) + .collect(); + (value, radii) + }; + // The circle's offset to the left of its curve is inside it; the other way is outside. + let (dragged, radii) = made_radius(true, false); + assert!((dragged + 1.0).abs() < 0.05, "dragging 1 mm back across the curve gave {dragged}"); + assert_eq!(radii.len(), 2, "{radii:?}"); + assert!((radii[1] - (5.0 - dragged)).abs() < 1e-6, "{radii:?}"); + let (_, typed) = made_radius(false, false); + assert!((typed[1] - 6.0).abs() < 1e-6 && (radii[1] - typed[1]).abs() < 0.05, "dragged {radii:?}, typed {typed:?}"); + let (symmetric, _) = made_radius(true, true); + assert!(symmetric > 0.0, "a symmetric offset's arrow went past its curve: {symmetric}"); +} + /// A sketch holding one circle 30 mm out and a lone point at the origin, with the circular /// pattern window open on it and both inputs filled. The point is the centre the pattern turns /// around; the command has no default for it. Returns the centre's bare point id.