From 756524277b6a1febcb4be97d3f5fc2ec9138aa52 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 01:48:21 +0000 Subject: [PATCH] Brush cursor: centre mark and selection badge on the tip circle and outline --- crates/behavior/src/cursor.rs | 34 ++++++-- .../src/canvas_controls/brush/cursor.rs | 77 ++++++++++++------- .../tests/it/canvas_controls/brush.rs | 32 ++++++++ .../ui-canvas/tests/it/canvas_controls/mod.rs | 2 + crates/ui-contract/src/contract/ghost.rs | 10 ++- .../src/contract/ghost/pointers.rs | 13 +++- 6 files changed, 131 insertions(+), 37 deletions(-) create mode 100644 crates/ui-canvas/tests/it/canvas_controls/brush.rs diff --git a/crates/behavior/src/cursor.rs b/crates/behavior/src/cursor.rs index f18da3d8f..2fcb38933 100644 --- a/crates/behavior/src/cursor.rs +++ b/crates/behavior/src/cursor.rs @@ -94,17 +94,25 @@ pub enum CursorBadge { pub enum PaintedCursor { /// A crosshair. Crosshair, - /// A circle of `radius` points. + /// A circle of `radius` points: a round brush tip at this zoom. #[non_exhaustive] Circle { /// The radius. radius: f32, + /// A crosshair of [`CENTRE_ARM`](PaintedCursor::CENTRE_ARM) arms on the pointer. + centre: bool, + /// A mark centred on the pointer. + badge: Option, }, - /// App shapes, as closed rings around the pointer. + /// App shapes, as closed rings around the pointer: a shaped brush tip. #[non_exhaustive] Outline { /// The rings. rings: Vec>, + /// A crosshair of [`CENTRE_ARM`](PaintedCursor::CENTRE_ARM) arms on the pointer. + centre: bool, + /// A mark centred on the pointer. + badge: Option, }, /// An open hand. Hand, @@ -156,14 +164,28 @@ pub enum PaintedCursor { } impl PaintedCursor { - /// A circle of `radius` points. + /// The arm of the centre mark of a circle or an outline, in points. + pub const CENTRE_ARM: f32 = 3.0; + + /// A circle of `radius` points, with no centre mark and no badge. pub const fn circle(radius: f32) -> Self { - PaintedCursor::Circle { radius } + PaintedCursor::Circle { radius, centre: false, badge: None } } - /// App shapes as closed rings. + /// App shapes as closed rings, with no centre mark and no badge. pub fn outline(rings: Vec>) -> Self { - PaintedCursor::Outline { rings } + PaintedCursor::Outline { rings, centre: false, badge: None } + } + + /// A circle or an outline with the centre mark when `centre` and `badge` centred on the pointer; any other cursor + /// unchanged. + #[must_use] + pub fn marked(self, centre: bool, badge: Option) -> Self { + match self { + PaintedCursor::Circle { radius, .. } => PaintedCursor::Circle { radius, centre, badge }, + PaintedCursor::Outline { rings, .. } => PaintedCursor::Outline { rings, centre, badge }, + other => other, + } } /// A magnifier, zooming out when `out`. diff --git a/crates/ui-canvas/src/canvas_controls/brush/cursor.rs b/crates/ui-canvas/src/canvas_controls/brush/cursor.rs index 2d8c71339..d38713d23 100644 --- a/crates/ui-canvas/src/canvas_controls/brush/cursor.rs +++ b/crates/ui-canvas/src/canvas_controls/brush/cursor.rs @@ -90,8 +90,10 @@ impl BrushCursorOptions { /// 6. An outline whose farthest point is nearer than the hit target (`size.hit.target`, the arm of the crosshair the /// host paints) to the hotspot: the crosshair. /// -/// A [`badge`](Self::badge) rides on the Standard icon and on the crosshair. NaN, infinite, negative and zero sizes -/// never panic and never give a non-finite ring (they fall to rule 6). +/// A [`badge`](Self::badge) rides on whichever pointer rules 1 to 6 give: in the Standard icon's own slot, beside the +/// crosshair, centred on the hotspot inside a circle or an outline; the hidden pointer of a pixel tip has none. A +/// circle or an outline draws its centre mark while [`centre`](Self::centre) is on. NaN, infinite, negative and zero +/// sizes never panic and never give a non-finite ring (they fall to rule 6). #[derive(Clone, Debug)] pub struct BrushCursor<'a> { tip: &'a BrushTip, @@ -116,8 +118,9 @@ impl<'a> BrushCursor<'a> { self } - /// Whether an aliased tip shows the crosshair rather than hiding the pointer: the app's rule (Show Crosshair in - /// Brush Tip; Clone Stamp only while Alt is held; the Background Eraser always). Default off. + /// Whether the circle or outline draws its centre mark and an aliased tip shows the crosshair rather than hiding + /// the pointer: the app's rule (Show Crosshair in Brush Tip; Clone Stamp only while Alt is held; the Background + /// Eraser always). Default off. #[must_use] pub fn centre(mut self, on: bool) -> Self { self.centre = on; @@ -145,7 +148,7 @@ impl<'a> BrushCursor<'a> { self } - /// A mark on the Standard icon and on the crosshair: the + or - of the selection mode. Default none. + /// A mark on the pointer: the + or - of the selection mode. Default none. #[must_use] pub fn badge(mut self, badge: Option) -> Self { self.badge = badge; @@ -226,10 +229,10 @@ impl<'a> BrushCursor<'a> { // A round tip stays a plain circle while the mapped axes are equal and square. let anisotropic = (x.length_sq() - y.length_sq()).abs() > ISOTROPY_TOLERANCE || x.dot(y).abs() > ISOTROPY_TOLERANCE; if !anisotropic { - return PaintedCursor::circle(x.length()); + return PaintedCursor::circle(x.length()).marked(self.centre, self.badge); } } - PaintedCursor::outline(rings) + PaintedCursor::outline(rings).marked(self.centre, self.badge) } /// The aliased tip's cell as a canvas control, for the tool to show beside the hidden pointer; `None` for another @@ -363,26 +366,26 @@ pub struct CursorBitmap { pub hotspot: [u16; 2], } -/// What a bitmap cursor shows. +/// What a bitmap cursor shows; a circle and an outline with their centre mark when the flag is on. enum Shape<'a> { - Circle(f32), + Circle(f32, bool), Crosshair(f32), - Outline(&'a [Vec]), + Outline(&'a [Vec], bool), } impl CursorBitmap { - /// The image of a painted `cursor` (a circle, the crosshair or an outline) by signed-distance coverage per pixel, - /// so the line stays one hardware pixel crisp at every scale. `None` for another cursor, a shape that is not - /// finite, or one larger than `spec.max_side`. + /// The image of a painted `cursor` (a circle, the crosshair or an outline, with its centre mark) by signed-distance + /// coverage per pixel, so the line stays one hardware pixel crisp at every scale; a badge is the overlay host's to + /// paint. `None` for another cursor, a shape that is not finite, or one larger than `spec.max_side`. pub fn render(cursor: &PaintedCursor, spec: &CursorBitmapSpec) -> Option { let scale = spec.scale; if !scale.is_finite() || scale <= 0.0 { return None; } let shape = match cursor { - PaintedCursor::Circle { radius, .. } => Shape::Circle(*radius), + PaintedCursor::Circle { radius, centre, .. } => Shape::Circle(*radius, *centre), PaintedCursor::Crosshair => Shape::Crosshair(spec.crosshair), - PaintedCursor::Outline { rings, .. } => Shape::Outline(rings), + PaintedCursor::Outline { rings, centre, .. } => Shape::Outline(rings, *centre), _ => return None, }; let extent = extent(&shape)?; @@ -404,7 +407,7 @@ impl CursorBitmap { } }; match shape { - Shape::Outline(rings) => { + Shape::Outline(rings, centre) => { // Splat each segment into its bounding box instead of measuring every ring per pixel: tip outlines // may carry hundreds of points. let reach = (0.5 * spec.stroke + 1.0) * scale + 2.0; @@ -418,26 +421,34 @@ impl CursorBitmap { splat_segment(&mut dist, n, hotf, a.to_vec2() * scale, ring[(i + 1) % len].to_vec2() * scale, reach); } } + if centre { + let arm = PaintedCursor::CENTRE_ARM * scale; + splat_segment(&mut dist, n, hotf, Vec2::new(-arm, 0.0), Vec2::new(arm, 0.0), reach); + splat_segment(&mut dist, n, hotf, Vec2::new(0.0, -arm), Vec2::new(0.0, arm), reach); + } for (i, &d) in dist.iter().enumerate() { if d.is_finite() { put(i % n, i / n, line_coverage(d, scale, spec.stroke)); } } } - Shape::Circle(_) | Shape::Crosshair(_) => { - // Columns to evaluate in each row: the ring's two arcs for a circle, the whole row otherwise. + Shape::Circle(..) | Shape::Crosshair(_) => { + // Columns to evaluate in each row: the ring's two arcs and the centre mark for a circle, the whole row + // otherwise. let last = f32::from(side - 1); for row in 0..n { let dy = row as f32 - hotf; - let spans: [Option<(f32, f32)>; 2] = match shape { - Shape::Circle(radius) => { + let spans: [Option<(f32, f32)>; 3] = match shape { + Shape::Circle(radius, centre) => { let fringe = (0.5 * spec.stroke + 1.0) * scale + 0.5; - match ring_spans(dy, radius * scale, fringe) { + let [left, right] = match ring_spans(dy, radius * scale, fringe) { Some([l, r]) => [Some(l), Some(r)], None => [None, None], - } + }; + let arm = PaintedCursor::CENTRE_ARM * scale + fringe; + [left, right, (centre && dy.abs() <= arm).then_some((-arm, arm))] } - _ => [Some((-hotf, hotf)), None], + _ => [Some((-hotf, hotf)), None, None], }; for (a, b) in spans.into_iter().flatten() { let (c0, c1) = ((hotf + a).floor().clamp(0.0, last) as usize, (hotf + b).ceil().clamp(0.0, last) as usize); @@ -455,8 +466,9 @@ impl CursorBitmap { /// The reach of `shape` from the hotspot in points, `None` for a shape that cannot be drawn. fn extent(shape: &Shape<'_>) -> Option { match shape { - Shape::Circle(length) | Shape::Crosshair(length) => (length.is_finite() && *length > 0.0).then_some(*length), - Shape::Outline(rings) => { + Shape::Circle(radius, centre) => (radius.is_finite() && *radius > 0.0).then_some(with_centre(*radius, *centre)), + Shape::Crosshair(length) => (length.is_finite() && *length > 0.0).then_some(*length), + Shape::Outline(rings, centre) => { let mut farthest = 0.0f32; for p in rings.iter().flatten() { if !p.x.is_finite() || !p.y.is_finite() { @@ -464,11 +476,16 @@ fn extent(shape: &Shape<'_>) -> Option { } farthest = farthest.max(p.to_vec2().length()); } - (farthest > 0.0).then_some(farthest) + (farthest > 0.0).then_some(with_centre(farthest, *centre)) } } } +/// The reach `reach` of a shape, at least the centre mark's arm when it has one. +fn with_centre(reach: f32, centre: bool) -> f32 { + if centre { reach.max(PaintedCursor::CENTRE_ARM) } else { reach } +} + /// Distance to the nearest arm of a crosshair of arm `length` (round caps), in the units of `x` and `y`. fn cross_distance(x: f32, y: f32, length: f32) -> f32 { let horizontal = (x - x.clamp(0.0, length)).hypot(y); @@ -488,9 +505,13 @@ fn seg_distance(x: f32, y: f32, a: Vec2, b: Vec2) -> f32 { fn coverage(shape: &Shape<'_>, dx: f32, dy: f32, scale: f32, width: f32) -> (f32, f32) { let (x, y) = (dx.abs(), dy.abs()); match shape { - Shape::Circle(radius) => line_coverage((x.hypot(y) - radius * scale).abs(), scale, width), + Shape::Circle(radius, centre) => { + let ring = (x.hypot(y) - radius * scale).abs(); + let distance = if *centre { ring.min(cross_distance(x, y, PaintedCursor::CENTRE_ARM * scale)) } else { ring }; + line_coverage(distance, scale, width) + } Shape::Crosshair(length) => line_coverage(cross_distance(x, y, length * scale), scale, width), - Shape::Outline(_) => (0.0, 0.0), + Shape::Outline(..) => (0.0, 0.0), } } diff --git a/crates/ui-canvas/tests/it/canvas_controls/brush.rs b/crates/ui-canvas/tests/it/canvas_controls/brush.rs new file mode 100644 index 000000000..f6bb17a89 --- /dev/null +++ b/crates/ui-canvas/tests/it/canvas_controls/brush.rs @@ -0,0 +1,32 @@ +//! The brush cursor: a circle or an outline carries the centre mark and the selection-mode badge the tool asks for. + +use craft_behavior::kurbo::Point; +use craft_behavior::{CanvasView, Cursor, CursorBadge, PaintedCursor}; +use craft_ui::canvas::ToolContext; +use craft_ui::canvas_controls::brush::{BrushCursor, BrushCursorOptions, BrushTip, BrushTipOutline, PaintingCursor, TipRings}; +use craft_ui::testing::trial; +use egui::{Pos2, Rect, vec2}; + +/// The centre mark and badge of the cursor `tip` shows with `centre` and `badge`, or `None` for a pointer that is +/// neither a circle nor an outline. +fn marks(tip: &BrushTip, centre: bool, badge: Option) -> Option<(&'static str, bool, Option)> { + let rect = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 400.0)); + let view = CanvasView::new(rect, Point::new(200.0, 200.0), 1.0); + let cx = ToolContext::new(rect, None); + let options = BrushCursorOptions::new(PaintingCursor::NormalBrushTip); + match BrushCursor::new(tip).centre(centre).badge(badge).cursor(&cx, &options, &view) { + Cursor::Painted(PaintedCursor::Circle { centre, badge, .. }) => Some(("circle", centre, badge)), + Cursor::Painted(PaintedCursor::Outline { centre, badge, .. }) => Some(("outline", centre, badge)), + _ => None, + } +} + +#[trial] +fn a_tip_outline_carries_the_centre_mark_and_the_badge_the_tool_asks_for() { + let square = TipRings::new(vec![vec![[-1.0, -1.0], [1.0, -1.0], [1.0, 1.0], [-1.0, 1.0]]]); + let tips = [("circle", BrushTip::round(200.0, 80.0)), ("outline", BrushTip::new(200.0, BrushTipOutline::traced(square.clone(), square)))]; + for (kind, tip) in &tips { + assert_eq!(marks(tip, true, Some(CursorBadge::Add)), Some((*kind, true, Some(CursorBadge::Add))), "{kind}: centre on, + mode"); + assert_eq!(marks(tip, false, None), Some((*kind, false, None)), "{kind}: plain"); + } +} diff --git a/crates/ui-canvas/tests/it/canvas_controls/mod.rs b/crates/ui-canvas/tests/it/canvas_controls/mod.rs index 4be4b759a..0cae77564 100644 --- a/crates/ui-canvas/tests/it/canvas_controls/mod.rs +++ b/crates/ui-canvas/tests/it/canvas_controls/mod.rs @@ -1,5 +1,7 @@ //! The canvas controls: `Guides`, `SmartGuides`, selection outlines, grids and brush overlays on a `Canvas`. +mod brush; + mod grid; mod marquee; diff --git a/crates/ui-contract/src/contract/ghost.rs b/crates/ui-contract/src/contract/ghost.rs index 4e01b9f30..ea70d3c2d 100644 --- a/crates/ui-contract/src/contract/ghost.rs +++ b/crates/ui-contract/src/contract/ghost.rs @@ -48,8 +48,14 @@ fn paint_pointer(ctx: &Context, env: &Env, painter: &egui::Painter) { let ink = crate::style::ink_color(&env.tokens, Ink::OnDark); let edge = env.tokens.stroke.thin; match &cursor { - PaintedCursor::Outline { rings, .. } => pointers::outline(&pen, rings), - PaintedCursor::Circle { radius, .. } => pointers::circle(&pen, *radius, edge, ink), + PaintedCursor::Outline { rings, centre, badge, .. } => { + pointers::outline(&pen, rings); + pointers::tip_marks(&pen, *centre, *badge, pen.thin, pen.fill); + } + PaintedCursor::Circle { radius, centre, badge, .. } => { + pointers::circle(&pen, *radius, edge, ink); + pointers::tip_marks(&pen, *centre, *badge, edge, ink); + } PaintedCursor::Crosshair => pointers::crosshair(&pen, env.tokens.size.icon.sm, edge, ink), PaintedCursor::Hand => pointers::hand(&pen, pointer::frame(ctx).buttons.contains(craft_behavior::Buttons::PRIMARY)), PaintedCursor::Zoom { out, .. } => pointers::zoom(&pen, *out), diff --git a/crates/ui-contract/src/contract/ghost/pointers.rs b/crates/ui-contract/src/contract/ghost/pointers.rs index 407fd37da..e7c094212 100644 --- a/crates/ui-contract/src/contract/ghost/pointers.rs +++ b/crates/ui-contract/src/contract/ghost/pointers.rs @@ -4,7 +4,7 @@ use crate::primitives::{Draw, IconSpec}; use crate::style::StyleColor; -use craft_behavior::{CursorBadge, IconId}; +use craft_behavior::{CursorBadge, IconId, PaintedCursor}; use egui::{Color32, Painter, Pos2, Rect, Shape, Stroke, Vec2}; /// The icon sets draw on a box this many units on a side; a tool pointer's hotspot is in these. @@ -141,6 +141,17 @@ pub(super) fn crosshair(pen: &Pen<'_>, arm: f32, width: f32, color: Color32) { pen.lines(width, color, &[Mark::Line(v([-arm, 0.0]), v([arm, 0.0])), Mark::Line(v([0.0, -arm]), v([0.0, arm]))]); } +/// A brush tip's marks on the pointer: the centre mark when `centre`, a crosshair of `PaintedCursor::CENTRE_ARM` arms +/// in the tip's own line of `width` in `color`, and `badge` centred over it. +pub(super) fn tip_marks(pen: &Pen<'_>, centre: bool, badge: Option, width: f32, color: Color32) { + if centre { + crosshair(pen, PaintedCursor::CENTRE_ARM, width, color); + } + if let Some(badge) = badge { + self::badge(pen, Vec2::ZERO, badge); + } +} + /// Rings of a brush tip, as offsets from the pointer. pub(super) fn outline(pen: &Pen<'_>, rings: &[Vec]) { for ring in rings.iter().filter(|ring| ring.len() >= 2) {