From ef628d1c86b948225f1590f259cf0f3cf8b52eb5 Mon Sep 17 00:00:00 2001 From: SilentWolf75 <134335235+SilentWolf75@users.noreply.github.com> Date: Fri, 30 Oct 2026 04:25:52 +0000 Subject: [PATCH] Canvas: pick Calibrate points on the image (#9108) (#9208) * claim: #9108 Co-authored-by: Cursor * Canvas calibrate: pick Point A and B with viewport clicks (#9108). The Calibrate card only had typed coordinates, so users could not mark the image. Clicks on the canvas now fill A then B, with on-image markers and a short hint. Co-authored-by: Cursor * Calibrate canvas: ja/zh/ar/he/vi strings for the click hints Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Cursor Co-authored-by: bherbruck Co-authored-by: Claude Opus 5.5 --- assets/locales/en.json | 2 + .../pending/canvas-calibrate-click.json | 22 +++ crates/ui-egui/src/browser.rs | 4 +- crates/ui-egui/src/browser_canvas.rs | 128 +++++++++++++++++- crates/ui-egui/src/context_menu.rs | 6 +- crates/ui-egui/src/viewport.rs | 5 +- 6 files changed, 163 insertions(+), 4 deletions(-) create mode 100644 assets/locales/pending/canvas-calibrate-click.json diff --git a/assets/locales/en.json b/assets/locales/en.json index f62a5d564..f0fc082d4 100644 --- a/assets/locales/en.json +++ b/assets/locales/en.json @@ -775,6 +775,8 @@ "at the face": "at the face", "bom.group_children": "Sub-parts in their part's row", "browser.body_name": "Body{number}", + "browser.calibrate_click_a": "Click the image for Point A, or type its coordinates.", + "browser.calibrate_click_b": "Click the image for Point B, or type its coordinates.", "browser.calibrate_title": "Calibrate: {name}", "browser.capture_many": "{count} components were moved but their positions aren't captured.", "browser.capture_note": "Capture keeps the new position; Revert puts it back.", diff --git a/assets/locales/pending/canvas-calibrate-click.json b/assets/locales/pending/canvas-calibrate-click.json new file mode 100644 index 000000000..f42767acb --- /dev/null +++ b/assets/locales/pending/canvas-calibrate-click.json @@ -0,0 +1,22 @@ +{ + "ja": { + "browser.calibrate_click_a": "画像をクリックして点Aを指定するか、座標を入力してください。", + "browser.calibrate_click_b": "画像をクリックして点Bを指定するか、座標を入力してください。" + }, + "zh": { + "browser.calibrate_click_a": "点击图像选取点 A,或输入其坐标。", + "browser.calibrate_click_b": "点击图像选取点 B,或输入其坐标。" + }, + "ar": { + "browser.calibrate_click_a": "انقر على الصورة لتحديد النقطة A، أو اكتب إحداثياتها.", + "browser.calibrate_click_b": "انقر على الصورة لتحديد النقطة B، أو اكتب إحداثياتها." + }, + "he": { + "browser.calibrate_click_a": "לחצו על התמונה כדי לבחור את נקודה A, או הקלידו את הקואורדינטות שלה.", + "browser.calibrate_click_b": "לחצו על התמונה כדי לבחור את נקודה B, או הקלידו את הקואורדינטות שלה." + }, + "vi": { + "browser.calibrate_click_a": "Nhấp vào ảnh để chọn điểm A, hoặc nhập tọa độ của điểm đó.", + "browser.calibrate_click_b": "Nhấp vào ảnh để chọn điểm B, hoặc nhập tọa độ của điểm đó." + } +} diff --git a/crates/ui-egui/src/browser.rs b/crates/ui-egui/src/browser.rs index 3fa35fe53..28b7f9708 100644 --- a/crates/ui-egui/src/browser.rs +++ b/crates/ui-egui/src/browser.rs @@ -2098,6 +2098,8 @@ pub struct CanvasPanel { pub a: [f64; 2], pub b: [f64; 2], pub distance: f64, + /// Next viewport click sets Point A (`0`) or Point B (`1`) (#9108). + pub next: u8, } impl CanvasPanel { @@ -2111,7 +2113,7 @@ impl CanvasPanel { .find(|c| c.id == canvas) .map(|c| ([c.center.x - c.width / 2.0, c.center.y], [c.center.x + c.width / 2.0, c.center.y], c.width)) .unwrap_or(([0.0, 0.0], [100.0, 0.0], 100.0)); - CanvasPanel { canvas, calibrate, depth: app.session.undo.len(), a, b, distance: d } + CanvasPanel { canvas, calibrate, depth: app.session.undo.len(), a, b, distance: d, next: 0 } } } diff --git a/crates/ui-egui/src/browser_canvas.rs b/crates/ui-egui/src/browser_canvas.rs index 96e4337f8..dc1c28114 100644 --- a/crates/ui-egui/src/browser_canvas.rs +++ b/crates/ui-egui/src/browser_canvas.rs @@ -1,12 +1,17 @@ //! The Edit Canvas / Calibrate card the Browser opens for a reference image: position, size, //! angle, opacity and flip (live, one undo step per opening), or two points on the image and -//! their true distance. +//! their true distance. Calibrate accepts clicks in the viewport on the image (#9108). +use egui::{Pos2, Stroke, vec2}; use serde_json::{Value, json}; +use solidcraft_engine::doc::canvas::Canvas; +use solidcraft_engine::geom::Vec2; use crate::SolveApp; use crate::browser::panel_pos; +use crate::theme::Tokens; use crate::ui_kit; +use crate::viewport::Proj; pub(crate) fn panel(app: &mut SolveApp, ctx: &egui::Context) { let Some(mut m) = app.tree.canvas_panel.clone() else { return }; @@ -26,6 +31,11 @@ pub(crate) fn panel(app: &mut SolveApp, ctx: &egui::Context) { .actions(ok, cancel, None) .show(ctx, true, |ui| { if calibrate { + ui_kit::text( + ui, + crate::i18n::tr(if m.next == 0 { "browser.calibrate_click_a" } else { "browser.calibrate_click_b" }), + ui_kit::Text::Note("calibrate_hint"), + ); for (label, p) in [("Point A", &mut m.a), ("Point B", &mut m.b)] { ui_kit::row(ui, crate::i18n::tr_label(label), |ui| { let [x, y] = p; @@ -74,6 +84,89 @@ pub(crate) fn panel(app: &mut SolveApp, ctx: &egui::Context) { app.tree.canvas_panel = if closed || action == ui_kit::Action::Cancel { None } else { Some(m) }; } +/// A viewport click while Calibrate is open: set Point A or B on the image (#9108). +pub(crate) fn click(app: &mut SolveApp, proj: &Proj, screen: Pos2) -> bool { + let Some(mut panel) = app.tree.canvas_panel.clone() else { + return false; + }; + if !panel.calibrate { + return false; + } + let doc = &app.session.doc; + let Some(c) = doc.canvases.iter().find(|c| c.id == panel.canvas).cloned() else { + return false; + }; + let (vals, _) = doc.param_values(); + let Ok(plane) = doc.canvas_plane(&vals, &c) else { + return false; + }; + let (origin, direction) = proj.ray(screen); + let Some(world) = plane.intersect_ray(origin, direction) else { + return false; + }; + let local = plane.to_local(world); + if !on_image(&c, local) { + return false; + } + let point = [local.x, local.y]; + if panel.next == 0 { + panel.a = point; + panel.next = 1; + app.set_status(crate::i18n::tr("browser.calibrate_click_b"), false); + } else { + panel.b = point; + panel.next = 0; + app.set_status(crate::i18n::tr("browser.calibrate_click_a"), false); + } + app.tree.canvas_panel = Some(panel); + true +} + +/// Markers for the two calibration points on the image. +pub(crate) fn paint_marks(app: &SolveApp, painter: &egui::Painter, proj: &Proj) { + let Some(panel) = app.tree.canvas_panel.as_ref().filter(|p| p.calibrate) else { + return; + }; + let doc = &app.session.doc; + let Some(c) = doc.canvases.iter().find(|c| c.id == panel.canvas) else { + return; + }; + let (vals, _) = doc.param_values(); + let Ok(plane) = doc.canvas_plane(&vals, c) else { + return; + }; + let t = Tokens::get(); + for (i, point) in [panel.a, panel.b].into_iter().enumerate() { + let Some(screen) = proj.to_screen(plane.to_world(Vec2::new(point[0], point[1]))) else { + continue; + }; + let active = panel.next as usize == i; + let stroke = Stroke::new(if active { 2.0 } else { 1.5 }, if active { t.accent } else { t.sel_edge }); + painter.circle_stroke(screen, 7.0, stroke); + painter.circle_filled(screen, 2.5, if active { t.accent } else { t.sel_vertex }); + let label = if i == 0 { "A" } else { "B" }; + painter.text( + screen + vec2(10.0, -10.0), + egui::Align2::LEFT_BOTTOM, + label, + egui::FontId::proportional(t.font_body), + if active { t.accent } else { t.text }, + ); + } +} + +fn on_image(c: &Canvas, p: Vec2) -> bool { + let [bl, br, _, tl] = c.corners(); + let (u, v) = (br - bl, tl - bl); + let (uu, vv) = (u.dot(u), v.dot(v)); + if !(uu > 1e-18 && vv > 1e-18) { + return true; + } + let w = p - bl; + let (s, t) = (w.dot(u) / uu, w.dot(v) / vv); + (-0.05..=1.05).contains(&s) && (-0.05..=1.05).contains(&t) +} + #[cfg(test)] mod tests { use serde_json::json; @@ -126,4 +219,37 @@ mod tests { assert!(h.app.tree.canvas_panel.is_none()); assert!((width(&h) - 40.0).abs() < 1e-6, "calibrating to half the distance halves the image: {}", width(&h)); } + + /// Viewport clicks on the image fill Point A then Point B (#9108). + #[test] + fn calibrate_picks_two_points_from_viewport_clicks() { + let mut h = crate::scenario::Harness::new(); + h.app.home.open = false; + let id = canvas(&mut h); + h.app.tree.canvas_panel = Some(crate::browser::CanvasPanel::new(&h.app, id, true)); + h.frames(6); + let rect = h.app.viewport.rect.expect("viewport"); + let proj = crate::viewport::projection(&h.app, rect); + let c = h.app.session.doc.canvases[0].clone(); + let (vals, _) = h.app.session.doc.param_values(); + let plane = h.app.session.doc.canvas_plane(&vals, &c).unwrap(); + let ax = c.center.x - 20.0; + let bx = c.center.x + 20.0; + let sa = proj.to_screen(plane.to_world(solidcraft_engine::geom::Vec2::new(ax, c.center.y))).expect("point A on screen"); + let sb = proj.to_screen(plane.to_world(solidcraft_engine::geom::Vec2::new(bx, c.center.y))).expect("point B on screen"); + assert!(super::click(&mut h.app, &proj, sa), "first click sets A"); + let panel = h.app.tree.canvas_panel.as_ref().unwrap(); + assert_eq!(panel.next, 1); + assert!((panel.a[0] - ax).abs() < 0.5, "{:?}", panel.a); + assert!(super::click(&mut h.app, &proj, sb), "second click sets B"); + let panel = h.app.tree.canvas_panel.as_ref().unwrap(); + assert_eq!(panel.next, 0); + assert!((panel.b[0] - bx).abs() < 0.5, "{:?}", panel.b); + let before = panel.a; + let miss = plane.to_world(solidcraft_engine::geom::Vec2::new(c.center.x + 500.0, c.center.y)); + if let Some(sm) = proj.to_screen(miss) { + assert!(!super::click(&mut h.app, &proj, sm)); + assert_eq!(h.app.tree.canvas_panel.as_ref().unwrap().a, before); + } + } } diff --git a/crates/ui-egui/src/context_menu.rs b/crates/ui-egui/src/context_menu.rs index 84842b57d..b072194e5 100644 --- a/crates/ui-egui/src/context_menu.rs +++ b/crates/ui-egui/src/context_menu.rs @@ -1721,7 +1721,11 @@ pub fn run_item(app: &mut SolveApp, item: &Item, at: Pos2) { } "ui.editCanvas" | "ui.calibrateCanvas" => { if let Some(c) = id_of(&p, "canvas") { - app.tree.canvas_panel = Some(crate::browser::CanvasPanel::new(app, c, item.id == "ui.calibrateCanvas")); + let calibrate = item.id == "ui.calibrateCanvas"; + app.tree.canvas_panel = Some(crate::browser::CanvasPanel::new(app, c, calibrate)); + if calibrate { + app.set_status(crate::i18n::tr("browser.calibrate_click_a"), false); + } } } "ui.newGroup" | "ui.ungroup" | "ui.groupSelected" => crate::browser::group_action(app, &item.id, &p), diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 51313607f..3736bd43a 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -4451,7 +4451,9 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { dialog.restore_value_focus = true; } if resp.clicked() && !held && !reference_pick && !joint_handle_pointer && !app.viewport.draw_drag_handled { - if app.tool.is_some() { + if crate::browser_canvas::click(app, &proj, p) { + // Calibrate Canvas: the click sets Point A or B on the image (#9108). + } else if app.tool.is_some() { crate::tools::on_click(app, &proj, p, mods.shift); } else if app.dialog.as_ref().is_some_and(|d| d.wants_picks()) { if let Some((hit, Some(sel))) = cand { @@ -4824,6 +4826,7 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) { } crate::preview::paint_sketches(app, &painter, &proj); crate::ref_images::show(app, ui, &painter, &proj); + crate::browser_canvas::paint_marks(app, &painter, &proj); crate::point_clouds::show(app, &painter, &proj); crate::dialogs_assembly::origins(app, &painter, &proj); if !crate::joint_handles::visible(app) {