From cce337f83fbeb0f6b2f2bcd3506a8928d0df456d Mon Sep 17 00:00:00 2001 From: PaddleStroke Date: Sat, 31 Oct 2026 02:00:07 +0000 Subject: [PATCH] Sketch: alignment guides from any hovered point while drawing (#9105) (#9673) * claim: #9105 * Sketch: alignment guides from any hovered point while drawing (#9105) While a drawing tool waits for a point, hovering a line or arc end, a rectangle corner or a free point wakes the same temporary horizontal/ vertical tracking guide as a circle centre or line midpoint. Idle hovering keeps centres and midpoints only. Preference sketch_tracking_points (on) turns it off. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- .../pending/sketch-tracking-points.json | 42 ++++ crates/ui-egui/src/inference.rs | 204 ++++++++++++++---- crates/ui-egui/src/prefs.rs | 4 + crates/ui-egui/src/prefs/sketch.rs | 4 + .../ui-egui/src/sketch_guidance_ui_tests.rs | 31 +++ docs/book/src/guide/sketching.md | 7 +- docs/control-protocol.md | 5 +- 7 files changed, 253 insertions(+), 44 deletions(-) create mode 100644 assets/locales/pending/sketch-tracking-points.json diff --git a/assets/locales/pending/sketch-tracking-points.json b/assets/locales/pending/sketch-tracking-points.json new file mode 100644 index 000000000..8a1812c26 --- /dev/null +++ b/assets/locales/pending/sketch-tracking-points.json @@ -0,0 +1,42 @@ +{ + "en": { + "prefs.sketch_tracking_points": "Alignment guides from end points and corners while drawing", + "prefs.sketch_tracking_points_note": "While a drawing tool waits for a point, hovering a line or arc end, a rectangle corner or a free point shows the same temporary dotted guide as a center or midpoint. Off: only centers and midpoints." + }, + "ar": { + "prefs.sketch_tracking_points": "أدلة محاذاة من نقاط النهاية والزوايا أثناء الرسم", + "prefs.sketch_tracking_points_note": "عندما تنتظر أداة رسم نقطة، يؤدي تمرير المؤشر فوق نهاية خط أو قوس أو زاوية مستطيل أو نقطة حرة إلى إظهار الدليل المنقّط المؤقت نفسه الذي يظهر للمركز أو نقطة المنتصف. عند الإيقاف: المراكز ونقاط المنتصف فقط." + }, + "de": { + "prefs.sketch_tracking_points": "Ausrichtungshilfen von Endpunkten und Ecken beim Zeichnen", + "prefs.sketch_tracking_points_note": "Während ein Zeichenwerkzeug auf einen Punkt wartet, zeigt das Überfahren eines Linien- oder Bogenendes, einer Rechteckecke oder eines freien Punkts dieselbe temporäre gepunktete Hilfslinie wie ein Mittelpunkt. Aus: nur Mittelpunkte." + }, + "es": { + "prefs.sketch_tracking_points": "Guías de alineación desde extremos y esquinas al dibujar", + "prefs.sketch_tracking_points_note": "Mientras una herramienta de dibujo espera un punto, al pasar el ratón por el extremo de una línea o un arco, la esquina de un rectángulo o un punto libre se muestra la misma guía punteada temporal que en un centro o punto medio. Desactivado: solo centros y puntos medios." + }, + "fr": { + "prefs.sketch_tracking_points": "Guides d'alignement depuis les extrémités et les coins pendant le dessin", + "prefs.sketch_tracking_points_note": "Quand un outil de dessin attend un point, survoler l'extrémité d'une ligne ou d'un arc, le coin d'un rectangle ou un point libre affiche le même guide pointillé temporaire qu'un centre ou un point milieu. Désactivé : seulement les centres et les points milieux." + }, + "he": { + "prefs.sketch_tracking_points": "קווי עזר ליישור מנקודות קצה ומפינות בזמן שרטוט", + "prefs.sketch_tracking_points_note": "כשכלי שרטוט ממתין לנקודה, ריחוף מעל קצה של קו או קשת, פינה של מלבן או נקודה חופשית מציג את אותו קו עזר מקווקו זמני כמו במרכז או בנקודת אמצע. כבוי: מרכזים ונקודות אמצע בלבד." + }, + "ja": { + "prefs.sketch_tracking_points": "作図中は端点と角からも整列ガイド", + "prefs.sketch_tracking_points_note": "作図ツールが点を待っている間、線や円弧の端点、長方形の角、独立した点にカーソルを合わせると、中心や中点と同じ一時的な点線ガイドを表示します。オフ: 中心と中点のみ。" + }, + "ko": { + "prefs.sketch_tracking_points": "그리는 동안 끝점과 모서리에서도 정렬 안내선", + "prefs.sketch_tracking_points_note": "그리기 도구가 점을 기다리는 동안 선이나 호의 끝점, 직사각형 모서리, 자유 점에 마우스를 올리면 중심이나 중점과 같은 임시 점선 안내선이 표시됩니다. 끄면: 중심과 중점만." + }, + "vi": { + "prefs.sketch_tracking_points": "Đường căn chỉnh từ điểm đầu mút và góc khi vẽ", + "prefs.sketch_tracking_points_note": "Khi một công cụ vẽ đang chờ một điểm, di con trỏ lên đầu mút của đường thẳng hoặc cung, góc của hình chữ nhật hoặc một điểm tự do sẽ hiển thị đường căn chỉnh nét đứt tạm thời giống như ở tâm hoặc trung điểm. Tắt: chỉ tâm và trung điểm." + }, + "zh": { + "prefs.sketch_tracking_points": "绘制时也从端点和角点显示对齐辅助线", + "prefs.sketch_tracking_points_note": "绘图工具等待点时,将光标悬停在直线或圆弧的端点、矩形的角点或独立点上,会显示与圆心或中点相同的临时虚线辅助线。关闭:仅圆心和中点。" + } +} diff --git a/crates/ui-egui/src/inference.rs b/crates/ui-egui/src/inference.rs index 8ad958171..cdf307928 100644 --- a/crates/ui-egui/src/inference.rs +++ b/crates/ui-egui/src/inference.rs @@ -250,14 +250,23 @@ pub fn show(app: &SolveApp, painter: &egui::Painter, proj: &Proj) { } } -/// The two most recent hovered centres or straight-edge midpoints. Transient, per viewport, -/// never a constraint. A third hover drops the oldest. Endpoints are ordinary point snaps. +/// What a tracking guide starts from: a curve's centre or straight-edge midpoint, or, while a +/// drawing tool waits for a point, a sketch point itself (a line or arc end, a rectangle corner, +/// a free point; #9105). +#[derive(Clone, Debug, PartialEq)] +pub(crate) enum TrackRef { + Curve(String), + Point(String), +} + +/// The two most recent hovered references ([`TrackRef`]). Transient, per viewport, never a +/// constraint. A third hover drops the oldest. #[derive(Default)] pub(crate) struct Tracking { sketch: Option, document: std::sync::Weak, - anchor: Option, - previous: Option, + anchor: Option, + previous: Option, pub(crate) guide: Option<(Vec2, Vec2)>, pub(crate) remembered_guide: Option<(Vec2, Vec2)>, } @@ -274,28 +283,58 @@ impl Tracking { } } - fn point_of(sk: &Sketch, id: &str) -> Option { - Self::reference(sk, sk.curve_index(id)?) + /// The sketch points a guide may start from: shown points other than the origin (the sketch + /// axes already run through it) that are free or belong to at least one shown curve. + fn point_references(sk: &Sketch) -> Vec { + let visible = solidcraft_engine::view::SketchPointVisibility::new(sk); + let mut used = vec![false; sk.points.len()]; + let mut shown = vec![false; sk.points.len()]; + for (index, curve) in sk.curves.iter().enumerate() { + let on = solidcraft_engine::view::sketch_curve_visible(sk, index); + for p in curve.kind.point_ids() { + if let Some(u) = used.get_mut(p) { + *u = true; + } + if on && let Some(s) = shown.get_mut(p) { + *s = true; + } + } + } + sk.points + .iter() + .enumerate() + .map(|(i, p)| i != 0 && visible.takes(p) && (shown.get(i).copied().unwrap_or(false) || !used.get(i).copied().unwrap_or(true))) + .collect() } - /// Keep `id` as one of the two remembered references. Hovering the older one swaps them, - /// so going back does not drop it. A third, different reference drops the oldest. - fn remember(&mut self, id: String) { - if self.anchor.as_deref() == Some(id.as_str()) { + fn point_of(sk: &Sketch, reference: &TrackRef) -> Option { + match reference { + TrackRef::Curve(id) => Self::reference(sk, sk.curve_index(id)?), + TrackRef::Point(id) => { + let index = sk.point_index(id)?; + Self::point_references(sk).get(index).copied().unwrap_or(false).then(|| sk.point(index)).flatten() + } + } + } + + /// Keep `reference` as one of the two remembered references. Hovering the older one swaps + /// them, so going back does not drop it. A third, different reference drops the oldest. + fn remember(&mut self, reference: TrackRef) { + if self.anchor.as_ref() == Some(&reference) { return; } - if self.previous.as_deref() == Some(id.as_str()) { + if self.previous.as_ref() == Some(&reference) { std::mem::swap(&mut self.anchor, &mut self.previous); return; } - self.previous = self.anchor.replace(id); + self.previous = self.anchor.replace(reference); } /// Where the horizontal through one reference meets the vertical through the other, when /// that point is within a few pixels of the cursor and is not either reference itself. pub(crate) fn meeting(&self, sk: &Sketch, plane: &solidcraft_engine::geom::Plane, proj: &Proj, cursor: egui::Pos2) -> Option { - let a = self.anchor.as_deref().and_then(|id| Self::point_of(sk, id))?; - let b = self.previous.as_deref().and_then(|id| Self::point_of(sk, id))?; + let a = self.anchor.as_ref().and_then(|r| Self::point_of(sk, r))?; + let b = self.previous.as_ref().and_then(|r| Self::point_of(sk, r))?; if a.dist(b) < 1e-6 { return None; } @@ -329,33 +368,57 @@ impl Tracking { end.map(|end| (anchor, end)) } - fn update(&mut self, sk: &Sketch, plane: &solidcraft_engine::geom::Plane, proj: &Proj, cursor: egui::Pos2) { + /// Follow the cursor: remember the reference under it and lay out the guides. With + /// `points`, sketch points are references too (a drawing tool waits for a point); + /// without, remembered points are let go. + fn update(&mut self, sk: &Sketch, plane: &solidcraft_engine::geom::Plane, proj: &Proj, cursor: egui::Pos2, points: bool) { self.guide = None; self.remembered_guide = None; + if !points { + for slot in [&mut self.anchor, &mut self.previous] { + if matches!(slot, Some(TrackRef::Point(_))) { + *slot = None; + } + } + if self.anchor.is_none() { + self.anchor = self.previous.take(); + } + } let to = |p: Vec2| proj.to_screen(plane.to_world(p)); // Screen-space tolerances stay the same at any zoom; only visible references wake. + // Centres and midpoints come first, so a point on one of them names the curve. let mut nearest = 8.0; - let mut nearest_id = None; + let mut nearest_ref = None; for (index, curve) in sk.curves.iter().enumerate() { let Some(p) = Self::reference(sk, index).and_then(to).filter(|p| proj.rect.contains(*p)) else { continue }; let distance = cursor.distance(p); if distance < nearest { nearest = distance; - nearest_id = Some(curve.id.clone()); + nearest_ref = Some(TrackRef::Curve(curve.id.clone())); } } - if let Some(id) = nearest_id { - self.remember(id); + if points { + for (point, usable) in sk.points.iter().zip(Self::point_references(sk)) { + let Some(p) = usable.then_some(point.pos).and_then(to).filter(|p| proj.rect.contains(*p)) else { continue }; + let distance = cursor.distance(p); + if distance < nearest { + nearest = distance; + nearest_ref = Some(TrackRef::Point(point.id.clone())); + } + } } - let Some(anchor) = self.anchor.as_deref().and_then(|id| Self::point_of(sk, id)).or_else(|| { + if let Some(reference) = nearest_ref { + self.remember(reference); + } + let Some(anchor) = self.anchor.as_ref().and_then(|r| Self::point_of(sk, r)).or_else(|| { self.anchor = self.previous.take(); - self.anchor.as_deref().and_then(|id| Self::point_of(sk, id)) + self.anchor.as_ref().and_then(|r| Self::point_of(sk, r)) }) else { self.anchor = None; self.previous = None; return; }; - if self.previous.as_deref().and_then(|id| Self::point_of(sk, id)).is_none() { + if self.previous.as_ref().and_then(|r| Self::point_of(sk, r)).is_none() { self.previous = None; } let Some(origin) = to(anchor) else { return }; @@ -373,7 +436,7 @@ impl Tracking { if let Some(meet) = self.meeting(sk, plane, proj, cursor) { let span = |from: Vec2| to(from).zip(to(meet)).filter(|(a, b)| a.distance(*b) > 8.0).map(|_| (from, meet)); self.guide = span(anchor); - if let Some(previous) = self.previous.as_deref().and_then(|id| Self::point_of(sk, id)) { + if let Some(previous) = self.previous.as_ref().and_then(|r| Self::point_of(sk, r)) { self.remembered_guide = span(previous); } return; @@ -381,7 +444,7 @@ impl Tracking { // Finite guides connect the reference to the cursor's projection onto its axis. // No model-space length cap: the viewport clips them, as it does other sketch guides. self.guide = Self::axis_guide(anchor, at, cursor, &to); - if let Some(previous) = self.previous.as_deref().and_then(|id| Self::point_of(sk, id)) { + if let Some(previous) = self.previous.as_ref().and_then(|r| Self::point_of(sk, r)) { self.remembered_guide = Self::axis_guide(previous, at, cursor, &to); } } @@ -389,11 +452,14 @@ impl Tracking { /// Visual H/V tracking works before a tool's first click and while simply editing a sketch. /// The crossing of the two remembered references is also a snap. A single guide stays visual. -/// Existing snaps, picks and constraints retain their behaviour. +/// Existing snaps, picks and constraints retain their behaviour. While a drawing tool waits for +/// a point, sketch points (ends, corners, free points) are references too, unless the Sketch +/// preferences turn that off; idle hovering only wakes centres and midpoints (#9105). pub(crate) fn track_and_show(app: &mut SolveApp, painter: &egui::Painter, proj: &Proj, cursor: Option) { crate::scenario::publish_count("sketch_tracking_guides", 0.0); let document = std::sync::Arc::downgrade(&app.session.doc); let active = app.session.active_sketch; + let points = app.preferences.sketch_tracking_points && app.tool.as_ref().is_some_and(|t| matches!(t.kind, crate::tools::Kind::Draw(_))); let tracking = &mut app.viewport.tracking; if tracking.sketch != active || !tracking.document.ptr_eq(&document) { *tracking = Tracking { sketch: active, document, ..Default::default() }; @@ -407,7 +473,7 @@ pub(crate) fn track_and_show(app: &mut SolveApp, painter: &egui::Painter, proj: *tracking = Tracking::default(); return; }; - tracking.update(&ss.sketch, &ss.plane, proj, cursor); + tracking.update(&ss.sketch, &ss.plane, proj, cursor, points); let shown = tracking.guide.is_some() || tracking.remembered_guide.is_some(); crate::scenario::publish_count("sketch_tracking_guides", usize::from(shown) as f64); let tk = Tokens::get(); @@ -451,29 +517,85 @@ mod tests { let to = |p: Vec2| proj.to_screen(Plane::XY.to_world(p)).unwrap(); let mut tracking = Tracking::default(); let centre = Vec2::new(-10.0, 3.0); - tracking.update(&sk, &Plane::XY, &proj, to(centre)); + tracking.update(&sk, &Plane::XY, &proj, to(centre), false); assert!(tracking.guide.is_none(), "no zero-length guide at the centre"); for end in [Vec2::new(35.0, 3.0), Vec2::new(-35.0, 3.0), Vec2::new(-10.0, 38.0), Vec2::new(-10.0, -38.0)] { - tracking.update(&sk, &Plane::XY, &proj, to(end)); + tracking.update(&sk, &Plane::XY, &proj, to(end), false); let (a, b) = tracking.guide.expect("all four directions"); assert!(a.dist(centre) < 1e-4 && b.dist(end) < 1e-4); } let end = Vec2::new(35.0, 3.0); - tracking.update(&sk, &Plane::XY, &proj, to(end) + egui::vec2(0.0, 5.0)); + tracking.update(&sk, &Plane::XY, &proj, to(end) + egui::vec2(0.0, 5.0), false); assert!(tracking.guide.is_some(), "5 screen points from the axis"); - tracking.update(&sk, &Plane::XY, &proj, to(end) + egui::vec2(0.0, 7.0)); + tracking.update(&sk, &Plane::XY, &proj, to(end) + egui::vec2(0.0, 7.0), false); assert!(tracking.guide.is_none(), "7 screen points from the axis"); let mid = Vec2::new(20.0, -20.0); - tracking.update(&sk, &Plane::XY, &proj, to(mid)); - tracking.update(&sk, &Plane::XY, &proj, to(Vec2::new(20.0, 20.0))); + tracking.update(&sk, &Plane::XY, &proj, to(mid), false); + tracking.update(&sk, &Plane::XY, &proj, to(Vec2::new(20.0, 20.0)), false); assert!(tracking.guide.unwrap().0.dist(mid) < 1e-8, "new MID replaces centre"); sk.curves.iter_mut().find(|c| c.id == "l").unwrap().id = "renamed".into(); - tracking.update(&sk, &Plane::XY, &proj, to(Vec2::new(20.0, 20.0))); + tracking.update(&sk, &Plane::XY, &proj, to(Vec2::new(20.0, 20.0)), false); assert!(tracking.guide.is_none(), "removed references cannot leave a stale guide"); sk.curves.iter_mut().find(|c| c.id == "renamed").unwrap().id = "l".into(); } } + #[test] + fn end_points_corners_and_free_points_start_guides_only_while_drawing() { + use solidcraft_engine::geom::Plane; + let mut app = SolveApp::new(Default::default(), Default::default()); + app.cam.yaw = 0.0; + app.cam.pitch = std::f64::consts::FRAC_PI_2; + app.cam.distance = 180.0; + let mut sk = Sketch::new(); + sk.add_line(Vec2::new(-30.0, 20.0), Vec2::new(30.0, 20.0), None, None, Some("top")).unwrap(); + sk.add_line(Vec2::new(30.0, 20.0), Vec2::new(30.0, -20.0), None, None, Some("right")).unwrap(); + sk.add_arc(Vec2::new(-20.0, -10.0), Vec2::new(-10.0, -10.0), Vec2::new(-20.0, 0.0), [None; 3], Some("arc")).unwrap(); + sk.add_point(Vec2::new(15.0, -25.0), Some("free")).unwrap(); + let hidden = sk.add_line(Vec2::new(45.0, 35.0), Vec2::new(55.0, 35.0), None, None, Some("hidden")).unwrap(); + sk.curves.get_mut(hidden).unwrap().construction = true; + sk.view.hide_construction = true; + let proj = crate::viewport::projection(&app, egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1200.0, 800.0))); + let to = |p: Vec2| proj.to_screen(Plane::XY.to_world(p)).unwrap(); + // Each point, then a cursor level with it far to the right: the guide runs from the point. + for (point, end) in [ + (Vec2::new(30.0, 20.0), Vec2::new(70.0, 20.0)), // a corner two lines share + (Vec2::new(-30.0, 20.0), Vec2::new(-30.0, -45.0)), // a line end, plumb below it + (Vec2::new(-20.0, 0.0), Vec2::new(60.0, 0.0)), // an arc end + (Vec2::new(15.0, -25.0), Vec2::new(15.0, 45.0)), // a free point + ] { + let mut drawing = Tracking::default(); + drawing.update(&sk, &Plane::XY, &proj, to(point), true); + drawing.update(&sk, &Plane::XY, &proj, to(end), true); + let (a, b) = drawing.guide.unwrap_or_else(|| panic!("a guide from {point:?}")); + assert!(a.dist(point) < 1e-9 && b.dist(end) < 1e-4, "{a:?} {b:?}"); + let mut idle = Tracking::default(); + idle.update(&sk, &Plane::XY, &proj, to(point), false); + idle.update(&sk, &Plane::XY, &proj, to(end), false); + assert!(idle.guide.is_none(), "idle hovering takes no point {point:?}"); + } + // The origin (the axes run through it) and the ends of a hidden curve start nothing. + for point in [Vec2::ZERO, Vec2::new(45.0, 35.0)] { + let mut tracking = Tracking::default(); + tracking.update(&sk, &Plane::XY, &proj, to(point), true); + assert!(tracking.anchor.is_none(), "{point:?}"); + } + // A centre or midpoint still names its curve; a point and a midpoint meet as two + // centres do, and leaving the drawing tool lets the point go but keeps the midpoint. + let mut tracking = Tracking::default(); + tracking.update(&sk, &Plane::XY, &proj, to(Vec2::new(-20.0, -10.0)), true); + assert_eq!(tracking.anchor, Some(TrackRef::Curve("arc".into()))); + tracking.update(&sk, &Plane::XY, &proj, to(Vec2::new(30.0, 0.0)), true); + tracking.update(&sk, &Plane::XY, &proj, to(Vec2::new(15.0, -25.0)), true); + assert_eq!(tracking.anchor, Some(TrackRef::Point("free".into()))); + assert_eq!(tracking.previous, Some(TrackRef::Curve("right".into()))); + let cross = Vec2::new(15.0, 0.0); + assert!(tracking.meeting(&sk, &Plane::XY, &proj, to(cross)).unwrap().dist(cross) < 1e-9); + tracking.update(&sk, &Plane::XY, &proj, to(Vec2::new(60.0, 0.0)), false); + assert_eq!(tracking.anchor, Some(TrackRef::Curve("right".into()))); + assert!(tracking.previous.is_none()); + } + #[test] fn two_recent_references_meet_and_a_third_drops_the_oldest() { use solidcraft_engine::geom::Plane; @@ -489,13 +611,13 @@ mod tests { let proj = crate::viewport::projection(&app, egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1200.0, 800.0))); let to = |p: Vec2| proj.to_screen(Plane::XY.to_world(p)).unwrap(); let mut tracking = Tracking::default(); - let hover = |tracking: &mut Tracking, p: Vec2| tracking.update(&sk, &Plane::XY, &proj, to(p)); + let hover = |tracking: &mut Tracking, p: Vec2| tracking.update(&sk, &Plane::XY, &proj, to(p), false); hover(&mut tracking, Vec2::new(0.0, 20.0)); hover(&mut tracking, Vec2::new(30.0, 0.0)); let centre = Vec2::ZERO; let near = to(centre) + egui::vec2(4.0, 3.0); assert!(tracking.meeting(&sk, &Plane::XY, &proj, near).unwrap().dist(centre) < 1e-6); - tracking.update(&sk, &Plane::XY, &proj, near); + tracking.update(&sk, &Plane::XY, &proj, near, false); let (a, b) = tracking.guide.unwrap(); let (c, d) = tracking.remembered_guide.unwrap(); assert!(b.dist(centre) < 1e-6 && d.dist(centre) < 1e-6, "both guides end on the crossing"); @@ -503,8 +625,8 @@ mod tests { assert!(tracking.meeting(&sk, &Plane::XY, &proj, to(centre) + egui::vec2(20.0, 0.0)).is_none()); // Back to the first reference: both stay, in swapped order. hover(&mut tracking, Vec2::new(0.0, 20.0)); - assert_eq!(tracking.anchor.as_deref(), Some("top")); - assert_eq!(tracking.previous.as_deref(), Some("right")); + assert_eq!(tracking.anchor, Some(TrackRef::Curve("top".into()))); + assert_eq!(tracking.previous, Some(TrackRef::Curve("right".into()))); // A circle centre and a midpoint meet the same way. hover(&mut tracking, Vec2::new(-10.0, 3.0)); let cross = Vec2::new(0.0, 3.0); @@ -513,12 +635,12 @@ mod tests { // so their axes do not invent the rectangle centre. hover(&mut tracking, Vec2::new(30.0, 0.0)); hover(&mut tracking, Vec2::new(50.0, 0.0)); - assert_eq!(tracking.anchor.as_deref(), Some("far")); - assert_eq!(tracking.previous.as_deref(), Some("right")); + assert_eq!(tracking.anchor, Some(TrackRef::Curve("far".into()))); + assert_eq!(tracking.previous, Some(TrackRef::Curve("right".into()))); assert!(tracking.meeting(&sk, &Plane::XY, &proj, to(centre)).is_none()); sk.curves.iter_mut().find(|c| c.id == "far").unwrap().id = "gone".into(); - tracking.update(&sk, &Plane::XY, &proj, to(Vec2::new(40.0, 40.0))); - assert_eq!(tracking.anchor.as_deref(), Some("right")); + tracking.update(&sk, &Plane::XY, &proj, to(Vec2::new(40.0, 40.0)), false); + assert_eq!(tracking.anchor, Some(TrackRef::Curve("right".into()))); assert!(tracking.previous.is_none()); assert!(tracking.guide.is_none(), "a removed reference leaves no guide"); } diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs index e7cd026fd..a4c38755c 100644 --- a/crates/ui-egui/src/prefs.rs +++ b/crates/ui-egui/src/prefs.rs @@ -1320,6 +1320,9 @@ preference_settings! { suppress_sketch_snap: ModifierCombo = ModifierCombo::CTRL_ALT, PAGE_SKETCH; /// Temporary horizontal/vertical guides from hovered centres and edge midpoints. sketch_tracking_guides: bool = true, PAGE_SKETCH; + /// While a drawing tool waits for a point, hovered sketch points (ends, corners, free + /// points) start tracking guides too (#9105). Off: only centres and midpoints, as before. + sketch_tracking_points: bool = true, PAGE_SKETCH; /// Snap a drawing point to where the two most recent centre or midpoint guides cross. /// Off: the guides stay visual and the cursor does not move (#8694). sketch_recent_snap: bool = false, PAGE_SKETCH; @@ -5080,6 +5083,7 @@ mod tests { ("suppress_sketch_snap", json!(ModifierCombo::NONE), PAGE_SKETCH), ("sketch_state_icons", json!(false), PAGE_SKETCH), ("sketch_tracking_guides", json!(false), PAGE_SKETCH), + ("sketch_tracking_points", json!(false), PAGE_SKETCH), ("sketch_align_points", json!(true), PAGE_SKETCH), ("sketch_recent_snap", json!(true), PAGE_SKETCH), ("msaa", json!(8), PAGE_GRAPHICS), diff --git a/crates/ui-egui/src/prefs/sketch.rs b/crates/ui-egui/src/prefs/sketch.rs index 2cd6c76dd..28df8185a 100644 --- a/crates/ui-egui/src/prefs/sketch.rs +++ b/crates/ui-egui/src/prefs/sketch.rs @@ -7,6 +7,10 @@ pub(super) fn show(app: &mut SolveApp, ui: &mut egui::Ui) { ui_kit::settings_section(ui, "sketch", app.tr("prefs.sketching"), |ui| { ui_kit::checkbox(ui, app.tr("prefs.sketch_tracking_guides"), &mut app.preferences.sketch_tracking_guides) .on_hover_text(app.tr("prefs.sketch_tracking_guides_note")); + ui.add_enabled_ui(app.preferences.sketch_tracking_guides, |ui| { + ui_kit::checkbox(ui, app.tr("prefs.sketch_tracking_points"), &mut app.preferences.sketch_tracking_points) + .on_hover_text(app.tr("prefs.sketch_tracking_points_note")); + }); ui_kit::checkbox(ui, app.tr("prefs.sketch_recent_snap"), &mut app.preferences.sketch_recent_snap) .on_hover_text(app.tr("prefs.sketch_recent_snap_note")); ui_kit::checkbox(ui, app.tr("prefs.sketch_align_points"), &mut app.preferences.sketch_align_points) diff --git a/crates/ui-egui/src/sketch_guidance_ui_tests.rs b/crates/ui-egui/src/sketch_guidance_ui_tests.rs index 8a7f57dad..2b3d076cc 100644 --- a/crates/ui-egui/src/sketch_guidance_ui_tests.rs +++ b/crates/ui-egui/src/sketch_guidance_ui_tests.rs @@ -307,6 +307,37 @@ fn centre_and_midpoint_tracking_is_visual_in_idle_and_drawing_modes() { assert_eq!(h.app.session.undo.len(), undo); } +/// #9105: while a drawing tool waits for a point, a rectangle corner starts a guide as a +/// midpoint does; idle hovering and the preference turned off keep the old centre/MID-only guides. +#[test] +fn a_corner_starts_a_guide_while_drawing_unless_turned_off() { + use solidcraft_engine::geom::Vec2; + let mut h = tracking_fixture(); + assert!(h.app.preferences.sketch_tracking_points, "on by default"); + let before = h.app.session.doc.to_json(); + let corner = Vec2::new(30.0, 20.0); + let hover_corner_then_level = |h: &mut Harness| { + step(h, json!({"move":{"sketch":[30,20]}})); + step(h, json!({"move":{"sketch":[45,20]}})); + h.app.viewport.tracking.guide + }; + assert!(hover_corner_then_level(&mut h).is_none(), "idle: a corner is no reference"); + for tool in ["sketch.line", "sketch.rectangle.two_point", "sketch.circle.center"] { + h.app.start(tool); + let (a, b) = hover_corner_then_level(&mut h).unwrap_or_else(|| panic!("{tool}: a guide from the corner")); + assert!(a.dist(corner) < 1e-9 && b.dist(Vec2::new(45.0, 20.0)) < 1e-4, "{tool}: {a:?} {b:?}"); + step(&mut h, json!({"key":"Escape"})); + } + h.call("ui.prefs", json!({"sketch_tracking_points": false})); + h.app.start("sketch.line"); + assert!(hover_corner_then_level(&mut h).is_none(), "the preference keeps the old guides"); + step(&mut h, json!({"move":{"sketch":[0,20]}})); + step(&mut h, json!({"move":{"sketch":[0,-10]}})); + assert!(h.app.viewport.tracking.guide.is_some(), "midpoints still guide"); + step(&mut h, json!({"key":"Escape"})); + assert_eq!(h.app.session.doc.to_json(), before, "guides add no geometry or constraints"); +} + #[test] fn two_recent_midpoints_snap_where_their_guides_meet() { use solidcraft_engine::geom::Vec2; diff --git a/docs/book/src/guide/sketching.md b/docs/book/src/guide/sketching.md index b01ea3543..fb4095ac2 100644 --- a/docs/book/src/guide/sketching.md +++ b/docs/book/src/guide/sketching.md @@ -33,8 +33,11 @@ Hover over a circle or arc centre, or the midpoint of a straight sketch edge, th horizontally or vertically to see a dotted alignment guide. The guide connects that reference to the cursor's projection on the sketch axis, within the viewport; it disappears when you move away from the axis. SolidCraft keeps the two most recent of these references and can -draw both guides. One guide on its own does not move the cursor, and endpoints are ordinary -point snaps rather than remembered references. Preferences › Sketch › **Auto snap to recent +draw both guides. One guide on its own does not move the cursor. While a drawing tool (Line, +Rectangle, Circle, Arc, Polygon, Slot, Point…) waits for a point, any sketch point is a +reference too: the end of a line or arc, a rectangle's corner, or a free point. Preferences › +Sketch › **Alignment guides from end points and corners while drawing** turns that off and +keeps only centres and midpoints. Preferences › Sketch › **Auto snap to recent centers and midpoints** is off until you turn it on. With it on, a drawing tool snaps the cursor where the horizontal through one reference meets the vertical through the other. On a rectangle, the top midpoint and a side midpoint then meet at the rectangle's centre, so that diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 434afbedb..b06405fcd 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -522,7 +522,10 @@ The guide spans the reference and cursor projection, including positions inside The two most recent references are kept, and both guides can be drawn. A single guide does not move the cursor. Set the preference to `false` to hide the guides. Snap suppression (Alt, unless rebound) does the same for that gesture. Snapping the cursor to where those -guides cross is separate, and off unless `sketch_recent_snap` is true. +guides cross is separate, and off unless `sketch_recent_snap` is true. While a drawing tool +waits for a point, `sketch_tracking_points` (default true) makes every shown sketch point +other than the origin a reference too (line and arc ends, rectangle corners, free points); +`false` keeps centres and midpoints only. ### Continuous camera motion