Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Sketch: a click that moves on the way places its point (#2448) (#2449)

HiroKenEzca9c363a9808
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 3d344ca6bf..799369a194 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -168,6 +168,8 @@ mod sketch_guidance_ui_tests;
pub mod sketch_modify_ui;
#[cfg(test)]
mod sketch_modify_ui_tests;
+#[cfg(test)]
+mod sketch_moving_click_tests;
pub mod sketch_palette;
mod sketch_pattern_tree;
#[cfg(test)]
diff --git a/crates/ui-egui/src/sketch_moving_click_tests.rs b/crates/ui-egui/src/sketch_moving_click_tests.rs
new file mode 100644
index 0000000000..effeafe5fa
--- /dev/null
+++ b/crates/ui-egui/src/sketch_moving_click_tests.rs
@@ -0,0 +1,90 @@
+//! A sketch tool's click that moves on the way (#2448): egui takes a press that moves past its
+//! click distance as a drag, which the tools dropped, so a hand that moved while clicking placed
+//! nothing.
+
+use crate::scenario::Harness;
+use serde_json::json;
+use solidcraft_engine::sketch::CurveKind;
+
+fn harness(command: &str) -> Harness {
+ let mut h = Harness::new();
+ h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
+ h.app.cam.distance = 120.0;
+ h.settle();
+ h.step(&json!({"start":command})).unwrap();
+ h
+}
+
+/// Press `dx` screen points left of sketch point `at`, move there and let go.
+fn moving_click(h: &mut Harness, at: [f64; 2], dx: f64) {
+ let p = h.call("ui.at", json!({"sketch":at}))["result"].clone();
+ let (x, y) = (p[0].as_f64().unwrap(), p[1].as_f64().unwrap());
+ h.call("ui.drag", json!({"x0":x - dx,"y0":y,"x1":x,"y1":y,"steps":4}));
+}
+
+fn curves(h: &Harness) -> usize {
+ let sid = h.app.session.active_sketch.unwrap();
+ h.app.session.model.state().sketch(sid).unwrap().sketch.curves.len()
+}
+
+#[test]
+fn clicks_that_move_place_their_points() {
+ for (command, points, finish) in [
+ ("sketch.slot.center_to_center", vec![[-20.0, 0.0], [20.0, 0.0], [0.0, 8.0]], None),
+ ("sketch.line", vec![[-20.0, 0.0], [20.0, 5.0]], Some("Escape")),
+ ("sketch.arc.three_point", vec![[-20.0, 0.0], [20.0, 0.0], [0.0, 10.0]], None),
+ ("sketch.rectangle.three_point", vec![[-20.0, 0.0], [20.0, 0.0], [20.0, 10.0]], None),
+ ("sketch.circle.three_point", vec![[-20.0, 0.0], [20.0, 0.0], [0.0, 20.0]], None),
+ ("sketch.ellipse", vec![[0.0, 0.0], [25.0, 0.0], [0.0, 10.0]], None),
+ ("sketch.spline.fit_point", vec![[-20.0, 0.0], [0.0, 10.0], [20.0, 0.0]], Some("right")),
+ ] {
+ let mut h = harness(command);
+ for at in &points {
+ moving_click(&mut h, *at, 12.0);
+ }
+ match finish {
+ Some("right") => {
+ let p = h.call("ui.at", json!({"sketch":points[points.len() - 1]}))["result"].clone();
+ h.call("ui.click", json!({"x":p[0],"y":p[1],"button":"right"}));
+ }
+ Some(key) => h.step(&json!({"key":key})).unwrap(),
+ None => {}
+ }
+ h.settle();
+ assert!(curves(&h) > 0, "{command}: nothing drawn; {:?} {:?}", h.app.tool, h.app.status);
+ // The points went where the button was let go: the first one is at its spot.
+ let sid = h.app.session.active_sketch.unwrap();
+ let sk = h.app.session.model.state().sketch(sid).unwrap().sketch.clone();
+ let first = solidcraft_engine::geom::Vec2::new(points[0][0], points[0][1]);
+ // (A three-point circle keeps only its centre: through all three, its radius is 20.)
+ let through = sk.curves.iter().any(|c| matches!(c.kind, CurveKind::Circle { r, .. } if (r - 20.0).abs() < 0.5));
+ assert!(sk.points.iter().any(|p| (p.pos - first).len() < 0.5) || through, "{command}: no point at {first:?}");
+ }
+}
+
+#[test]
+fn a_short_drag_starts_a_rectangle_circle_or_polygon_by_clicks() {
+ for (command, second, count) in [
+ ("sketch.rectangle.two_point", [20.0, 10.0], 4),
+ ("sketch.rectangle.center", [20.0, 10.0], 6),
+ ("sketch.circle.center", [15.0, 0.0], 1),
+ ("sketch.polygon.inscribed", [15.0, 0.0], 7),
+ ] {
+ let mut h = harness(command);
+ // A press that moves 12 points (past egui's click distance, short of a drawn shape) is
+ // the first point, not a sliver of a shape.
+ moving_click(&mut h, [0.0, 0.0], 12.0);
+ h.settle();
+ assert_eq!(curves(&h), 0, "{command}: a short drag drew a shape");
+ assert_eq!(h.app.tool.as_ref().unwrap().pts.len(), 1, "{command}");
+ moving_click(&mut h, second, 12.0);
+ h.settle();
+ assert_eq!(curves(&h), count, "{command}: {:?}", h.app.status);
+ let sid = h.app.session.active_sketch.unwrap();
+ let sk = h.app.session.model.state().sketch(sid).unwrap().sketch.clone();
+ let far = solidcraft_engine::geom::Vec2::new(second[0], second[1]);
+ let circle = |c: &solidcraft_engine::sketch::Curve| matches!(c.kind, CurveKind::Circle { r, .. } if (r - far.len()).abs() < 0.5);
+ let on_shape = sk.points.iter().any(|p| (p.pos - far).len() < 0.5) || sk.curves.iter().any(circle);
+ assert!(on_shape, "{command}: the second click wasn't where it was let go");
+ }
+}
diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs
index 0267a2d421..8e8a177a94 100644
--- a/crates/ui-egui/src/viewport.rs
+++ b/crates/ui-egui/src/viewport.rs
@@ -40,9 +40,14 @@ pub enum NavMode {
#[derive(Default)]
pub struct ViewportState {
- /// A two-point drawing tool's first point was placed by a press that became a drag: the
- /// release places the second (#1630).
- pub(crate) draw_drag: bool,
+ /// A two-point drawing tool's first point was placed by a press (here) that became a drag:
+ /// the release places the second (#1630), unless it lands within [`DRAW_DRAG_MIN`] of the
+ /// press. Such a drag was a click that moved on the way (#2448): the release places the
+ /// first point instead and the shape goes on by clicks, rather than a sliver of a shape.
+ pub(crate) draw_drag: Option<Pos2>,
+ /// A tool's click moved on the way (#2448): egui takes a press that moves past its
+ /// click distance, or is held past its click time, as a drag, so its release is the click.
+ pub(crate) tool_click: bool,
/// Per-body boxes of one world state, held (not just its address, which a later state can
/// reuse once the old one is freed, so stale boxes would come back).
pick_bounds: std::sync::Mutex<Option<BodyBounds>>,
@@ -398,6 +403,10 @@ fn supersampling(app: &mut SolveApp, ctx: &egui::Context, view_proj: [[f32; 4];
/// How long the view holds still before adaptive supersampling draws it at full quality (s).
const SS_SETTLE: f64 = 0.25;
+/// How far (screen points) a press must be dragged for its release to draw a rectangle, circle
+/// or polygon (#1630); a shorter drag is a click that moved on the way (#2448).
+const DRAW_DRAG_MIN: f32 = 24.0;
+
/// The display plan the model scene draws `b` from on the GPU, or `None` when its faces are built
/// here from the reference mesh. Opaque shaded bodies (`opaque`: not faded, and no see-through
/// body or face) are drawn from their plans where the device evaluates them; faded and
@@ -2472,15 +2481,34 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) {
crate::frame_rate::paint(app, ui.ctx(), &painter, rect);
// ---- interaction ----
+ // A tool's click that moved on the way (#2448) clicks where the button is let go.
+ if app.viewport.tool_click && resp.drag_stopped() {
+ app.viewport.tool_click = false;
+ if let Some(p) = ui.input(|i| i.pointer.latest_pos()).filter(|p| rect.contains(*p))
+ && app.tool.is_some()
+ {
+ crate::tools::on_hover(app, &proj, p);
+ crate::tools::on_click(app, &proj, p);
+ }
+ }
// A two-point shape being dragged out (#1630) follows the pointer and ends where it is let go,
// even over the shape's own dimension box, which rides along beside the pointer.
- if app.viewport.draw_drag {
+ if let Some(from) = app.viewport.draw_drag {
let at = ui.input(|i| i.pointer.latest_pos()).filter(|p| rect.contains(*p));
if resp.drag_stopped() {
- app.viewport.draw_drag = false;
+ app.viewport.draw_drag = None;
if let Some(p) = at
&& app.tool.as_ref().is_some_and(|t| t.pts.len() == 1)
{
+ // Let go close to the press, it was a click that moved on the way (#2448): the
+ // first point goes where it was let go, as for the other tools, and the shape
+ // goes on by clicks.
+ if p.distance(from) < DRAW_DRAG_MIN
+ && let Some(t) = app.tool.as_mut()
+ {
+ t.pts.clear();
+ crate::sketch_dims::reset(t);
+ }
crate::tools::on_hover(app, &proj, p);
crate::tools::on_click(app, &proj, p);
}
@@ -2507,7 +2535,11 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) {
&& let Some(a) = ui.input(|i| i.pointer.press_origin())
{
crate::tools::on_click(app, &proj, a);
- app.viewport.draw_drag = app.tool.as_ref().is_some_and(|t| t.pts.len() == 1);
+ app.viewport.draw_drag = app.tool.as_ref().is_some_and(|t| t.pts.len() == 1).then_some(a);
+ } else if app.tool.is_some() && !held && primary_nav.is_none() && mode.is_none() && resp.drag_started_by(egui::PointerButton::Primary) {
+ // Any other press of a tool that turns into a drag is a click that moved on the way
+ // (#2448): its release clicks.
+ app.viewport.tool_click = true;
}
let reference_pick = (resp.clicked() || resp.double_clicked())
&& !held