mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 01:44:25 +00:00
RasterDisplay stories tiled and beyond
This commit is contained in:
@@ -100,7 +100,8 @@ impl Tiles {
|
||||
/// 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));
|
||||
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,
|
||||
@@ -269,8 +270,17 @@ impl<'a> RasterDisplay<'a> {
|
||||
}
|
||||
|
||||
fn of(label: impl Into<Label>, picture: Picture<'a>) -> Self {
|
||||
RasterDisplay { common: Common::new(), picture, checkerboard: None, outlined: false, extent: Extent::Document, read: PixelRect::default(), shown: None, mapping: None }
|
||||
.label(label)
|
||||
RasterDisplay {
|
||||
common: Common::new(),
|
||||
picture,
|
||||
checkerboard: None,
|
||||
outlined: false,
|
||||
extent: Extent::Document,
|
||||
read: PixelRect::default(),
|
||||
shown: None,
|
||||
mapping: None,
|
||||
}
|
||||
.label(label)
|
||||
}
|
||||
|
||||
/// Draws the image through `frame` (the unit square onto the document) on a `cells` by `cells` mesh, so a
|
||||
@@ -443,8 +453,14 @@ impl Component for RasterDisplay<'_> {
|
||||
let corners = shown.view.quad(Rect64::new(0.0, 0.0, f64::from(shown.size.width), f64::from(shown.size.height)));
|
||||
if let Some(checker) = &self.checkerboard {
|
||||
let bounds = shown.bounds;
|
||||
let covered = Rect64::new(f64::from(bounds.x0), f64::from(bounds.y0), f64::from(bounds.x1), f64::from(bounds.y1)).union(shown.tiles.cover(shown.size));
|
||||
painter.painter.add(checker.shape(painter.painter.ctx(), &ui.env.tokens, CheckerArea::Quad(shown.view.quad(covered)), shown.view.rect.min));
|
||||
let covered = Rect64::new(f64::from(bounds.x0), f64::from(bounds.y0), f64::from(bounds.x1), f64::from(bounds.y1))
|
||||
.union(shown.tiles.cover(shown.size));
|
||||
painter.painter.add(checker.shape(
|
||||
painter.painter.ctx(),
|
||||
&ui.env.tokens,
|
||||
CheckerArea::Quad(shown.view.quad(covered)),
|
||||
shown.view.rect.min,
|
||||
));
|
||||
}
|
||||
if let (Picture::Source(_), Some(textures), Some(mapping)) = (&self.picture, &shown.textures, &self.mapping) {
|
||||
let (texture, factor) = textures.base;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//! Stories of the raster display.
|
||||
|
||||
use super::RasterDisplay;
|
||||
use super::RasterDisplayOutput;
|
||||
use super::{Extent, RasterDisplay};
|
||||
use crate::canvas::testing::canvas_story::{StoryPicture, on_canvas_view, story_view};
|
||||
use crate::canvas::{AppPaint, PageFrame, Pasteboard};
|
||||
use crate::canvas::{AppPaint, PageFrame, Pasteboard, PixelRect};
|
||||
use crate::canvas_controls::PixelGrid;
|
||||
use crate::contract::Show as _;
|
||||
use crate::primitives::{Checker, ContentPainterCx};
|
||||
@@ -134,3 +134,52 @@ fn painted(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
output
|
||||
})
|
||||
}
|
||||
|
||||
/// Copies of the gradient around it, as Pattern Preview shows a seamless tile: zoomed out, the canvas fills with
|
||||
/// copies a whole document apart, the edge on the document alone.
|
||||
#[story(RasterDisplay<'static>, covers = [Default], drive = Drive::Never)]
|
||||
fn tiled(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let view = view_values(data, 30.0, 0.0);
|
||||
let checker = checker_of(data);
|
||||
on_canvas_view(ui, view, None, |ui, at, _| {
|
||||
let source = StoryPicture::new(DOCUMENT.width, DOCUMENT.height, gradient_pixel);
|
||||
let display = RasterDisplay::new("Picture", &source).outlined().extent(Extent::Tiled);
|
||||
match checker {
|
||||
Some(checker) => display.checkerboard(checker),
|
||||
None => display,
|
||||
}
|
||||
.show_at(ui, at)
|
||||
})
|
||||
}
|
||||
|
||||
/// Where the disc past the document's right edge lies, and its radius, in document pixels.
|
||||
const DISC: (Point, f64) = (Point::new(640.0, 300.0), 160.0);
|
||||
|
||||
/// The gradient inside the document and a disc reaching past its right edge, as a layer moved past the canvas.
|
||||
fn beyond_pixel(p: Point) -> Color32 {
|
||||
let inside = (0.0..f64::from(DOCUMENT.width)).contains(&p.x) && (0.0..f64::from(DOCUMENT.height)).contains(&p.y);
|
||||
if inside {
|
||||
gradient_pixel(p)
|
||||
} else if (p - DISC.0).hypot() <= DISC.1 {
|
||||
Color32::from_rgb(220, 120, 40)
|
||||
} else {
|
||||
Color32::TRANSPARENT
|
||||
}
|
||||
}
|
||||
|
||||
/// The document with the pixels past its edge out to a larger region, as Crop shows layers past the canvas while a
|
||||
/// frame is edited: the checkerboard reaches the region, the edge stays on the document.
|
||||
#[story(RasterDisplay<'static>, covers = [Default], drive = Drive::Never)]
|
||||
fn beyond(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let view = view_values(data, 60.0, 0.0);
|
||||
let checker = checker_of(data);
|
||||
on_canvas_view(ui, view, None, |ui, at, _| {
|
||||
let source = StoryPicture::new(DOCUMENT.width, DOCUMENT.height, beyond_pixel);
|
||||
let display = RasterDisplay::new("Picture", &source).outlined().extent(Extent::Region(PixelRect::new(-40, -40, 840, 480)));
|
||||
match checker {
|
||||
Some(checker) => display.checkerboard(checker),
|
||||
None => display,
|
||||
}
|
||||
.show_at(ui, at)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -197,7 +197,14 @@ fn a_painted_display_paints_once_a_frame() {
|
||||
/// A canvas looking at `center` at `zoom` that shows a display of `document` with the checkerboard, the edge and
|
||||
/// `extent`: a source display, or, while `painted` is set, a painted one whose paint adds a GPU paint callback and
|
||||
/// records the view of each call in `paints`.
|
||||
fn extent_canvas(document: Rc<Document>, center: Point, zoom: f64, extent: Rc<Cell<Extent>>, painted: bool, paints: Rc<RefCell<Vec<CanvasView>>>) -> Fixture {
|
||||
fn extent_canvas(
|
||||
document: Rc<Document>,
|
||||
center: Point,
|
||||
zoom: f64,
|
||||
extent: Rc<Cell<Extent>>,
|
||||
painted: bool,
|
||||
paints: Rc<RefCell<Vec<CanvasView>>>,
|
||||
) -> Fixture {
|
||||
let view = CanvasView::new(Rect::from_min_size(Pos2::ZERO, CANVAS), center, zoom);
|
||||
let callback: Arc<dyn std::any::Any + Send + Sync> = Arc::new(());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
@@ -264,7 +271,8 @@ fn tiled_copies_fill_the_canvas_a_document_apart() {
|
||||
let center = Point::new(50.0, 35.0);
|
||||
let view = CanvasView::new(Rect::from_min_size(Pos2::ZERO, CANVAS), center, 0.5);
|
||||
let canvas = view.rect;
|
||||
let at = |i: i32, j: i32| doc_rect(&view, Rect64::new(f64::from(i) * 100.0, f64::from(j) * 70.0, f64::from(i + 1) * 100.0, f64::from(j + 1) * 70.0));
|
||||
let at =
|
||||
|i: i32, j: i32| doc_rect(&view, Rect64::new(f64::from(i) * 100.0, f64::from(j) * 70.0, f64::from(i + 1) * 100.0, f64::from(j + 1) * 70.0));
|
||||
let in_view: Vec<Rect> = (-40..40).flat_map(|j| (-40..40).map(move |i| (i, j))).map(|(i, j)| at(i, j)).filter(|r| r.intersects(canvas)).collect();
|
||||
for painted in [false, true] {
|
||||
let (placed, checker, edge) = pictures(&document, center, 0.5, Extent::Tiled, painted);
|
||||
@@ -273,7 +281,10 @@ fn tiled_copies_fill_the_canvas_a_document_apart() {
|
||||
assert!(near(placed[0], at(0, 0)), "painted {painted}: the document draws first, in its place");
|
||||
let all = in_view.iter().fold(Rect::NOTHING, |all, r| all.union(*r));
|
||||
assert!(near(checker, all), "painted {painted}: the checkerboard lies under the copies: {checker:?} vs {all:?}");
|
||||
assert!(edge.expand(-3.0).contains_rect(at(0, 0).expand(-6.0)) && at(0, 0).expand(3.0).contains_rect(edge), "painted {painted}: the edge stays on the document: {edge:?}");
|
||||
assert!(
|
||||
edge.expand(-3.0).contains_rect(at(0, 0).expand(-6.0)) && at(0, 0).expand(3.0).contains_rect(edge),
|
||||
"painted {painted}: the edge stays on the document: {edge:?}"
|
||||
);
|
||||
|
||||
let (filled, _, _) = pictures(&document, center, 20.0, Extent::Tiled, painted);
|
||||
assert_eq!(filled.len(), 1, "painted {painted}: a document filling the canvas has no copies");
|
||||
@@ -298,7 +309,10 @@ fn a_region_past_the_document_draws_out_to_the_region() {
|
||||
document.reads.borrow_mut().clear();
|
||||
let (placed, checker, edge) = pictures(&document, center, 2.0, Extent::Region(region), painted);
|
||||
assert!(near(checker, on_screen(region)), "painted {painted}: the checkerboard covers the region: {checker:?}");
|
||||
assert!(edge.expand(-3.0).contains_rect(page.expand(-6.0)) && page.expand(3.0).contains_rect(edge), "painted {painted}: the edge stays on the document: {edge:?}");
|
||||
assert!(
|
||||
edge.expand(-3.0).contains_rect(page.expand(-6.0)) && page.expand(3.0).contains_rect(edge),
|
||||
"painted {painted}: the edge stays on the document: {edge:?}"
|
||||
);
|
||||
if painted {
|
||||
assert_eq!(placed.len(), 1, "the app paints once, through the view on screen");
|
||||
assert!(document.reads.borrow().is_empty(), "a painted display reads nothing");
|
||||
|
||||
Reference in New Issue
Block a user