3D (Classic): a track cube drag ends when the cube is no longer drawn

A drag that was in progress when its dialog closed, or when the Rotation
tab or the 3D and Materials panel was left, stayed in memory, and Escape
was then held back in the next 3D dialog. Each drag now records the last
pass that drew the cube, and a drag the cube did not draw in the current
or previous pass is over.
This commit is contained in:
dotCooCoo
2026-11-14 23:04:38 -08:00
parent b306b73fb6
commit ba94ea65df
2 changed files with 55 additions and 8 deletions
+28 -1
View File
@@ -49,7 +49,7 @@ fn angles_stay_in_the_fields_range_to_a_hundredth() {
}
fn drag(grip: Grip, angles: [f64; 3], from: Pos2, to: Pos2, shift: bool) -> [f64; 3] {
let mut g = CubeDrag { grip, matrix: rotation(angles), origin: from, last: from, lock: None, fields: Default::default() };
let mut g = CubeDrag { grip, matrix: rotation(angles), origin: from, last: from, lock: None, fields: Default::default(), pass: 0 };
// In five steps, as a pointer moves.
let mut near = angles;
for k in 1..=5 {
@@ -108,6 +108,33 @@ fn a_face_drag_turns_the_front_toward_the_pointer_and_shift_keeps_one_axis() {
assert!(free[0].abs() > 1.0, "without Shift a slanted drag also tilts: {free:?}");
}
/// A gesture ends when the cube stops being drawn during it (its dialog closed, or the Rotation tab
/// was left), so Escape is not held back for a cube that is no longer there.
#[test]
fn a_drag_ends_when_the_cube_is_no_longer_drawn() {
let ctx = egui::Context::default();
let run = |draw: &mut dyn FnMut(&mut egui::Ui)| ctx.run_ui(Default::default(), draw).textures_delta.clear();
run(&mut |ui| {
let pass = ui.ctx().cumulative_pass_nr();
let drag = CubeDrag {
grip: Grip::Face,
matrix: rotation([0.0; 3]),
origin: Pos2::ZERO,
last: Pos2::ZERO,
lock: None,
fields: Default::default(),
pass,
};
ui.data_mut(|m| m.insert_temp(cube_drag_id(), drag));
assert!(cube_dragging(ui.ctx()));
});
assert!(cube_dragging(&ctx), "the next pass still sees the gesture");
for _ in 0..2 {
run(&mut |_| {});
}
assert!(!cube_dragging(&ctx), "two passes without the cube end the gesture");
}
/// The centre is on the front face, the ring's band on the ring, and an edge's middle on that edge.
#[test]
fn grips_follow_what_is_under_the_pointer() {
+27 -7
View File
@@ -273,6 +273,16 @@ struct CubeDrag {
lock: Option<usize>,
/// The settings before the gesture, which Escape puts back.
fields: serde_json::Map<String, Value>,
/// The last pass that drew the cube during the gesture.
pass: u64,
}
impl CubeDrag {
/// Was the cube drawn in this pass or the one before? When it was not (its dialog closed, or
/// the Rotation tab or the panel was left during the drag), the gesture is over.
fn live(&self, pass: u64) -> bool {
self.pass.saturating_add(1) >= pass
}
}
fn cube_drag_id() -> Id {
@@ -281,7 +291,8 @@ fn cube_drag_id() -> Id {
/// Is a track cube gesture in progress?
pub(super) fn cube_dragging(ctx: &egui::Context) -> bool {
ctx.data(|m| m.get_temp::<CubeDrag>(cube_drag_id())).is_some()
let pass = ctx.cumulative_pass_nr();
ctx.data(|m| m.get_temp::<CubeDrag>(cube_drag_id())).is_some_and(|g| g.live(pass))
}
/// The rotation after the pointer moves from `drag.last` to `p` on a cube about `center`.
@@ -361,24 +372,33 @@ pub(super) fn track_cube(ui: &mut egui::Ui, d: &mut Dialog) {
// A value being typed ends first; the cube then takes the keyboard focus below.
ui.ctx().memory_mut(|m| m.stop_text_input());
let matrix = rotation(now);
let drag = CubeDrag { grip: grip_at(matrix, center, origin), matrix, origin, last: origin, lock: None, fields: d.fields.clone() };
let pass = ui.ctx().cumulative_pass_nr();
let drag = CubeDrag { grip: grip_at(matrix, center, origin), matrix, origin, last: origin, lock: None, fields: d.fields.clone(), pass };
ui.data_mut(|m| m.insert_temp(cube_drag_id(), drag));
}
if enabled && (resp.drag_started() || resp.clicked()) {
resp.request_focus();
}
let pass = ui.ctx().cumulative_pass_nr();
let mut drag = ui.data(|m| m.get_temp::<CubeDrag>(cube_drag_id()));
if drag.as_ref().is_some_and(|g| !g.live(pass)) {
ui.data_mut(|m| m.remove::<CubeDrag>(cube_drag_id()));
drag = None;
}
if let Some(current) = &mut drag {
current.pass = pass;
if ui.input_mut(|i| i.consume_key(i.modifiers, egui::Key::Escape)) {
d.fields = current.fields.clone();
d.fields.remove(super::form::PREVIEWED);
ui.data_mut(|m| m.remove::<CubeDrag>(cube_drag_id()));
drag = None;
} else if let Some(p) = resp.interact_pointer_pos() {
let shift = ui.input(|i| i.modifiers.shift);
current.matrix = drag_turn(current, p, center, shift);
current.last = p;
next = Some(angles_of(current.matrix, now));
} else {
if let Some(p) = resp.interact_pointer_pos() {
let shift = ui.input(|i| i.modifiers.shift);
current.matrix = drag_turn(current, p, center, shift);
current.last = p;
next = Some(angles_of(current.matrix, now));
}
ui.data_mut(|m| m.insert_temp(cube_drag_id(), current.clone()));
}
}