diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs index 0966e4b68..f5e996275 100644 --- a/crates/ui-egui/src/prefs.rs +++ b/crates/ui-egui/src/prefs.rs @@ -1599,8 +1599,9 @@ preference_settings! { /// glyphs and dimensions are red, in `error`, as before. sketch_conflict_colour: bool = true, PAGE_SKETCH; /// Hide constraint glyphs when the sketch is zoomed out so far that a millimetre of the plane - /// is smaller than `sketch_tools::MIN_GLYPH_PX_PER_MM` on screen (#8980). Off: glyphs stay at a - /// fixed screen size at every zoom, as before. + /// is smaller than `sketch_tools::MIN_GLYPH_PX_PER_MM` on screen (#8980) and the sketch spans + /// less than `sketch_tools::MIN_GLYPH_SPAN_PX` (#10518). Off: glyphs stay at a fixed screen + /// size at every zoom, as before. hide_constraint_glyphs_zoomed_out: bool = true, PAGE_SKETCH; /// Drive Joints shows the moved parts' old place as a ghost (its Show ghost box, #5905). drive_joints_ghost: bool = true, PAGE_TOOLS => check_tr("prefs.design_tools.ghost"); diff --git a/crates/ui-egui/src/sketch_tools.rs b/crates/ui-egui/src/sketch_tools.rs index f6d888073..1c5d056df 100644 --- a/crates/ui-egui/src/sketch_tools.rs +++ b/crates/ui-egui/src/sketch_tools.rs @@ -1639,9 +1639,54 @@ const GLYPH: f32 = 15.0; /// a 10 mm feature is then smaller than a glyph. pub const MIN_GLYPH_PX_PER_MM: f32 = 2.0; -/// Whether constraint glyphs should draw at this sketch-plane scale (#8980). -pub fn constraint_glyphs_visible_at_scale(px_per_mm: f32, hide_when_zoomed_out: bool) -> bool { - !hide_when_zoomed_out || px_per_mm >= MIN_GLYPH_PX_PER_MM +/// Below `MIN_GLYPH_PX_PER_MM`, glyphs still draw while the sketch's points and curves span at +/// least this many screen pixels (the larger side of their bounds), twenty glyphs across (#10518): +/// a large sketch that still fills the view keeps its glyphs; a small one far away loses them, as +/// before. +pub const MIN_GLYPH_SPAN_PX: f32 = 20.0 * GLYPH; + +/// Whether constraint glyphs should draw at this sketch-plane scale (#8980), given how large the +/// sketch is on screen (`span_px`, only measured when zoomed out, #10518). +pub fn constraint_glyphs_visible_at_scale(px_per_mm: f32, hide_when_zoomed_out: bool, span_px: impl FnOnce() -> f32) -> bool { + !hide_when_zoomed_out || px_per_mm >= MIN_GLYPH_PX_PER_MM || span_px() >= MIN_GLYPH_SPAN_PX +} + +/// The larger side, in screen pixels, of the bounds of the active sketch's points and curves. It +/// stops measuring once that reaches `MIN_GLYPH_SPAN_PX`, so a large sketch costs no more. +fn sketch_span_px(ss: &solidcraft_engine::doc::SolvedSketch, proj: &Proj) -> f32 { + let sk = &ss.sketch; + let at = sk.points.iter().map(|p| p.pos).chain((0..sk.curves.len()).flat_map(|c| sk.polyline(c))); + let mut r = egui::Rect::NOTHING; + for q in at { + if let Some(p) = proj.to_screen(ss.plane.to_world(q)) { + r.extend_with(p); + if r.width().max(r.height()) >= MIN_GLYPH_SPAN_PX { + return r.width().max(r.height()); + } + } + } + if r.min.x <= r.max.x { r.width().max(r.height()) } else { 0.0 } +} + +/// The constraints that draw a glyph: no dimensions, one MidPoint glyph where lines' midpoints +/// meet, and a pattern's constraints once, on its first instance. +fn glyph_constraints(sk: &solidcraft_engine::sketch::Sketch) -> impl Iterator { + let mut midpoints = std::collections::HashSet::new(); + sk.constraints.iter().filter(move |c| { + if c.kind.is_dimension() { + return false; + } + if let solidcraft_engine::sketch::ConstraintKind::Midpoint { p, .. } = c.kind + && !c.driven + && !midpoints.insert(p) + { + return false; + } + !(matches!( + c.kind, + solidcraft_engine::sketch::ConstraintKind::PatternPoint { .. } | solidcraft_engine::sketch::ConstraintKind::PatternRadius { .. } + ) && !sk.patterns.iter().any(|p| p.instances.first().and_then(|i| i.constraints.first()) == Some(&c.id))) + }) } /// Pixels on screen for one millimetre along the sketch plane's U axis, near the plane origin. @@ -1659,9 +1704,10 @@ fn glyphs(app: &SolveApp, painter: &egui::Painter, proj: &Proj, hover: Option = Vec::new(); - // The point of each MidPoint glyph drawn so far: lines whose midpoints meet share one glyph. - let mut midpoints: Vec = Vec::new(); - for c in &ss.sketch.constraints { - if c.kind.is_dimension() { - continue; - } - if let solidcraft_engine::sketch::ConstraintKind::Midpoint { p, .. } = c.kind - && !c.driven - { - if midpoints.contains(&p) { - continue; - } - midpoints.push(p); - } - if matches!( - c.kind, - solidcraft_engine::sketch::ConstraintKind::PatternPoint { .. } | solidcraft_engine::sketch::ConstraintKind::PatternRadius { .. } - ) && !ss.sketch.patterns.iter().any(|p| p.instances.first().and_then(|i| i.constraints.first()) == Some(&c.id)) - { - continue; - } + for c in glyph_constraints(&ss.sketch) { for (at, dir) in glyph_anchors(&ss.sketch, &c.kind) { let Some(base) = proj.to_screen(ss.plane.to_world(at)) else { continue }; // The entity's direction on screen, and its side. @@ -1923,24 +1949,38 @@ pub fn show(app: &mut SolveApp, ui: &egui::Ui, painter: &egui::Painter, proj: &P #[cfg(test)] mod glyph_zoom_tests { - use super::{MIN_GLYPH_PX_PER_MM, constraint_glyphs_visible_at_scale}; + use super::{MIN_GLYPH_PX_PER_MM, MIN_GLYPH_SPAN_PX, constraint_glyphs_visible_at_scale}; + + /// A sketch that is small on screen. + fn small() -> f32 { + 40.0 + } #[test] fn glyphs_stay_visible_when_zoomed_in() { - assert!(constraint_glyphs_visible_at_scale(MIN_GLYPH_PX_PER_MM, true)); - assert!(constraint_glyphs_visible_at_scale(10.0, true)); + assert!(constraint_glyphs_visible_at_scale(MIN_GLYPH_PX_PER_MM, true, small)); + assert!(constraint_glyphs_visible_at_scale(10.0, true, small)); } #[test] fn glyphs_hide_below_the_zoom_threshold() { - assert!(!constraint_glyphs_visible_at_scale(MIN_GLYPH_PX_PER_MM - f32::EPSILON, true)); - assert!(!constraint_glyphs_visible_at_scale(0.5, true)); + assert!(!constraint_glyphs_visible_at_scale(MIN_GLYPH_PX_PER_MM - f32::EPSILON, true, small)); + assert!(!constraint_glyphs_visible_at_scale(0.5, true, small)); + assert!(!constraint_glyphs_visible_at_scale(0.5, true, || MIN_GLYPH_SPAN_PX - 1.0)); } #[test] fn preference_off_keeps_glyphs_at_every_zoom() { - assert!(constraint_glyphs_visible_at_scale(0.1, false)); - assert!(constraint_glyphs_visible_at_scale(MIN_GLYPH_PX_PER_MM - 1.0, false)); + assert!(constraint_glyphs_visible_at_scale(0.1, false, small)); + assert!(constraint_glyphs_visible_at_scale(MIN_GLYPH_PX_PER_MM - 1.0, false, small)); + } + + /// A 500 mm sketch zoomed to fit is below two pixels a millimetre yet fills the view: its + /// glyphs stay (#10518). + #[test] + fn glyphs_stay_while_a_large_sketch_fills_the_view() { + assert!(constraint_glyphs_visible_at_scale(1.5, true, || 500.0 * 1.5)); + assert!(constraint_glyphs_visible_at_scale(0.05, true, || MIN_GLYPH_SPAN_PX)); } } diff --git a/crates/ui-egui/tests/scenarios/constraint_glyph_stays_on_one_long_line.json b/crates/ui-egui/tests/scenarios/constraint_glyph_stays_on_one_long_line.json new file mode 100644 index 000000000..0b2438386 --- /dev/null +++ b/crates/ui-egui/tests/scenarios/constraint_glyph_stays_on_one_long_line.json @@ -0,0 +1,12 @@ +[ + {"call": "engine.execute", "params": {"command": "sketch.create", "params": {"plane": "XY"}}}, + {"call": "engine.execute", "params": {"command": "sketch.line", "params": {"points": [[1000, 200], [4000, 200]]}}}, + {"call": "engine.execute", "params": {"command": "sketch.constraint.horizontal_vertical", "params": {"line": "l1", "mode": "horizontal"}}}, + {"call": "ui.view", "params": {"view": "top"}}, + {"call": "ui.view", "params": {"view": "fit"}}, + {"expect": {"glyphs": {"k1": 1}}}, + {"call": "ui.camera", "params": {"distance": 20000}}, + {"expect": {"glyphs": {"k1": 0}}}, + {"call": "ui.prefs", "params": {"hide_constraint_glyphs_zoomed_out": false}}, + {"expect": {"glyphs": {"k1": 1}}} +] diff --git a/crates/ui-egui/tests/scenarios/constraint_glyphs_hide_when_zoomed_out.json b/crates/ui-egui/tests/scenarios/constraint_glyphs_hide_when_zoomed_out.json index 8d88baec8..ce1522793 100644 --- a/crates/ui-egui/tests/scenarios/constraint_glyphs_hide_when_zoomed_out.json +++ b/crates/ui-egui/tests/scenarios/constraint_glyphs_hide_when_zoomed_out.json @@ -12,5 +12,13 @@ {"expect": {"glyphs": {"k1": 1, "k2": 1}}}, {"call": "ui.prefs", "params": {"hide_constraint_glyphs_zoomed_out": true}}, {"call": "ui.view", "params": {"view": "fit"}}, - {"expect": {"glyphs": {"k1": 1, "k2": 1}}} + {"expect": {"glyphs": {"k1": 1, "k2": 1}}}, + {"call": "engine.execute", "params": {"command": "sketch.line", "params": {"points": [[0, 0], [500, 0]]}}}, + {"call": "engine.execute", "params": {"command": "sketch.constraint.horizontal_vertical", "params": {"line": "l2", "mode": "horizontal"}}}, + {"call": "engine.execute", "params": {"command": "sketch.line", "params": {"points": [[500, 0], [500, 500]]}}}, + {"call": "engine.execute", "params": {"command": "sketch.constraint.horizontal_vertical", "params": {"line": "l3", "mode": "vertical"}}}, + {"call": "ui.view", "params": {"view": "fit"}}, + {"expect": {"glyphs": {"k1": 1, "k2": 1, "k3": 1, "k4": 1}}}, + {"call": "ui.camera", "params": {"distance": 20000}}, + {"expect": {"glyphs": {"k1": 0, "k2": 0, "k3": 0, "k4": 0}}} ]