From dbd8ec10c6a906d9c022129a086bcfd4bdec9d8d Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 13 Nov 2026 01:28:17 +0000 Subject: [PATCH] RasterDisplay extent: tiled copies and a region past the document, for source and painted pictures --- crates/pages/src/ui/view/pictures.rs | 2 +- crates/ui-canvas/src/canvas/mod.rs | 2 +- crates/ui-canvas/src/canvas/raster/detail.rs | 25 ++- crates/ui-canvas/src/canvas/raster/mod.rs | 174 +++++++++++++++---- crates/ui-canvas/src/canvas/raster_source.rs | 10 +- crates/ui-canvas/tests/it/canvas/raster.rs | 129 +++++++++++++- 6 files changed, 294 insertions(+), 48 deletions(-) diff --git a/crates/pages/src/ui/view/pictures.rs b/crates/pages/src/ui/view/pictures.rs index f3110516c..be3a5a747 100644 --- a/crates/pages/src/ui/view/pictures.rs +++ b/crates/pages/src/ui/view/pictures.rs @@ -246,7 +246,7 @@ impl Detail { px(seen.max.y, page.min.y, true), ); // One page pixel is one device pixel, so the plan's zoom is 1. - let plan = DetailPlan::of(size, side, 1.0, visible)?; + let plan = DetailPlan::of(PixelRect::of_size(size), side, 1.0, visible)?; let r = plan.region; let (w, h) = (size.width as f32, size.height as f32); let part = Rect::from_min_max(pos2(r.x0 as f32 / w, r.y0 as f32 / h), pos2(r.x1 as f32 / w, r.y1 as f32 / h)); diff --git a/crates/ui-canvas/src/canvas/mod.rs b/crates/ui-canvas/src/canvas/mod.rs index 2b998bc43..d828c055a 100644 --- a/crates/ui-canvas/src/canvas/mod.rs +++ b/crates/ui-canvas/src/canvas/mod.rs @@ -51,7 +51,7 @@ pub use words::SplitWords; mod app_paint; mod raster; pub use app_paint::AppPaint; -pub use raster::{DetailPlan, RasterDisplay, RasterDisplayOutput, RasterStyle, snap_to_texels}; +pub use raster::{DetailPlan, Extent, RasterDisplay, RasterDisplayOutput, RasterStyle, snap_to_texels}; // l4d-space mod opacity_keys; diff --git a/crates/ui-canvas/src/canvas/raster/detail.rs b/crates/ui-canvas/src/canvas/raster/detail.rs index 6cb2cce04..ba1df83bc 100644 --- a/crates/ui-canvas/src/canvas/raster/detail.rs +++ b/crates/ui-canvas/src/canvas/raster/detail.rs @@ -3,7 +3,7 @@ use crate::canvas::PixelRect; use crate::primitives::texture_factor; -use craft_behavior::{CanvasView, PixelSize}; +use craft_behavior::CanvasView; /// The detail region snaps outward to multiples of this many texels. const DETAIL_QUANTUM: i32 = 256; @@ -38,14 +38,13 @@ fn proxy_factor(device_zoom: f64) -> u32 { impl DetailPlan { /// The plan for a view at `device_zoom` (device pixels per document pixel) over `visible`, or `None` when the - /// base texture is already as fine as the screen or nothing is in view. `size` is the document and `side` the - /// device's texture side. - pub fn of(size: PixelSize, side: u32, device_zoom: f64, visible: PixelRect) -> Option { + /// base texture is already as fine as the screen or nothing is in view. `bounds` is what the base texture holds + /// (the document, or a region reaching past it) and `side` the device's texture side. + pub fn of(bounds: PixelRect, side: u32, device_zoom: f64, visible: PixelRect) -> Option { if !device_zoom.is_finite() || device_zoom <= 0.0 { return None; } - let base = texture_factor(size, side); - let bounds = PixelRect::of_size(size); + let base = texture_factor(bounds.size(), side); let seen = visible.intersect(&bounds); if seen.is_empty() { return None; @@ -69,17 +68,17 @@ impl DetailPlan { None } - /// The plan for `view` over a document of `size`: the document pixels it shows, and the device zoom of its - /// finer axis at `pixels_per_point`. - pub fn in_view(view: &CanvasView, size: PixelSize, side: u32, pixels_per_point: f32) -> Option { + /// The plan for `view` over a base texture of `bounds`: the pixels of it the view shows, and the device zoom of + /// its finer axis at `pixels_per_point`. + pub fn in_view(view: &CanvasView, bounds: PixelRect, side: u32, pixels_per_point: f32) -> Option { let device_zoom = view.zoom.max(view.zoom_x()) * f64::from(pixels_per_point); - Self::of(size, side, device_zoom, visible_pixels(view, size)) + Self::of(bounds, side, device_zoom, visible_pixels(view, bounds)) } } /// The document pixels `view` shows (its bounding box when turned), rounded outward with a margin for filtering and -/// clipped to a document of `size`. -fn visible_pixels(view: &CanvasView, size: PixelSize) -> PixelRect { +/// clipped to `bounds`. +fn visible_pixels(view: &CanvasView, bounds: PixelRect) -> PixelRect { let shown = view.visible(); let clamp = |n: f64| n.clamp(-1e9, 1e9) as i32; let seen = PixelRect::new( @@ -88,7 +87,7 @@ fn visible_pixels(view: &CanvasView, size: PixelSize) -> PixelRect { clamp(shown.x1.ceil()) + FILTER_MARGIN, clamp(shown.y1.ceil()) + FILTER_MARGIN, ); - seen.intersect(&PixelRect::of_size(size)) + seen.intersect(&bounds) } /// `region` grown outward to whole texels of `factor` pixels counted from `origin`, the document pixel where the diff --git a/crates/ui-canvas/src/canvas/raster/mod.rs b/crates/ui-canvas/src/canvas/raster/mod.rs index 427407392..9e3990211 100644 --- a/crates/ui-canvas/src/canvas/raster/mod.rs +++ b/crates/ui-canvas/src/canvas/raster/mod.rs @@ -9,6 +9,9 @@ //! pixels at 100 percent and the cost follows the window, not the document. Pixels are drawn sharp (nearest) from //! 100 percent up and filtered below. //! +//! Past the document's edge the display shows nothing, the source's pixels out to a larger region (a crop preview of +//! layers past the canvas), or copies of the document around it (a pattern preview); see [`Extent`]. +//! //! The display takes no pointer and no keys and declares no parts: a press on it reaches the canvas's tool. mod detail; @@ -33,6 +36,7 @@ use detail::{texel_extent, texel_origin}; use egui::epaint::{Mesh, Vertex}; use egui::{Color32, ColorImage, Context, Id, Pos2, Response, Shape, TextureFilter, TextureOptions, Vec2, pos2}; use std::collections::HashMap; +use std::ops::RangeInclusive; /// The style of a raster display: it has no look of its own. #[derive(Clone, Debug, Default, PartialEq, ComponentStyle)] @@ -42,11 +46,100 @@ pub struct RasterStyle { metrics: Metrics, } +/// How far a display's picture reaches past the document's edge. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum Extent { + /// The document alone. + #[default] + Document, + /// The document and the pixels around it out to this region (Crop's preview of layers past the canvas). A source + /// picture reads the region from the source; a painted picture is expected to paint it. The checkerboard covers + /// the region; the edge stays at the document's edge. + Region(PixelRect), + /// Copies of the document a whole document size apart, filling the canvas around it (Pattern Preview), at most + /// 31 along each axis, centred on the view. A source picture draws its textures again for each copy; a painted picture is + /// called once per copy with the view shifted by the copy's offset. The checkerboard covers the copies; the edge + /// stays at the document's edge. + Tiled, +} + +/// Most copies a tiled display draws along each axis, the document included. A tiny document zoomed far out would +/// otherwise need thousands of copies per pass; past this the pattern stops, and the run is centred on the view. +const MOST_COPIES: i32 = 31; + +/// The copies a tiled display draws, as columns and rows of whole document sizes from the document: copy `(i, j)` is +/// the document moved by `i` widths and `j` heights. The document itself is drawn whether or not it is in the run. +#[derive(Clone, Debug, PartialEq)] +struct Tiles { + columns: RangeInclusive, + rows: RangeInclusive, +} + +impl Tiles { + /// The document alone. + fn document() -> Self { + Tiles { columns: 0..=0, rows: 0..=0 } + } + + /// The copies of a document of `size` that cover `visible`, the document area a view shows. + fn covering(visible: Rect64, size: PixelSize) -> Self { + match (tile_run(visible.x0, visible.x1, size.width), tile_run(visible.y0, visible.y1, size.height)) { + (Some(columns), Some(rows)) => Tiles { columns, rows }, + _ => Self::document(), + } + } + + /// The offset in document pixels of each copy to draw, the document's own first. + fn offsets(&self, size: PixelSize) -> impl Iterator + '_ { + let (width, height) = (size.width as f32, size.height as f32); + let copies = self.rows.clone().flat_map(move |j| self.columns.clone().map(move |i| (i, j))).filter(|©| copy != (0, 0)); + std::iter::once((0, 0)).chain(copies).map(move |(i, j)| Vec2::new(i as f32 * width, j as f32 * height)) + } + + /// The area the copies and the document cover, in document pixels. + fn cover(&self, size: PixelSize) -> Rect64 { + let (width, height) = (f64::from(size.width), f64::from(size.height)); + let (columns, rows) = ((*self.columns.start()).min(0)..=(*self.columns.end()).max(0), (*self.rows.start()).min(0)..=(*self.rows.end()).max(0)); + Rect64::new( + f64::from(*columns.start()) * width, + f64::from(*rows.start()) * height, + f64::from(*columns.end() + 1) * width, + f64::from(*rows.end() + 1) * height, + ) + } +} + +/// The tiles `len` pixels long along one axis that overlap `lo..hi`, at most [`MOST_COPIES`] of them, centred on the +/// middle of `lo..hi` when more would fit; `None` for an empty or non-finite span. +fn tile_run(lo: f64, hi: f64, len: u32) -> Option> { + if len == 0 || !lo.is_finite() || !hi.is_finite() || hi <= lo { + return None; + } + let n = f64::from(len); + // Far past any real view; keeps the ± MOST_COPIES arithmetic inside i32. + let index = |v: f64| v.clamp(-1e9, 1e9) as i32; + let (first, last) = (index((lo / n).floor()), index((hi / n).ceil()) - 1); + if last.saturating_sub(first) < MOST_COPIES { + return Some(first..=last); + } + let middle = index(((lo + hi) / 2.0 / n).floor()); + let half = MOST_COPIES / 2; + Some(middle - half..=middle + half) +} + +/// `view` looking `offset` document pixels back, so the document drawn through it lands `offset` along on screen. +fn shifted(mut view: CanvasView, offset: Vec2) -> CanvasView { + view.center -= craft_behavior::kurbo::Vec2::new(f64::from(offset.x), f64::from(offset.y)); + view +} + /// Which picture of a display a texture holds. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] enum Level { - /// The whole document at one texel per `texture_factor` pixels. - Base, + /// What the picture covers (the document, or the region of [`Extent::Region`]) at one texel per `texture_factor` + /// pixels. A new region is a new key, so a texture never shows one region at another's place. + Base(PixelRect), /// The part in view at the screen's resolution. A new plan is a new key, so a texture is never partly of /// two plans. Detail(DetailPlan), @@ -76,10 +169,14 @@ struct Textures { detail: Option<(CachedTexture, DetailPlan)>, } -/// What `update` leaves for `paint` and `finish`: the view to draw through and, for a source picture, its textures. +/// What `update` leaves for `paint` and `finish`: the view to draw through, what the picture covers and where its +/// copies go, and, for a source picture, its textures. struct Shown { view: CanvasView, size: PixelSize, + /// The document, or the region of [`Extent::Region`] around it. + bounds: PixelRect, + tiles: Tiles, textures: Option, } @@ -121,6 +218,8 @@ impl Outcome for RasterDisplayOutput { /// - **Transparency.** [`checkerboard`](Self::checkerboard) draws a transparency grid under the document, holding /// still while the document scrolls; the app's [`Checker`] sets its cells, and an empty one is the theme's. /// Without it the document draws straight on the canvas (an opaque document, a video frame). +/// - **Extent.** [`extent`](Self::extent) lets the picture reach past the document's edge: out to a larger region, +/// or as copies around it ([`Extent`]). /// - **Edge.** [`outlined`](Self::outlined) draws the divider rule just outside the document's edge, so an image /// reads apart from the surface around it; a mapped display draws none. /// - **Turned views.** A rotated or mirrored view draws each texture as one quad on the view's mapped corners. @@ -138,6 +237,9 @@ pub struct RasterDisplay<'a> { /// The divider rule just outside the document's edge. #[prop(flag)] outlined: bool, + /// How far the picture reaches past the document's edge. + #[prop] + extent: Extent, read: PixelRect, shown: Option, mapping: Option, @@ -167,7 +269,7 @@ impl<'a> RasterDisplay<'a> { } fn of(label: impl Into