Fix occurrence Move/Copy viewport gizmo input (#3014) (#3015)

Thanks @amolinari-autotex.
This commit is contained in:
amolinari-autotex
2026-10-20 00:07:32 +02:00
committed by GitHub
parent 206fffdd80
commit 504d7c9488
4 changed files with 100 additions and 10 deletions
+20 -10
View File
@@ -10,6 +10,10 @@
#[path = "occurrence_ui_tests.rs"]
mod occurrence_ui_tests;
#[cfg(test)]
#[path = "occurrence_gizmo_tests.rs"]
mod occurrence_gizmo_tests;
#[path = "browser_model.rs"]
mod model;
@@ -1938,7 +1942,6 @@ fn occurrence_panel(app: &mut SolveApp, ui: &mut egui::Ui) {
}
});
});
triad(app, ui, &mut m);
if (m.translate, m.angles) != before {
apply_occurrence_move(app, &m);
}
@@ -1987,17 +1990,20 @@ pub fn apply_occurrence_move(app: &mut SolveApp, m: &OccMove) {
}
/// The shared move triad at the occurrence's origin: arrows and squares move it, rings turn it.
fn triad(app: &mut SolveApp, ui: &mut egui::Ui, m: &mut OccMove) {
pub(crate) fn occurrence_triad_model(app: &SolveApp) -> Option<crate::gizmo::Triad> {
let movement = app.tree.occurrence_move.as_ref()?;
let center = movement.pivot? + solidcraft_engine::geom::Vec3::new(movement.translate[0], movement.translate[1], movement.translate[2]);
Some(crate::gizmo::Triad { center, translate: true, rotate: true })
}
/// Occurrence handles belong to the viewport, not the clipped Move panel's child UI.
pub(crate) fn occurrence_triad(app: &mut SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, proj: &crate::viewport::Proj) {
use crate::gizmo::{self, Change};
let (Some(rect), Some(pivot)) = (app.viewport.rect, m.pivot) else { return };
let proj = crate::viewport::projection(app, rect);
let center = pivot + solidcraft_engine::geom::Vec3::new(m.translate[0], m.translate[1], m.translate[2]);
let layer = egui::LayerId::new(egui::Order::Middle, egui::Id::new("sc_occ_triad_layer"));
let mut gui = ui.new_child(egui::UiBuilder::new().layer_id(layer).max_rect(rect));
let painter = gui.painter().with_clip_rect(rect);
let t = gizmo::Triad { center, translate: true, rotate: true };
let Some(mut m) = app.tree.occurrence_move.clone() else { return };
let before = (m.translate, m.angles);
let Some(t) = occurrence_triad_model(app) else { return };
let step = crate::canvas::snap_step(app.cam.half_height());
let Some(g) = gizmo::show(&mut gui, &painter, &proj, egui::Id::new("sc_occ_triad"), &t, step) else { return };
let Some(g) = gizmo::show(ui, painter, proj, egui::Id::new("sc_occ_triad"), &t, step) else { return };
if g.started || m.drag0.is_none() {
m.drag0 = Some((m.translate, m.angles));
}
@@ -2013,6 +2019,10 @@ fn triad(app: &mut SolveApp, ui: &mut egui::Ui, m: &mut OccMove) {
if g.done {
m.drag0 = None;
}
if (m.translate, m.angles) != before {
apply_occurrence_move(app, &m);
}
app.tree.occurrence_move = Some(m);
}
/// Dimensions of finished sketches whose dimensions are shown (Show Dimensions), drawn over the
+6
View File
@@ -269,6 +269,9 @@ pub(crate) fn owns_pointer(app: &SolveApp, proj: &Proj, p: Pos2) -> bool {
// The triad only registers an egui hit widget after a handle is under the pointer.
// Test its geometry before navigation, including the first frame entering a handle.
app.dialog.as_ref().and_then(|d| crate::dialogs_move::triad(app, d)).is_some_and(|t| crate::gizmo::contains_pointer(proj, &t, p))
|| (app.dialog.is_none()
&& app.tool.is_none()
&& crate::browser::occurrence_triad_model(app).is_some_and(|t| crate::gizmo::contains_pointer(proj, &t, p)))
}
/// Draw the manipulator and the value box (called by the viewport after the model is drawn).
@@ -277,6 +280,9 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, proj
if app.applying.is_some() {
return;
}
if app.dialog.is_none() && app.tool.is_none() {
crate::browser::occurrence_triad(app, ui, painter, proj);
}
let Some(mut d) = app.dialog.take() else { return };
if matches!(d.kind, Kind::Assembly(_)) {
crate::dialogs_assembly::overlay(app, painter, proj, &mut d);
@@ -0,0 +1,67 @@
//! Regression for the existing occurrence Move/Copy arrows, independent of clipboard paste.
use crate::{
context_menu::{self, Target},
scenario::Harness,
};
use serde_json::json;
use solidcraft_engine::{Sel, doc, geom::Vec3};
#[test]
fn occurrence_gizmo_drag_matches_numeric_input_and_preserves_joints_capture_and_undo() {
let mut h = Harness::new();
let part = h.app.run("component.create", json!({"name":"Block"})).unwrap();
let first = part["occurrence"].as_u64().unwrap();
h.app.run("solid.box", json!({"length":10,"width":10,"height":10})).unwrap();
h.app.run("component.activate", json!({"component":0})).unwrap();
let second = h.app.run("occurrence.copy", json!({"occurrence":first,"translate":[50,0,0]})).unwrap()["occurrence"].as_u64().unwrap();
h.app
.run(
"joint.create",
json!({"name":"Fixed peer","type":"rigid","flip":true,"a":{"occurrence":0,"point":[50,0,0]},"b":{"occurrence":second,"point":[50,0,0]}}),
)
.unwrap();
let joints = serde_json::to_value(&h.app.session.doc.assembly.joints).unwrap();
let original = h.app.session.doc.to_json();
let initial = doc::apply_point(&h.app.session.occurrence_placement(&[first]).unwrap().world, Vec3::ZERO);
let peer = doc::apply_point(&h.app.session.occurrence_placement(&[second]).unwrap().world, Vec3::ZERO);
h.app.cam.target = initial;
h.app.cam.distance = 100.;
h.frames(3);
let target = Target::Occurrence { path: vec![first] };
let item = context_menu::items(&h.app, &target).into_iter().find(|item| item.id == "ui.moveOccurrence").unwrap();
context_menu::run_item(&mut h.app, &item, egui::Pos2::ZERO);
h.frames(3);
let camera = h.app.cam;
let start = crate::gizmo::handle_point("x", Vec3::ZERO, 60.).unwrap();
let end = start + Vec3::X * 12.;
h.step(&json!({"drag":[{"world":[start.x,start.y,start.z]},{"world":[end.x,end.y,end.z]}],"steps":12})).unwrap();
h.frames(3);
let arrow = doc::apply_point(&h.app.session.occurrence_placement(&[first]).unwrap().world, Vec3::ZERO);
assert!(arrow.x > initial.x + 1., "visible arrow must actually move this occurrence: {initial:?}->{arrow:?}");
assert_eq!(h.app.cam, camera);
assert_eq!(h.app.session.doc.to_json(), original, "drag is a pending position");
assert_eq!(doc::apply_point(&h.app.session.occurrence_placement(&[second]).unwrap().world, Vec3::ZERO), peer);
assert_eq!(serde_json::to_value(&h.app.session.doc.assembly.joints).unwrap(), joints);
h.app.run("component.revert_position", json!({"occurrences":[first]})).unwrap();
h.app.tree.occurrence_move.as_mut().unwrap().translate = [0.; 3];
h.frames(2);
let at = h.call("ui.at", json!({"handle":"occurrence-move:distance:0"}));
h.call("ui.click", json!({"x":at["result"][0],"y":at["result"][1],"double":true}));
h.frames(2);
h.step(&json!({"key":"A","cmd":true})).unwrap();
h.step(&json!({"text":format!("{}",arrow.x-initial.x)})).unwrap();
h.step(&json!({"key":"Enter"})).unwrap();
h.frames(3);
let numeric = doc::apply_point(&h.app.session.occurrence_placement(&[first]).unwrap().world, Vec3::ZERO);
assert!((numeric - arrow).len() < 1e-6, "numeric and arrow routes must agree: {numeric:?}, {arrow:?}");
let undo = h.app.session.undo.len();
h.app.run("component.capture_position", json!({})).unwrap();
h.app.tree.occurrence_move = None;
assert_eq!(h.app.session.undo.len(), undo + 1);
h.app.run("edit.undo", json!({})).unwrap();
assert_eq!(h.app.session.doc.to_json(), original);
h.app.run("edit.redo", json!({})).unwrap();
assert!((doc::apply_point(&h.app.session.occurrence_placement(&[first]).unwrap().world, Vec3::ZERO) - numeric).len() < 1e-6);
assert_eq!(serde_json::to_value(&h.app.session.doc.assembly.joints).unwrap(), joints);
assert!(!h.app.session.selection.contains(&Sel::Occurrence { path: vec![second] }));
}
+7
View File
@@ -0,0 +1,7 @@
# Occurrence Move/Copy arrows
Move/Copy draws and processes occurrence translation and rotation handles in the viewport that owns their pointer events. The numeric fields continue to use the same pending placement state. Capture Position commits the placement; Revert Position discards it.
This repairs ordinary occurrence editing as well as pasted linked designs. It does not change joint definitions or introduce free dragging of arbitrary faces. The clipboard pointer-placement contribution is separate (#2433); the arrow input repair is tracked in #3014 and #3015.
The regression exercises an ordinary occurrence, compares an actual viewport arrow drag with numeric input, checks that a second constrained occurrence and its joint remain unchanged, and checks capture, undo, and redo. It does not require clipboard functionality.