Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Perspective Warp: replacing the planes mid corner drag ends the drag (#952) (#1398)

kayrockscreenprinting4f2197d37ef0
diff --git a/crates/ui-egui/src/perspective_ui.rs b/crates/ui-egui/src/perspective_ui.rs
index 996df6a0c..9115b141e 100644
--- a/crates/ui-egui/src/perspective_ui.rs
+++ b/crates/ui-egui/src/perspective_ui.rs
@@ -104,11 +104,15 @@ impl PerspSession {
}
fn set_corner(&mut self, group: &[(usize, usize)], p: [f64; 2]) {
+ let layout = self.mode == PerspMode::Layout;
for &(pi, ci) in group {
- if self.mode == PerspMode::Layout {
- self.planes[pi].src[ci] = p;
+ let Some(pl) = self.planes.get_mut(pi) else { continue };
+ if layout && let Some(c) = pl.src.get_mut(ci) {
+ *c = p;
+ }
+ if let Some(c) = pl.dst.get_mut(ci) {
+ *c = p;
}
- self.planes[pi].dst[ci] = p;
}
}
}
@@ -180,6 +184,8 @@ pub fn control(app: &mut PhotocraftApp, ui: &Value) -> Result<Value, String> {
}
if let Some(p) = ui.get("planes") {
s.planes = serde_json::from_value(p.clone()).map_err(|e| format!("bad planes: {e}"))?;
+ // A corner drag holds (plane, corner) indices into the old list (#952).
+ s.drag = None;
s.update_grid();
}
if let Some(st) = ui.get("straighten").and_then(Value::as_str) {
@@ -376,4 +382,39 @@ mod tests {
commit(&mut app);
assert_eq!(app.session.active().unwrap().history.past_len(), before + 1);
}
+
+ #[test]
+ fn replacing_the_planes_mid_corner_drag_ends_the_drag() {
+ // #952: the drag kept (plane, corner) indices into the old list, so the next move or
+ // release indexed the new, shorter one and panicked.
+ for release in [false, true] {
+ let ctx = egui::Context::default();
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
+ app.session.execute("file.new", json!({"width": 100, "height": 80, "depth": 8})).unwrap();
+ app.session.execute("layer.new.layer", json!({})).unwrap();
+ app.session
+ .edit("paint", |doc, active| {
+ doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(20, 30, 80, 50), &[1.0, 0.0, 0.0, 1.0]);
+ Ok(())
+ })
+ .unwrap();
+ app.sync_views();
+ crate::distort_ui::menu(&mut app, &ctx, "edit.perspectiveWarp", &json!({})).unwrap().unwrap();
+ let ev = |app: &mut PhotocraftApp, e| crate::distort_ui::pointer(app, e, egui::Modifiers::NONE);
+ ev(&mut app, ToolEvent::Down { x: 20.0, y: 20.0, pressure: 1.0 });
+ ev(&mut app, ToolEvent::Move { x: 90.0, y: 70.0, pressure: 1.0 });
+ ev(&mut app, ToolEvent::Up { x: 90.0, y: 70.0 });
+ // Press a corner, then replace the planes while it's held.
+ ev(&mut app, ToolEvent::Down { x: 20.0, y: 20.0, pressure: 1.0 });
+ assert!(matches!(app.distort.perspective.as_ref().unwrap().drag, Some(Drag::Corner { .. })));
+ control(&mut app, &json!({"planes": []})).unwrap();
+ if release {
+ ev(&mut app, ToolEvent::Up { x: 30.0, y: 30.0 });
+ } else {
+ ev(&mut app, ToolEvent::Move { x: 30.0, y: 30.0, pressure: 1.0 });
+ }
+ let s = app.distort.perspective.as_ref().unwrap();
+ assert!(s.planes.is_empty() && s.drag.is_none(), "release {release}");
+ }
+ }
}