diff --git a/crates/ui-canvas/src/canvas_controls/brush/mod.rs b/crates/ui-canvas/src/canvas_controls/brush/mod.rs index 5b8775f4e..25475f0c5 100644 --- a/crates/ui-canvas/src/canvas_controls/brush/mod.rs +++ b/crates/ui-canvas/src/canvas_controls/brush/mod.rs @@ -14,6 +14,9 @@ //! diameter, up and down the hardness ([`BrushLevel`]), reported as [`ResizeStep`]s. //! - [`TipFalloff`] and [`FalloffPreview`]: the tip's falloff and the canvas control that shows it with the brush //! circle round it. +//! - [`StrokeTrail`] and [`CoverageMask`]: the footprint of the stroke being painted, a coverage raster the tool +//! stamps and the control uploads by its changed rows, in a [`TrailTone`]. +//! - [`SourceMarker`]: the clone source's crosshair at a document point. //! //! A brush tool returns [`BrushCursor::cursor`] from `CanvasTool::cursor`. The app shows [`BrushResize::preview`] or //! [`BrushCursor::tip_cell`] at the canvas rect in the canvas body, and the tool hands diff --git a/crates/ui-canvas/src/canvas_controls/brush/stories.rs b/crates/ui-canvas/src/canvas_controls/brush/stories.rs index 2ebba918e..75dbeb456 100644 --- a/crates/ui-canvas/src/canvas_controls/brush/stories.rs +++ b/crates/ui-canvas/src/canvas_controls/brush/stories.rs @@ -3,8 +3,8 @@ //! Alt + right-drag resizes it with the falloff preview; Play runs either drag. use super::{ - BrushCursor, BrushCursorOptions, BrushDimensions, BrushResize, BrushResizeOptions, BrushTip, BrushTipOutline, FalloffPreview, PaintingCursor, - TipCell, TipFalloff, TipPose, TipRings, + BrushCursor, BrushCursorOptions, BrushDimensions, BrushResize, BrushResizeOptions, BrushTip, BrushTipOutline, CoverageMask, FalloffPreview, + PaintingCursor, SourceMarker, StrokeTrail, TipCell, TipFalloff, TipPose, TipRings, TrailTone, }; use crate::canvas::testing::canvas_story::{StoryDocument, on_canvas_view, story_view}; use crate::canvas::testing::tool_story::{ToolScript, ToolStory, tool_story}; @@ -17,7 +17,8 @@ use crate::testing::{Drive, StoryData, StoryOutput, story}; use craft_behavior::kurbo::{Point, Rect as DocRect, Vec2}; use craft_behavior::{CancelReason, CanvasView, Cursor, PaintedCursor, PointerButton}; use craft_keymap::Mods; -use egui::{Rect, Ui}; +use egui::{Id, Rect, Ui}; +use std::sync::{Arc, Mutex, PoisonError}; /// The zoom, in percent of one device pixel per document pixel. const ZOOM: f64 = 400.0; @@ -55,6 +56,42 @@ fn hard_tip(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { on_canvas_view(ui, brush_view, Some(&StoryDocument::STANDARD), |ui, rect, _| FalloffPreview::new(&falloff, CENTRE, RADIUS).show_at(ui, rect)) } +/// A drag-shaped mask whose stroke runs at height `y`. +fn stamped(y: f64) -> CoverageMask { + let mut mask = CoverageMask::new(DocRect::new(40.0, 60.0, 160.0, 140.0), 256); + mask.begin_stroke(); + mask.stamp_capsule(Point::new(60.0, y), Point::new(100.0, y + 6.0), 5.0); + mask.stamp_capsule(Point::new(100.0, y + 6.0), Point::new(140.0, y - 4.0), 5.0); + mask +} + +/// The footprint of one tone. The mask is kept across passes (stamped once), so the trail uploads only what changed. +fn footprint_of(ui: &mut Ui, name: &'static str, label: &'static str, tone: TrailTone, y: f64) -> StoryOutput { + let kept = ui.data_mut(|data| data.get_temp_mut_or_insert_with(Id::new(("footprint", name)), || Arc::new(Mutex::new(stamped(y)))).clone()); + let mut mask = kept.lock().unwrap_or_else(PoisonError::into_inner); + on_canvas_view(ui, brush_view, Some(&StoryDocument::STANDARD), |ui, rect, _| StrokeTrail::new(label, &mut *mask).tone(tone).show_at(ui, rect)) +} + +#[story(StrokeTrail<'static>, covers = [Default])] +fn footprint_light(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + footprint_of(ui, "light", "Light footprint", TrailTone::Light, 100.0) +} + +#[story(StrokeTrail<'static>, covers = [Default])] +fn footprint_shade(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + footprint_of(ui, "shade", "Shade footprint", TrailTone::Shade, 100.0) +} + +#[story(StrokeTrail<'static>, covers = [Default])] +fn footprint_danger(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + footprint_of(ui, "danger", "Danger footprint", TrailTone::Danger, 100.0) +} + +#[story(SourceMarker, covers = [Default])] +fn source(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + on_canvas_view(ui, brush_view, Some(&StoryDocument::STANDARD), |ui, rect, _| SourceMarker::new("Clone source", CENTRE).show_at(ui, rect)) +} + #[story(TipCell, covers = [Default])] fn square_cell(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { on_canvas_view(ui, brush_view, Some(&StoryDocument::STANDARD), |ui, rect, _| TipCell::new(CELL, false).show_at(ui, rect)) @@ -173,7 +210,7 @@ impl CanvasTool for BrushPass<'_> { *self.dimensions = step.dimensions; } let rows = self.brush.resize.readout_rows(&cx.units(), &cx.words::()); - cx.readout(rows); + cx.hud(rows); } fn release(&mut self, pointer: &ToolPointer, cx: &mut ToolContext<'_>) { diff --git a/crates/ui-canvas/src/canvas_controls/line_preview.rs b/crates/ui-canvas/src/canvas_controls/line_preview.rs index 2a3f4d6ed..f05c668b2 100644 --- a/crates/ui-canvas/src/canvas_controls/line_preview.rs +++ b/crates/ui-canvas/src/canvas_controls/line_preview.rs @@ -23,10 +23,16 @@ pub struct LineGuideStyle { line: StyleColor, /// The halo under the line, so it reads on any image. halo: StyleColor, + /// The radius of the dot at the line's start: the thick stroke, so the disc is twice that across. + dot: f32, } +/// The shortest line, in points on screen, that is drawn: a lag shorter than this shows nothing. +const MIN_LENGTH: f32 = 2.0; + /// The Shift-click line from `from` (the end of the last stroke) to `to` (the pointer), both in document units. A -/// non-finite end draws nothing. The brush tip at the pointer is the cursor's, not this line's. +/// non-finite end draws nothing. It draws nothing shorter than `MIN_LENGTH`, for the Smoothing string and the +/// Shift-click line alike. The brush tip at the pointer is the cursor's, not this line's. #[derive(crate::contract::Component, crate::contract::Props)] #[component(canvas_control)] pub struct StraightLinePreview { @@ -36,7 +42,6 @@ pub struct StraightLinePreview { to: Point, /// A filled disc in the line colour at `from`, twice the thick stroke wide: the brush end of the smoothing string. #[prop(flag)] - #[allow(dead_code)] // P3(tools-w17): cw.C36 dot: bool, } @@ -59,7 +64,12 @@ impl Component for StraightLinePreview { fn style(cx: &StyleCx<'_>, _: StraightLinePreviewStyleKey) -> LineGuideStyle { let overlay = &cx.tokens.color.overlay; - LineGuideStyle { metrics: Metrics::new(), line: cx.color(overlay.stroke), halo: cx.color(overlay.halo) } + LineGuideStyle { + metrics: Metrics::new(), + line: cx.color(overlay.stroke), + halo: cx.color(overlay.halo), + dot: cx.tokens.stroke.thick, + } } fn measure(&mut self, _: &MeasureCx<'_, Self>) -> egui::Vec2 { @@ -69,10 +79,17 @@ impl Component for StraightLinePreview { fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) { let finite = [self.from, self.to].iter().all(|point| point.x.is_finite() && point.y.is_finite()); let Some(space) = canvas_space(&cx.env).filter(|_| finite) else { return }; - let ends = PaintShape::Path(vec![space.view.to_screen(self.from), space.view.to_screen(self.to)]); + let (start, end) = (space.view.to_screen(self.from), space.view.to_screen(self.to)); + if start.distance(end) < MIN_LENGTH { + return; + } + let ends = PaintShape::Path(vec![start, end]); let draw = ui.draw(); draw.stroke(&ends, StrokeWidth::Thick, cx.style.halo); draw.stroke(&ends, StrokeWidth::Thin, cx.style.line); + if self.dot { + draw.fill(&PaintShape::Circle(start, cx.style.dot), cx.style.line); + } } fn access(&self, access: &mut Access, _: &Cx<'_, Self>) { diff --git a/crates/ui-canvas/src/canvas_controls/marquee/stories.rs b/crates/ui-canvas/src/canvas_controls/marquee/stories.rs index 045987241..96302d48a 100644 --- a/crates/ui-canvas/src/canvas_controls/marquee/stories.rs +++ b/crates/ui-canvas/src/canvas_controls/marquee/stories.rs @@ -55,6 +55,13 @@ fn shift_click_line(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { on_canvas(ui, |ui, at, _| StraightLinePreview::new("Straight line", Point::new(240.0, 235.0), Point::new(400.0, 155.0)).show_at(ui, at)) } +#[story(StraightLinePreview, covers = [Default], drive = Drive::Never)] +fn smoothing_string(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + on_canvas(ui, |ui, at, _| { + StraightLinePreview::new("Smoothing string", Point::new(240.0, 235.0), Point::new(400.0, 155.0)).dot().show_at(ui, at) + }) +} + /// The strokes the stroke story painted, each its points in document units. type Strokes = Vec>;