diff --git a/ROADMAP.md b/ROADMAP.md index 11bdb23e..84a8adb4 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -137,6 +137,8 @@ PCB / Package, Render, Animation, Simulation, Manufacture/CAM, Drawings. ## View navigation +- OpenSCAD navigation preset: left drag orbits, right/middle drag pans, Shift+right/middle + drag zooms, and the wheel zooms. Available in the shared desktop/web Navigation preferences. - Compact clickable signed X/Y/Z axis tips, smooth drag-to-orbit, and Shift-orbit snapping to 26 standard and diagonal views. Pointer motion accumulates independently of a lightly damped spring, with a boundary margin to prevent flicker. Z-up/Y-up and the nearest square diff --git a/crates/ui-egui/src/context_menu.rs b/crates/ui-egui/src/context_menu.rs index d78397ca..71dadd3d 100644 --- a/crates/ui-egui/src/context_menu.rs +++ b/crates/ui-egui/src/context_menu.rs @@ -1326,7 +1326,7 @@ fn track_hold(app: &mut SolveApp, ctx: &egui::Context) { i.time, ) }); - let free = app.tool.is_none() && app.dialog.is_none(); + let free = app.tool.is_none() && app.dialog.is_none() && app.preferences.nav != crate::prefs::OPENSCAD_NAV; if pressed && free && app.viewport.context_menu.is_none() && app.viewport.mouse.is_some() { app.menu.press = pos.map(|p| (p, time)); } diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rs index 1302069e..f811712d 100644 --- a/crates/ui-egui/src/prefs.rs +++ b/crates/ui-egui/src/prefs.rs @@ -18,6 +18,7 @@ use crate::theme::Tokens; use crate::viewport::NavMode; pub const UNITS: [&str; 5] = ["mm", "cm", "m", "in", "ft"]; +pub const OPENSCAD_NAV: &str = "openscad"; /// A mouse button that can drag the camera. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum NavButton { @@ -81,7 +82,7 @@ use NavButton::{Left, Middle, Right}; use NavMode::{Orbit, Pan, Zoom}; /// The mouse mappings. The first is the default. -pub const NAV_PRESETS: [NavPreset; 5] = [ +pub const NAV_PRESETS: [NavPreset; 6] = [ NavPreset { id: "fusion", label: "Fusion", @@ -134,6 +135,19 @@ pub const NAV_PRESETS: [NavPreset; 5] = [ reverse_zoom: false, bindings: &[bind(Right, NavKeys::SHIFT, Pan), bind(Right, NavKeys::NONE, Orbit), bind(Middle, NavKeys::NONE, Pan)], }, + NavPreset { + id: OPENSCAD_NAV, + label: "OpenSCAD", + help: "Left drag on empty space orbits, Shift+left drag box-selects, sketch geometry drags edit; right or middle drag pans, Shift+right or Shift+middle drag zooms", + reverse_zoom: false, + bindings: &[ + bind(Left, NavKeys::NONE, Orbit), + bind(Right, NavKeys::SHIFT, Zoom), + bind(Middle, NavKeys::SHIFT, Zoom), + bind(Right, NavKeys::NONE, Pan), + bind(Middle, NavKeys::NONE, Pan), + ], + }, ]; /// The preset with this id (the default for an unknown one). @@ -714,7 +728,8 @@ fn page(app: &mut SolveApp, ui: &mut egui::Ui, t: &Tokens) { } }); ui.end_row(); - note(ui, &format!("{}; hold Shift during orbit to snap; the wheel zooms", nav_preset(&app.preferences.nav).help)); + let snap_help = if app.preferences.nav == OPENSCAD_NAV { "" } else { "; hold Shift during orbit to snap" }; + note(ui, &format!("{}{snap_help}; the wheel zooms", nav_preset(&app.preferences.nav).help)); ui.label("Context menu"); ui.checkbox(&mut app.preferences.radial_context_menu, "Radial context menu"); ui.end_row(); @@ -871,7 +886,7 @@ pub fn set(app: &mut SolveApp, p: &serde_json::Value) -> Result crate::prefs::nav_mode(app, b, without_shift) == Some(NavMode::Orbit), None => app.viewport.nav.is_some(), @@ -1723,6 +1737,7 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { || middle || secondary_down || (primary_nav.is_some() && primary_down) + || mode.is_some() || app.viewport.axis_drag.is_some() || app.viewport.spin || app.viewport.orbit_snap.is_some() @@ -1881,7 +1896,7 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { } } // Dragging a sketch point of the active sketch moves it; the solver keeps the constraints. - if app.tool.is_none() && app.dialog.is_none() && primary_nav.is_none() && app.session.active_sketch.is_some() { + if app.tool.is_none() && app.dialog.is_none() && primary_nav.is_none() && mode.is_none() && app.session.active_sketch.is_some() { let on_plane = |app: &SolveApp, p: Pos2| { let ss = app.session.active_sketch.and_then(|sid| app.session.world_state().sketch(sid).cloned())?; let (o, d) = proj.ray(p); @@ -1994,6 +2009,7 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { // Box selection: a primary drag on the model when no navigation mode is on. if app.tool.is_none() && primary_nav.is_none() + && mode.is_none() && app.viewport.point_drag.is_none() && !crate::dim_view::dragging() && !crate::sketch3d::dragging() @@ -2707,7 +2723,14 @@ fn nav_bar(app: &mut SolveApp, ui: &mut egui::Ui, rect: Rect) { } let t = Tokens::get(); let items: [(&str, &str); 10] = [ - ("orbit", "Orbit (right drag; hold Shift to snap)"), + ( + "orbit", + if app.preferences.nav == crate::prefs::OPENSCAD_NAV { + "Orbit (left drag on empty space; Shift+left drag box-selects)" + } else { + "Orbit (right drag; hold Shift to snap)" + }, + ), ("spin", "Continuous orbit (stops at the next click or key)"), ("pan", "Pan (drag; or middle drag)"), ("zoom", "Zoom (drag; or wheel)"), @@ -2965,6 +2988,109 @@ mod tests { app } + #[test] + fn openscad_left_drag_preserves_editing_and_selection() { + for action in ["point", "box", "tool"] { + let ctx = egui::Context::default(); + let mut app = hover_app(false); + app.preferences.nav = crate::prefs::OPENSCAD_NAV.into(); + app.ui.show_origin = false; + hover_frame(&mut app, &ctx, vec![]); + let point = projection(&app, app.viewport.rect.unwrap()).to_screen(Vec3::ZERO).unwrap(); + let (start, end) = + if action == "point" { (point, point + vec2(30.0, 20.0)) } else { (point - vec2(40.0, 40.0), point + vec2(40.0, 40.0)) }; + if action == "tool" { + app.start("sketch.line"); + assert!(app.tool.is_some()); + } + let before = app.cam; + let undo = app.session.undo.len(); + let modifiers = egui::Modifiers { shift: action == "box", ..Default::default() }; + let frame = |app: &mut SolveApp, mut events: Vec| { + events.insert(0, egui::Event::ModifiersChanged(modifiers)); + let mut out = ctx.run_ui( + egui::RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0))), events, ..Default::default() }, + |ui| show(app, ui), + ); + out.textures_delta.clear(); + }; + frame( + &mut app, + vec![ + egui::Event::PointerMoved(start), + egui::Event::PointerButton { pos: start, button: egui::PointerButton::Primary, pressed: true, modifiers }, + ], + ); + frame(&mut app, vec![egui::Event::PointerMoved(end)]); + frame(&mut app, vec![]); + assert_eq!(app.cam, before, "{action} drag must not orbit"); + if action == "point" { + assert!(app.viewport.point_drag.is_some()); + assert_eq!(app.session.undo.len(), undo + 1); + } else if action == "box" { + assert!(app.viewport.boxsel.is_some()); + } else { + assert!(app.tool.is_some()); + assert!(app.viewport.primary_nav.is_none()); + } + frame(&mut app, vec![egui::Event::PointerButton { pos: end, button: egui::PointerButton::Primary, pressed: false, modifiers }]); + if action == "box" { + assert!(!app.session.selection.is_empty()); + } + } + } + + #[test] + fn openscad_empty_space_drags_navigate_without_editing_or_box_selecting() { + for (button, shift) in [ + (egui::PointerButton::Primary, false), + (egui::PointerButton::Secondary, false), + (egui::PointerButton::Middle, false), + (egui::PointerButton::Secondary, true), + (egui::PointerButton::Middle, true), + ] { + let ctx = egui::Context::default(); + let mut app = hover_app(false); + app.preferences.nav = crate::prefs::OPENSCAD_NAV.into(); + hover_frame(&mut app, &ctx, vec![]); + let point = projection(&app, app.viewport.rect.unwrap()).to_screen(Vec3::ZERO).unwrap(); + let p = if button == egui::PointerButton::Primary { point + vec2(80.0, 80.0) } else { point }; + let before = app.cam; + let undo = app.session.undo.len(); + let modifiers = egui::Modifiers { shift, ..Default::default() }; + let frame = |app: &mut SolveApp, mut events: Vec| { + events.insert(0, egui::Event::ModifiersChanged(modifiers)); + let _ = ctx.run_ui( + egui::RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0))), events, ..Default::default() }, + |ui| show(app, ui), + ); + }; + frame(&mut app, vec![egui::Event::PointerMoved(p), egui::Event::PointerButton { pos: p, button, pressed: true, modifiers }]); + let end = p + vec2(30.0, 20.0); + frame(&mut app, vec![egui::Event::PointerMoved(end)]); + frame(&mut app, vec![]); + assert!(app.viewport.hover.is_none()); + assert!(app.viewport.boxsel.is_none() && app.viewport.point_drag.is_none()); + frame(&mut app, vec![egui::Event::PointerButton { pos: end, button, pressed: false, modifiers }]); + assert_eq!(app.session.undo.len(), undo); + assert!(app.session.selection.is_empty()); + assert!(app.viewport.context_menu.is_none()); + if button == egui::PointerButton::Primary { + assert_ne!(app.cam.yaw, before.yaw); + assert_eq!(app.cam.target, before.target); + assert_eq!(app.cam.distance, before.distance); + } else if shift { + assert_ne!(app.cam.distance, before.distance); + assert_eq!(app.cam.target, before.target); + assert_eq!(app.cam.yaw, before.yaw); + } else { + assert_ne!(app.cam.target, before.target); + assert_eq!(app.cam.distance, before.distance); + assert_eq!(app.cam.yaw, before.yaw); + } + } + } + #[test] fn hover_stays_off_between_navigation_events_and_returns_on_release() { for gpu in [false, true] { diff --git a/docs/control-protocol.md b/docs/control-protocol.md index baa4e240..8fb9e8ad 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -53,7 +53,7 @@ unit regardless of the design, pass an expression with one: `"value": "50 mm"` o | `ui.start` | `command` | like a toolbar click: starts the sketch tool or dialog for the command | | `ui.click` | `x`, `y` (screen points), `button?`: left/right/middle, `shift?`, `ctrl?`, `double?` | real pointer input | | `ui.move` | `x`, `y` | | -| `ui.drag` | `x0`, `y0`, `x1`, `y1`, `button?`, `shift?`, `ctrl?`, `steps?`, `hold?`, `hold_end?` | press, move, release; a left drag on the model is a box selection (left to right: window, right to left: crossing); `hold` keeps the button down that many frames first (a right press-and-hold opens the marking menu, releasing over a direction picks it), `hold_end` that many frames at the end (a sketch drag shows its snap) | +| `ui.drag` | `x0`, `y0`, `x1`, `y1`, `button?`, `shift?`, `ctrl?`, `steps?`, `hold?`, `hold_end?` | press, move, release; a left drag on the model is a box selection (left to right: window, right to left: crossing); with OpenSCAD navigation, plain left-drag on empty space orbits, Shift+left-drag box-selects, and drags starting on sketch geometry edit it; `hold` keeps the button down that many frames first (a right press-and-hold opens the marking menu, releasing over a direction picks it), `hold_end` that many frames at the end (a sketch drag shows its snap) | | `ui.menu` | `x?`, `y?`, `target?`, `close?` | with a point: open a context menu there as a right-click would (the viewport's marking menu, or a browser menu with `target: {"type": "body", "name"} \| {"type": "sketch", "id"} \| {"type": "component", "id"}`); without: the open menu (`radial` and `items`: id, label, shortcut, enabled) | | `ui.menuPick` | `item`: id or label | runs an item of the open menu, as clicking it does | | `ui.rename` | `text?`, `commit?` (default true) | finishes the rename box a Rename item opened | @@ -76,7 +76,7 @@ unit regardless of the design, pass an expression with one: `"value": "50 mm"` o | `ui.home` | `open?`: bool; `sample?`: index | the start page: show or hide it, open a built-in sample (once built); returns `open` and the recent designs | | `ui.shortcut` | `command`, `key?`, `replace?` | a command's key, or rebinds it (`""` clears; a key another command uses is refused unless `replace`) | | `ui.help` | `item?`: `about`, `shortcuts` (read only), `report` (copies diagnostics) | the version, commit, build date and diagnostic text | -| `ui.prefs` | any of `default_units`, `nav` (`fusion`, `solidworks`, `inventor`, `alias`, `tinkercad`), `zoom_reverse`, `zoom_to_cursor`, `smooth_wheel_zoom` (default true), `orbit_cursor`, `look_at_anim`, `look_at_speed`, `msaa`, `length_decimals`, `angle_decimals`, `theme` (`dark`, `light`, `system`), `language` (`en`, `ja`; see [localization](localization.md)), `ui_scale`, `up_axis` (`z`, `y`), `offer_stand_up`; `open` (a page: General, Navigation, Keyboard Shortcuts, Design, Graphics, Sketch, Selection) opens the Preferences dialog there; `button`: `ok`, `cancel`, `apply`, `defaults`; `stand_up: true` stands up a just-opened Y-up file | sets preferences (kept between runs), returns them all plus `dialog` (the open page or null), `dark` and `stand_up_offer` | +| `ui.prefs` | any of `default_units`, `nav` (`fusion`, `solidworks`, `inventor`, `alias`, `tinkercad`, `openscad`), `zoom_reverse`, `zoom_to_cursor`, `smooth_wheel_zoom` (default true), `orbit_cursor`, `look_at_anim`, `look_at_speed`, `msaa`, `length_decimals`, `angle_decimals`, `theme` (`dark`, `light`, `system`), `language` (`en`, `ja`; see [localization](localization.md)), `ui_scale`, `up_axis` (`z`, `y`), `offer_stand_up`; `open` (a page: General, Navigation, Keyboard Shortcuts, Design, Graphics, Sketch, Selection) opens the Preferences dialog there; `button`: `ok`, `cancel`, `apply`, `defaults`; `stand_up: true` stands up a just-opened Y-up file | sets preferences (kept between runs), returns them all plus `dialog` (the open page or null), `dark` and `stand_up_offer` | | `ui.capture` | `action?`: `capture`, `revert`, `cancel` | answers the Capture Position question (asked when a command starts or the design is saved while components were moved without capturing); without `action`, what waits | | `ui.window` | `action`: minimize, maximize, restore, toggle, close | what the title bar's caption buttons do (on Windows and Linux the application bar is the title bar); close asks about unsaved designs first | | `ui.agent_cursor` | `show` (bool), `speed`: a number from 1 to 5 (times the normal speed) or `"instant"` (no animation: the command runs at once and the pointer jumps to where it acted), `follow_camera` (bool); all optional | the settings. While the cursor is shown, `engine.execute` first glides to the command's toolbar button and to each pick, then runs and replies; later requests wait their turn. `"animate": false` on a call skips it |