mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 04:04:25 +00:00
Canvas: RasterDisplay shows an app-painted document beside a source one (AppPaint)
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
//! `AppPaint`: a picture the app paints itself inside a framework control, which places it and draws its own parts
|
||||
//! around it.
|
||||
|
||||
use crate::primitives::ContentPainterCx;
|
||||
|
||||
type AppPaintFn<'a, A> = Box<dyn FnMut(&ContentPainterCx<'_>, A) + 'a>;
|
||||
|
||||
/// A closure that paints the app's picture where a control places it: the control calls it from its paint with its
|
||||
/// content painter and the placement `A` (the [`Navigator`](super::Navigator)'s fitted rect, the
|
||||
/// [`RasterDisplay`](super::RasterDisplay)'s canvas view). The app adds what it likes to the painter, a GPU paint
|
||||
/// callback included; the control's own parts paint before and after it.
|
||||
pub struct AppPaint<'a, A>(AppPaintFn<'a, A>);
|
||||
|
||||
impl<'a, A> AppPaint<'a, A> {
|
||||
/// A painter that draws the picture with `paint`, given the content painter and the placement.
|
||||
pub fn new(paint: impl FnMut(&ContentPainterCx<'_>, A) + 'a) -> Self {
|
||||
AppPaint(Box::new(paint))
|
||||
}
|
||||
|
||||
/// The same painter lent for one run of a body that may run more than once per pass.
|
||||
pub fn reborrowed(&mut self) -> AppPaint<'_, A> {
|
||||
let paint = &mut self.0;
|
||||
AppPaint::new(move |painter, at| paint(painter, at))
|
||||
}
|
||||
|
||||
/// Paints the picture with `painter` at `at`.
|
||||
pub fn paint(&mut self, painter: &ContentPainterCx<'_>, at: A) {
|
||||
(self.0)(painter, at);
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,9 @@ pub use pasteboard::{PageBorder, PageFrame, Pasteboard, PasteboardColor, Pastebo
|
||||
pub use split::{SplitAt, SplitLayout, SplitPane, SplitStyle, SplitViews, SplitViewsOutput};
|
||||
pub use words::SplitWords;
|
||||
|
||||
mod app_paint;
|
||||
mod raster;
|
||||
pub use app_paint::AppPaint;
|
||||
pub use raster::{DetailPlan, RasterDisplay, RasterDisplayOutput, RasterStyle, snap_to_texels};
|
||||
|
||||
// l4d-space
|
||||
@@ -83,8 +85,8 @@ pub use scrollbars::{CanvasScrollbars, CanvasScrollbarsOutput};
|
||||
// canvas-navigator-a
|
||||
mod navigator;
|
||||
pub use navigator::{
|
||||
Navigator, NavigatorCommands, NavigatorOutput, NavigatorPaint, NavigatorPreview, NavigatorZoomRow, NavigatorZoomRowOutput, preview_rect,
|
||||
proxy_quad, view_center_for, zoom_to_document_rect,
|
||||
Navigator, NavigatorCommands, NavigatorOutput, NavigatorPreview, NavigatorZoomRow, NavigatorZoomRowOutput, preview_rect, proxy_quad,
|
||||
view_center_for, zoom_to_document_rect,
|
||||
};
|
||||
// End of canvas-navigator-a.
|
||||
// l4e-navprev
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
//! | Part | Names |
|
||||
//! |---|---|
|
||||
//! | Geometry, for tests and for apps with their own preview | [`preview_rect`], [`proxy_quad`], [`zoom_to_document_rect`], [`view_center_for`] |
|
||||
//! | The preview box | [`Navigator`], [`NavigatorPreview`], [`NavigatorPaint`], [`NavigatorOutput`], [`NavigatorStyle`] |
|
||||
//! | The preview box | [`Navigator`], [`NavigatorPreview`], [`NavigatorOutput`], [`NavigatorStyle`] |
|
||||
//! | The zoom row | [`NavigatorZoomRow`], [`NavigatorZoomRowOutput`] |
|
||||
//! | Panel Options | [`NavigatorSettings`], [`NavigatorOptions`], [`ProxyColor`]; the dialog is `patterns::form_dialogs::NavigatorOptionsDialog` |
|
||||
//! | The panel menu | [`NavigatorCommands`] |
|
||||
@@ -17,6 +17,6 @@ mod stories;
|
||||
mod zoom_row;
|
||||
|
||||
pub use menu::NavigatorCommands;
|
||||
pub use preview::{Navigator, NavigatorOutput, NavigatorPaint, NavigatorPreview, NavigatorStyle};
|
||||
pub use preview::{Navigator, NavigatorOutput, NavigatorPreview, NavigatorStyle};
|
||||
pub use proxy::{preview_rect, proxy_quad, view_center_for, zoom_to_document_rect};
|
||||
pub use zoom_row::{NavigatorZoomRow, NavigatorZoomRowOutput};
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
//! wheel and `+` and `-` zoom about the view centre, and the arrow and page keys move it.
|
||||
|
||||
use super::proxy::{preview_rect, proxy_quad, view_center_for, zoom_to_document_rect};
|
||||
use crate::canvas::AppPaint;
|
||||
use crate::canvas::zoom_motion::{self, Steer};
|
||||
use crate::canvas::{CanvasWords, NavigationOptions, PageKey, ViewCommands, WheelAction, WheelNavigation};
|
||||
use crate::canvas_data::ProxyColor;
|
||||
@@ -10,8 +11,8 @@ use crate::contract::{
|
||||
Access, Bind, BindMut, Common, Component, Cx, DisabledLook, Edit as _, EditResponse, Focus as _, Input, MeasureCx, Name, Outcome, PaintUi,
|
||||
PartsCx, Pointer as _, Props as _, Remember as _, Role, WordSetCx as _,
|
||||
};
|
||||
use crate::primitives::PaintShape;
|
||||
use crate::primitives::textures::CachedTexture;
|
||||
use crate::primitives::{ContentPainterCx, PaintShape};
|
||||
use crate::runtime::access::Numeric;
|
||||
use crate::runtime::commands;
|
||||
use crate::runtime::env::Label;
|
||||
@@ -47,25 +48,13 @@ const SHIFT_STEP: f32 = 0.5;
|
||||
const ZOOM_KEYS: [(Chord, i32); 4] =
|
||||
[(Chord::plain(Key::Plus), 1), (Chord::plain(Key::Equals), 1), (Chord::new(Mods::SHIFT, Key::Equals), 1), (Chord::plain(Key::Minus), -1)];
|
||||
|
||||
type NavigatorPaintFn<'a> = Box<dyn FnMut(&ContentPainterCx<'_>, Rect) + 'a>;
|
||||
|
||||
/// A closure that paints the document's picture into the fitted rect.
|
||||
pub struct NavigatorPaint<'a>(NavigatorPaintFn<'a>);
|
||||
|
||||
impl<'a> NavigatorPaint<'a> {
|
||||
/// A painter that draws the picture with `paint`, given the content painter and the rect the picture fills.
|
||||
pub fn new(paint: impl FnMut(&ContentPainterCx<'_>, Rect) + 'a) -> Self {
|
||||
NavigatorPaint(Box::new(paint))
|
||||
}
|
||||
}
|
||||
|
||||
/// What the preview box shows: the document's picture, made and cached by the app.
|
||||
#[non_exhaustive]
|
||||
pub enum NavigatorPreview<'a> {
|
||||
/// A texture from the app's own texture store, drawn over the checkerboard into the fitted rect.
|
||||
Texture(CachedTexture),
|
||||
/// The app paints the picture itself into the fitted rect; the checkerboard is not drawn.
|
||||
Paint(NavigatorPaint<'a>),
|
||||
Paint(AppPaint<'a, Rect>),
|
||||
/// No picture yet: the checkerboard alone, with the proxy over it.
|
||||
None,
|
||||
}
|
||||
@@ -75,7 +64,7 @@ impl NavigatorPreview<'_> {
|
||||
pub fn reborrowed(&mut self) -> NavigatorPreview<'_> {
|
||||
match self {
|
||||
NavigatorPreview::Texture(texture) => NavigatorPreview::Texture(*texture),
|
||||
NavigatorPreview::Paint(NavigatorPaint(paint)) => NavigatorPreview::Paint(NavigatorPaint::new(move |painter, rect| paint(painter, rect))),
|
||||
NavigatorPreview::Paint(paint) => NavigatorPreview::Paint(paint.reborrowed()),
|
||||
NavigatorPreview::None => NavigatorPreview::None,
|
||||
}
|
||||
}
|
||||
@@ -463,7 +452,7 @@ impl Component for Navigator<'_> {
|
||||
painter.checker(fitted);
|
||||
painter.picture(texture.id, fitted, Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), 0.0);
|
||||
}
|
||||
NavigatorPreview::Paint(NavigatorPaint(mut paint)) => paint(&painter, fitted),
|
||||
NavigatorPreview::Paint(mut paint) => paint.paint(&painter, fitted),
|
||||
NavigatorPreview::None => painter.checker(fitted),
|
||||
}
|
||||
let Some(view) = cx.value().copied() else { return };
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! Stories of the Navigator and its zoom row.
|
||||
|
||||
use super::preview::WELL;
|
||||
use super::{Navigator, NavigatorPaint, NavigatorPreview, NavigatorZoomRow};
|
||||
use super::{Navigator, NavigatorPreview, NavigatorZoomRow};
|
||||
use crate::canvas::AppPaint;
|
||||
use crate::contract::{Bind, Show as _, Validity};
|
||||
use crate::runtime::env::Message;
|
||||
use crate::testing::{Drive, Grip, StoryData, StoryOutput, story};
|
||||
@@ -56,7 +57,7 @@ fn views_that_differ(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
fn painted_by_the_app(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let view = data.value("view", CanvasView::new(CANVAS, Point::new(1000.0, 750.0), 1.0));
|
||||
Navigator::new("Navigator", Bind::bound(view), DOCUMENT)
|
||||
.preview(NavigatorPreview::Paint(NavigatorPaint::new(|painter, rect| {
|
||||
.preview(NavigatorPreview::Paint(AppPaint::new(|painter, rect| {
|
||||
painter.painter.rect_filled(rect, 0.0, Color32::from_gray(PICTURE_GRAY));
|
||||
})))
|
||||
.show(ui)
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
//! `RasterDisplay`: the CPU raster path of a canvas. The app gives the document's composite as RGBA for a region
|
||||
//! ([`RasterSource`]); the display keeps it in `TextureStore` textures, draws it through the canvas's
|
||||
//! [`CanvasSpace`](super::CanvasSpace) over a checkerboard, and re-reads only what the source reports damaged.
|
||||
//! `RasterDisplay`: the document display of a canvas. It draws the document through the canvas's
|
||||
//! [`CanvasSpace`](super::CanvasSpace) over a checkerboard, with the edge rule around it. The picture is either the
|
||||
//! document's composite as RGBA for a region ([`RasterSource`]), which the display keeps in `TextureStore` textures and
|
||||
//! re-reads only where the source reports damage, or the app's own paint ([`AppPaint`]), such as a GPU paint callback
|
||||
//! over the app's compositor.
|
||||
//!
|
||||
//! A document larger than the device's texture limit (`texture_side`) is uploaded reduced; zoomed in, the part in
|
||||
//! view is added at the screen's resolution as a detail texture ([`DetailPlan`]), so a large document shows its own
|
||||
//! 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.
|
||||
//!
|
||||
//!
|
||||
//! The display takes no pointer and no keys and declares no parts: a press on it reaches the canvas's tool.
|
||||
|
||||
mod detail;
|
||||
@@ -15,7 +18,7 @@ mod stories;
|
||||
|
||||
pub use detail::{DetailPlan, snap_to_texels};
|
||||
|
||||
use super::{PixelRect, RasterSource, canvas_space};
|
||||
use super::{AppPaint, PixelRect, RasterSource, canvas_space};
|
||||
use crate::contract::{Access, Common, Component, Cx, NoValue, Outcome, PaintUi, Props as _, Role};
|
||||
use crate::primitives::textures::UNTINTED;
|
||||
use crate::primitives::{
|
||||
@@ -50,12 +53,35 @@ enum Level {
|
||||
Detail(DetailPlan),
|
||||
}
|
||||
|
||||
/// What `update` leaves for `paint` and `finish`: the textures to draw and the view to draw them through.
|
||||
/// What a display shows.
|
||||
enum Picture<'a> {
|
||||
/// The source's pixels, kept in the display's textures.
|
||||
Source(&'a dyn RasterSource),
|
||||
/// The app paints the document of `size` pixels itself, through the view it is given.
|
||||
Painted { size: PixelSize, paint: AppPaint<'a, CanvasView> },
|
||||
}
|
||||
|
||||
impl Picture<'_> {
|
||||
fn size(&self) -> PixelSize {
|
||||
match self {
|
||||
Picture::Source(source) => source.size(),
|
||||
Picture::Painted { size, .. } => *size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The textures of a source picture: the whole document at one texel per `factor` pixels, and the part in view at the
|
||||
/// screen's resolution while zoomed in.
|
||||
struct Textures {
|
||||
base: (CachedTexture, u32),
|
||||
detail: Option<(CachedTexture, DetailPlan)>,
|
||||
}
|
||||
|
||||
/// What `update` leaves for `paint` and `finish`: the view to draw through and, for a source picture, its textures.
|
||||
struct Shown {
|
||||
view: CanvasView,
|
||||
size: PixelSize,
|
||||
base: (CachedTexture, u32),
|
||||
detail: Option<(CachedTexture, DetailPlan)>,
|
||||
textures: Option<Textures>,
|
||||
}
|
||||
|
||||
/// The revision each texture of a display shows, which is what the source is asked about: what changed since then.
|
||||
@@ -83,9 +109,13 @@ impl Outcome for RasterDisplayOutput {
|
||||
}
|
||||
}
|
||||
|
||||
/// The CPU raster display of one canvas: the document drawn through the view of the [`Canvas`](super::Canvas) it
|
||||
/// is shown in, at the canvas rect. Outside a canvas, or in one without a view, it draws nothing.
|
||||
/// The document display of one canvas: the document drawn through the view of the [`Canvas`](super::Canvas) it is
|
||||
/// shown in, at the canvas rect. Outside a canvas, or in one without a view, it draws nothing.
|
||||
///
|
||||
/// - **Picture.** [`new`](Self::new) draws a [`RasterSource`]'s pixels from textures the display keeps;
|
||||
/// [`painted`](Self::painted) calls the app's paint once per pass, in the display's place between the checkerboard
|
||||
/// and the edge, with the view on screen. Both pictures get the same checkerboard and edge; the shadow is the
|
||||
/// [`Pasteboard`](super::Pasteboard)'s and the pixel grid the `PixelGrid` control's, shown around and over either.
|
||||
/// - **Reading.** The display asks the source for pixels only when it has none for the source's `revision`. A
|
||||
/// changed revision re-reads what [`RasterSource::damage`] reports since the revision the display shows (snapped
|
||||
/// outward to whole texels), or everything where the source cannot tell.
|
||||
@@ -102,7 +132,7 @@ impl Outcome for RasterDisplayOutput {
|
||||
pub struct RasterDisplay<'a> {
|
||||
#[common]
|
||||
common: Common,
|
||||
source: &'a dyn RasterSource,
|
||||
picture: Picture<'a>,
|
||||
/// The transparency grid under the document; none draws the document straight on the canvas.
|
||||
#[prop]
|
||||
checkerboard: Option<Checker>,
|
||||
@@ -127,7 +157,18 @@ impl<'a> RasterDisplay<'a> {
|
||||
/// A display named `label` of the document `source` gives. An empty label makes a display that only paints, with
|
||||
/// no AccessKit node: a further image over a named one.
|
||||
pub fn new(label: impl Into<Label>, source: &'a dyn RasterSource) -> Self {
|
||||
RasterDisplay { common: Common::new(), source, checkerboard: None, outlined: false, read: PixelRect::default(), shown: None, mapping: None }
|
||||
Self::of(label, Picture::Source(source))
|
||||
}
|
||||
|
||||
/// A display named `label` of a document of `size` pixels that the app paints itself with `paint`, given the view
|
||||
/// on screen (its `rect` is the canvas rect). The app draws the document's pixels only: the display draws the
|
||||
/// checkerboard under them and the edge around them, as for a source.
|
||||
pub fn painted(label: impl Into<Label>, size: PixelSize, paint: AppPaint<'a, CanvasView>) -> Self {
|
||||
Self::of(label, Picture::Painted { size, paint })
|
||||
}
|
||||
|
||||
fn of(label: impl Into<Label>, picture: Picture<'a>) -> Self {
|
||||
RasterDisplay { common: Common::new(), picture, checkerboard: None, outlined: false, read: PixelRect::default(), shown: None, mapping: None }
|
||||
.label(label)
|
||||
}
|
||||
|
||||
@@ -135,7 +176,7 @@ impl<'a> RasterDisplay<'a> {
|
||||
/// perspective frame shows the image foreshortened. The image fills the unit square unless
|
||||
/// [`placed`](Self::placed) says otherwise; a cell whose centre lies outside the unit square is left out. A
|
||||
/// mapped display draws its whole-document texture alone: the detail texture of a flat display has no place on
|
||||
/// a mesh.
|
||||
/// a mesh. A source display only: a painted one draws through the view as the app paints it.
|
||||
#[must_use]
|
||||
pub fn mapped(mut self, frame: Homography, cells: u32) -> Self {
|
||||
let placed = self.mapping.map_or(Rect64::new(0.0, 0.0, 1.0, 1.0), |mapping| mapping.placed);
|
||||
@@ -160,9 +201,9 @@ impl<'a> RasterDisplay<'a> {
|
||||
TextureStore::in_context(ctx, (id, sharp), TextureStoreOptions::pages().filter(filter))
|
||||
}
|
||||
|
||||
fn read_region(&self, region: PixelRect, factor: u32) -> ColorImage {
|
||||
fn read_region(source: &dyn RasterSource, region: PixelRect, factor: u32) -> ColorImage {
|
||||
let mut image = ColorImage::filled(texel_extent(region, factor), Color32::TRANSPARENT);
|
||||
self.source.read(region, factor, &mut image);
|
||||
source.read(region, factor, &mut image);
|
||||
image
|
||||
}
|
||||
|
||||
@@ -172,12 +213,20 @@ impl<'a> RasterDisplay<'a> {
|
||||
/// - A texture of an older revision is updated with what the source reports damaged since then: that part is
|
||||
/// read again alone, and damage elsewhere moves the texture to the revision without a read.
|
||||
/// - Anything else (no texture, another size, damage the source cannot tell) is read whole.
|
||||
fn bring_up_to_date(&self, ctx: &Context, id: Id, held: &mut Held, level: Level, region: PixelRect, factor: u32) -> (CachedTexture, PixelRect) {
|
||||
fn bring_up_to_date(
|
||||
source: &dyn RasterSource,
|
||||
ctx: &Context,
|
||||
id: Id,
|
||||
held: &mut Held,
|
||||
level: Level,
|
||||
region: PixelRect,
|
||||
factor: u32,
|
||||
) -> (CachedTexture, PixelRect) {
|
||||
let sharp = factor == 1;
|
||||
// A key never lives in both samplings: the other store's copy is from before the factor changed.
|
||||
Self::store(ctx, id, !sharp).forget(ctx, &level);
|
||||
let store = Self::store(ctx, id, sharp);
|
||||
let revision = self.source.revision();
|
||||
let revision = source.revision();
|
||||
let origin = [region.x0, region.y0];
|
||||
let expected = texel_extent(region, factor);
|
||||
let state = store.state(ctx, &level, revision);
|
||||
@@ -187,13 +236,13 @@ impl<'a> RasterDisplay<'a> {
|
||||
held.0.insert(level, revision);
|
||||
return (texture, PixelRect::default());
|
||||
}
|
||||
if let Some(damage) = held.0.get(&level).and_then(|since| self.source.damage(*since)) {
|
||||
if let Some(damage) = held.0.get(&level).and_then(|since| source.damage(*since)) {
|
||||
let dirty = snap_to_texels(damage.intersect(®ion), origin, factor).intersect(®ion);
|
||||
let updated = if dirty.is_empty() {
|
||||
// The pixels changed elsewhere: no texel of this texture is new, and the entry takes the revision.
|
||||
store.put_region(ctx, &level, revision, [0, 0], ColorImage::new([0, 0], Vec::new())).map(|texture| (texture, dirty))
|
||||
} else {
|
||||
let image = self.read_region(dirty, factor);
|
||||
let image = Self::read_region(source, dirty, factor);
|
||||
store.put_region(ctx, &level, revision, texel_origin(dirty, origin, factor), image).map(|texture| (texture, dirty))
|
||||
};
|
||||
if let Some(updated) = updated {
|
||||
@@ -203,7 +252,7 @@ impl<'a> RasterDisplay<'a> {
|
||||
}
|
||||
}
|
||||
held.0.insert(level, revision);
|
||||
(store.put(ctx, level, revision, self.read_region(region, factor)), region)
|
||||
(store.put(ctx, level, revision, Self::read_region(source, region, factor)), region)
|
||||
}
|
||||
|
||||
/// Drops every texture of this display.
|
||||
@@ -246,32 +295,41 @@ impl Component for RasterDisplay<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads what changed from the source and uploads it.
|
||||
/// Reads what changed from a source and uploads it.
|
||||
fn update(&mut self, cx: &mut Cx<'_, Self>) {
|
||||
self.read = PixelRect::default();
|
||||
self.shown = None;
|
||||
let Some(space) = canvas_space(&cx.env) else { return };
|
||||
let (ctx, id) = (cx.ctx.clone(), cx.id);
|
||||
let size = self.source.size();
|
||||
let size = self.picture.size();
|
||||
let bounds = PixelRect::of_size(size);
|
||||
let mut held = std::mem::take(cx.keep::<Held>());
|
||||
if bounds.is_empty() {
|
||||
Self::release(&ctx, id);
|
||||
return;
|
||||
}
|
||||
let source = match &self.picture {
|
||||
Picture::Source(source) if !bounds.is_empty() => *source,
|
||||
// An empty document, or a picture the app paints: the display holds no textures.
|
||||
_ => {
|
||||
if !held.0.is_empty() {
|
||||
Self::release(&ctx, id);
|
||||
}
|
||||
if !bounds.is_empty() {
|
||||
self.shown = Some(Shown { view: space.view, size, textures: None });
|
||||
}
|
||||
return;
|
||||
}
|
||||
};
|
||||
let side = texture_side(&ctx);
|
||||
let factor = texture_factor(size, side);
|
||||
let (base, base_read) = self.bring_up_to_date(&ctx, id, &mut held, Level::Base, bounds, factor);
|
||||
let (base, base_read) = Self::bring_up_to_date(source, &ctx, id, &mut held, Level::Base, bounds, factor);
|
||||
let plan = DetailPlan::in_view(&space.view, size, side, space.pixels_per_point).filter(|_| self.mapping.is_none());
|
||||
let detail = plan.map(|plan| {
|
||||
let (texture, read) = self.bring_up_to_date(&ctx, id, &mut held, Level::Detail(plan), plan.region, plan.factor);
|
||||
let (texture, read) = Self::bring_up_to_date(source, &ctx, id, &mut held, Level::Detail(plan), plan.region, plan.factor);
|
||||
self.read = self.read.union(&read);
|
||||
(texture, plan)
|
||||
});
|
||||
Self::retain_plan(&ctx, id, &mut held, plan);
|
||||
*cx.keep::<Held>() = held;
|
||||
self.read = self.read.union(&base_read);
|
||||
self.shown = Some(Shown { view: space.view, size, base: (base, factor), detail });
|
||||
self.shown = Some(Shown { view: space.view, size, textures: Some(Textures { base: (base, factor), detail }) });
|
||||
}
|
||||
|
||||
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
|
||||
@@ -281,15 +339,20 @@ impl Component for RasterDisplay<'_> {
|
||||
if let Some(checker) = &self.checkerboard {
|
||||
painter.painter.add(checker.shape(painter.painter.ctx(), &ui.env.tokens, CheckerArea::Quad(corners), shown.view.rect.min));
|
||||
}
|
||||
let (texture, factor) = shown.base;
|
||||
if let Some(mapping) = &self.mapping {
|
||||
painter.painter.add(mapped_mesh(&shown.view, texture, factor, shown.size, mapping));
|
||||
return;
|
||||
}
|
||||
let whole = PixelRect::of_size(shown.size);
|
||||
painter.painter.add(textured_quad(&shown.view, texture, factor, whole));
|
||||
if let Some((texture, plan)) = shown.detail {
|
||||
painter.painter.add(textured_quad(&shown.view, texture, plan.factor, plan.region));
|
||||
match (&mut self.picture, &shown.textures) {
|
||||
(Picture::Painted { paint, .. }, _) => paint.paint(&painter, shown.view),
|
||||
(Picture::Source(_), Some(textures)) => {
|
||||
let (texture, factor) = textures.base;
|
||||
if let Some(mapping) = &self.mapping {
|
||||
painter.painter.add(mapped_mesh(&shown.view, texture, factor, shown.size, mapping));
|
||||
return;
|
||||
}
|
||||
painter.painter.add(textured_quad(&shown.view, texture, factor, PixelRect::of_size(shown.size)));
|
||||
if let Some((texture, plan)) = textures.detail {
|
||||
painter.painter.add(textured_quad(&shown.view, texture, plan.factor, plan.region));
|
||||
}
|
||||
}
|
||||
(Picture::Source(_), None) => {}
|
||||
}
|
||||
if self.outlined {
|
||||
painter.with_draw(|draw| edge(draw, corners));
|
||||
@@ -297,7 +360,7 @@ impl Component for RasterDisplay<'_> {
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> RasterDisplayOutput {
|
||||
let detail = self.shown.as_ref().and_then(|shown| shown.detail.as_ref().map(|(_, plan)| *plan));
|
||||
let detail = self.shown.as_ref().and_then(|shown| shown.textures.as_ref()?.detail.as_ref().map(|(_, plan)| *plan));
|
||||
RasterDisplayOutput { response: cx.response.clone(), read: (!self.read.is_empty()).then_some(self.read), detail }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,15 @@
|
||||
use super::RasterDisplay;
|
||||
use super::RasterDisplayOutput;
|
||||
use crate::canvas::testing::canvas_story::{StoryPicture, on_canvas_view, story_view};
|
||||
use crate::canvas::{AppPaint, PageFrame, Pasteboard};
|
||||
use crate::canvas_controls::PixelGrid;
|
||||
use crate::contract::Show as _;
|
||||
use crate::primitives::Checker;
|
||||
use crate::primitives::{Checker, ContentPainterCx};
|
||||
use crate::testing::{Drive, StoryData, StoryOutput, story};
|
||||
use craft_behavior::kurbo::Point;
|
||||
use craft_behavior::kurbo::{Point, Rect as Rect64};
|
||||
use craft_behavior::{CanvasView, PixelSize};
|
||||
use egui::{Color32, Rect, Ui};
|
||||
use egui::epaint::{Mesh, Vertex};
|
||||
use egui::{Color32, Rect, Shape, Ui};
|
||||
|
||||
/// The document's size in pixels.
|
||||
const DOCUMENT: PixelSize = PixelSize::new(640, 400);
|
||||
@@ -30,6 +33,26 @@ fn gradient_pixel(p: Point) -> Color32 {
|
||||
if seen { Color32::from_rgb(level, 160, u8::MAX - level) } else { Color32::TRANSPARENT }
|
||||
}
|
||||
|
||||
/// The gradient as the app paints it: one colour mesh per seen part of the document through `view`. The colours are
|
||||
/// linear in the document point, so the mesh's interpolation gives `gradient_pixel`'s pixels.
|
||||
fn paint_gradient(painter: &ContentPainterCx<'_>, view: CanvasView) {
|
||||
let (width, height) = (f64::from(DOCUMENT.width), f64::from(DOCUMENT.height));
|
||||
let seen = [Rect64::new(width / 2.0, 0.0, width, height), Rect64::new(0.0, height / 2.0, width / 2.0, height)];
|
||||
let shapes = seen.map(|part| {
|
||||
let corners = [Point::new(part.x0, part.y0), Point::new(part.x1, part.y0), Point::new(part.x1, part.y1), Point::new(part.x0, part.y1)];
|
||||
let mut mesh = Mesh::default();
|
||||
let level = |p: Point| ((p.x / width + p.y / height) * 127.0) as u8;
|
||||
mesh.vertices.extend(corners.map(|p| Vertex {
|
||||
pos: view.to_screen(p),
|
||||
uv: egui::epaint::WHITE_UV,
|
||||
color: Color32::from_rgb(level(p), 160, u8::MAX - level(p)),
|
||||
}));
|
||||
mesh.indices.extend([0, 1, 2, 0, 2, 3]);
|
||||
Shape::mesh(mesh)
|
||||
});
|
||||
painter.data_shapes(shapes);
|
||||
}
|
||||
|
||||
/// The checkerboard cell sizes the stories offer.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
enum CheckerSize {
|
||||
@@ -90,3 +113,24 @@ fn turned(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let checker = checker_of(data);
|
||||
on_canvas_view(ui, view, None, |ui, at, _| gradient(ui, at, checker))
|
||||
}
|
||||
|
||||
/// The gradient painted by the app, on a pasteboard with its page shadow and under the pixel grid, which reads the
|
||||
/// same gradient from a source: the display draws the checkerboard and the edge around the app's picture.
|
||||
#[story(RasterDisplay<'static>, covers = [Default], drive = Drive::Never)]
|
||||
fn painted(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let view = view_values(data, 100.0, 0.0);
|
||||
let checker = checker_of(data);
|
||||
let pages = [PageFrame::new(Rect64::new(0.0, 0.0, f64::from(DOCUMENT.width), f64::from(DOCUMENT.height)))];
|
||||
let source = StoryPicture::new(DOCUMENT.width, DOCUMENT.height, gradient_pixel);
|
||||
on_canvas_view(ui, view, None, |ui, at, _| {
|
||||
let _ = Pasteboard::new("Pasteboard", &pages).show_at(ui, at);
|
||||
let display = RasterDisplay::painted("Picture", DOCUMENT, AppPaint::new(paint_gradient)).outlined();
|
||||
let output = match checker {
|
||||
Some(checker) => display.checkerboard(checker),
|
||||
None => display,
|
||||
}
|
||||
.show_at(ui, at);
|
||||
let _ = PixelGrid::new("Pixel grid", &source).show_at(ui, at);
|
||||
output
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
//! `RasterDisplay` and `Thumbnails`: what is read again, the detail plan, the fit maths and the request rules.
|
||||
|
||||
use craft_behavior::kurbo::Point;
|
||||
use craft_behavior::kurbo::{Point, Rect as Rect64};
|
||||
use craft_behavior::{CanvasView, PixelSize};
|
||||
use craft_ui::canvas::{Canvas, PixelRect, RasterDisplay, RasterSource};
|
||||
use craft_ui::canvas::{AppPaint, Canvas, PageFrame, Pasteboard, PixelRect, RasterDisplay, RasterSource};
|
||||
use craft_ui::canvas_controls::PixelGrid;
|
||||
use craft_ui::contract::{Show as _, ShowContainer as _};
|
||||
use craft_ui::primitives::{Checker, Revision};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Color32, ColorImage, Pos2, Rect, Vec2};
|
||||
use egui::{Color32, ColorImage, PaintCallback, Pos2, Rect, Shape, Vec2};
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// The canvas the display is shown in, in points.
|
||||
const CANVAS: Vec2 = Vec2::new(800.0, 600.0);
|
||||
@@ -124,3 +126,61 @@ fn the_checkerboard_paints_the_same_shapes_at_any_area() {
|
||||
let large = shapes(Document::new(400, 300), 4.0);
|
||||
assert_eq!(small, large, "a checkerboard over the whole canvas paints as many shapes as one over a corner of it");
|
||||
}
|
||||
|
||||
/// A canvas at 800 percent whose body shows, as a document canvas does, the pasteboard with the document's page and
|
||||
/// its shadow, a display of `document` with the checkerboard and the edge, and the pixel grid over it. A painted
|
||||
/// display (`paints` given) gets the app's paint: a GPU paint callback, as an app with its own compositor adds, which
|
||||
/// counts its calls in `paints`.
|
||||
fn document_canvas(document: Rc<Document>, paints: Option<Rc<Cell<usize>>>) -> Fixture {
|
||||
let size = document.size;
|
||||
let center = Point::new(f64::from(size.width) / 2.0, f64::from(size.height) / 2.0);
|
||||
let view = CanvasView::new(Rect::from_min_size(Pos2::ZERO, CANVAS), center, 8.0);
|
||||
let pages = [PageFrame::new(Rect64::new(0.0, 0.0, f64::from(size.width), f64::from(size.height)))];
|
||||
let callback: Arc<dyn std::any::Any + Send + Sync> = Arc::new(());
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
Canvas::new("Document").view(view).show(ui, |ui, body| {
|
||||
let _ = Pasteboard::new("Pasteboard", &pages).show_at(ui, body.rect);
|
||||
let display = match &paints {
|
||||
Some(paints) => RasterDisplay::painted(
|
||||
"Picture",
|
||||
size,
|
||||
AppPaint::new(|painter, view: CanvasView| {
|
||||
paints.set(paints.get() + 1);
|
||||
painter.painter.add(Shape::Callback(PaintCallback { rect: view.rect, callback: Arc::clone(&callback) }));
|
||||
}),
|
||||
),
|
||||
None => RasterDisplay::new("Picture", &*document),
|
||||
};
|
||||
display.checkerboard(Checker::new()).outlined().show_at(ui, body.rect);
|
||||
let _ = PixelGrid::new("Pixel grid", &*document).show_at(ui, body.rect);
|
||||
});
|
||||
});
|
||||
fixture.room(CANVAS);
|
||||
fixture.settle();
|
||||
fixture
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_painted_display_draws_the_same_surround_as_a_source_display() {
|
||||
let from_source = document_canvas(Document::new(40, 30), None).frame_shapes();
|
||||
let painted = document_canvas(Document::new(40, 30), Some(Rc::new(Cell::new(0)))).frame_shapes();
|
||||
let callbacks: Vec<usize> = painted.iter().enumerate().filter_map(|(at, shape)| matches!(shape, Shape::Callback(_)).then_some(at)).collect();
|
||||
let [at] = callbacks[..] else { panic!("the app's paint is in the frame once: {callbacks:?}") };
|
||||
let (under, over) = (&painted[..at], &painted[at + 1..]);
|
||||
assert!(!under.is_empty() && !over.is_empty(), "the shadow and the checkerboard paint under the picture, the edge and the grid over it");
|
||||
assert!(
|
||||
from_source.len() > under.len() + over.len() && from_source.starts_with(under) && from_source.ends_with(over),
|
||||
"the app's picture takes the place of the source's textures between the same shapes"
|
||||
);
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_painted_display_paints_once_a_frame() {
|
||||
let paints = Rc::new(Cell::new(0));
|
||||
let mut fixture = document_canvas(Document::new(40, 30), Some(Rc::clone(&paints)));
|
||||
for _ in 0..3 {
|
||||
paints.set(0);
|
||||
fixture.frame(Vec::new());
|
||||
assert_eq!(paints.get(), 1, "one paint in a frame of {} passes", fixture.passes());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user