Skip to content

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

fix(ui): guides draw over the Puppet Warp, Perspective Warp and Vanishing Point previews (#7781)

Gert Pellin1489dafb4a5b
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index db11fc730..cc50aba6a 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -3907,6 +3907,8 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
// The transform preview is image content: it goes under the grid and guides, which
// stay visible across a layer being transformed (#5086); the box is drawn later.
crate::transform_tool::draw_content(app, &painter, &xf);
+ // So are Puppet Warp's, Perspective Warp's and Vanishing Point's warped pixels (#7781).
+ crate::distort_ui::draw_content(app, &painter, &xf);
if app.ui.extras.grid && app.ui.view.extras {
crate::rulers::draw_grid(app, &painter, &xf, &doc);
}
@@ -8088,6 +8090,85 @@ mod tests {
assert!(guide < frame, "the box ({frame}) draws over the guide ({guide})");
}
+ #[test]
+ fn guides_draw_over_the_puppet_perspective_and_vanishing_point_previews() {
+ // #7781: like Free Transform's (#5086), the warped pixels of Puppet Warp and Perspective
+ // Warp and Vanishing Point's paste are image content. They were drawn with their pins and
+ // planes, after the guides, hiding every guide across them; the guides now go over them,
+ // the mesh lines, pins and planes over the guides, as in Photoshop.
+ for command in ["edit.puppetWarp", "edit.perspectiveWarp", photocraft_engine::vp_cmds::VP] {
+ let ctx = egui::Context::default();
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
+ app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
+ app.run("layer.new.layer", json!({})).unwrap();
+ app.run("select.rect", json!({"x": 100, "y": 100, "width": 100, "height": 60})).unwrap();
+ app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ if command == photocraft_engine::vp_cmds::VP {
+ app.run("edit.copy", json!({})).unwrap();
+ }
+ app.run("select.deselect", json!({})).unwrap();
+ app.run("view.newGuide", json!({"orientation": "vertical", "position": 150})).unwrap();
+ app.ui.extras.guides = true;
+ app.ui.extras.rulers = false;
+ app.sync_views();
+ crate::distort_ui::menu(&mut app, &ctx, command, &json!({})).expect("an on-canvas mode").expect("it starts");
+ if command == photocraft_engine::vp_cmds::VP {
+ // A plane over the document; the clipboard pastes onto it.
+ for (x, y) in [(20.0, 280.0), (380.0, 280.0), (300.0, 40.0), (100.0, 40.0)] {
+ crate::distort_ui::pointer(&mut app, ToolEvent::Down { x, y, pressure: 1.0 }, egui::Modifiers::NONE);
+ crate::distort_ui::pointer(&mut app, ToolEvent::Up { x, y }, egui::Modifiers::NONE);
+ }
+ }
+ let mut view = app.ui.views[0].clone();
+ view.zoom = 1.0;
+ view.center = [200.0, 150.0];
+ view.fit_pending = false;
+ view.fill_pending = false;
+ let rect = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0));
+ let input = egui::RawInput { screen_rect: Some(rect), ..Default::default() };
+ let mut out = ctx.run_ui(input, |ui| {
+ canvas_view(&mut app, ui, 0, rect, view.clone(), true);
+ });
+ out.textures_delta.clear();
+ // At 100 % centred on the document, screen and document coordinates coincide.
+ let layer = Rect::from_min_max(pos2(100.0, 100.0), pos2(200.0, 160.0));
+ // Puppet Warp's mesh reaches its Expansion (2 px) past the pixels.
+ let near = |r: Rect| (r.min - layer.min).length() < 4.0 && (r.max - layer.max).length() < 4.0;
+ let shapes: Vec<&egui::Shape> = out.shapes.iter().map(|s| &s.shape).collect();
+ // The warped pixels (or the paste): a textured mesh over the layer (or on the plane,
+ // well inside the document, unlike the checkerboard and document image).
+ let preview = shapes.iter().position(|s| match s {
+ egui::Shape::Mesh(m) if m.texture_id != egui::TextureId::default() => {
+ let r = m.calc_bounds();
+ if command == photocraft_engine::vp_cmds::VP {
+ r.width() > 10.0 && r.width() < 300.0 && r.min.x < 150.0 && r.max.x > 150.0
+ } else {
+ near(r)
+ }
+ }
+ _ => false,
+ });
+ // The guide: a vertical line at x = 150 across the whole view.
+ let is_guide = |s: &egui::Shape| matches!(s, egui::Shape::LineSegment { points, .. } if points.iter().all(|p| (p.x - 150.0).abs() < 0.01) && (points[1].y - points[0].y).abs() > 200.0);
+ let guide = shapes.iter().position(|s| is_guide(s));
+ // Over the guide: Puppet Warp's mesh lines, the Vanishing Point plane's outline.
+ let overlay = shapes.iter().position(|s| match s {
+ egui::Shape::LineSegment { points, .. } => !is_guide(s) && points.iter().all(|p| layer.expand(3.0).contains(*p)),
+ egui::Shape::Path(p) => p.closed && p.points.len() == 4,
+ _ => false,
+ });
+ let (Some(preview), Some(guide)) = (preview, guide) else {
+ panic!("{command}: all painted: preview {preview:?}, guide {guide:?}");
+ };
+ assert!(preview < guide, "{command}: the guide ({guide}) draws over the warped pixels ({preview})");
+ if command != "edit.perspectiveWarp" {
+ // Perspective Warp has no plane yet: nothing of its own over the guide.
+ let Some(overlay) = overlay else { panic!("{command}: its mesh or plane is drawn") };
+ assert!(guide < overlay, "{command}: the mesh or plane ({overlay}) draws over the guide ({guide})");
+ }
+ }
+ }
+
#[test]
fn brush_drags_paint_no_stand_in_shape_over_the_canvas() {
// #189: v0.1 drew the stroke being dragged as a foreground-coloured egui polyline as wide
diff --git a/crates/ui-egui/src/distort_ui.rs b/crates/ui-egui/src/distort_ui.rs
index 4987d8cc4..8f6412f48 100644
--- a/crates/ui-egui/src/distort_ui.rs
+++ b/crates/ui-egui/src/distort_ui.rs
@@ -226,7 +226,22 @@ pub fn keys(app: &mut PhotocraftApp, ctx: &egui::Context) -> bool {
false
}
-/// Canvas overlay for the on-canvas modes.
+/// The warped (or pasted) pixels of the open on-canvas mode: image content, which the canvas
+/// draws right after the document and under the grid and guides (#7781), as Free Transform's
+/// preview is (#5086). Its pins, planes and meshes are [`draw_overlay`], drawn over the guides.
+pub fn draw_content(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) {
+ if let Some(p) = &app.distort.puppet {
+ crate::puppet_ui::draw_content(p, painter, xf);
+ }
+ if let Some(p) = &app.distort.perspective {
+ crate::perspective_ui::draw_content(p, painter, xf);
+ }
+ if let Some(v) = &app.distort.vanishing {
+ crate::vp_ui::draw_content(v, painter, xf);
+ }
+}
+
+/// Canvas overlay for the on-canvas modes: pins, planes and meshes over the guides.
pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) {
if let Some(p) = &app.distort.puppet {
crate::puppet_ui::draw(p, painter, xf);
diff --git a/crates/ui-egui/src/perspective_ui.rs b/crates/ui-egui/src/perspective_ui.rs
index 8afe1113d..e39a791d4 100644
--- a/crates/ui-egui/src/perspective_ui.rs
+++ b/crates/ui-egui/src/perspective_ui.rs
@@ -255,7 +255,9 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, _mods: egui::Modifiers) {
}
}
-pub fn draw(s: &PerspSession, painter: &egui::Painter, xf: &ViewXform) {
+/// The warped pixels: image content, drawn under the grid and guides like the document so a
+/// guide stays visible across the layer being warped (#7781). The planes and pins are [`draw`].
+pub fn draw_content(s: &PerspSession, painter: &egui::Painter, xf: &ViewXform) {
let scr = |p: [f64; 2]| xf.to_screen(p[0] as f32, p[1] as f32);
let mut mesh = egui::Mesh::with_texture(s.texture.id());
let tint = Color32::from_white_alpha((s.opacity.clamp(0.0, 1.0) * 255.0) as u8);
@@ -273,6 +275,11 @@ pub fn draw(s: &PerspSession, painter: &egui::Painter, xf: &ViewXform) {
}
}
painter.add(mesh);
+}
+
+/// The planes, their grids and pins over the warped pixels ([`draw_content`]) and the guides.
+pub fn draw(s: &PerspSession, painter: &egui::Painter, xf: &ViewXform) {
+ let scr = |p: [f64; 2]| xf.to_screen(p[0] as f32, p[1] as f32);
let accent = crate::theme::Tokens::get(painter.ctx()).accent;
let warp = s.mode == PerspMode::Warp;
for p in &s.planes {
diff --git a/crates/ui-egui/src/puppet_ui.rs b/crates/ui-egui/src/puppet_ui.rs
index e233396cf..3508b46b2 100644
--- a/crates/ui-egui/src/puppet_ui.rs
+++ b/crates/ui-egui/src/puppet_ui.rs
@@ -318,7 +318,9 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) {
}
}
-pub fn draw(s: &PuppetSession, painter: &egui::Painter, xf: &ViewXform) {
+/// The warped pixels: image content, drawn under the grid and guides like the document so a
+/// guide stays visible across the layer being warped (#7781). The mesh lines and pins are [`draw`].
+pub fn draw_content(s: &PuppetSession, painter: &egui::Painter, xf: &ViewXform) {
let b = s.bounds;
let (w, h) = (f64::from(b.width()), f64::from(b.height()));
let mut mesh = egui::Mesh::with_texture(s.texture.id());
@@ -332,6 +334,10 @@ pub fn draw(s: &PuppetSession, painter: &egui::Painter, xf: &ViewXform) {
mesh.add_triangle(a as u32, bb as u32, c as u32);
}
painter.add(mesh);
+}
+
+/// The mesh lines and pins over the warped pixels ([`draw_content`]) and the guides.
+pub fn draw(s: &PuppetSession, painter: &egui::Painter, xf: &ViewXform) {
if s.show_mesh {
let line = Stroke::new(0.6, Color32::from_rgba_unmultiplied(40, 40, 40, 150));
let mut seen = std::collections::HashSet::new();
diff --git a/crates/ui-egui/src/vp_ui.rs b/crates/ui-egui/src/vp_ui.rs
index bd0353d67..3034c1c5f 100644
--- a/crates/ui-egui/src/vp_ui.rs
+++ b/crates/ui-egui/src/vp_ui.rs
@@ -438,18 +438,24 @@ pub fn escape(app: &mut PhotocraftApp) {
}
}
-pub fn draw(s: &VpSession, painter: &egui::Painter, xf: &ViewXform) {
+/// The paste on its plane: image content, drawn under the grid and guides like the document so a
+/// guide stays visible across it (#7781). The planes and their grids are [`draw`].
+pub fn draw_content(s: &VpSession, painter: &egui::Painter, xf: &ViewXform) {
let scr = |p: [f64; 2]| xf.to_screen(p[0] as f32, p[1] as f32);
- let tokens = crate::theme::Tokens::get(painter.ctx());
- let center = s.center();
- // The paste first, under the grids.
if let Some(i) = s.paste_plane()
- && s.planes.get(i).is_some_and(|p| p.check(center) != PlaneCheck::Invalid)
+ && s.planes.get(i).is_some_and(|p| p.check(s.center()) != PlaneCheck::Invalid)
&& let (Some((h, lu, lv)), Some(c)) = (s.frame(i), s.clip.as_ref())
&& let Some(ext) = s.paste_extent(lu, lv)
{
draw_paste(painter, &scr, &h, s.paste_at, ext, &c.texture);
}
+}
+
+/// The planes, their grids and nodes over the paste ([`draw_content`]) and the guides.
+pub fn draw(s: &VpSession, painter: &egui::Painter, xf: &ViewXform) {
+ let scr = |p: [f64; 2]| xf.to_screen(p[0] as f32, p[1] as f32);
+ let tokens = crate::theme::Tokens::get(painter.ctx());
+ let center = s.center();
for (i, p) in s.planes.iter().enumerate() {
let check = p.check(center);
let colour = match check {