diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 2acfbdb77..7c5b8f878 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -124,6 +124,10 @@ pub struct ViewportState { pub(crate) modifier_nav: Option<(NavMode, Pos2)>, /// What a click would pick now (after the active command's filter). pub hover: Option, + /// The centre of the circle or arc of the sketch being edited whose edge was hovered last + /// (sketch, point id): it is drawn, and it takes the pointer from further away + /// ([`WOKEN_CENTRE_PX`]), until another curve or point is hovered. + pub woken_centre: Option<(u64, String)>, pub mouse: Option, /// Box selection being dragged. pub boxsel: Option, @@ -1531,6 +1535,7 @@ pub(crate) fn pick_cached(app: &mut SolveApp, proj: &Proj, pos: Pos2) -> Vec = None; for ss in point_sketches.iter().filter_map(|sid| st.sketch(*sid)) { let sid = ss.feature; let shown = snapping.then(|| crate::dialogs::SnapPoints::of(&ss.sketch)); @@ -1689,8 +1697,11 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool, let dd = sp.distance(pos) - if s.active_sketch == Some(sid) { 2.0 } else { 0.0 }; // On a distance tie, grab a movable point before the origin or a linked point. let rank = (dd, prefer_free && locked); + let woken = app.viewport.woken_centre.as_ref().is_some_and(|(k, id)| *k == sid && *id == p.id); if dd < 8.0 && best.wants(rank) { best.offer(rank, Hit::SketchPoint { sketch: sid, id: p.id.clone(), at: p.pos }); + } else if woken && dd < WOKEN_CENTRE_PX && woken_far.as_ref().is_none_or(|(d0, _)| dd < *d0) { + woken_far = Some((dd, Hit::SketchPoint { sketch: sid, id: p.id.clone(), at: p.pos })); } } } @@ -1733,7 +1744,14 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool, } } } + let before_curves = hits.len(); hits.extend(bestc.hits()); + if let Some((_, far)) = woken_far + && hits.len() == before_curves + && !hits.iter().any(|h| matches!(h, Hit::SketchPoint { .. })) + { + hits.push(far); + } // A section hides the material its normal points at: nothing there is picked, and where the // plane crosses a body its cap hides what lies behind it (#6328), as `view.pick` answers. let section = section_plane(app); @@ -3197,6 +3215,7 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { candidate(app, &hits).map(|(h, s)| (h, Some(s))) }; app.viewport.hover = cand.as_ref().map(|c| c.0.clone()); + wake_centre(app, &hits); // Select Other: a press held still opens the list of everything under it; while the list is // open its row under the pointer is what lights up. let held = @@ -4478,6 +4497,56 @@ fn hover_highlight(app: &SolveApp, painter: &egui::Painter, proj: &Proj) { } _ => {} } + // The centre of the circle or arc hovered last: a cross in a ring, where a click takes it. + if let Some((sketch, id)) = &app.viewport.woken_centre + && !matches!(&app.viewport.hover, Some(Hit::SketchPoint { id: h, .. }) if h == id) + && let Some(ss) = st.sketch(*sketch) + && let Some(at) = ss.sketch.resolve_point(id).and_then(|i| ss.sketch.point(i)) + && let Some(p) = proj.to_screen(ss.plane.to_world(at)) + { + let stroke = Stroke::new(1.5, col); + painter.circle_stroke(p, 5.0, stroke); + painter.line_segment([p - vec2(4.0, 0.0), p + vec2(4.0, 0.0)], stroke); + painter.line_segment([p - vec2(0.0, 4.0), p + vec2(0.0, 4.0)], stroke); + crate::scenario::publish_handle("woken_centre", p); + } +} + +/// How near the pointer must come to the woken centre of a circle or arc (screen points), +/// against 8 for other sketch points. +const WOKEN_CENTRE_PX: f32 = 16.0; + +/// Hovering a circle's or an arc's edge in the sketch being edited wakes its centre: drawn, and +/// picked from further away, so a line or a dimension is easily taken to it, and it stays awake +/// while the pointer crosses the circle's inside to it. Hovering another curve or point (but +/// that centre) puts it back to sleep, as does leaving the sketch. +fn wake_centre(app: &mut SolveApp, hits: &[Hit]) { + let Some(active) = app.session.active_sketch else { + app.viewport.woken_centre = None; + return; + }; + let first = hits.iter().find(|h| matches!(h, Hit::SketchPoint { sketch, .. } | Hit::SketchCurve { sketch, .. } if *sketch == active)); + let st = crate::open_meshing::shown_world(app); + match first { + Some(Hit::SketchCurve { id, .. }) => { + let centre = st.sketch(active).and_then(|ss| { + let sk = &ss.sketch; + let c = match sk.curves.get(sk.curve_index(id)?)?.kind { + CurveKind::Circle { c, .. } | CurveKind::Arc { c, .. } => c, + _ => return None, + }; + Some(sk.points.get(c)?.id.clone()) + }); + app.viewport.woken_centre = centre.map(|id| (active, id)); + } + Some(Hit::SketchPoint { id, .. }) if app.viewport.woken_centre.as_ref().is_some_and(|(_, w)| w == id) => {} + Some(_) => app.viewport.woken_centre = None, + None => { + if app.viewport.woken_centre.as_ref().is_some_and(|(k, _)| *k != active) { + app.viewport.woken_centre = None; + } + } + } } /// A midpoint stays attached to its edge; hover and selection share the same marker. diff --git a/crates/ui-egui/tests/woken_centre.rs b/crates/ui-egui/tests/woken_centre.rs new file mode 100644 index 000000000..3cc09cd12 --- /dev/null +++ b/crates/ui-egui/tests/woken_centre.rs @@ -0,0 +1,58 @@ +use serde_json::json; +use solidcraft_ui_egui::scenario::Harness; + +fn sketch() -> Harness { + let mut h = Harness::new(); + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + h.app.run("sketch.circle.center", json!({"center":[20,20],"radius":10})).unwrap(); + h.app.run("sketch.line", json!({"points":[[38,10],[38,30]],"infer":false,"connect":false})).unwrap(); + h.call("ui.view", json!({"view":"top"})); + h.call("ui.view", json!({"view":"fit"})); + h.frames(3); + h +} + +/// A line's first point: the circle's centre, or a point of its own. +fn starts_at_centre(h: &Harness) -> bool { + let st = h.app.session.world_state(); + let sk = &st.sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch; + let centre = sk.resolve_point("c1.center").unwrap(); + sk.curves.iter().any(|c| matches!(c.kind, solidcraft_engine::sketch::CurveKind::Line { a, b } if a == centre || b == centre)) +} + +/// Hovering a circle's edge wakes its centre: it is drawn, and a click 12 points from it (beyond +/// a point's usual reach) takes it, here as the start of a line. +#[test] +fn hovering_a_circles_edge_makes_its_centre_easy_to_take() { + for wake in [false, true] { + let mut h = sketch(); + h.step(&json!({"start":"sketch.line"})).unwrap(); + if wake { + h.step(&json!({"move":{"world":[30,20,0]}})).unwrap(); + h.frames(2); + assert!(h.at(&json!({"handle":"woken_centre"})).is_ok(), "the centre shows"); + } + let [cx, cy] = h.at(&json!({"world":[20,20,0]})).unwrap(); + h.step(&json!({"move":[cx + 12.0, cy]})).unwrap(); + h.step(&json!({"click":[cx + 12.0, cy]})).unwrap(); + h.step(&json!({"click":{"world":[20,27,0]}})).unwrap(); + h.step(&json!({"key":"Escape"})).unwrap(); + assert_eq!(starts_at_centre(&h), wake, "wake={wake}"); + } +} + +/// Another curve hovered puts the centre back to sleep. +#[test] +fn another_curve_puts_the_centre_back_to_sleep() { + let mut h = sketch(); + h.step(&json!({"move":{"world":[30,20,0]}})).unwrap(); + h.frames(2); + assert!(h.app.viewport.woken_centre.is_some()); + // Across the circle's inside toward the centre it stays awake. + h.step(&json!({"move":{"world":[25,20,0]}})).unwrap(); + h.frames(2); + assert!(h.app.viewport.woken_centre.is_some()); + h.step(&json!({"move":{"world":[38,20,0]}})).unwrap(); + h.frames(2); + assert_eq!(h.app.viewport.woken_centre, None); +}