PhotoCraft: An open-source, photo editor written by scratch in pure Rust
feat(ui): the shaped brush cursor turns and flips with the view (#4233)
FinnNkd3d735c6c773
diff --git a/crates/ui-egui/src/brush_cursor.rs b/crates/ui-egui/src/brush_cursor.rsindex 2cb30806a..c53fc5de6 100644--- a/crates/ui-egui/src/brush_cursor.rs+++ b/crates/ui-egui/src/brush_cursor.rs@@ -42,11 +42,25 @@ impl Key {}}-/// The brush cursor at `at`. `radius` is the tip's screen radius (size / 2 at the current zoom);-/// `normal` is Preferences › Cursors "Normal Brush Tip" (the 50 % contour, not the full tip).-pub(crate) fn cursor(painter: &Painter, at: Pos2, brush: &BrushSettings, radius: f32, normal: bool, centre: bool) -> CursorIcon {+/// The screen vectors of one tip radius (`size / 2` document pixels) along the document's x and+/// y axes: the view's zoom, rotation, flip and pixel aspect, which the outline follows as the+/// stroke does.+pub(crate) fn tip_axes(xf: &crate::canvas::ViewXform, size: f32) -> [Vec2; 2] {+ let r = if size.is_finite() { size.max(0.0) / 2.0 } else { 0.0 };+ [xf.map_vec(Vec2::new(r, 0.0)), xf.map_vec(Vec2::new(0.0, r))]+}++/// Rings in radius units (document orientation) on screen, through [`tip_axes`].+fn on_screen(rings: &[Vec<[f32; 2]>], axes: [Vec2; 2]) -> Vec<Vec<Vec2>> {+ rings.iter().map(|r| r.iter().map(|p| axes[0] * p[0] + axes[1] * p[1]).collect()).collect()+}++/// The brush cursor at `at`. `radius` is the tip's screen radius (size / 2 at the current zoom),+/// for the plain circle; `axes` comes from [`tip_axes`], for a traced outline. `normal` is+/// Preferences › Cursors "Normal Brush Tip" (the 50 % contour, not the full tip).+pub(crate) fn cursor(painter: &Painter, at: Pos2, brush: &BrushSettings, radius: f32, axes: [Vec2; 2], normal: bool, centre: bool) -> CursorIcon {if let Some(rings) = rings(painter.ctx(), brush, !normal) {- let pts: Vec<Vec<Vec2>> = rings.iter().map(|r| r.iter().map(|p| Vec2::new(p[0], p[1]) * radius).collect()).collect();+ let pts = on_screen(&rings, axes);let extent = pts.iter().flatten().fold(0.0f32, |m, p| m.max(p.length()));if extent < MIN_RADIUS {return crate::tool_cursor::crosshair(painter, at, 4.0, 0.0);@@ -73,3 +87,59 @@ fn rings(ctx: &egui::Context, brush: &BrushSettings, full: bool) -> Option<Arc<Vctx.data_mut(|d| d.insert_temp(id, Cached { key: Key::of(brush, full), rings: traced.clone() }));traced}++#[cfg(test)]+mod tests {+ use super::*;+ use crate::canvas::ViewXform;+ use egui::{Rect, pos2};++ fn view(rotation: f32, flip: bool, aspect: f32) -> ViewXform {+ ViewXform { rect: Rect::from_min_size(pos2(0.0, 0.0), egui::vec2(400.0, 300.0)), zoom: 2.0, center: [50.0, 40.0], flip, rotation, aspect }+ }++ fn close(a: Vec2, b: Vec2) -> bool {+ (a - b).length() < 1e-4+ }++ /// The axes are the screen offsets of the document points one radius right of and below the+ /// pointer, whatever the view does.+ #[test]+ fn tip_axes_follow_the_view_like_to_screen() {+ for (rotation, flip, aspect) in [(0.0, false, 1.0), (30.0, false, 1.0), (-90.0, true, 1.0), (45.0, true, 2.0), (200.0, false, 0.5)] {+ let xf = view(rotation, flip, aspect);+ let [ex, ey] = tip_axes(&xf, 20.0);+ let o = xf.to_screen(60.0, 30.0);+ assert!(close(ex, xf.to_screen(70.0, 30.0) - o), "x axis at {rotation}°, flip {flip}, aspect {aspect}");+ assert!(close(ey, xf.to_screen(60.0, 40.0) - o), "y axis at {rotation}°, flip {flip}, aspect {aspect}");+ }+ }++ /// An unrotated, unflipped view gives what upstream's radius scaling gave.+ #[test]+ fn a_plain_view_scales_the_outline_by_the_screen_radius() {+ let rings = vec![vec![[1.0, 0.0], [0.0, 0.5], [-1.0, 0.0]]];+ let pts = on_screen(&rings, tip_axes(&view(0.0, false, 1.0), 20.0));+ let want = [Vec2::new(20.0, 0.0), Vec2::new(0.0, 10.0), Vec2::new(-20.0, 0.0)];+ assert!(pts[0].iter().zip(want).all(|(a, b)| close(*a, b)), "{pts:?}");+ }++ /// A tip pointing along the document's x axis points down the screen in a view turned 90°,+ /// and left in a flipped one.+ #[test]+ fn the_outline_turns_and_flips_with_the_view() {+ let rings = vec![vec![[1.0, 0.0]]];+ let turned = on_screen(&rings, tip_axes(&view(90.0, false, 1.0), 20.0));+ assert!(close(turned[0][0], Vec2::new(0.0, 20.0)), "{turned:?}");+ let flipped = on_screen(&rings, tip_axes(&view(0.0, true, 1.0), 20.0));+ assert!(close(flipped[0][0], Vec2::new(-20.0, 0.0)), "{flipped:?}");+ }++ #[test]+ fn hostile_sizes_give_finite_axes() {+ for size in [f32::NAN, f32::INFINITY, -5.0, 0.0] {+ let [ex, ey] = tip_axes(&view(30.0, true, 1.0), size);+ assert!(ex.x.is_finite() && ex.y.is_finite() && ey.x.is_finite() && ey.y.is_finite(), "size {size}");+ }+ }+}diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 3c067c756..b053af1a4 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -864,6 +864,15 @@ impl ViewXform {vec2((d.x * c + d.y * s) / self.sx(), -d.x * s + d.y * c)}+ /// A document-space vector on screen: stretched, rotated, flipped and zoomed as the view+ /// draws it (the inverse of [`Self::unmap_vec`], with the zoom).+ pub fn map_vec(self, d: Vec2) -> Vec2 {+ let (c, s) = self.sincos();+ let dx = d.x * self.sx();+ let v = vec2(dx * c - d.y * s, dx * s + d.y * c) * self.zoom;+ if self.flip { vec2(-v.x, v.y) } else { v }+ }+pub fn to_screen(&self, x: f32, y: f32) -> Pos2 {let (c, s) = self.sincos();let dx = (x - self.center[0]) * self.sx();@@ -3575,10 +3584,12 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:egui::CursorIcon::None}// A shaped tip (sampled bitmap, or a rotated/flattened round tip)- // traces its outline; a plain round tip keeps the circle.+ // traces its outline, turned and flipped with the view; a plain round+ // tip keeps the circle._ => {let centre = brush_tip_centre(tool, alt || app.ui.shell.sticky_alt, cur.show_crosshair_in_brush_tip);- crate::brush_cursor::cursor(&painter, p, brush, full, cur.painting == PaintingCursor::NormalTip, centre)+ let axes = crate::brush_cursor::tip_axes(&xf, brush.size);+ crate::brush_cursor::cursor(&painter, p, brush, full, axes, cur.painting == PaintingCursor::NormalTip, centre)}}}diff --git a/docs/brush-parity.md b/docs/brush-parity.mdindex ad1ea5c83..4022f7a65 100644--- a/docs/brush-parity.md+++ b/docs/brush-parity.md@@ -1,6 +1,6 @@# Brush and painting parity-> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-12 · **Change:** minor (checkbox-only Brush Settings rows and non-destructive Smoothing switch) · **Target:** Adobe Photoshop 2026+> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-12 · **Change:** minor (checkbox-only Brush Settings rows and non-destructive Smoothing switch; shaped-tip cursor follows the view) · **Target:** Adobe Photoshop 2026Painting is where Photoshop users notice differences first, so it gets its own checklist: thebrush engine (`crates/paint/src/brush.rs`), the Brush Settings UI@@ -55,7 +55,7 @@ tablets against Photoshop side by side.| Interpolation and smoothing match Photoshop | partial | #2138, #2057 || Soft-edge falloff matches Photoshop's curve | partial | #2149, #1619 || Brush preview and cursor crisp, not pixelated | partial | #1433 |-| Cursor outline equals the painted footprint at every zoom | partial | #1761, #2364 |+| Cursor outline equals the painted footprint at every zoom | partial | #1761, #2364; shaped tips trace their outline (#3467), turned and flipped with a rotated or flipped view || Latency: dab to screen within a frame | partial | budgets P13/P16/P33 over (scorecard); big-brush lag #2619, #1994; cursor lag #2566, #1343 || No hitch at mouse-up on a long stroke | not measurable | P32 needs a frame harness || Live stroke for Brush and Eraser | done | |@@ -116,3 +116,4 @@ tablets against Photoshop side by side.| 2026-10-10 | major | First version, from a source audit and the open issues || 2026-10-11 | minor | `~` held with the Brush or Pencil erases with that tool's brush, and Clear joins their Mode menus (#3439) || 2026-10-12 | minor | Five Brush Settings switch rows toggle without selecting a properties pane; Smoothing has a default-on switch and retains its settings while disabled (#4283). No brush-feel estimate changed. |+| 2026-10-12 | minor | A shaped tip's cursor outline turns and flips with a rotated or flipped view, and follows pixel aspect correction |