diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 787aa1a2fa..4de57c67d0 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -16,9 +16,11 @@ mod cursor; mod cycle; mod draw; mod dyn_boxes; +mod frame; mod grid; mod grips; mod list; +mod paint; mod pick; mod viewport; mod widgets; @@ -29,6 +31,7 @@ pub(crate) use draw::{Look, draw_highlight, draw_list}; use draw::{draw_drag_gpu, draw_highlight_gpu, draw_list_gpu, list_mesh_key, selection_area_fill}; use dyn_boxes::dynamic_input; pub(crate) use dyn_boxes::length_units; +use frame::{Frame, clicks, navigate}; use grid::draw_grid; use grips::{click_grip, grip_at, grip_menu, grip_options, grip_rect}; use list::{PrimIndex, ensure_list, rebuilt_preview}; @@ -226,703 +229,17 @@ pub struct SnapCycle { /// just its items), a tooltip or a floating palette above the canvas keeps the normal OS cursor /// visible (#1701), and so do the menus painted on the canvas itself (see [`over_canvas_menu`]). pub fn show(app: &mut CadApp, ui: &mut egui::Ui) { - let t = Tokens::get(); - let full = ui.available_rect_before_wrap(); - // Tiled model viewports (VPORTS): the current tile is the drawing area below; `draw_tiles` - // draws the others. - let tiles = tile_rects(app, full); - let rect = tiles.iter().find(|(_, tile)| tile.active).map_or(full, |(r, _)| *r); - let resp = ui.interact(full, ui.scope_id().with("canvas"), Sense::click_and_drag()); - let painter = ui.painter_at(rect); - app.canvas.rect = Some(rect); - app.session.viewport_px = (f64::from(rect.width()).max(1.0), f64::from(rect.height()).max(1.0)); - if app.canvas.zoom_pending { - app.canvas.zoom_pending = false; - let _ = app.session.zoom_extents(); - } - let Ok(st) = app.session.state() else { return }; - let view = st.view(); - let scale = f64::from(rect.height()) / view.height.max(1e-12); - let xf = Xf { rect, center: view.center, scale }; - app.canvas.xf = Some(xf); - - // ---------- input ---------- - let (hover_pos, scroll, mods, middle_down, pressed_primary, pressed_secondary, dbl_middle, dbl_primary) = ui.input(|i| { - ( - i.pointer.hover_pos(), - i.smooth_scroll_delta, - i.modifiers, - i.pointer.middle_down(), - i.pointer.primary_clicked(), - i.pointer.secondary_clicked(), - i.pointer.button_double_clicked(egui::PointerButton::Middle), - i.pointer.button_double_clicked(egui::PointerButton::Primary), - ) - }); - // The drawing area alone: a menu popup's frame (between its items and its border), a tooltip - // or a floating palette lives on a layer above the canvas, where `hovered()` alone still - // counts the canvas. Over those the OS cursor stays visible and the crosshair stays put, as - // over the menu bar (#1701). - let inside = hover_pos.is_some_and(|p| rect.contains(p) && ui.ctx().layer_id_at(p) == Some(ui.layer_id()) && resp.hovered()); - // The menus the canvas paints itself (selection cycling's rows, Dynamic Input's options) are - // menus too, but they share the canvas's layer: `layer_id_at` reports the drawing under them, - // so only their rectangles tell them apart from it ([`over_canvas_menu`]). They are excluded - // from the cursor contract below, though not from the canvas's input: a click on a row is - // taken by the row. - let on_menu = hover_pos.is_some_and(|p| over_canvas_menu(app, p)); - // Whether Ctrl is held right now. Written every frame (not just while a command prompts), so - // previews that read `Session::ctrl_down` always see the current state. The engine - // deliberately does not carry modifiers through `Input`, which is UI-agnostic (see PR - // #407's Limits note: modifiers used to be dropped on the floor at this point). - app.session.ctrl_down = mods.ctrl; - // Which device is scrolling? On macOS, trackpads (and Magic Mouse) report precise point - // deltas; notched mouse wheels report lines. The answer is sticky because egui spreads a wheel - // notch over several frames. Elsewhere (browsers, Wayland smooth-scroll mice) ordinary wheels - // also report points, so the wheel keeps zooming there. - ui.input(|i| { - for e in &i.raw.events { - if let egui::Event::MouseWheel { unit, .. } = e { - app.canvas.scroll_pans = cfg!(target_os = "macos") && *unit == egui::MouseWheelUnit::Point; - } - } - }); - if inside && app.canvas.scroll_pans && scroll != egui::Vec2::ZERO { - // Two-finger swipe pans: the drawing follows the fingers, like a middle-drag. - // Pinch (and Cmd+scroll) still zoom via `zoom_delta` below. - if let Ok(st) = app.session.state_mut() { - let v = st.view(); - st.set_view_quiet(cadcraft_engine::View { - center: v.center - Vec2::new(f64::from(scroll.x) / scale, -f64::from(scroll.y) / scale), - height: v.height, - }); - } - } - let opts = ui.ctx().options(|o| o.input_options); - let wheel = ui.input(|i| wheel_zoom_points(&i.raw.events, &opts, rect.height())); - if inside - && !app.canvas.scroll_pans - && wheel != 0.0 - && let Some(hp) = hover_pos - { - // Zoom with the mouse wheel about the cursor, each notch at once. - let f = app.session.wheel_zoom_factor(f64::from(wheel)); - let about = xf.to_world(hp); - let _ = app.session.zoom_about(f, about); - } - let zoom_pinch = ui.input(|i| crate::animation::zoom_delta(i, &opts, app.shows_animations())); - if inside - && (zoom_pinch - 1.0).abs() > 1e-4 - && let Some(hp) = hover_pos - { - let _ = app.session.zoom_about(f64::from(zoom_pinch), xf.to_world(hp)); - } - // Pan with the middle button (or Shift+right-drag / two-finger drag on trackpads is scroll). - let middle_pressed = ui.input(|i| i.pointer.button_pressed(egui::PointerButton::Middle)); - let focused = ui.input(|i| i.focused); - if middle_down - && focused - && (app.canvas.pan_last.is_some() || (middle_pressed && inside)) - && let Some(hp) = hover_pos - { - if let Some(last) = app.canvas.pan_last { - let d = hp - last; - if let Ok(st) = app.session.state_mut() { - let v = st.view(); - st.set_view_quiet(cadcraft_engine::View { - center: v.center - Vec2::new(f64::from(d.x) / scale, -f64::from(d.y) / scale), - height: v.height, - }); - } - } - app.canvas.pan_last = Some(hp); - } else { - app.canvas.pan_last = None; - } - if dbl_middle && inside { - let _ = app.session.zoom_extents(); - } - // ZOOM Realtime: dragging up zooms in, down zooms out. - if resp.dragged_by(egui::PointerButton::Primary) && app.session.realtime_zoom_active() { - let _ = app.session.realtime_zoom(f64::from(resp.drag_delta().y)); - } - // A click in another tile makes it current (or answers -VPORTS Join's prompts). - if pressed_primary - && let Some(hp) = hover_pos - && !rect.contains(hp) - && let Some(i) = tiles.iter().position(|(r, _)| r.contains(hp)) - { - if let Err(e) = app.session.click_tile(i) { - app.session.echo(e.to_string()); - } - ui.ctx().request_repaint(); + let Some(mut frame) = Frame::setup(app, ui) else { return }; + if navigate(app, ui, &mut frame).is_break() { return; } - // Re-read the view after navigation. - let view = app.session.state().map(|s| s.view()).unwrap_or(view); - let scale = f64::from(rect.height()) / view.height.max(1e-12); - let xf = Xf { rect, center: view.center, scale }; - app.canvas.xf = Some(xf); - // The paper (display) transform; equal to `xf` except inside an MSPACE viewport. - let (xf_paper, active_vp) = match app.session.state() { - Ok(st) => { - let pv = st.paper_view(); - let xp = Xf { rect, center: pv.center, scale: f64::from(rect.height()) / pv.height.max(1e-12) }; - (if st.mspace.is_some() { xp } else { xf }, st.active_viewport().map(|(_, v)| v)) - } - Err(_) => (xf, None), - }; - // A clipped active viewport's outline (picks and its heavy border follow it). - let active_clip = app.session.state().ok().and_then(|st| st.active_viewport().and_then(|(h, v)| cadcraft_render::viewport_clip(&st.doc, h, &v))); - // Double-click a viewport to work inside it; double-click the sheet outside it to return. - if inside - && dbl_primary - && !app.session.prompting() - && matches!(app.session.layout_space(), cadcraft_doc::Space::Paper(_)) - && let Some(hp) = hover_pos - { - let pp = xf_paper.to_world(hp); - let in_rect = |v: &cadcraft_doc::Viewport| (pp.x - v.center.x).abs() <= v.width / 2.0 && (pp.y - v.center.y).abs() <= v.height / 2.0; - let in_active = active_vp.as_ref().is_some_and(|v| active_clip.as_ref().map_or(in_rect(v), |c| c.contains(pp))); - if !in_active { - app.canvas.hot_grips.clear(); - app.session.pending_window = None; - app.canvas.drag_window = false; - if app.run("mspace", serde_json::json!({"at": [pp.x, pp.y]})).is_err() && active_vp.is_some() { - let _ = app.run("pspace", serde_json::json!({})); - } - app.session.pending_window = None; - app.canvas.drag_window = false; - app.canvas.list = None; - return; - } - } - // Double-click text to edit it, as in AutoCAD: TEXTEDIT with that object already picked. - if inside - && dbl_primary - && app.session.running.is_none() - && let Some(hp) = hover_pos - && edit_text_at(app, xf.to_world(hp)) - { + frame.reread_view(app); + if clicks(app, ui, &mut frame).is_break() { return; } - - // Double-click a hatch (no command running) to edit it in the Hatch Edit dialog. The click is - // used up: it must not also start a grip edit on the hatch's centre grip below. - let mut hatch_dialog = false; - if inside - && dbl_primary - && app.session.running.is_none() - && let Some(hp) = hover_pos - && let Some(h) = hatch_at(app, xf.to_world(hp), scale) - { - app.session.set_selection(vec![h]); - crate::hatchdlg::open_edit(app); - hatch_dialog = true; - } - - // While a command asks for a point, every click places one, however fast the drafting (#815). - // egui drops a click whose button comes up a few pixels from where it went down (a drag to it), - // and eframe hands a slow frame's clicks over together, which egui reports as one. - let point_prompt = app.session.current_prompt().is_some_and(|p| cadcraft_engine::pointmod::accepts(&p)) - && app.session.pending_window.is_none() - && !app.session.realtime_zoom_active(); - let pressed_primary = pressed_primary || (point_prompt && resp.drag_stopped_by(egui::PointerButton::Primary)); - if point_prompt && inside && pressed_primary && !middle_down && app.cmd.options.is_none() && app.cmd.buffer.is_empty() { - let releases: Vec = ui.input(|i| { - i.events - .iter() - .filter_map(|e| match e { - egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed: false, .. } => Some(*pos), - _ => None, - }) - .collect() - }); - // The last one is placed below, at the cursor, like any click. - if let Some((_, earlier)) = releases.split_last() { - for pos in earlier.iter().filter(|p| rect.contains(**p)) { - let w = effective_point(app, xf.to_world(*pos), &xf, ui.ctx()); - let result = match app.canvas.snap { - Some(hit) => app.session.input_snapped(hit), - None => app.session.input(Input::Point(w)), - }; - if let Err(e) = result { - app.session.echo(e.to_string()); - } - } - } - } - - let raw_world = hover_pos.filter(|_| inside).map(|p| xf.to_world(p)); - let dyn_frame = crate::cmdline::dyn_frame(app); - if let Some(w) = raw_world { - let mut eff = effective_point(app, w, &xf, ui.ctx()); - // A locked Dynamic Input value (`40,` or `40<` typed) holds the cursor and overrides snaps. - if let Some(f) = dyn_frame - && let Some(e) = crate::dyninput::parse(&app.cmd.buffer, &f) - && e.first_locked().is_some() - { - eff = crate::dyninput::constrain(&e, &f, eff, app.cmd.second_locked()); - app.canvas.snap = None; - app.canvas.track = None; - } - // Grip dimensional input: typed lengths and angles hold the grip and override snaps. - let grip_held = crate::grip_dyn::constrain(app, eff); - if let Some(q) = grip_held { - eff = q; - app.canvas.snap = None; - app.canvas.track = None; - } - app.canvas.cursor = Some(eff); - app.session.cursor = eff; - app.session.cursor_deferred = app.canvas.snap.and_then(|h| h.deferred); - // Typed or menu snap overrides (END, NON…) and Tab cycling; a locked Dynamic Input value wins. - if dyn_frame.and_then(|f| crate::dyninput::parse(&app.cmd.buffer, &f)).is_none_or(|e| e.first_locked().is_none()) { - override_cursor(app, w, &xf); - } - // Typed grip dimensions win over snap overrides too. - if let Some(q) = grip_held { - app.canvas.cursor = Some(q); - app.session.cursor = q; - app.canvas.snap = None; - app.session.cursor_deferred = None; - } - } else { - app.canvas.cursor = None; - app.canvas.snap = None; - app.session.cursor_deferred = None; - } - - // A click on the Dynamic Input options menu picks the keyword; elsewhere it closes the menu. - let mut picked = false; - if pressed_primary && app.cmd.options.is_some() { - match hover_pos.and_then(|h| app.cmd.option_at(h)) { - Some(k) => { - crate::cmdline::pick_option(app, k); - picked = true; - } - None => app.cmd.options = None, - } - } - // A double-click with no command running (on a layout viewport, say) leaves no selection - // window behind; while a command asks for points, every click still counts (#815). - let idle_dbl = dbl_primary && app.session.running.is_none(); - if idle_dbl { - app.session.pending_window = None; - app.canvas.drag_window = false; - } - let pressed_primary = pressed_primary && !picked && !hatch_dialog && !idle_dbl; - let key: CycleKey = ( - app.session.running.as_ref().map(|r| r.id.clone()), - app.session.current_prompt().map(|p| p.message), - app.session.state().map_or(0, |st| st.uid), - app.session.state().map_or(0, |st| st.revision), - xf.center, - xf.scale, - ); - if app.canvas.cycle_key.as_ref() != Some(&key) { - app.canvas.cycle = None; - app.canvas.hover_cycle = None; - app.canvas.cycle_key = Some(key); - } - let cycled = pressed_primary && !middle_down && cycle_click(app, &xf, hover_pos, raw_world.filter(|_| inside), mods.shift); - // Clicks. - if inside - && pressed_primary - && !middle_down - && !cycled - && let Some(p) = app.canvas.cursor - { - if app.session.prompting() { - // Ctrl held while picking the point of a grip edit makes copies (the Copy option). - if mods.ctrl && app.session.running.as_ref().is_some_and(|r| r.id == "grip") { - let _ = app.session.input(Input::Keyword("Copy".into())); - } - // Clicking places the point; a value typed into the Dynamic Input boxes is used up. - if let Some(f) = dyn_frame - && crate::dyninput::parse(&app.cmd.buffer, &f).is_some() - { - app.cmd.buffer.clear(); - } - // Preserve a snap's viewport origin for DIST, and its deferred curve where needed. - let result = match app.canvas.snap { - Some(hit) => app.session.input_snapped(hit), - None => app.session.input(Input::Point(p)), - }; - if let Err(e) = result { - app.session.echo(e.to_string()); - } - } else if let Some(g) = crate::dyngrips::at(app, &xf, hover_pos) { - crate::dyngrips::click(app, g); - } else if let Some(g) = grip_at(app, &xf, hover_pos) { - click_grip(app, g, mods.shift); - } else if let Err(e) = app.session.idle_click(raw_world.unwrap_or(p), mods.shift) { - app.session.echo(e.to_string()); - } - } - // Press-and-drag selection window (PICKDRAG = 2; click-click still works): a drag - // that starts while objects are being selected opens a window where the button went down, - // and letting go closes it. Drags on grips and drags while drawing are left alone. - if !app.session.settings.pickdrag { - app.canvas.drag_window = false; - } - if app.session.settings.pickdrag - && resp.drag_started_by(egui::PointerButton::Primary) - && !middle_down - && app.canvas.cycle.is_none() - && app.session.pending_window.is_none() - && let Some(origin) = ui.input(|i| i.pointer.press_origin()) - && rect.contains(origin) - && grip_at(app, &xf, Some(origin)).is_none() - && crate::dyngrips::at(app, &xf, Some(origin)).is_none() - { - app.canvas.drag_window = app.session.begin_window(xf.to_world(origin)); - } - if app.canvas.drag_window && resp.drag_stopped_by(egui::PointerButton::Primary) { - app.canvas.drag_window = false; - if app.session.pending_window.is_some() - && let Some(end) = ui.input(|i| i.pointer.interact_pos()) - { - let end = xf.to_world(end); - let done = if app.session.prompting() { app.session.input(Input::Point(end)) } else { app.session.idle_click(end, mods.shift) }; - if let Err(e) = done { - app.session.echo(e.to_string()); - } - } - } - // Right-click acts as Enter while a command runs (the classic CAD default; held, it opens the - // command menu); otherwise it opens the shortcut menu (Repeat, Clipboard, …). - // Shift/Ctrl+right-click opens the object snap menu. - let idle = !app.session.prompting(); - if inside && pressed_secondary { - app.canvas.snap_menu = mods.shift || mods.ctrl; - } - if crate::context_menu::in_command(app, ui, inside, pressed_secondary) { - let _ = app.session.input(Input::Enter); - } - // Right-click on a grip with options (polyline vertex, line end…) opens the grip menu. - if inside && pressed_secondary && idle && !app.canvas.snap_menu { - app.canvas.grip_menu = grip_at(app, &xf, hover_pos).filter(|g| !grip_options(app, *g).is_empty()); - } - crate::dyngrips::menu(app, ui.ctx(), &xf); - if idle && !app.canvas.snap_menu && app.canvas.grip_menu.is_some() { - grip_menu(app, &resp); - } else if idle || app.canvas.snap_menu { - crate::context_menu::show(app, &resp); - } - - // Hover highlight (throttled to cursor movement). - // The cursor picks objects (pick box, rollover highlight): "Select objects" and single-object - // prompts, or no command with PICKFIRST on; SELECTIONPREVIEW says in which of those it shows. - let selecting = app.session.previews_selection(); - if inside && selecting && app.session.pending_window.is_none() { - if hover_pos != app.canvas.hover_at - && let (Some(w), Ok(st)) = (raw_world, app.session.state()) - { - let filter = app.session.settings.previewfilter; - app.canvas.hover = cadcraft_engine::select::pick(&st.doc, &st.edit_space(), w, app.session.settings.pickbox.max(1.0) * 1.5 / scale) - .filter(|h| st.doc.entity(*h).is_none_or(|e| !cadcraft_engine::select::preview_filtered(&st.doc, e, filter))); - app.canvas.hover_at = hover_pos; - } - } else { - app.canvas.hover = None; - app.canvas.hover_at = None; - } - // The open selection cycling list highlights the object of the row under the pointer. - if app.canvas.cycle.is_some() && ui.input(|i| i.key_pressed(egui::Key::Escape)) { - app.canvas.cycle = None; - } - if let Some(c) = &app.canvas.cycle { - app.canvas.hover = hover_pos.and_then(|hp| c.row_at(hp)); - app.canvas.hover_at = None; - } - shift_space_cycle(app, ui, hover_pos.filter(|_| inside), raw_world); - if inside && hover_pos != app.canvas.cycle_badge_at { - app.canvas.cycle_badge = raw_world.is_some_and(|w| app.session.cycling_badge(w)); - app.canvas.cycle_badge_at = hover_pos; - } else if !inside { - app.canvas.cycle_badge = false; - app.canvas.cycle_badge_at = None; - } - - // ---------- paint ---------- - painter.rect_filled(rect, 0.0, t.canvas); - let t0 = crate::now_ms(); - let mut views = vec![(xf_paper.world_bounds(), 1.0 / xf_paper.scale)]; - views.extend(tiles.iter().filter(|(r, tile)| !tile.active && r.width() >= 1.0 && r.height() >= 1.0).map(|(r, tile)| { - let xf = tile_xf(*r, tile); - (xf.world_bounds(), 1.0 / xf.scale) - })); - ensure_list(app, &xf_paper, &views, ui.ctx()); - let sheet = app.canvas.list.as_ref().and_then(|l| l.sheet); - let surround = Color32::from_rgb(0x50, 0x57, 0x63); - let (bg, paper_rect) = match sheet { - Some(sh) => { - // Paper space: grey surround, the sheet with a shadow, the printable area dashed. - painter.rect_filled(rect, 0.0, surround); - let a = xf_paper.to_screen(Vec2::new(0.0, sh.size.y)); - let b = xf_paper.to_screen(Vec2::new(sh.size.x, 0.0)); - let paper = Rect::from_two_pos(a, b); - painter.rect_filled(paper.translate(vec2(5.0, 5.0)), 0.0, Color32::from_black_alpha(110)); - painter.rect_filled(paper, 0.0, t.paper); - let pa = Rect::from_two_pos( - xf_paper.to_screen(Vec2::new(sh.printable.min.x, sh.printable.max.y)), - xf_paper.to_screen(Vec2::new(sh.printable.max.x, sh.printable.min.y)), - ); - let pts = [pa.left_top(), pa.right_top(), pa.right_bottom(), pa.left_bottom(), pa.left_top()]; - painter.extend(Shape::dashed_line(&pts, Stroke::new(1.0, Color32::from_gray(150)), 4.0, 4.0)); - (Rgb(t.paper.r(), t.paper.g(), t.paper.b()), Some(paper)) - } - None => { - blockedit::wash(app, &painter, rect); - draw_grid(app, &painter, &xf); - (Rgb(t.canvas.r(), t.canvas.g(), t.canvas.b()), None) - } - }; - let sel = app.session.selection(); - // How the object under the cursor and the selected ones are highlighted (PREVIEWEFFECT, - // HIGHLIGHT, SELECTIONEFFECT); with HIGHLIGHT off only their grips show. - let (preview_look, selection_look) = (Look::preview(&app.session.settings), Look::selection(&app.session.settings)); - // The active viewport's contents, drawn apart from the layout's list, under its own objects. - // `masked`: its outline is in the stencil buffer this frame. - let mut masked = false; - if let Some(f) = ensure_vp_content(app, &xf_paper, ui.ctx()) { - let gpu = app.canvas.gpu.is_some(); - let clipped = painter.with_clip_rect(f.on_screen.intersect(rect)); - draw_vp_content(&mut app.canvas.vp, gpu, &clipped, &f, &xf_paper, bg, app.session.settings.lwdisplay); - masked = gpu && f.outline.is_some(); - } - if app.canvas.gpu.is_some() { - draw_list_gpu(&mut app.canvas, &painter, &xf_paper, bg, app.session.settings.lwdisplay); - } else if let Some(list) = &app.canvas.list { - draw_list(&painter, &xf_paper, list, bg, app.session.settings.lwdisplay); - } - if let Some(vp) = &active_vp { - // The active viewport: a heavy border, highlights in model units clipped to it. - let r = Rect::from_two_pos( - xf_paper.to_screen(Vec2::new(vp.center.x - vp.width / 2.0, vp.center.y + vp.height / 2.0)), - xf_paper.to_screen(Vec2::new(vp.center.x + vp.width / 2.0, vp.center.y - vp.height / 2.0)), - ); - let heavy = Stroke::new(3.0, Color32::from_rgb(0x2a, 0x2a, 0x2a)); - match &active_clip { - Some(c) => { - let mut pts: Vec = c.polygon().iter().map(|q| xf_paper.to_screen(*q)).collect(); - pts.extend(pts.first().copied()); - painter.add(egui::Shape::line(pts, heavy)); - } - None => { - painter.rect_stroke(r, 0.0, heavy, egui::StrokeKind::Middle); - } - } - if let Ok(st) = app.session.state() { - // A clipped viewport's outline bounds (its outline is masked when `masked`). - let bounds = active_clip.as_ref().map_or(r, |c| { - let b = c.bounds(); - Rect::from_two_pos(xf_paper.to_screen(Vec2::new(b.min.x, b.max.y)), xf_paper.to_screen(Vec2::new(b.max.x, b.min.y))) - }); - let clipped = painter.with_clip_rect(bounds.intersect(rect)); - let c = &mut app.canvas; - if let Some(h) = c.hover - && !sel.contains(&h) - { - c.vp_hover.update(st, &[h], scale); - let list = &c.vp_hover.list; - draw_highlight(&clipped, &xf, list, &(0..list.prims.len()).collect::>(), t.hover, preview_look); - } - if let Some(look) = selection_look - && !sel.is_empty() - { - c.vp_sel.update(st, &sel, scale); - let list = &c.vp_sel.list; - let all = || (0..list.prims.len()).collect::>(); - if c.gpu.is_some() { - // Shares the GPU highlight layer with the layout's own selection; the high bit - // keeps this list's generations apart from the layout list's. - let list_id = c.vp_sel.list_gen | 1 << 63; - let stencil = if masked { crate::gpu::Stencil::Inside } else { crate::gpu::Stencil::Off }; - let hl = (&mut c.hl_key, &c.hl_slot, &mut c.hl_origin, &mut c.hl_infinite); - draw_highlight_gpu(&clipped, &xf, list, list_id, all, &sel, t.selection, look, hl, stencil); - draw_highlight(&clipped, &xf, list, &c.hl_infinite, t.selection, look); - } else { - draw_highlight(&clipped, &xf, list, &all(), t.selection, look); - } - } - } - } else if let CanvasState { list: Some(list), list_gen, prim_index, hover, gpu, hl_key, hl_slot, hl_origin, hl_infinite, .. } = &mut app.canvas { - if let Some(h) = *hover - && !sel.contains(&h) - { - draw_highlight(&painter, &xf, list, &prim_index.prims(*list_gen, list, &[h]), t.hover, preview_look); - } - if let Some(look) = selection_look { - if gpu.is_some() && !sel.is_empty() { - let list_id = *list_gen; - let hl = (hl_key, &*hl_slot, hl_origin, &mut *hl_infinite); - draw_highlight_gpu( - &painter, - &xf, - list, - list_id, - || prim_index.prims(list_id, list, &sel), - &sel, - t.selection, - look, - hl, - crate::gpu::Stencil::Off, - ); - draw_highlight(&painter, &xf, list, hl_infinite, t.selection, look); - } else { - draw_highlight(&painter, &xf, list, &prim_index.prims(*list_gen, list, &sel), t.selection, look); - } - } - } - app.canvas.draw_ms = crate::now_ms() - t0; - compare::paint(app, ui.ctx(), &painter, &xf_paper, rect); - blockedit::paint(app, ui.ctx(), &painter, &xf_paper, rect); - // Constraint bars and dynamic dimensional constraints (model space, or inside a viewport). - if sheet.is_none() || active_vp.is_some() { - let clip = match &active_vp { - Some(vp) => Rect::from_two_pos( - xf_paper.to_screen(Vec2::new(vp.center.x - vp.width / 2.0, vp.center.y + vp.height / 2.0)), - xf_paper.to_screen(Vec2::new(vp.center.x + vp.width / 2.0, vp.center.y - vp.height / 2.0)), - ) - .intersect(rect), - None => rect, - }; - let p = painter.with_clip_rect(clip); - crate::parametric::draw_overlay(app, ui, &p, &xf); - } - // Grips on selected objects (when idle or editing grips). - let grip_edit = app.session.running.as_ref().is_some_and(|r| r.id == "grip"); - if (!grip_edit && (app.canvas.grip_editing || app.session.prompting())) || sel.is_empty() { - app.canvas.hot_grips.clear(); - } - if !grip_edit { - app.canvas.dyn_hot = None; - } - app.canvas.grip_editing = grip_edit; - if (!app.session.prompting() || grip_edit) - && app.session.shows_grips(sel.len()) - && let Ok(st) = app.session.state() - { - let gs = app.session.settings.gripsize as f32; - for h in sel.iter().take(200) { - if let Some(e) = st.doc.entity(*h) { - let grips = e.kind.grips(); - let count = grips.len(); - for (gi, g) in grips.into_iter().enumerate() { - let sp = xf.to_screen(g); - if rect.contains(sp) { - let r = grip_rect(&e.kind, count, gi, sp, gs); - let hot = app.canvas.hot_grips.contains(&(*h, gi)); - let warm = !hot && hover_pos.is_some_and(|hp| r.expand(2.0).contains(hp)); - painter.rect_filled( - r, - 0.0, - if hot { - t.grip_hot - } else if warm { - Color32::from_rgb(0xff, 0x7f, 0x9f) - } else { - t.grip - }, - ); - painter.rect_stroke(r, 0.0, Stroke::new(1.0, Color32::from_rgb(0x10, 0x30, 0x80)), egui::StrokeKind::Middle); - } - } - } - } - crate::dyngrips::draw(app, &painter, &xf, rect, hover_pos); - } - // Rubber band preview of the active command. - // `xf` is the space being edited: paper space, or model space inside the active viewport. - let drag = match (app.canvas.cursor, app.canvas.gpu.is_some()) { - (Some(c), true) => app.session.drag(c), - _ => None, - }; - if let Some((drag, extra)) = drag { - if let Ok(st) = app.session.state() { - let serial = app.session.clipboard_source.as_ref().map_or(0, |c| c.serial); - let transparency = app.session.settings.transparency_display; - draw_drag_gpu(&mut app.canvas, &painter, &xf, &st.doc, &drag, (&app.session.clipboard, serial), bg, transparency); - let list = rebuilt_preview(&st.doc, extra.iter(), scale); - draw_list(&painter, &xf, &list, bg, false); - } - } else if let (Some(c), true) = (app.canvas.cursor, app.session.prompting()) { - let ents = app.session.preview(c); - if !ents.is_empty() - && let Ok(st) = app.session.state() - { - let list = rebuilt_preview(&st.doc, ents.iter(), scale); - draw_list(&painter, &xf, &list, bg, false); - } - } - // Pending selection window. - if let (Some(pw), Some(c)) = (app.session.pending_window, raw_world) { - let a = xf.to_screen(pw.corner); - let b = xf.to_screen(c); - let r = Rect::from_two_pos(a, b); - let crossing = c.x < pw.corner.x; - if let Some(fill) = selection_area_fill(&app.session.settings, crossing) { - painter.rect_filled(r, 0.0, fill); - } - let st = Stroke::new(1.0, if crossing { t.crossing_stroke } else { t.window_stroke }); - if crossing { - let pts = vec![r.left_top(), r.right_top(), r.right_bottom(), r.left_bottom(), r.left_top()]; - painter.extend(Shape::dashed_line(&pts, st, 5.0, 3.0)); - } else { - painter.rect_stroke(r, 0.0, st, egui::StrokeKind::Middle); - } - } - // Acquired tracking points and the polar / tracking paths under the cursor. - if app.canvas.cursor.is_some() { - draw_tracking(&painter, &xf, &app.session.tracking.acquired, app.canvas.track.as_ref(), view.height * 4.0); - } - if let (Some(hit), Some(_)) = (app.canvas.snap, app.canvas.cursor) { - draw_snap_marker(&painter, xf.to_screen(hit.point), &hit); - } - draw_tiles(app, ui, &tiles); - // UCS icon, ViewCube and viewport label. - if app.ui.show_ucs_icon { - draw_ucs_icon(&painter, rect); - crate::viewcube::ucs_icon(app, ui, rect); - } - if app.ui.show_viewcube && sheet.is_none() { - draw_viewcube(app, ui, rect); - } - viewport_label(&painter, rect); - // Typing and Escape reach the command line even when its overlay is hidden. - crate::cmdline::keyboard(app, ui.ctx()); - // Command line overlay. - if app.ui.show_command_line { - crate::cmdline::show(app, ui, full); - } - // Crosshair cursor. - if inside && !on_menu { - // Hide the OS cursor under the drawn crosshair, unless assistive tech needs it (#39). - ui.ctx().set_cursor_icon(if app.ui.system_cursor { egui::CursorIcon::Crosshair } else { egui::CursorIcon::None }); - if let Some(hp) = hover_pos { - // Use the painted sheet even in MSPACE, where the editing transform follows a - // model viewport. The whole cursor takes one colour from its centre. - let ink = paper_rect.map_or(t.crosshair, |paper| crate::theme::crosshair_color(if paper.contains(hp) { t.paper } else { surround })); - draw_crosshair(app, &painter, rect, hp, selecting, ink); - if app.canvas.cycle_badge && app.canvas.cycle.is_none() { - cycle_badge(&painter, hp); - } - } - } - if inside - && app.session.settings.dynmode - && let Some(hp) = hover_pos - { - // A stretched grip shows its dimensions instead of the pointer boxes; a prompt asking for - // a distance shows its one field and keeps the prompt and its options. - let fields = crate::grip_dyn::draw(app, &painter); - if !fields || app.session.current_prompt().is_some_and(|p| p.distance) { - dynamic_input(app, &painter, hp, fields); - } - } - // Over the crosshair, so the row under the pointer reads clearly. - cycle_list(app, &painter); + let (sheet, bg, paper_rect) = paint::drawing(app, ui, &frame); + paint::overlays(app, ui, &frame, sheet, bg); + paint::cursor(app, ui, &frame, paper_rect); // No continuous repaint while hovering: input events repaint by themselves. Repainting every // frame kept a vsync swapchain's queue full (every frame waited behind queued ones) and would // spin the CPU with the low-latency present mode (`gpu::surface_config`). diff --git a/crates/ui-egui/src/canvas/frame.rs b/crates/ui-egui/src/canvas/frame.rs new file mode 100644 index 0000000000..c023b5782f --- /dev/null +++ b/crates/ui-egui/src/canvas/frame.rs @@ -0,0 +1,514 @@ +//! One frame of the canvas, input side: what [`show`] sets up and every phase reads ([`Frame`]), +//! navigation (pan, zoom, tiles) and clicks (prompts, picks, grips, menus, hover). + +use std::ops::ControlFlow; + +use super::*; + +/// What crosses the phases of [`show`]: the drawing area, its transforms and this frame's input. +pub(super) struct Frame { + pub(super) t: Tokens, + /// The whole canvas, tiles included. + pub(super) full: Rect, + pub(super) tiles: Vec<(Rect, cadcraft_engine::ModelTile)>, + /// The drawing area (the current tile). + pub(super) rect: Rect, + pub(super) resp: egui::Response, + pub(super) painter: egui::Painter, + pub(super) view: cadcraft_engine::View, + pub(super) scale: f64, + pub(super) xf: Xf, + /// The paper (display) transform; equal to `xf` except inside an MSPACE viewport. + pub(super) xf_paper: Xf, + pub(super) active_vp: Option, + /// A clipped active viewport's outline (picks and its heavy border follow it). + pub(super) active_clip: Option, + pub(super) hover_pos: Option, + pub(super) scroll: egui::Vec2, + pub(super) mods: egui::Modifiers, + pub(super) middle_down: bool, + pub(super) pressed_primary: bool, + pub(super) pressed_secondary: bool, + pub(super) dbl_middle: bool, + pub(super) dbl_primary: bool, + pub(super) inside: bool, + /// The pointer is over a menu the canvas paints itself ([`over_canvas_menu`]). + pub(super) on_menu: bool, + pub(super) raw_world: Option, + pub(super) selecting: bool, +} + +impl Frame { + /// The drawing area, its view and this frame's input; `None` without a drawing. + pub(super) fn setup(app: &mut CadApp, ui: &mut egui::Ui) -> Option { + let t = Tokens::get(); + let full = ui.available_rect_before_wrap(); + // Tiled model viewports (VPORTS): the current tile is the drawing area below; `draw_tiles` + // draws the others. + let tiles = tile_rects(app, full); + let rect = tiles.iter().find(|(_, tile)| tile.active).map_or(full, |(r, _)| *r); + let resp = ui.interact(full, ui.scope_id().with("canvas"), Sense::click_and_drag()); + let painter = ui.painter_at(rect); + app.canvas.rect = Some(rect); + app.session.viewport_px = (f64::from(rect.width()).max(1.0), f64::from(rect.height()).max(1.0)); + if app.canvas.zoom_pending { + app.canvas.zoom_pending = false; + let _ = app.session.zoom_extents(); + } + let Ok(st) = app.session.state() else { return None }; + let view = st.view(); + let scale = f64::from(rect.height()) / view.height.max(1e-12); + let xf = Xf { rect, center: view.center, scale }; + app.canvas.xf = Some(xf); + + // ---------- input ---------- + let (hover_pos, scroll, mods, middle_down, pressed_primary, pressed_secondary, dbl_middle, dbl_primary) = ui.input(|i| { + ( + i.pointer.hover_pos(), + i.smooth_scroll_delta, + i.modifiers, + i.pointer.middle_down(), + i.pointer.primary_clicked(), + i.pointer.secondary_clicked(), + i.pointer.button_double_clicked(egui::PointerButton::Middle), + i.pointer.button_double_clicked(egui::PointerButton::Primary), + ) + }); + // The drawing area alone: a menu popup's frame (between its items and its border), a tooltip + // or a floating palette lives on a layer above the canvas, where `hovered()` alone still + // counts the canvas. Over those the OS cursor stays visible and the crosshair stays put, as + // over the menu bar (#1701). + let inside = hover_pos.is_some_and(|p| rect.contains(p) && ui.ctx().layer_id_at(p) == Some(ui.layer_id()) && resp.hovered()); + // The menus the canvas paints itself (selection cycling's rows, Dynamic Input's options) are + // menus too, but they share the canvas's layer: `layer_id_at` reports the drawing under them, + // so only their rectangles tell them apart from it ([`over_canvas_menu`]). They are excluded + // from the cursor contract in `paint::cursor`, though not from the canvas's input: a click on + // a row is taken by the row. + let on_menu = hover_pos.is_some_and(|p| over_canvas_menu(app, p)); + // Whether Ctrl is held right now. Written every frame (not just while a command prompts), so + // previews that read `Session::ctrl_down` always see the current state. The engine + // deliberately does not carry modifiers through `Input`, which is UI-agnostic (see PR + // #407's Limits note: modifiers used to be dropped on the floor at this point). + app.session.ctrl_down = mods.ctrl; + Some(Frame { + t, + full, + tiles, + rect, + resp, + painter, + view, + scale, + xf, + xf_paper: xf, + active_vp: None, + active_clip: None, + hover_pos, + scroll, + mods, + middle_down, + pressed_primary, + pressed_secondary, + dbl_middle, + dbl_primary, + inside, + on_menu, + raw_world: None, + selecting: false, + }) + } + + /// Re-read the view after navigation. + pub(super) fn reread_view(&mut self, app: &mut CadApp) { + let rect = self.rect; + self.view = app.session.state().map(|s| s.view()).unwrap_or(self.view); + self.scale = f64::from(rect.height()) / self.view.height.max(1e-12); + self.xf = Xf { rect, center: self.view.center, scale: self.scale }; + let xf = self.xf; + app.canvas.xf = Some(xf); + // The paper (display) transform; equal to `xf` except inside an MSPACE viewport. + (self.xf_paper, self.active_vp) = match app.session.state() { + Ok(st) => { + let pv = st.paper_view(); + let xp = Xf { rect, center: pv.center, scale: f64::from(rect.height()) / pv.height.max(1e-12) }; + (if st.mspace.is_some() { xp } else { xf }, st.active_viewport().map(|(_, v)| v)) + } + Err(_) => (xf, None), + }; + // A clipped active viewport's outline (picks and its heavy border follow it). + self.active_clip = + app.session.state().ok().and_then(|st| st.active_viewport().and_then(|(h, v)| cadcraft_render::viewport_clip(&st.doc, h, &v))); + } +} + +/// Scroll, wheel and pinch zoom, middle-button pan, ZOOM Realtime and clicks on other tiles. +/// `Break`: the frame ends here (a click made another tile current). +pub(super) fn navigate(app: &mut CadApp, ui: &mut egui::Ui, frame: &mut Frame) -> ControlFlow<()> { + let Frame { rect, scale, xf, hover_pos, scroll, middle_down, pressed_primary, dbl_middle, inside, .. } = *frame; + let resp = frame.resp.clone(); + let tiles = &frame.tiles; + // Which device is scrolling? On macOS, trackpads (and Magic Mouse) report precise point + // deltas; notched mouse wheels report lines. The answer is sticky because egui spreads a wheel + // notch over several frames. Elsewhere (browsers, Wayland smooth-scroll mice) ordinary wheels + // also report points, so the wheel keeps zooming there. + ui.input(|i| { + for e in &i.raw.events { + if let egui::Event::MouseWheel { unit, .. } = e { + app.canvas.scroll_pans = cfg!(target_os = "macos") && *unit == egui::MouseWheelUnit::Point; + } + } + }); + if inside && app.canvas.scroll_pans && scroll != egui::Vec2::ZERO { + // Two-finger swipe pans: the drawing follows the fingers, like a middle-drag. + // Pinch (and Cmd+scroll) still zoom via `zoom_delta` below. + if let Ok(st) = app.session.state_mut() { + let v = st.view(); + st.set_view_quiet(cadcraft_engine::View { + center: v.center - Vec2::new(f64::from(scroll.x) / scale, -f64::from(scroll.y) / scale), + height: v.height, + }); + } + } + let opts = ui.ctx().options(|o| o.input_options); + let wheel = ui.input(|i| wheel_zoom_points(&i.raw.events, &opts, rect.height())); + if inside + && !app.canvas.scroll_pans + && wheel != 0.0 + && let Some(hp) = hover_pos + { + // Zoom with the mouse wheel about the cursor, each notch at once. + let f = app.session.wheel_zoom_factor(f64::from(wheel)); + let about = xf.to_world(hp); + let _ = app.session.zoom_about(f, about); + } + let zoom_pinch = ui.input(|i| crate::animation::zoom_delta(i, &opts, app.shows_animations())); + if inside + && (zoom_pinch - 1.0).abs() > 1e-4 + && let Some(hp) = hover_pos + { + let _ = app.session.zoom_about(f64::from(zoom_pinch), xf.to_world(hp)); + } + // Pan with the middle button (or Shift+right-drag / two-finger drag on trackpads is scroll). + let middle_pressed = ui.input(|i| i.pointer.button_pressed(egui::PointerButton::Middle)); + let focused = ui.input(|i| i.focused); + if middle_down + && focused + && (app.canvas.pan_last.is_some() || (middle_pressed && inside)) + && let Some(hp) = hover_pos + { + if let Some(last) = app.canvas.pan_last { + let d = hp - last; + if let Ok(st) = app.session.state_mut() { + let v = st.view(); + st.set_view_quiet(cadcraft_engine::View { + center: v.center - Vec2::new(f64::from(d.x) / scale, -f64::from(d.y) / scale), + height: v.height, + }); + } + } + app.canvas.pan_last = Some(hp); + } else { + app.canvas.pan_last = None; + } + if dbl_middle && inside { + let _ = app.session.zoom_extents(); + } + // ZOOM Realtime: dragging up zooms in, down zooms out. + if resp.dragged_by(egui::PointerButton::Primary) && app.session.realtime_zoom_active() { + let _ = app.session.realtime_zoom(f64::from(resp.drag_delta().y)); + } + // A click in another tile makes it current (or answers -VPORTS Join's prompts). + if pressed_primary + && let Some(hp) = hover_pos + && !rect.contains(hp) + && let Some(i) = tiles.iter().position(|(r, _)| r.contains(hp)) + { + if let Err(e) = app.session.click_tile(i) { + app.session.echo(e.to_string()); + } + ui.ctx().request_repaint(); + return ControlFlow::Break(()); + } + ControlFlow::Continue(()) +} + +/// Double-clicks (viewports, text, hatches), point prompts, the options menu, clicks, the +/// press-and-drag selection window, right-click menus and the hover highlight. +/// `Break`: the frame ends here (a double-click switched spaces or started TEXTEDIT). +pub(super) fn clicks(app: &mut CadApp, ui: &mut egui::Ui, frame: &mut Frame) -> ControlFlow<()> { + let Frame { rect, scale, xf, xf_paper, hover_pos, mods, middle_down, pressed_primary, pressed_secondary, dbl_primary, inside, .. } = *frame; + let resp = frame.resp.clone(); + let (active_vp, active_clip) = (&frame.active_vp, &frame.active_clip); + // Double-click a viewport to work inside it; double-click the sheet outside it to return. + if inside + && dbl_primary + && !app.session.prompting() + && matches!(app.session.layout_space(), cadcraft_doc::Space::Paper(_)) + && let Some(hp) = hover_pos + { + let pp = xf_paper.to_world(hp); + let in_rect = |v: &cadcraft_doc::Viewport| (pp.x - v.center.x).abs() <= v.width / 2.0 && (pp.y - v.center.y).abs() <= v.height / 2.0; + let in_active = active_vp.as_ref().is_some_and(|v| active_clip.as_ref().map_or(in_rect(v), |c| c.contains(pp))); + if !in_active { + app.canvas.hot_grips.clear(); + app.session.pending_window = None; + app.canvas.drag_window = false; + if app.run("mspace", serde_json::json!({"at": [pp.x, pp.y]})).is_err() && active_vp.is_some() { + let _ = app.run("pspace", serde_json::json!({})); + } + app.session.pending_window = None; + app.canvas.drag_window = false; + app.canvas.list = None; + return ControlFlow::Break(()); + } + } + // Double-click text to edit it, as in AutoCAD: TEXTEDIT with that object already picked. + if inside + && dbl_primary + && app.session.running.is_none() + && let Some(hp) = hover_pos + && edit_text_at(app, xf.to_world(hp)) + { + return ControlFlow::Break(()); + } + + // Double-click a hatch (no command running) to edit it in the Hatch Edit dialog. The click is + // used up: it must not also start a grip edit on the hatch's centre grip below. + let mut hatch_dialog = false; + if inside + && dbl_primary + && app.session.running.is_none() + && let Some(hp) = hover_pos + && let Some(h) = hatch_at(app, xf.to_world(hp), scale) + { + app.session.set_selection(vec![h]); + crate::hatchdlg::open_edit(app); + hatch_dialog = true; + } + + // While a command asks for a point, every click places one, however fast the drafting (#815). + // egui drops a click whose button comes up a few pixels from where it went down (a drag to it), + // and eframe hands a slow frame's clicks over together, which egui reports as one. + let point_prompt = app.session.current_prompt().is_some_and(|p| cadcraft_engine::pointmod::accepts(&p)) + && app.session.pending_window.is_none() + && !app.session.realtime_zoom_active(); + let pressed_primary = pressed_primary || (point_prompt && resp.drag_stopped_by(egui::PointerButton::Primary)); + if point_prompt && inside && pressed_primary && !middle_down && app.cmd.options.is_none() && app.cmd.buffer.is_empty() { + let releases: Vec = ui.input(|i| { + i.events + .iter() + .filter_map(|e| match e { + egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed: false, .. } => Some(*pos), + _ => None, + }) + .collect() + }); + // The last one is placed below, at the cursor, like any click. + if let Some((_, earlier)) = releases.split_last() { + for pos in earlier.iter().filter(|p| rect.contains(**p)) { + let w = effective_point(app, xf.to_world(*pos), &xf, ui.ctx()); + let result = match app.canvas.snap { + Some(hit) => app.session.input_snapped(hit), + None => app.session.input(Input::Point(w)), + }; + if let Err(e) = result { + app.session.echo(e.to_string()); + } + } + } + } + + frame.raw_world = hover_pos.filter(|_| inside).map(|p| xf.to_world(p)); + let raw_world = frame.raw_world; + let dyn_frame = crate::cmdline::dyn_frame(app); + if let Some(w) = raw_world { + let mut eff = effective_point(app, w, &xf, ui.ctx()); + // A locked Dynamic Input value (`40,` or `40<` typed) holds the cursor and overrides snaps. + if let Some(f) = dyn_frame + && let Some(e) = crate::dyninput::parse(&app.cmd.buffer, &f) + && e.first_locked().is_some() + { + eff = crate::dyninput::constrain(&e, &f, eff, app.cmd.second_locked()); + app.canvas.snap = None; + app.canvas.track = None; + } + // Grip dimensional input: typed lengths and angles hold the grip and override snaps. + let grip_held = crate::grip_dyn::constrain(app, eff); + if let Some(q) = grip_held { + eff = q; + app.canvas.snap = None; + app.canvas.track = None; + } + app.canvas.cursor = Some(eff); + app.session.cursor = eff; + app.session.cursor_deferred = app.canvas.snap.and_then(|h| h.deferred); + // Typed or menu snap overrides (END, NON…) and Tab cycling; a locked Dynamic Input value wins. + if dyn_frame.and_then(|f| crate::dyninput::parse(&app.cmd.buffer, &f)).is_none_or(|e| e.first_locked().is_none()) { + override_cursor(app, w, &xf); + } + // Typed grip dimensions win over snap overrides too. + if let Some(q) = grip_held { + app.canvas.cursor = Some(q); + app.session.cursor = q; + app.canvas.snap = None; + app.session.cursor_deferred = None; + } + } else { + app.canvas.cursor = None; + app.canvas.snap = None; + app.session.cursor_deferred = None; + } + + // A click on the Dynamic Input options menu picks the keyword; elsewhere it closes the menu. + let mut picked = false; + if pressed_primary && app.cmd.options.is_some() { + match hover_pos.and_then(|h| app.cmd.option_at(h)) { + Some(k) => { + crate::cmdline::pick_option(app, k); + picked = true; + } + None => app.cmd.options = None, + } + } + // A double-click with no command running (on a layout viewport, say) leaves no selection + // window behind; while a command asks for points, every click still counts (#815). + let idle_dbl = dbl_primary && app.session.running.is_none(); + if idle_dbl { + app.session.pending_window = None; + app.canvas.drag_window = false; + } + let pressed_primary = pressed_primary && !picked && !hatch_dialog && !idle_dbl; + let key: CycleKey = ( + app.session.running.as_ref().map(|r| r.id.clone()), + app.session.current_prompt().map(|p| p.message), + app.session.state().map_or(0, |st| st.uid), + app.session.state().map_or(0, |st| st.revision), + xf.center, + xf.scale, + ); + if app.canvas.cycle_key.as_ref() != Some(&key) { + app.canvas.cycle = None; + app.canvas.hover_cycle = None; + app.canvas.cycle_key = Some(key); + } + let cycled = pressed_primary && !middle_down && cycle_click(app, &xf, hover_pos, raw_world.filter(|_| inside), mods.shift); + // Clicks. + if inside + && pressed_primary + && !middle_down + && !cycled + && let Some(p) = app.canvas.cursor + { + if app.session.prompting() { + // Ctrl held while picking the point of a grip edit makes copies (the Copy option). + if mods.ctrl && app.session.running.as_ref().is_some_and(|r| r.id == "grip") { + let _ = app.session.input(Input::Keyword("Copy".into())); + } + // Clicking places the point; a value typed into the Dynamic Input boxes is used up. + if let Some(f) = dyn_frame + && crate::dyninput::parse(&app.cmd.buffer, &f).is_some() + { + app.cmd.buffer.clear(); + } + // Preserve a snap's viewport origin for DIST, and its deferred curve where needed. + let result = match app.canvas.snap { + Some(hit) => app.session.input_snapped(hit), + None => app.session.input(Input::Point(p)), + }; + if let Err(e) = result { + app.session.echo(e.to_string()); + } + } else if let Some(g) = crate::dyngrips::at(app, &xf, hover_pos) { + crate::dyngrips::click(app, g); + } else if let Some(g) = grip_at(app, &xf, hover_pos) { + click_grip(app, g, mods.shift); + } else if let Err(e) = app.session.idle_click(raw_world.unwrap_or(p), mods.shift) { + app.session.echo(e.to_string()); + } + } + // Press-and-drag selection window (PICKDRAG = 2; click-click still works): a drag + // that starts while objects are being selected opens a window where the button went down, + // and letting go closes it. Drags on grips and drags while drawing are left alone. + if !app.session.settings.pickdrag { + app.canvas.drag_window = false; + } + if app.session.settings.pickdrag + && resp.drag_started_by(egui::PointerButton::Primary) + && !middle_down + && app.canvas.cycle.is_none() + && app.session.pending_window.is_none() + && let Some(origin) = ui.input(|i| i.pointer.press_origin()) + && rect.contains(origin) + && grip_at(app, &xf, Some(origin)).is_none() + && crate::dyngrips::at(app, &xf, Some(origin)).is_none() + { + app.canvas.drag_window = app.session.begin_window(xf.to_world(origin)); + } + if app.canvas.drag_window && resp.drag_stopped_by(egui::PointerButton::Primary) { + app.canvas.drag_window = false; + if app.session.pending_window.is_some() + && let Some(end) = ui.input(|i| i.pointer.interact_pos()) + { + let end = xf.to_world(end); + let done = if app.session.prompting() { app.session.input(Input::Point(end)) } else { app.session.idle_click(end, mods.shift) }; + if let Err(e) = done { + app.session.echo(e.to_string()); + } + } + } + // Right-click acts as Enter while a command runs (the classic CAD default; held, it opens the + // command menu); otherwise it opens the shortcut menu (Repeat, Clipboard, …). + // Shift/Ctrl+right-click opens the object snap menu. + let idle = !app.session.prompting(); + if inside && pressed_secondary { + app.canvas.snap_menu = mods.shift || mods.ctrl; + } + if crate::context_menu::in_command(app, ui, inside, pressed_secondary) { + let _ = app.session.input(Input::Enter); + } + // Right-click on a grip with options (polyline vertex, line end…) opens the grip menu. + if inside && pressed_secondary && idle && !app.canvas.snap_menu { + app.canvas.grip_menu = grip_at(app, &xf, hover_pos).filter(|g| !grip_options(app, *g).is_empty()); + } + crate::dyngrips::menu(app, ui.ctx(), &xf); + if idle && !app.canvas.snap_menu && app.canvas.grip_menu.is_some() { + grip_menu(app, &resp); + } else if idle || app.canvas.snap_menu { + crate::context_menu::show(app, &resp); + } + + // Hover highlight (throttled to cursor movement). + // The cursor picks objects (pick box, rollover highlight): "Select objects" and single-object + // prompts, or no command with PICKFIRST on; SELECTIONPREVIEW says in which of those it shows. + frame.selecting = app.session.previews_selection(); + if inside && frame.selecting && app.session.pending_window.is_none() { + if hover_pos != app.canvas.hover_at + && let (Some(w), Ok(st)) = (raw_world, app.session.state()) + { + let filter = app.session.settings.previewfilter; + app.canvas.hover = cadcraft_engine::select::pick(&st.doc, &st.edit_space(), w, app.session.settings.pickbox.max(1.0) * 1.5 / scale) + .filter(|h| st.doc.entity(*h).is_none_or(|e| !cadcraft_engine::select::preview_filtered(&st.doc, e, filter))); + app.canvas.hover_at = hover_pos; + } + } else { + app.canvas.hover = None; + app.canvas.hover_at = None; + } + // The open selection cycling list highlights the object of the row under the pointer. + if app.canvas.cycle.is_some() && ui.input(|i| i.key_pressed(egui::Key::Escape)) { + app.canvas.cycle = None; + } + if let Some(c) = &app.canvas.cycle { + app.canvas.hover = hover_pos.and_then(|hp| c.row_at(hp)); + app.canvas.hover_at = None; + } + shift_space_cycle(app, ui, hover_pos.filter(|_| inside), raw_world); + if inside && hover_pos != app.canvas.cycle_badge_at { + app.canvas.cycle_badge = raw_world.is_some_and(|w| app.session.cycling_badge(w)); + app.canvas.cycle_badge_at = hover_pos; + } else if !inside { + app.canvas.cycle_badge = false; + app.canvas.cycle_badge_at = None; + } + ControlFlow::Continue(()) +} diff --git a/crates/ui-egui/src/canvas/paint.rs b/crates/ui-egui/src/canvas/paint.rs new file mode 100644 index 0000000000..c35d604cb4 --- /dev/null +++ b/crates/ui-egui/src/canvas/paint.rs @@ -0,0 +1,313 @@ +//! One frame of the canvas, painting side: the drawing with its highlights and grips, then the +//! overlays (rubber bands, selection window, tracking, tiles, icons, command line), then the cursor. + +use super::frame::Frame; +use super::*; + +/// The grey surround of a paper-space sheet. +const SURROUND: Color32 = Color32::from_rgb(0x50, 0x57, 0x63); + +/// The background (paper sheet, or wash and grid), the display list, the active viewport's +/// contents and highlights, constraint bars and grips. Returns the paper sheet (`None` in model +/// space), the background colour the overlays are drawn against, and the sheet's screen rectangle +/// (`None` in model space). +pub(super) fn drawing(app: &mut CadApp, ui: &mut egui::Ui, frame: &Frame) -> (Option, Rgb, Option) { + let Frame { t, rect, scale, xf, xf_paper, hover_pos, .. } = *frame; + let painter = frame.painter.clone(); + let (tiles, active_vp, active_clip) = (&frame.tiles, &frame.active_vp, &frame.active_clip); + // ---------- paint ---------- + painter.rect_filled(rect, 0.0, t.canvas); + let t0 = crate::now_ms(); + let mut views = vec![(xf_paper.world_bounds(), 1.0 / xf_paper.scale)]; + views.extend(tiles.iter().filter(|(r, tile)| !tile.active && r.width() >= 1.0 && r.height() >= 1.0).map(|(r, tile)| { + let xf = tile_xf(*r, tile); + (xf.world_bounds(), 1.0 / xf.scale) + })); + ensure_list(app, &xf_paper, &views, ui.ctx()); + let sheet = app.canvas.list.as_ref().and_then(|l| l.sheet); + let (bg, paper_rect) = match sheet { + Some(sh) => { + // Paper space: grey surround, the sheet with a shadow, the printable area dashed. + painter.rect_filled(rect, 0.0, SURROUND); + let a = xf_paper.to_screen(Vec2::new(0.0, sh.size.y)); + let b = xf_paper.to_screen(Vec2::new(sh.size.x, 0.0)); + let paper = Rect::from_two_pos(a, b); + painter.rect_filled(paper.translate(vec2(5.0, 5.0)), 0.0, Color32::from_black_alpha(110)); + painter.rect_filled(paper, 0.0, t.paper); + let pa = Rect::from_two_pos( + xf_paper.to_screen(Vec2::new(sh.printable.min.x, sh.printable.max.y)), + xf_paper.to_screen(Vec2::new(sh.printable.max.x, sh.printable.min.y)), + ); + let pts = [pa.left_top(), pa.right_top(), pa.right_bottom(), pa.left_bottom(), pa.left_top()]; + painter.extend(Shape::dashed_line(&pts, Stroke::new(1.0, Color32::from_gray(150)), 4.0, 4.0)); + (Rgb(t.paper.r(), t.paper.g(), t.paper.b()), Some(paper)) + } + None => { + blockedit::wash(app, &painter, rect); + draw_grid(app, &painter, &xf); + (Rgb(t.canvas.r(), t.canvas.g(), t.canvas.b()), None) + } + }; + let sel = app.session.selection(); + // How the object under the cursor and the selected ones are highlighted (PREVIEWEFFECT, + // HIGHLIGHT, SELECTIONEFFECT); with HIGHLIGHT off only their grips show. + let (preview_look, selection_look) = (Look::preview(&app.session.settings), Look::selection(&app.session.settings)); + // The active viewport's contents, drawn apart from the layout's list, under its own objects. + // `masked`: its outline is in the stencil buffer this frame. + let mut masked = false; + if let Some(f) = ensure_vp_content(app, &xf_paper, ui.ctx()) { + let gpu = app.canvas.gpu.is_some(); + let clipped = painter.with_clip_rect(f.on_screen.intersect(rect)); + draw_vp_content(&mut app.canvas.vp, gpu, &clipped, &f, &xf_paper, bg, app.session.settings.lwdisplay); + masked = gpu && f.outline.is_some(); + } + if app.canvas.gpu.is_some() { + draw_list_gpu(&mut app.canvas, &painter, &xf_paper, bg, app.session.settings.lwdisplay); + } else if let Some(list) = &app.canvas.list { + draw_list(&painter, &xf_paper, list, bg, app.session.settings.lwdisplay); + } + if let Some(vp) = &active_vp { + // The active viewport: a heavy border, highlights in model units clipped to it. + let r = Rect::from_two_pos( + xf_paper.to_screen(Vec2::new(vp.center.x - vp.width / 2.0, vp.center.y + vp.height / 2.0)), + xf_paper.to_screen(Vec2::new(vp.center.x + vp.width / 2.0, vp.center.y - vp.height / 2.0)), + ); + let heavy = Stroke::new(3.0, Color32::from_rgb(0x2a, 0x2a, 0x2a)); + match &active_clip { + Some(c) => { + let mut pts: Vec = c.polygon().iter().map(|q| xf_paper.to_screen(*q)).collect(); + pts.extend(pts.first().copied()); + painter.add(egui::Shape::line(pts, heavy)); + } + None => { + painter.rect_stroke(r, 0.0, heavy, egui::StrokeKind::Middle); + } + } + if let Ok(st) = app.session.state() { + // A clipped viewport's outline bounds (its outline is masked when `masked`). + let bounds = active_clip.as_ref().map_or(r, |c| { + let b = c.bounds(); + Rect::from_two_pos(xf_paper.to_screen(Vec2::new(b.min.x, b.max.y)), xf_paper.to_screen(Vec2::new(b.max.x, b.min.y))) + }); + let clipped = painter.with_clip_rect(bounds.intersect(rect)); + let c = &mut app.canvas; + if let Some(h) = c.hover + && !sel.contains(&h) + { + c.vp_hover.update(st, &[h], scale); + let list = &c.vp_hover.list; + draw_highlight(&clipped, &xf, list, &(0..list.prims.len()).collect::>(), t.hover, preview_look); + } + if let Some(look) = selection_look + && !sel.is_empty() + { + c.vp_sel.update(st, &sel, scale); + let list = &c.vp_sel.list; + let all = || (0..list.prims.len()).collect::>(); + if c.gpu.is_some() { + // Shares the GPU highlight layer with the layout's own selection; the high bit + // keeps this list's generations apart from the layout list's. + let list_id = c.vp_sel.list_gen | 1 << 63; + let stencil = if masked { crate::gpu::Stencil::Inside } else { crate::gpu::Stencil::Off }; + let hl = (&mut c.hl_key, &c.hl_slot, &mut c.hl_origin, &mut c.hl_infinite); + draw_highlight_gpu(&clipped, &xf, list, list_id, all, &sel, t.selection, look, hl, stencil); + draw_highlight(&clipped, &xf, list, &c.hl_infinite, t.selection, look); + } else { + draw_highlight(&clipped, &xf, list, &all(), t.selection, look); + } + } + } + } else if let CanvasState { list: Some(list), list_gen, prim_index, hover, gpu, hl_key, hl_slot, hl_origin, hl_infinite, .. } = &mut app.canvas { + if let Some(h) = *hover + && !sel.contains(&h) + { + draw_highlight(&painter, &xf, list, &prim_index.prims(*list_gen, list, &[h]), t.hover, preview_look); + } + if let Some(look) = selection_look { + if gpu.is_some() && !sel.is_empty() { + let list_id = *list_gen; + let hl = (hl_key, &*hl_slot, hl_origin, &mut *hl_infinite); + draw_highlight_gpu( + &painter, + &xf, + list, + list_id, + || prim_index.prims(list_id, list, &sel), + &sel, + t.selection, + look, + hl, + crate::gpu::Stencil::Off, + ); + draw_highlight(&painter, &xf, list, hl_infinite, t.selection, look); + } else { + draw_highlight(&painter, &xf, list, &prim_index.prims(*list_gen, list, &sel), t.selection, look); + } + } + } + app.canvas.draw_ms = crate::now_ms() - t0; + compare::paint(app, ui.ctx(), &painter, &xf_paper, rect); + blockedit::paint(app, ui.ctx(), &painter, &xf_paper, rect); + // Constraint bars and dynamic dimensional constraints (model space, or inside a viewport). + if sheet.is_none() || active_vp.is_some() { + let clip = match &active_vp { + Some(vp) => Rect::from_two_pos( + xf_paper.to_screen(Vec2::new(vp.center.x - vp.width / 2.0, vp.center.y + vp.height / 2.0)), + xf_paper.to_screen(Vec2::new(vp.center.x + vp.width / 2.0, vp.center.y - vp.height / 2.0)), + ) + .intersect(rect), + None => rect, + }; + let p = painter.with_clip_rect(clip); + crate::parametric::draw_overlay(app, ui, &p, &xf); + } + // Grips on selected objects (when idle or editing grips). + let grip_edit = app.session.running.as_ref().is_some_and(|r| r.id == "grip"); + if (!grip_edit && (app.canvas.grip_editing || app.session.prompting())) || sel.is_empty() { + app.canvas.hot_grips.clear(); + } + if !grip_edit { + app.canvas.dyn_hot = None; + } + app.canvas.grip_editing = grip_edit; + if (!app.session.prompting() || grip_edit) + && app.session.shows_grips(sel.len()) + && let Ok(st) = app.session.state() + { + let gs = app.session.settings.gripsize as f32; + for h in sel.iter().take(200) { + if let Some(e) = st.doc.entity(*h) { + let grips = e.kind.grips(); + let count = grips.len(); + for (gi, g) in grips.into_iter().enumerate() { + let sp = xf.to_screen(g); + if rect.contains(sp) { + let r = grip_rect(&e.kind, count, gi, sp, gs); + let hot = app.canvas.hot_grips.contains(&(*h, gi)); + let warm = !hot && hover_pos.is_some_and(|hp| r.expand(2.0).contains(hp)); + painter.rect_filled( + r, + 0.0, + if hot { + t.grip_hot + } else if warm { + Color32::from_rgb(0xff, 0x7f, 0x9f) + } else { + t.grip + }, + ); + painter.rect_stroke(r, 0.0, Stroke::new(1.0, Color32::from_rgb(0x10, 0x30, 0x80)), egui::StrokeKind::Middle); + } + } + } + } + crate::dyngrips::draw(app, &painter, &xf, rect, hover_pos); + } + (sheet, bg, paper_rect) +} + +/// The active command's rubber band, the pending selection window, tracking paths and the snap +/// marker, the other tiles, the UCS icon, ViewCube and viewport label, and the command line. +pub(super) fn overlays(app: &mut CadApp, ui: &mut egui::Ui, frame: &Frame, sheet: Option, bg: Rgb) { + let Frame { t, full, rect, view, scale, xf, raw_world, .. } = *frame; + let painter = frame.painter.clone(); + let tiles = &frame.tiles; + // Rubber band preview of the active command. + // `xf` is the space being edited: paper space, or model space inside the active viewport. + let drag = match (app.canvas.cursor, app.canvas.gpu.is_some()) { + (Some(c), true) => app.session.drag(c), + _ => None, + }; + if let Some((drag, extra)) = drag { + if let Ok(st) = app.session.state() { + let serial = app.session.clipboard_source.as_ref().map_or(0, |c| c.serial); + let transparency = app.session.settings.transparency_display; + draw_drag_gpu(&mut app.canvas, &painter, &xf, &st.doc, &drag, (&app.session.clipboard, serial), bg, transparency); + let list = rebuilt_preview(&st.doc, extra.iter(), scale); + draw_list(&painter, &xf, &list, bg, false); + } + } else if let (Some(c), true) = (app.canvas.cursor, app.session.prompting()) { + let ents = app.session.preview(c); + if !ents.is_empty() + && let Ok(st) = app.session.state() + { + let list = rebuilt_preview(&st.doc, ents.iter(), scale); + draw_list(&painter, &xf, &list, bg, false); + } + } + // Pending selection window. + if let (Some(pw), Some(c)) = (app.session.pending_window, raw_world) { + let a = xf.to_screen(pw.corner); + let b = xf.to_screen(c); + let r = Rect::from_two_pos(a, b); + let crossing = c.x < pw.corner.x; + if let Some(fill) = selection_area_fill(&app.session.settings, crossing) { + painter.rect_filled(r, 0.0, fill); + } + let st = Stroke::new(1.0, if crossing { t.crossing_stroke } else { t.window_stroke }); + if crossing { + let pts = vec![r.left_top(), r.right_top(), r.right_bottom(), r.left_bottom(), r.left_top()]; + painter.extend(Shape::dashed_line(&pts, st, 5.0, 3.0)); + } else { + painter.rect_stroke(r, 0.0, st, egui::StrokeKind::Middle); + } + } + // Acquired tracking points and the polar / tracking paths under the cursor. + if app.canvas.cursor.is_some() { + draw_tracking(&painter, &xf, &app.session.tracking.acquired, app.canvas.track.as_ref(), view.height * 4.0); + } + if let (Some(hit), Some(_)) = (app.canvas.snap, app.canvas.cursor) { + draw_snap_marker(&painter, xf.to_screen(hit.point), &hit); + } + draw_tiles(app, ui, tiles); + // UCS icon, ViewCube and viewport label. + if app.ui.show_ucs_icon { + draw_ucs_icon(&painter, rect); + crate::viewcube::ucs_icon(app, ui, rect); + } + if app.ui.show_viewcube && sheet.is_none() { + draw_viewcube(app, ui, rect); + } + viewport_label(&painter, rect); + // Typing and Escape reach the command line even when its overlay is hidden. + crate::cmdline::keyboard(app, ui.ctx()); + // Command line overlay. + if app.ui.show_command_line { + crate::cmdline::show(app, ui, full); + } +} + +/// The crosshair (or the system cursor) and the Dynamic Input tooltip. +/// +/// `paper_rect` is the paper-space sheet's screen rectangle (`None` in model space). +pub(super) fn cursor(app: &mut CadApp, ui: &mut egui::Ui, frame: &Frame, paper_rect: Option) { + let Frame { t, rect, hover_pos, inside, on_menu, selecting, .. } = *frame; + let painter = frame.painter.clone(); + // Crosshair cursor. + if inside && !on_menu { + // Hide the OS cursor under the drawn crosshair, unless assistive tech needs it (#39). + ui.ctx().set_cursor_icon(if app.ui.system_cursor { egui::CursorIcon::Crosshair } else { egui::CursorIcon::None }); + if let Some(hp) = hover_pos { + // Use the painted sheet even in MSPACE, where the editing transform follows a + // model viewport. The whole cursor takes one colour from its centre. + let ink = paper_rect.map_or(t.crosshair, |paper| crate::theme::crosshair_color(if paper.contains(hp) { t.paper } else { SURROUND })); + draw_crosshair(app, &painter, rect, hp, selecting, ink); + if app.canvas.cycle_badge && app.canvas.cycle.is_none() { + cycle_badge(&painter, hp); + } + } + } + if inside + && app.session.settings.dynmode + && let Some(hp) = hover_pos + { + // A stretched grip shows its dimensions instead of the pointer boxes; a prompt asking for + // a distance shows its one field and keeps the prompt and its options. + let fields = crate::grip_dyn::draw(app, &painter); + if !fields || app.session.current_prompt().is_some_and(|p| p.distance) { + dynamic_input(app, &painter, hp, fields); + } + } + // Over the crosshair, so the row under the pointer reads clearly. + cycle_list(app, &painter); +}