From 40f45d7b7e35dd4937abe6b5ce16b6256b4f1eeb Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 29 Sep 2022 10:52:58 -0700 Subject: Adds linear gradient support to 2D meshes in the canvas widget. --- graphics/src/widget/canvas/stroke.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'graphics/src/widget/canvas/stroke.rs') diff --git a/graphics/src/widget/canvas/stroke.rs b/graphics/src/widget/canvas/stroke.rs index 6accc2fb..c319b398 100644 --- a/graphics/src/widget/canvas/stroke.rs +++ b/graphics/src/widget/canvas/stroke.rs @@ -1,10 +1,14 @@ use iced_native::Color; +use crate::widget::canvas::Gradient; + /// The style of a stroke. -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)] pub struct Stroke<'a> { - /// The color of the stroke. - pub color: Color, + /// The color or gradient of the stroke. + /// + /// By default, it is set to [`StrokeStyle::Solid`] `BLACK`. + pub style: StrokeStyle<'a>, /// The distance between the two edges of the stroke. pub width: f32, /// The shape to be used at the end of open subpaths when they are stroked. @@ -19,7 +23,10 @@ pub struct Stroke<'a> { impl<'a> Stroke<'a> { /// Sets the color of the [`Stroke`]. pub fn with_color(self, color: Color) -> Self { - Stroke { color, ..self } + Stroke { + style: StrokeStyle::Solid(color), + ..self + } } /// Sets the width of the [`Stroke`]. @@ -41,7 +48,7 @@ impl<'a> Stroke<'a> { impl<'a> Default for Stroke<'a> { fn default() -> Self { Stroke { - color: Color::BLACK, + style: StrokeStyle::Solid(Color::BLACK), width: 1.0, line_cap: LineCap::default(), line_join: LineJoin::default(), @@ -50,6 +57,15 @@ impl<'a> Default for Stroke<'a> { } } +/// The color or gradient of a [`Stroke`]. +#[derive(Debug, Clone, Copy)] +pub enum StrokeStyle<'a> { + /// A solid color + Solid(Color), + /// A color gradient + Gradient(&'a Gradient), +} + /// The shape used at the end of open subpaths when they are stroked. #[derive(Debug, Clone, Copy)] pub enum LineCap { -- cgit From 5d0fffc626928177239336757507b986b081b878 Mon Sep 17 00:00:00 2001 From: shan Date: Fri, 30 Sep 2022 10:27:00 -0700 Subject: Fixed some importing issues since you can use a Shader::Gradient outside a Canvas widget, where it was previously only accessible. --- graphics/src/widget/canvas/stroke.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'graphics/src/widget/canvas/stroke.rs') diff --git a/graphics/src/widget/canvas/stroke.rs b/graphics/src/widget/canvas/stroke.rs index c319b398..ed82f189 100644 --- a/graphics/src/widget/canvas/stroke.rs +++ b/graphics/src/widget/canvas/stroke.rs @@ -1,6 +1,6 @@ use iced_native::Color; - -use crate::widget::canvas::Gradient; +use crate::gradient::Gradient; +use crate::shader::Shader; /// The style of a stroke. #[derive(Debug, Clone)] @@ -66,6 +66,15 @@ pub enum StrokeStyle<'a> { Gradient(&'a Gradient), } +impl <'a> Into for StrokeStyle<'a> { + fn into(self) -> Shader { + match self { + StrokeStyle::Solid(color) => Shader::Solid(color), + StrokeStyle::Gradient(gradient) => gradient.clone().into() + } + } +} + /// The shape used at the end of open subpaths when they are stroked. #[derive(Debug, Clone, Copy)] pub enum LineCap { -- cgit From 6e7b3ced0b1daf368e44e181ecdb4ae529877eb6 Mon Sep 17 00:00:00 2001 From: shan Date: Tue, 4 Oct 2022 18:24:46 -0700 Subject: Reworked wgpu buffers, updated glow side to have proper transform location storage, attempting to fix visibility modifiers, implemented some of the feedback received in initial PR. --- graphics/src/widget/canvas/stroke.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'graphics/src/widget/canvas/stroke.rs') diff --git a/graphics/src/widget/canvas/stroke.rs b/graphics/src/widget/canvas/stroke.rs index ed82f189..7ce5ff1d 100644 --- a/graphics/src/widget/canvas/stroke.rs +++ b/graphics/src/widget/canvas/stroke.rs @@ -8,7 +8,7 @@ pub struct Stroke<'a> { /// The color or gradient of the stroke. /// /// By default, it is set to [`StrokeStyle::Solid`] `BLACK`. - pub style: StrokeStyle<'a>, + pub style: Style<'a>, /// The distance between the two edges of the stroke. pub width: f32, /// The shape to be used at the end of open subpaths when they are stroked. @@ -24,7 +24,7 @@ impl<'a> Stroke<'a> { /// Sets the color of the [`Stroke`]. pub fn with_color(self, color: Color) -> Self { Stroke { - style: StrokeStyle::Solid(color), + style: Style::Solid(color), ..self } } @@ -48,7 +48,7 @@ impl<'a> Stroke<'a> { impl<'a> Default for Stroke<'a> { fn default() -> Self { Stroke { - style: StrokeStyle::Solid(Color::BLACK), + style: Style::Solid(Color::BLACK), width: 1.0, line_cap: LineCap::default(), line_join: LineJoin::default(), @@ -59,18 +59,18 @@ impl<'a> Default for Stroke<'a> { /// The color or gradient of a [`Stroke`]. #[derive(Debug, Clone, Copy)] -pub enum StrokeStyle<'a> { +pub enum Style<'a> { /// A solid color Solid(Color), /// A color gradient Gradient(&'a Gradient), } -impl <'a> Into for StrokeStyle<'a> { +impl <'a> Into for Style<'a> { fn into(self) -> Shader { match self { - StrokeStyle::Solid(color) => Shader::Solid(color), - StrokeStyle::Gradient(gradient) => gradient.clone().into() + Style::Solid(color) => Shader::Solid(color), + Style::Gradient(gradient) => gradient.clone().into() } } } -- cgit From 30432cbade3d9b25c4df62656a7494db3f4ea82a Mon Sep 17 00:00:00 2001 From: shan Date: Wed, 5 Oct 2022 10:49:58 -0700 Subject: Readjusted namespaces, removed Geometry example as it's no longer relevant. --- graphics/src/widget/canvas/stroke.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'graphics/src/widget/canvas/stroke.rs') diff --git a/graphics/src/widget/canvas/stroke.rs b/graphics/src/widget/canvas/stroke.rs index 7ce5ff1d..a19937ea 100644 --- a/graphics/src/widget/canvas/stroke.rs +++ b/graphics/src/widget/canvas/stroke.rs @@ -1,6 +1,8 @@ +//! Create lines from a [crate::widget::canvas::Path] and render with various attributes/styles. + use iced_native::Color; use crate::gradient::Gradient; -use crate::shader::Shader; +use crate::layer::mesh; /// The style of a stroke. #[derive(Debug, Clone)] @@ -66,10 +68,10 @@ pub enum Style<'a> { Gradient(&'a Gradient), } -impl <'a> Into for Style<'a> { - fn into(self) -> Shader { +impl <'a> Into for Style<'a> { + fn into(self) -> mesh::Style { match self { - Style::Solid(color) => Shader::Solid(color), + Style::Solid(color) => mesh::Style::Solid(color), Style::Gradient(gradient) => gradient.clone().into() } } -- cgit 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/widget/canvas/stroke.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'graphics/src/widget/canvas/stroke.rs') 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 From f9a6efcaa03728f43aaa105af8936c1ed4778388 Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 6 Oct 2022 19:41:00 -0700 Subject: Fixed some more imports/documentation. --- graphics/src/widget/canvas/stroke.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'graphics/src/widget/canvas/stroke.rs') diff --git a/graphics/src/widget/canvas/stroke.rs b/graphics/src/widget/canvas/stroke.rs index aaac15bb..5cb63a91 100644 --- a/graphics/src/widget/canvas/stroke.rs +++ b/graphics/src/widget/canvas/stroke.rs @@ -1,4 +1,4 @@ -//! Create lines from a [crate::widget::canvas::Path] and render with various attributes/styles. +//! Create lines from a [crate::widget::canvas::Path] and assigns them various attributes/styles. use iced_native::Color; use crate::gradient::Gradient; @@ -60,7 +60,7 @@ impl<'a> Default for Stroke<'a> { } } -/// The color or gradient of a [`Stroke`]. +/// The style of a [`Stroke`]. #[derive(Debug, Clone, Copy)] pub enum Style<'a> { /// A solid color -- cgit From a4a1262fa2d625be5ad7a37e409e0e9c399b09b6 Mon Sep 17 00:00:00 2001 From: shan Date: Fri, 7 Oct 2022 16:28:13 -0700 Subject: Fixed import issue with canvas in the gradient mod for situations where canvas feature is not enabled. --- graphics/src/widget/canvas/stroke.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'graphics/src/widget/canvas/stroke.rs') diff --git a/graphics/src/widget/canvas/stroke.rs b/graphics/src/widget/canvas/stroke.rs index 5cb63a91..6e49f994 100644 --- a/graphics/src/widget/canvas/stroke.rs +++ b/graphics/src/widget/canvas/stroke.rs @@ -77,7 +77,11 @@ impl<'a> Style<'a> { mesh::Style::Solid(*color) }, Style::Gradient(gradient) => { - mesh::Style::Gradient((*gradient).clone().transform(transform)) + let mut gradient = (*gradient).clone(); + let coordinates = gradient.coords(); + transform.transform_point(coordinates.0); + transform.transform_point(coordinates.1); + mesh::Style::Gradient(gradient) } } } -- cgit From fd5e1e5ab0b712cd6719733b5a68602176ae5ec4 Mon Sep 17 00:00:00 2001 From: shan Date: Fri, 7 Oct 2022 16:55:55 -0700 Subject: Adjusted gradient transform function to be more readable. --- graphics/src/widget/canvas/stroke.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'graphics/src/widget/canvas/stroke.rs') diff --git a/graphics/src/widget/canvas/stroke.rs b/graphics/src/widget/canvas/stroke.rs index 6e49f994..2f02a2f3 100644 --- a/graphics/src/widget/canvas/stroke.rs +++ b/graphics/src/widget/canvas/stroke.rs @@ -1,9 +1,9 @@ //! Create lines from a [crate::widget::canvas::Path] and assigns them various attributes/styles. -use iced_native::Color; use crate::gradient::Gradient; use crate::layer::mesh; use crate::widget::canvas::frame::Transform; +use iced_native::Color; /// The style of a stroke. #[derive(Debug, Clone)] @@ -73,16 +73,10 @@ 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) => { - let mut gradient = (*gradient).clone(); - let coordinates = gradient.coords(); - transform.transform_point(coordinates.0); - transform.transform_point(coordinates.1); - mesh::Style::Gradient(gradient) - } + Style::Solid(color) => mesh::Style::Solid(*color), + Style::Gradient(gradient) => mesh::Style::Gradient( + transform.transform_gradient((*gradient).clone()), + ), } } } -- cgit From 84d1b79fefc88534835fdfbe79bc0eb3b43627cf Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 05:50:53 +0100 Subject: Move `mesh::Style` to `triangle` and reuse it in `fill` and `stroke` --- graphics/src/widget/canvas/stroke.rs | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) (limited to 'graphics/src/widget/canvas/stroke.rs') diff --git a/graphics/src/widget/canvas/stroke.rs b/graphics/src/widget/canvas/stroke.rs index 2f02a2f3..f9b8e447 100644 --- a/graphics/src/widget/canvas/stroke.rs +++ b/graphics/src/widget/canvas/stroke.rs @@ -1,8 +1,6 @@ //! Create lines from a [crate::widget::canvas::Path] and assigns them various attributes/styles. +pub use crate::triangle::Style; -use crate::gradient::Gradient; -use crate::layer::mesh; -use crate::widget::canvas::frame::Transform; use iced_native::Color; /// The style of a stroke. @@ -11,7 +9,7 @@ pub struct Stroke<'a> { /// The color or gradient of the stroke. /// /// By default, it is set to [`StrokeStyle::Solid`] `BLACK`. - pub style: Style<'a>, + pub style: Style, /// The distance between the two edges of the stroke. pub width: f32, /// The shape to be used at the end of open subpaths when they are stroked. @@ -60,27 +58,6 @@ impl<'a> Default for Stroke<'a> { } } -/// The style of a [`Stroke`]. -#[derive(Debug, Clone, Copy)] -pub enum Style<'a> { - /// A solid color - Solid(Color), - /// A color gradient - Gradient(&'a Gradient), -} - -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) => mesh::Style::Gradient( - transform.transform_gradient((*gradient).clone()), - ), - } - } -} - /// The shape used at the end of open subpaths when they are stroked. #[derive(Debug, Clone, Copy)] pub enum LineCap { -- cgit From 1480ab20306e463b69b2229dcd5e81d4c66b2a64 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 10 Nov 2022 00:10:53 +0100 Subject: Fix broken documentation links --- graphics/src/widget/canvas/stroke.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'graphics/src/widget/canvas/stroke.rs') diff --git a/graphics/src/widget/canvas/stroke.rs b/graphics/src/widget/canvas/stroke.rs index f9b8e447..a882531a 100644 --- a/graphics/src/widget/canvas/stroke.rs +++ b/graphics/src/widget/canvas/stroke.rs @@ -8,7 +8,7 @@ use iced_native::Color; pub struct Stroke<'a> { /// The color or gradient of the stroke. /// - /// By default, it is set to [`StrokeStyle::Solid`] `BLACK`. + /// By default, it is set to a [`Style::Solid`] with [`Color::BLACK`]. pub style: Style, /// The distance between the two edges of the stroke. pub width: f32, -- cgit From 33c3c0c0aa774bb7462e3c42aa04c591a66376a7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 14 Nov 2022 00:02:42 +0100 Subject: Group all solid triangles independently of color --- graphics/src/widget/canvas/stroke.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'graphics/src/widget/canvas/stroke.rs') diff --git a/graphics/src/widget/canvas/stroke.rs b/graphics/src/widget/canvas/stroke.rs index a882531a..4c19251d 100644 --- a/graphics/src/widget/canvas/stroke.rs +++ b/graphics/src/widget/canvas/stroke.rs @@ -1,5 +1,5 @@ //! Create lines from a [crate::widget::canvas::Path] and assigns them various attributes/styles. -pub use crate::triangle::Style; +pub use crate::widget::canvas::Style; use iced_native::Color; -- cgit