diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 81dc4e5b0..12db428bd 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -1962,22 +1962,39 @@ pub fn origin_planes(app: &SolveApp) -> Vec<(&'static str, Vec3, [Vec3; 4])> { .collect() } -/// The plane Create Sketch would put the sketch on, while it waits for one (#6975): the origin -/// or construction plane under the pointer, in the view or in the browser. The grid is drawn on -/// it, so the sketch's plane shows before the click. +/// The plane Create Sketch would put the sketch on: the origin or construction plane under the +/// pointer while it waits (#6975), or the plane selected in the view (#10804). The grid is drawn +/// on it, so the sketch's plane shows before the click. pub fn previewed_sketch_plane(app: &SolveApp) -> Option { - if app.session.active_sketch.is_some() || !app.picking_sketch_plane() { + if app.session.active_sketch.is_some() { return None; } - let name = match (&app.viewport.hover_plane, &app.viewport.hover) { - (Some(name), _) | (None, Some(Hit::Plane { name, .. })) => name, - _ => return None, - }; + if app.picking_sketch_plane() { + let name = match (&app.viewport.hover_plane, &app.viewport.hover) { + (Some(name), _) | (None, Some(Hit::Plane { name, .. })) => name, + _ => return selected_plane(app), + }; + return named_plane(app, name); + } + selected_plane(app) +} + +/// The plane selected in the view, when there is one: Create Sketch takes it, so the grid shows it. +fn selected_plane(app: &SolveApp) -> Option { + let name = app.session.selection.iter().find_map(|sel| match sel { + Sel::Plane { name } => Some(name.as_str()), + _ => None, + })?; + named_plane(app, name) +} + +/// Origin or construction plane `name`. +fn named_plane(app: &SolveApp, name: &str) -> Option { solidcraft_engine::view::construction_planes(&app.session) .into_iter() .find(|(_, n, _)| n == name) .map(|(_, _, p)| p) - .or_else(|| origin_planes(app).into_iter().find(|(n, _, _)| *n == name.as_str()).and_then(|(n, _, _)| Plane::named(n))) + .or_else(|| origin_planes(app).into_iter().find(|(n, _, _)| *n == name).and_then(|(n, _, _)| Plane::named(n))) } /// A point on origin plane `name`'s square that a click at its screen point picks: the middle, @@ -12133,6 +12150,37 @@ mod grid_plane_tests { assert!(on(&app, 1), "the preview grid is drawn even with the grid off"); } + /// #10804: a plane selected in the view, before Create Sketch, is the plane it would use, so + /// the grid lies on it until it is deselected. + #[test] + fn a_selected_plane_previews_on_the_grid_before_create_sketch() { + let rect = Rect::from_min_size(Pos2::ZERO, egui::vec2(800.0, 600.0)); + let mut app = SolveApp::new(solidcraft_engine::Session::default(), crate::Services::default()); + app.ui.show_grid = true; + app.preferences.grid_follows_view = false; + let on = |app: &SolveApp, axis: usize| { + let points: Vec<[f32; 3]> = build_sketch_scene(app, &projection(app, rect)) + .under + .as_chunks::<32>() + .0 + .iter() + .flat_map(|l| [&l[0..12], &l[12..24]]) + .map(|b| [0, 4, 8].map(|i| f32::from_le_bytes(b[i..i + 4].try_into().unwrap()))) + .collect(); + !points.is_empty() && points.iter().all(|p| p[axis].abs() < 1e-3) + }; + assert!(on(&app, 2), "the ground grid"); + // A plane under the pointer, with nothing selected, moves nothing. + app.viewport.hover = Some(Hit::Plane { name: "XZ".into(), point: Vec3::ZERO }); + assert!(on(&app, 2), "a hover alone moves nothing"); + // Select XZ: the grid lies on it before Create Sketch is clicked. + app.session.selection = vec![Sel::Plane { name: "XZ".into() }]; + assert!(on(&app, 1), "the selected plane shows on the grid"); + // Deselecting it returns the grid to the ground. + app.session.selection.clear(); + assert!(on(&app, 2), "deselecting returns the grid"); + } + /// The grid's centre is where the view's axis meets the ground, but at a grazing slant (a /// camera 10 mm above the ground, looking almost along it) no further than the grid's /// half-size from the point under the target, so the ground in front of the camera keeps