From 188db4da48954b95a3fe79bcd22689ffc3a661e0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 21 Mar 2024 05:52:48 +0100 Subject: Draft support for dynamic custom renderer injection --- renderer/src/geometry.rs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'renderer/src/geometry.rs') diff --git a/renderer/src/geometry.rs b/renderer/src/geometry.rs index 36435148..bf70c7fa 100644 --- a/renderer/src/geometry.rs +++ b/renderer/src/geometry.rs @@ -3,6 +3,7 @@ mod cache; pub use cache::Cache; use crate::core::{Point, Radians, Rectangle, Size, Transformation, Vector}; +use crate::custom; use crate::graphics::geometry::{Fill, Path, Stroke, Text}; use crate::Renderer; @@ -12,6 +13,7 @@ macro_rules! delegate { Self::TinySkia($name) => $body, #[cfg(feature = "wgpu")] Self::Wgpu($name) => $body, + Self::Custom($name) => $body, } }; } @@ -20,6 +22,7 @@ pub enum Geometry { TinySkia(iced_tiny_skia::Primitive), #[cfg(feature = "wgpu")] Wgpu(iced_wgpu::Primitive), + Custom(Box), } impl Geometry { @@ -32,6 +35,9 @@ impl Geometry { Self::Wgpu(primitive) => { Self::Wgpu(primitive.transform(transformation)) } + Self::Custom(geometry) => { + Self::Custom(geometry.transform(transformation)) + } } } } @@ -40,6 +46,7 @@ pub enum Frame { TinySkia(iced_tiny_skia::geometry::Frame), #[cfg(feature = "wgpu")] Wgpu(iced_wgpu::geometry::Frame), + Custom(Box), } impl Frame { @@ -52,6 +59,9 @@ impl Frame { Renderer::Wgpu(_) => { Frame::Wgpu(iced_wgpu::geometry::Frame::new(size)) } + Renderer::Custom(renderer) => { + Frame::Custom(renderer.new_frame(size)) + } } } @@ -82,7 +92,7 @@ impl Frame { /// Draws the given [`Path`] on the [`Frame`] by filling it with the /// provided style. pub fn fill(&mut self, path: &Path, fill: impl Into) { - delegate!(self, frame, frame.fill(path, fill)); + delegate!(self, frame, frame.fill(path, fill.into())); } /// Draws an axis-aligned rectangle given its top-left corner coordinate and @@ -93,13 +103,17 @@ impl Frame { size: Size, fill: impl Into, ) { - delegate!(self, frame, frame.fill_rectangle(top_left, size, fill)); + delegate!( + self, + frame, + frame.fill_rectangle(top_left, size, fill.into()) + ); } /// 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>) { - delegate!(self, frame, frame.stroke(path, stroke)); + delegate!(self, frame, frame.stroke(path, stroke.into())); } /// Draws the characters of the given [`Text`] on the [`Frame`], filling @@ -116,7 +130,7 @@ impl Frame { /// Support for vectorial text is planned, and should address all these /// limitations. pub fn fill_text(&mut self, text: impl Into) { - delegate!(self, frame, frame.fill_text(text)); + delegate!(self, frame, frame.fill_text(text.into())); } /// Stores the current transform of the [`Frame`] and executes the given @@ -155,6 +169,7 @@ impl Frame { Self::Wgpu(_) => { Self::Wgpu(iced_wgpu::geometry::Frame::new(region.size())) } + Self::Custom(frame) => Self::Custom(frame.new(region.size())), }; let result = f(&mut frame); @@ -169,6 +184,9 @@ impl Frame { (Self::Wgpu(target), Self::Wgpu(frame)) => { target.clip(frame, origin); } + (Self::Custom(target), Self::Custom(frame)) => { + target.clip(frame, origin); + } #[allow(unreachable_patterns)] _ => unreachable!(), }; @@ -185,19 +203,19 @@ impl Frame { /// Applies a rotation in radians to the current transform of the [`Frame`]. #[inline] pub fn rotate(&mut self, angle: impl Into) { - delegate!(self, frame, frame.rotate(angle)); + delegate!(self, frame, frame.rotate(angle.into())); } /// Applies a uniform scaling to the current transform of the [`Frame`]. #[inline] pub fn scale(&mut self, scale: impl Into) { - delegate!(self, frame, frame.scale(scale)); + delegate!(self, frame, frame.scale(scale.into())); } /// Applies a non-uniform scaling to the current transform of the [`Frame`]. #[inline] pub fn scale_nonuniform(&mut self, scale: impl Into) { - delegate!(self, frame, frame.scale_nonuniform(scale)); + delegate!(self, frame, frame.scale_nonuniform(scale.into())); } pub fn into_geometry(self) -> Geometry { @@ -205,6 +223,7 @@ impl Frame { Self::TinySkia(frame) => Geometry::TinySkia(frame.into_primitive()), #[cfg(feature = "wgpu")] Self::Wgpu(frame) => Geometry::Wgpu(frame.into_primitive()), + Self::Custom(frame) => Geometry::Custom(frame.into_geometry()), } } } -- cgit From 9171df1e356530410a7ceadadd78fd3dcf150dfd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 21 Mar 2024 06:00:55 +0100 Subject: Gate `Custom` variants in `iced_renderer` behind `custom` feature --- renderer/src/geometry.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'renderer/src/geometry.rs') diff --git a/renderer/src/geometry.rs b/renderer/src/geometry.rs index bf70c7fa..a16cecd5 100644 --- a/renderer/src/geometry.rs +++ b/renderer/src/geometry.rs @@ -3,7 +3,6 @@ mod cache; pub use cache::Cache; use crate::core::{Point, Radians, Rectangle, Size, Transformation, Vector}; -use crate::custom; use crate::graphics::geometry::{Fill, Path, Stroke, Text}; use crate::Renderer; @@ -13,6 +12,7 @@ macro_rules! delegate { Self::TinySkia($name) => $body, #[cfg(feature = "wgpu")] Self::Wgpu($name) => $body, + #[cfg(feature = "custom")] Self::Custom($name) => $body, } }; @@ -22,7 +22,8 @@ pub enum Geometry { TinySkia(iced_tiny_skia::Primitive), #[cfg(feature = "wgpu")] Wgpu(iced_wgpu::Primitive), - Custom(Box), + #[cfg(feature = "custom")] + Custom(Box), } impl Geometry { @@ -35,6 +36,7 @@ impl Geometry { Self::Wgpu(primitive) => { Self::Wgpu(primitive.transform(transformation)) } + #[cfg(feature = "custom")] Self::Custom(geometry) => { Self::Custom(geometry.transform(transformation)) } @@ -46,7 +48,8 @@ pub enum Frame { TinySkia(iced_tiny_skia::geometry::Frame), #[cfg(feature = "wgpu")] Wgpu(iced_wgpu::geometry::Frame), - Custom(Box), + #[cfg(feature = "custom")] + Custom(Box), } impl Frame { @@ -59,6 +62,7 @@ impl Frame { Renderer::Wgpu(_) => { Frame::Wgpu(iced_wgpu::geometry::Frame::new(size)) } + #[cfg(feature = "custom")] Renderer::Custom(renderer) => { Frame::Custom(renderer.new_frame(size)) } @@ -169,6 +173,7 @@ impl Frame { Self::Wgpu(_) => { Self::Wgpu(iced_wgpu::geometry::Frame::new(region.size())) } + #[cfg(feature = "custom")] Self::Custom(frame) => Self::Custom(frame.new(region.size())), }; @@ -184,6 +189,7 @@ impl Frame { (Self::Wgpu(target), Self::Wgpu(frame)) => { target.clip(frame, origin); } + #[cfg(feature = "custom")] (Self::Custom(target), Self::Custom(frame)) => { target.clip(frame, origin); } @@ -223,6 +229,7 @@ impl Frame { Self::TinySkia(frame) => Geometry::TinySkia(frame.into_primitive()), #[cfg(feature = "wgpu")] Self::Wgpu(frame) => Geometry::Wgpu(frame.into_primitive()), + #[cfg(feature = "custom")] Self::Custom(frame) => Geometry::Custom(frame.into_geometry()), } } -- cgit From 53a183fe0d6aed460fbb8155ac9541757277aab3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 22 Mar 2024 01:35:14 +0100 Subject: Restore `canvas::Frame` API --- renderer/src/geometry.rs | 236 ----------------------------------------------- 1 file changed, 236 deletions(-) delete mode 100644 renderer/src/geometry.rs (limited to 'renderer/src/geometry.rs') diff --git a/renderer/src/geometry.rs b/renderer/src/geometry.rs deleted file mode 100644 index a16cecd5..00000000 --- a/renderer/src/geometry.rs +++ /dev/null @@ -1,236 +0,0 @@ -mod cache; - -pub use cache::Cache; - -use crate::core::{Point, Radians, Rectangle, Size, Transformation, Vector}; -use crate::graphics::geometry::{Fill, Path, Stroke, Text}; -use crate::Renderer; - -macro_rules! delegate { - ($frame:expr, $name:ident, $body:expr) => { - match $frame { - Self::TinySkia($name) => $body, - #[cfg(feature = "wgpu")] - Self::Wgpu($name) => $body, - #[cfg(feature = "custom")] - Self::Custom($name) => $body, - } - }; -} - -pub enum Geometry { - TinySkia(iced_tiny_skia::Primitive), - #[cfg(feature = "wgpu")] - Wgpu(iced_wgpu::Primitive), - #[cfg(feature = "custom")] - Custom(Box), -} - -impl Geometry { - pub fn transform(self, transformation: Transformation) -> Self { - match self { - Self::TinySkia(primitive) => { - Self::TinySkia(primitive.transform(transformation)) - } - #[cfg(feature = "wgpu")] - Self::Wgpu(primitive) => { - Self::Wgpu(primitive.transform(transformation)) - } - #[cfg(feature = "custom")] - Self::Custom(geometry) => { - Self::Custom(geometry.transform(transformation)) - } - } - } -} - -pub enum Frame { - TinySkia(iced_tiny_skia::geometry::Frame), - #[cfg(feature = "wgpu")] - Wgpu(iced_wgpu::geometry::Frame), - #[cfg(feature = "custom")] - Custom(Box), -} - -impl Frame { - pub fn new(renderer: &Renderer, size: Size) -> Self { - match renderer { - Renderer::TinySkia(_) => { - Frame::TinySkia(iced_tiny_skia::geometry::Frame::new(size)) - } - #[cfg(feature = "wgpu")] - Renderer::Wgpu(_) => { - Frame::Wgpu(iced_wgpu::geometry::Frame::new(size)) - } - #[cfg(feature = "custom")] - Renderer::Custom(renderer) => { - Frame::Custom(renderer.new_frame(size)) - } - } - } - - /// Returns the width of the [`Frame`]. - #[inline] - pub fn width(&self) -> f32 { - delegate!(self, frame, frame.width()) - } - - /// Returns the height of the [`Frame`]. - #[inline] - pub fn height(&self) -> f32 { - delegate!(self, frame, frame.height()) - } - - /// Returns the dimensions of the [`Frame`]. - #[inline] - pub fn size(&self) -> Size { - delegate!(self, frame, frame.size()) - } - - /// Returns the coordinate of the center of the [`Frame`]. - #[inline] - pub fn center(&self) -> Point { - delegate!(self, frame, frame.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) { - delegate!(self, frame, frame.fill(path, fill.into())); - } - - /// 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, - ) { - delegate!( - self, - frame, - frame.fill_rectangle(top_left, size, fill.into()) - ); - } - - /// 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>) { - delegate!(self, frame, frame.stroke(path, stroke.into())); - } - - /// 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) { - delegate!(self, frame, frame.fill_text(text.into())); - } - - /// 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(&mut self, f: impl FnOnce(&mut Frame) -> R) -> R { - delegate!(self, frame, frame.push_transform()); - - let result = f(self); - - delegate!(self, frame, frame.pop_transform()); - - result - } - - /// 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( - &mut self, - region: Rectangle, - f: impl FnOnce(&mut Frame) -> R, - ) -> R { - let mut frame = match self { - Self::TinySkia(_) => Self::TinySkia( - iced_tiny_skia::geometry::Frame::new(region.size()), - ), - #[cfg(feature = "wgpu")] - Self::Wgpu(_) => { - Self::Wgpu(iced_wgpu::geometry::Frame::new(region.size())) - } - #[cfg(feature = "custom")] - Self::Custom(frame) => Self::Custom(frame.new(region.size())), - }; - - let result = f(&mut frame); - - let origin = Point::new(region.x, region.y); - - match (self, frame) { - (Self::TinySkia(target), Self::TinySkia(frame)) => { - target.clip(frame, origin); - } - #[cfg(feature = "wgpu")] - (Self::Wgpu(target), Self::Wgpu(frame)) => { - target.clip(frame, origin); - } - #[cfg(feature = "custom")] - (Self::Custom(target), Self::Custom(frame)) => { - target.clip(frame, origin); - } - #[allow(unreachable_patterns)] - _ => unreachable!(), - }; - - result - } - - /// Applies a translation to the current transform of the [`Frame`]. - #[inline] - pub fn translate(&mut self, translation: Vector) { - delegate!(self, frame, frame.translate(translation)); - } - - /// Applies a rotation in radians to the current transform of the [`Frame`]. - #[inline] - pub fn rotate(&mut self, angle: impl Into) { - delegate!(self, frame, frame.rotate(angle.into())); - } - - /// Applies a uniform scaling to the current transform of the [`Frame`]. - #[inline] - pub fn scale(&mut self, scale: impl Into) { - delegate!(self, frame, frame.scale(scale.into())); - } - - /// Applies a non-uniform scaling to the current transform of the [`Frame`]. - #[inline] - pub fn scale_nonuniform(&mut self, scale: impl Into) { - delegate!(self, frame, frame.scale_nonuniform(scale.into())); - } - - pub fn into_geometry(self) -> Geometry { - match self { - Self::TinySkia(frame) => Geometry::TinySkia(frame.into_primitive()), - #[cfg(feature = "wgpu")] - Self::Wgpu(frame) => Geometry::Wgpu(frame.into_primitive()), - #[cfg(feature = "custom")] - Self::Custom(frame) => Geometry::Custom(frame.into_geometry()), - } - } -} -- cgit