diff options
| author | 2024-03-22 01:35:14 +0100 | |
|---|---|---|
| committer | 2024-03-22 01:35:14 +0100 | |
| commit | 53a183fe0d6aed460fbb8155ac9541757277aab3 (patch) | |
| tree | 449574ac4d2d14a146db74a1fd96e8075a394db4 /graphics | |
| parent | b972ebca8f8c23d2df1b45bb26038789766a5a65 (diff) | |
| download | iced-53a183fe0d6aed460fbb8155ac9541757277aab3.tar.gz iced-53a183fe0d6aed460fbb8155ac9541757277aab3.tar.bz2 iced-53a183fe0d6aed460fbb8155ac9541757277aab3.zip | |
Restore `canvas::Frame` API
Diffstat (limited to 'graphics')
| -rw-r--r-- | graphics/src/geometry.rs | 254 | ||||
| -rw-r--r-- | graphics/src/geometry/cache.rs | 123 | ||||
| -rw-r--r-- | graphics/src/geometry/frame.rs | 208 | ||||
| -rw-r--r-- | graphics/src/renderer.rs | 3 | 
4 files changed, 340 insertions, 248 deletions
| diff --git a/graphics/src/geometry.rs b/graphics/src/geometry.rs index cd4c9267..2b18243e 100644 --- a/graphics/src/geometry.rs +++ b/graphics/src/geometry.rs @@ -1,12 +1,16 @@  //! Build and draw geometry.  pub mod fill; +pub mod frame;  pub mod path;  pub mod stroke; +mod cache;  mod style;  mod text; +pub use cache::Cache;  pub use fill::Fill; +pub use frame::Frame;  pub use path::Path;  pub use stroke::{LineCap, LineDash, LineJoin, Stroke};  pub use style::Style; @@ -14,18 +18,7 @@ pub use text::Text;  pub use crate::gradient::{self, Gradient}; -use crate::core::{Point, Radians, Rectangle, Size, Vector}; -use crate::Primitive; - -use std::cell::RefCell; -use std::sync::Arc; - -pub fn frame<Renderer>(renderer: &Renderer, size: Size) -> Renderer::Frame -where -    Renderer: self::Renderer, -{ -    renderer.new_frame(size) -} +use crate::core::Size;  /// A renderer capable of drawing some [`Self::Geometry`].  pub trait Renderer: crate::core::Renderer { @@ -33,7 +26,7 @@ pub trait Renderer: crate::core::Renderer {      type Geometry: Geometry;      /// The kind of [`Frame`] this renderer supports. -    type Frame: Frame<Geometry = Self::Geometry>; +    type Frame: frame::Backend<Geometry = Self::Geometry>;      fn new_frame(&self, size: Size) -> Self::Frame; @@ -43,127 +36,11 @@ pub trait Renderer: crate::core::Renderer {  pub trait Backend {      /// The kind of [`Frame`] this backend supports. -    type Frame: Frame; +    type Frame: frame::Backend;      fn new_frame(&self, size: Size) -> Self::Frame;  } -pub trait Frame: Sized + Into<Self::Geometry> { -    /// The kind of geometry this frame can draw. -    type Geometry: Geometry; - -    /// Returns the width of the [`Frame`]. -    fn width(&self) -> f32; - -    /// Returns the height of the [`Frame`]. -    fn height(&self) -> f32; - -    /// Returns the dimensions of the [`Frame`]. -    fn size(&self) -> Size; - -    /// Returns the coordinate of the center of the [`Frame`]. -    fn center(&self) -> Point; - -    /// Draws the given [`Path`] on the [`Frame`] by filling it with the -    /// provided style. -    fn fill(&mut self, path: &Path, fill: impl Into<Fill>); - -    /// Draws an axis-aligned rectangle given its top-left corner coordinate and -    /// its `Size` on the [`Frame`] by filling it with the provided style. -    fn fill_rectangle( -        &mut self, -        top_left: Point, -        size: Size, -        fill: impl Into<Fill>, -    ); - -    /// Draws the stroke of the given [`Path`] on the [`Frame`] with the -    /// provided style. -    fn stroke<'a>(&mut self, path: &Path, stroke: impl Into<Stroke<'a>>); - -    /// Draws the characters of the given [`Text`] on the [`Frame`], filling -    /// them with the given color. -    /// -    /// __Warning:__ Text currently does not work well with rotations and scale -    /// transforms! The position will be correctly transformed, but the -    /// resulting glyphs will not be rotated or scaled properly. -    /// -    /// Additionally, all text will be rendered on top of all the layers of -    /// a `Canvas`. Therefore, it is currently only meant to be used for -    /// overlays, which is the most common use case. -    /// -    /// Support for vectorial text is planned, and should address all these -    /// limitations. -    fn fill_text(&mut self, text: impl Into<Text>); - -    /// Stores the current transform of the [`Frame`] and executes the given -    /// drawing operations, restoring the transform afterwards. -    /// -    /// This method is useful to compose transforms and perform drawing -    /// operations in different coordinate systems. -    #[inline] -    fn with_save<R>(&mut self, f: impl FnOnce(&mut Self) -> R) -> R { -        self.push_transform(); - -        let result = f(self); - -        self.pop_transform(); - -        result -    } - -    /// Pushes the current transform in the transform stack. -    fn push_transform(&mut self); - -    /// Pops a transform from the transform stack and sets it as the current transform. -    fn pop_transform(&mut self); - -    /// Executes the given drawing operations within a [`Rectangle`] region, -    /// clipping any geometry that overflows its bounds. Any transformations -    /// performed are local to the provided closure. -    /// -    /// This method is useful to perform drawing operations that need to be -    /// clipped. -    #[inline] -    fn with_clip<R>( -        &mut self, -        region: Rectangle, -        f: impl FnOnce(&mut Self) -> R, -    ) -> R { -        let mut frame = self.draft(region.size()); - -        let result = f(&mut frame); - -        let origin = Point::new(region.x, region.y); - -        self.paste(frame, origin); - -        result -    } - -    /// Creates a new [`Frame`] with the given [`Size`]. -    /// -    /// Draw its contents back to this [`Frame`] with [`paste`]. -    /// -    /// [`paste`]: Self::paste -    fn draft(&mut self, size: Size) -> Self; - -    /// Draws the contents of the given [`Frame`] with origin at the given [`Point`]. -    fn paste(&mut self, frame: Self, at: Point); - -    /// Applies a translation to the current transform of the [`Frame`]. -    fn translate(&mut self, translation: Vector); - -    /// Applies a rotation in radians to the current transform of the [`Frame`]. -    fn rotate(&mut self, angle: impl Into<Radians>); - -    /// Applies a uniform scaling to the current transform of the [`Frame`]. -    fn scale(&mut self, scale: impl Into<f32>); - -    /// Applies a non-uniform scaling to the current transform of the [`Frame`]. -    fn scale_nonuniform(&mut self, scale: impl Into<Vector>); -} -  pub trait Geometry: Sized {      type Cache; @@ -171,120 +48,3 @@ pub trait Geometry: Sized {      fn cache(self) -> Self::Cache;  } - -/// A simple cache that stores generated [`Geometry`] to avoid recomputation. -/// -/// A [`Cache`] will not redraw its geometry unless the dimensions of its layer -/// change or it is explicitly cleared. -pub struct Cache<Renderer> -where -    Renderer: self::Renderer, -{ -    state: RefCell<State<Renderer::Geometry>>, -} - -impl<Renderer> Cache<Renderer> -where -    Renderer: self::Renderer, -{ -    /// Creates a new empty [`Cache`]. -    pub fn new() -> Self { -        Cache { -            state: RefCell::new(State::Empty), -        } -    } - -    /// Clears the [`Cache`], forcing a redraw the next time it is used. -    pub fn clear(&self) { -        *self.state.borrow_mut() = State::Empty; -    } - -    /// Draws [`Geometry`] using the provided closure and stores it in the -    /// [`Cache`]. -    /// -    /// The closure will only be called when -    /// - the bounds have changed since the previous draw call. -    /// - the [`Cache`] is empty or has been explicitly cleared. -    /// -    /// Otherwise, the previously stored [`Geometry`] will be returned. The -    /// [`Cache`] is not cleared in this case. In other words, it will keep -    /// returning the stored [`Geometry`] if needed. -    pub fn draw( -        &self, -        renderer: &Renderer, -        bounds: Size, -        draw_fn: impl FnOnce(&mut Renderer::Frame), -    ) -> Renderer::Geometry { -        use std::ops::Deref; - -        if let State::Filled { -            bounds: cached_bounds, -            geometry, -        } = self.state.borrow().deref() -        { -            if *cached_bounds == bounds { -                return Geometry::load(geometry); -            } -        } - -        let mut frame = frame(renderer, bounds); -        draw_fn(&mut frame); - -        let geometry = frame.into().cache(); -        let result = Geometry::load(&geometry); - -        *self.state.borrow_mut() = State::Filled { bounds, geometry }; - -        result -    } -} - -impl<Renderer> std::fmt::Debug for Cache<Renderer> -where -    Renderer: self::Renderer, -{ -    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { -        let state = self.state.borrow(); - -        match *state { -            State::Empty => write!(f, "Cache::Empty"), -            State::Filled { bounds, .. } => { -                write!(f, "Cache::Filled {{ bounds: {bounds:?} }}") -            } -        } -    } -} - -impl<Renderer> Default for Cache<Renderer> -where -    Renderer: self::Renderer, -{ -    fn default() -> Self { -        Self::new() -    } -} - -enum State<Geometry> -where -    Geometry: self::Geometry, -{ -    Empty, -    Filled { -        bounds: Size, -        geometry: Geometry::Cache, -    }, -} - -impl<T> Geometry for Primitive<T> { -    type Cache = Arc<Self>; - -    fn load(cache: &Arc<Self>) -> Self { -        Self::Cache { -            content: cache.clone(), -        } -    } - -    fn cache(self) -> Arc<Self> { -        Arc::new(self) -    } -} diff --git a/graphics/src/geometry/cache.rs b/graphics/src/geometry/cache.rs new file mode 100644 index 00000000..490e69e2 --- /dev/null +++ b/graphics/src/geometry/cache.rs @@ -0,0 +1,123 @@ +use crate::core::Size; +use crate::geometry::{self, Frame, Geometry}; +use crate::Primitive; + +use std::cell::RefCell; +use std::sync::Arc; + +/// A simple cache that stores generated [`Geometry`] to avoid recomputation. +/// +/// A [`Cache`] will not redraw its geometry unless the dimensions of its layer +/// change or it is explicitly cleared. +pub struct Cache<Renderer> +where +    Renderer: geometry::Renderer, +{ +    state: RefCell<State<Renderer::Geometry>>, +} + +impl<Renderer> Cache<Renderer> +where +    Renderer: geometry::Renderer, +{ +    /// Creates a new empty [`Cache`]. +    pub fn new() -> Self { +        Cache { +            state: RefCell::new(State::Empty), +        } +    } + +    /// Clears the [`Cache`], forcing a redraw the next time it is used. +    pub fn clear(&self) { +        *self.state.borrow_mut() = State::Empty; +    } + +    /// Draws [`Geometry`] using the provided closure and stores it in the +    /// [`Cache`]. +    /// +    /// The closure will only be called when +    /// - the bounds have changed since the previous draw call. +    /// - the [`Cache`] is empty or has been explicitly cleared. +    /// +    /// Otherwise, the previously stored [`Geometry`] will be returned. The +    /// [`Cache`] is not cleared in this case. In other words, it will keep +    /// returning the stored [`Geometry`] if needed. +    pub fn draw( +        &self, +        renderer: &Renderer, +        bounds: Size, +        draw_fn: impl FnOnce(&mut Frame<Renderer>), +    ) -> Renderer::Geometry { +        use std::ops::Deref; + +        if let State::Filled { +            bounds: cached_bounds, +            geometry, +        } = self.state.borrow().deref() +        { +            if *cached_bounds == bounds { +                return Geometry::load(geometry); +            } +        } + +        let mut frame = Frame::new(renderer, bounds); +        draw_fn(&mut frame); + +        let geometry = frame.into_geometry().cache(); +        let result = Geometry::load(&geometry); + +        *self.state.borrow_mut() = State::Filled { bounds, geometry }; + +        result +    } +} + +impl<Renderer> std::fmt::Debug for Cache<Renderer> +where +    Renderer: geometry::Renderer, +{ +    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +        let state = self.state.borrow(); + +        match *state { +            State::Empty => write!(f, "Cache::Empty"), +            State::Filled { bounds, .. } => { +                write!(f, "Cache::Filled {{ bounds: {bounds:?} }}") +            } +        } +    } +} + +impl<Renderer> Default for Cache<Renderer> +where +    Renderer: geometry::Renderer, +{ +    fn default() -> Self { +        Self::new() +    } +} + +enum State<Geometry> +where +    Geometry: self::Geometry, +{ +    Empty, +    Filled { +        bounds: Size, +        geometry: Geometry::Cache, +    }, +} + +impl<T> Geometry for Primitive<T> { +    type Cache = Arc<Self>; + +    fn load(cache: &Arc<Self>) -> Self { +        Self::Cache { +            content: cache.clone(), +        } +    } + +    fn cache(self) -> Arc<Self> { +        Arc::new(self) +    } +} diff --git a/graphics/src/geometry/frame.rs b/graphics/src/geometry/frame.rs new file mode 100644 index 00000000..e88c43b0 --- /dev/null +++ b/graphics/src/geometry/frame.rs @@ -0,0 +1,208 @@ +use crate::core::{Point, Radians, Rectangle, Size, Vector}; +use crate::geometry::{self, Geometry}; +use crate::geometry::{Fill, Path, Stroke, Text}; + +pub struct Frame<Renderer> +where +    Renderer: geometry::Renderer, +{ +    raw: Renderer::Frame, +} + +impl<Renderer> Frame<Renderer> +where +    Renderer: geometry::Renderer, +{ +    pub fn new(renderer: &Renderer, size: Size) -> Self { +        Self { +            raw: renderer.new_frame(size), +        } +    } + +    /// Returns the width of the [`Frame`]. +    pub fn width(&self) -> f32 { +        self.raw.width() +    } + +    /// Returns the height of the [`Frame`]. +    pub fn height(&self) -> f32 { +        self.raw.height() +    } + +    /// Returns the dimensions of the [`Frame`]. +    pub fn size(&self) -> Size { +        self.raw.size() +    } + +    /// Returns the coordinate of the center of the [`Frame`]. +    pub fn center(&self) -> Point { +        self.raw.center() +    } + +    /// Draws the given [`Path`] on the [`Frame`] by filling it with the +    /// provided style. +    pub fn fill(&mut self, path: &Path, fill: impl Into<Fill>) { +        self.raw.fill(path, fill); +    } + +    /// Draws an axis-aligned rectangle given its top-left corner coordinate and +    /// its `Size` on the [`Frame`] by filling it with the provided style. +    pub fn fill_rectangle( +        &mut self, +        top_left: Point, +        size: Size, +        fill: impl Into<Fill>, +    ) { +        self.raw.fill_rectangle(top_left, size, fill); +    } + +    /// Draws the stroke of the given [`Path`] on the [`Frame`] with the +    /// provided style. +    pub fn stroke<'a>(&mut self, path: &Path, stroke: impl Into<Stroke<'a>>) { +        self.raw.stroke(path, stroke); +    } + +    /// Draws the characters of the given [`Text`] on the [`Frame`], filling +    /// them with the given color. +    /// +    /// __Warning:__ Text currently does not work well with rotations and scale +    /// transforms! The position will be correctly transformed, but the +    /// resulting glyphs will not be rotated or scaled properly. +    /// +    /// Additionally, all text will be rendered on top of all the layers of +    /// a `Canvas`. Therefore, it is currently only meant to be used for +    /// overlays, which is the most common use case. +    /// +    /// Support for vectorial text is planned, and should address all these +    /// limitations. +    pub fn fill_text(&mut self, text: impl Into<Text>) { +        self.raw.fill_text(text); +    } + +    /// Stores the current transform of the [`Frame`] and executes the given +    /// drawing operations, restoring the transform afterwards. +    /// +    /// This method is useful to compose transforms and perform drawing +    /// operations in different coordinate systems. +    #[inline] +    pub fn with_save<R>(&mut self, f: impl FnOnce(&mut Self) -> R) -> R { +        self.push_transform(); + +        let result = f(self); + +        self.pop_transform(); + +        result +    } + +    /// Pushes the current transform in the transform stack. +    pub fn push_transform(&mut self) { +        self.raw.push_transform(); +    } + +    /// Pops a transform from the transform stack and sets it as the current transform. +    pub fn pop_transform(&mut self) { +        self.raw.pop_transform(); +    } + +    /// Executes the given drawing operations within a [`Rectangle`] region, +    /// clipping any geometry that overflows its bounds. Any transformations +    /// performed are local to the provided closure. +    /// +    /// This method is useful to perform drawing operations that need to be +    /// clipped. +    #[inline] +    pub fn with_clip<R>( +        &mut self, +        region: Rectangle, +        f: impl FnOnce(&mut Self) -> R, +    ) -> R { +        let mut frame = self.draft(region.size()); + +        let result = f(&mut frame); + +        let origin = Point::new(region.x, region.y); + +        self.paste(frame, origin); + +        result +    } + +    /// Creates a new [`Frame`] with the given [`Size`]. +    /// +    /// Draw its contents back to this [`Frame`] with [`paste`]. +    /// +    /// [`paste`]: Self::paste +    pub fn draft(&mut self, size: Size) -> Self { +        Self { +            raw: self.raw.draft(size), +        } +    } + +    /// Draws the contents of the given [`Frame`] with origin at the given [`Point`]. +    pub fn paste(&mut self, frame: Self, at: Point) { +        self.raw.paste(frame.raw, at); +    } + +    /// Applies a translation to the current transform of the [`Frame`]. +    pub fn translate(&mut self, translation: Vector) { +        self.raw.translate(translation); +    } + +    /// Applies a rotation in radians to the current transform of the [`Frame`]. +    pub fn rotate(&mut self, angle: impl Into<Radians>) { +        self.raw.rotate(angle); +    } + +    /// Applies a uniform scaling to the current transform of the [`Frame`]. +    pub fn scale(&mut self, scale: impl Into<f32>) { +        self.raw.scale(scale); +    } + +    /// Applies a non-uniform scaling to the current transform of the [`Frame`]. +    pub fn scale_nonuniform(&mut self, scale: impl Into<Vector>) { +        self.raw.scale_nonuniform(scale); +    } + +    pub fn into_geometry(self) -> Renderer::Geometry { +        self.raw.into_geometry() +    } +} + +/// The internal implementation of a [`Frame`]. +/// +/// Analogous to [`Frame`]. See [`Frame`] for the documentation +/// of each method. +#[allow(missing_docs)] +pub trait Backend: Sized { +    type Geometry: Geometry; + +    fn width(&self) -> f32; +    fn height(&self) -> f32; +    fn size(&self) -> Size; +    fn center(&self) -> Point; + +    fn push_transform(&mut self); +    fn pop_transform(&mut self); + +    fn translate(&mut self, translation: Vector); +    fn rotate(&mut self, angle: impl Into<Radians>); +    fn scale(&mut self, scale: impl Into<f32>); +    fn scale_nonuniform(&mut self, scale: impl Into<Vector>); + +    fn draft(&mut self, size: Size) -> Self; +    fn paste(&mut self, frame: Self, at: Point); + +    fn stroke<'a>(&mut self, path: &Path, stroke: impl Into<Stroke<'a>>); + +    fn fill(&mut self, path: &Path, fill: impl Into<Fill>); +    fn fill_text(&mut self, text: impl Into<Text>); +    fn fill_rectangle( +        &mut self, +        top_left: Point, +        size: Size, +        fill: impl Into<Fill>, +    ); + +    fn into_geometry(self) -> Self::Geometry; +} diff --git a/graphics/src/renderer.rs b/graphics/src/renderer.rs index 3b21aa11..2fcb55aa 100644 --- a/graphics/src/renderer.rs +++ b/graphics/src/renderer.rs @@ -257,7 +257,8 @@ impl<B: Backend> mesh::Renderer for Renderer<B> {  impl<B> crate::geometry::Renderer for Renderer<B>  where      B: Backend + crate::geometry::Backend, -    B::Frame: crate::geometry::Frame<Geometry = Primitive<B::Primitive>>, +    B::Frame: +        crate::geometry::frame::Backend<Geometry = Primitive<B::Primitive>>,  {      type Frame = B::Frame;      type Geometry = Primitive<B::Primitive>; | 
