From 9c7bf417ac9c1ac72bcc55aa3cd5e8eb962243a2 Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 6 Oct 2022 16:57:38 -0700 Subject: Added support for gradients to respect current frame transform. --- graphics/src/gradient.rs | 14 +++++++++++++- graphics/src/widget/canvas.rs | 2 +- graphics/src/widget/canvas/fill.rs | 14 ++++++++++---- graphics/src/widget/canvas/frame.rs | 32 ++++++++++++++++++++++---------- graphics/src/widget/canvas/stroke.rs | 14 ++++++++++---- 5 files changed, 56 insertions(+), 20 deletions(-) (limited to 'graphics') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 13600fb9..683a7413 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -1,9 +1,10 @@ //! For creating a Gradient. mod linear; -use iced_native::Color; pub use crate::gradient::linear::Linear; +use crate::widget::canvas::frame::Transform; use crate::Point; +use iced_native::Color; #[derive(Debug, Clone, PartialEq)] /// A fill which transitions colors progressively along a direction, either linearly, radially (TBD), @@ -28,4 +29,15 @@ impl Gradient { pub fn linear(start: Point, end: Point) -> linear::Builder { linear::Builder::new(start, end) } + + /// Modifies the start & end stops of the gradient to have a proper transform value. + pub(crate) fn transform(mut self, transform: &Transform) -> Self { + match &mut self { + Gradient::Linear(linear) => { + linear.start = transform.apply_to(linear.start); + linear.end = transform.apply_to(linear.end); + } + } + self + } } diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs index 4a2c5b2d..fe0f618f 100644 --- a/graphics/src/widget/canvas.rs +++ b/graphics/src/widget/canvas.rs @@ -9,12 +9,12 @@ pub mod path; mod cache; mod cursor; -mod frame; mod geometry; mod program; mod text; pub mod fill; pub mod stroke; +pub(crate) mod frame; pub use cache::Cache; pub use cursor::Cursor; diff --git a/graphics/src/widget/canvas/fill.rs b/graphics/src/widget/canvas/fill.rs index 55cb3966..768c2b9c 100644 --- a/graphics/src/widget/canvas/fill.rs +++ b/graphics/src/widget/canvas/fill.rs @@ -3,6 +3,7 @@ use crate::gradient::Gradient; use crate::layer::mesh; use iced_native::Color; +use crate::widget::canvas::frame::Transform; /// The style used to fill geometry. #[derive(Debug, Clone)] @@ -50,11 +51,16 @@ pub enum Style<'a> { Gradient(&'a Gradient), } -impl<'a> Into for Style<'a> { - fn into(self) -> mesh::Style { +impl<'a> Style<'a> { + /// Converts a fill's [Style] to a [mesh::Style] for use in the renderer's shader. + pub(crate) fn as_mesh_style(&self, transform: &Transform) -> mesh::Style { match self { - Style::Solid(color) => mesh::Style::Solid(color), - Style::Gradient(gradient) => gradient.clone().into(), + Style::Solid(color) => { + mesh::Style::Solid(*color) + }, + Style::Gradient(gradient) => { + mesh::Style::Gradient((*gradient).clone().transform(transform)) + } } } } diff --git a/graphics/src/widget/canvas/frame.rs b/graphics/src/widget/canvas/frame.rs index 427a2e2a..ccba840a 100644 --- a/graphics/src/widget/canvas/frame.rs +++ b/graphics/src/widget/canvas/frame.rs @@ -1,3 +1,4 @@ +use lyon::geom::euclid::Vector2D; use std::borrow::Cow; use iced_native::{Point, Rectangle, Size, Vector}; @@ -30,11 +31,22 @@ struct Transforms { } #[derive(Debug, Clone, Copy)] -struct Transform { +pub(crate) struct Transform { raw: lyon::math::Transform, is_identity: bool, } +impl Transform { + /// Transforms the given [Point] by the transformation matrix. + pub(crate) fn apply_to(&self, mut point: Point) -> Point { + let transformed = + self.raw.transform_vector(Vector2D::new(point.x, point.y)); + point.x = transformed.x + self.raw.m31; + point.y = transformed.y + self.raw.m32; + point + } +} + impl Frame { /// Creates a new empty [`Frame`] with the given dimensions. /// @@ -111,7 +123,8 @@ impl Frame { } .expect("Tessellate path."); - self.buffers.push((buf, style.into())) + self.buffers + .push((buf, style.as_mesh_style(&self.transforms.current))); } /// Draws an axis-aligned rectangle given its top-left corner coordinate and @@ -150,7 +163,8 @@ impl Frame { ) .expect("Fill rectangle"); - self.buffers.push((buf, style.into())) + self.buffers + .push((buf, style.as_mesh_style(&self.transforms.current))); } /// Draws the stroke of the given [`Path`] on the [`Frame`] with the @@ -192,7 +206,8 @@ impl Frame { } .expect("Stroke path"); - self.buffers.push((buf, stroke.style.into())) + self.buffers + .push((buf, stroke.style.as_mesh_style(&self.transforms.current))) } /// Draws the characters of the given [`Text`] on the [`Frame`], filling @@ -307,7 +322,7 @@ impl Frame { self.transforms.current.is_identity = false; } - /// Applies a rotation to the current transform of the [`Frame`]. + /// Applies a rotation in radians to the current transform of the [`Frame`]. #[inline] pub fn rotate(&mut self, angle: f32) { self.transforms.current.raw = self @@ -354,10 +369,7 @@ impl Frame { struct Vertex2DBuilder; impl tessellation::FillVertexConstructor for Vertex2DBuilder { - fn new_vertex( - &mut self, - vertex: tessellation::FillVertex<'_>, - ) -> Vertex2D { + fn new_vertex(&mut self, vertex: tessellation::FillVertex<'_>) -> Vertex2D { let position = vertex.position(); Vertex2D { @@ -377,4 +389,4 @@ impl tessellation::StrokeVertexConstructor for Vertex2DBuilder { position: [position.x, position.y], } } -} \ No newline at end of file +} diff --git a/graphics/src/widget/canvas/stroke.rs b/graphics/src/widget/canvas/stroke.rs index a19937ea..aaac15bb 100644 --- a/graphics/src/widget/canvas/stroke.rs +++ b/graphics/src/widget/canvas/stroke.rs @@ -3,6 +3,7 @@ use iced_native::Color; use crate::gradient::Gradient; use crate::layer::mesh; +use crate::widget::canvas::frame::Transform; /// The style of a stroke. #[derive(Debug, Clone)] @@ -68,11 +69,16 @@ pub enum Style<'a> { Gradient(&'a Gradient), } -impl <'a> Into for Style<'a> { - fn into(self) -> mesh::Style { +impl<'a> Style<'a> { + /// Converts a fill's [Style] to a [mesh::Style] for use in the renderer's shader. + pub(crate) fn as_mesh_style(&self, transform: &Transform) -> mesh::Style { match self { - Style::Solid(color) => mesh::Style::Solid(color), - Style::Gradient(gradient) => gradient.clone().into() + Style::Solid(color) => { + mesh::Style::Solid(*color) + }, + Style::Gradient(gradient) => { + mesh::Style::Gradient((*gradient).clone().transform(transform)) + } } } } -- cgit