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/gradient.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 graphics/src/gradient.rs (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs new file mode 100644 index 00000000..4d1eff62 --- /dev/null +++ b/graphics/src/gradient.rs @@ -0,0 +1,23 @@ +//! For creating a Gradient. + +use iced_native::Color; +use crate::widget::canvas::gradient::Linear; + +#[derive(Debug, Clone, PartialEq)] +/// A fill which transitions colors progressively along a direction, either linearly, radially, +/// or conically. +pub enum Gradient { + /// A linear gradient interpolates colors along a direction from its [`start`] to its [`end`] + /// point. + Linear(Linear), +} + + +#[derive(Debug, Clone, Copy, PartialEq)] +/// A point along the gradient vector where the specified [`color`] is unmixed. +pub struct ColorStop { + /// Offset along the gradient vector. + pub offset: f32, + /// The color of the gradient at the specified [`offset`]. + pub color: Color, +} \ No newline at end of file -- 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/gradient.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 6 deletions(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 4d1eff62..fa57842b 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -1,18 +1,17 @@ //! For creating a Gradient. - use iced_native::Color; -use crate::widget::canvas::gradient::Linear; +use crate::gradient::linear::Linear; +use crate::Point; #[derive(Debug, Clone, PartialEq)] -/// A fill which transitions colors progressively along a direction, either linearly, radially, -/// or conically. +/// A fill which transitions colors progressively along a direction, either linearly, radially (TBD), +/// or conically (TBD). pub enum Gradient { /// A linear gradient interpolates colors along a direction from its [`start`] to its [`end`] /// point. Linear(Linear), } - #[derive(Debug, Clone, Copy, PartialEq)] /// A point along the gradient vector where the specified [`color`] is unmixed. pub struct ColorStop { @@ -20,4 +19,85 @@ pub struct ColorStop { pub offset: f32, /// The color of the gradient at the specified [`offset`]. pub color: Color, -} \ No newline at end of file +} + +impl Gradient { + /// Creates a new linear [`linear::Builder`]. + pub fn linear(start: Point, end: Point) -> linear::Builder { + linear::Builder::new(start, end) + } +} + +/// Linear gradient builder & definition. +pub mod linear { + use crate::gradient::{ColorStop, Gradient}; + use crate::{Color, Point}; + + /// A linear gradient that can be used in the style of [`super::Fill`] or [`super::Stroke`]. + #[derive(Debug, Clone, PartialEq)] + pub struct Linear { + /// The point where the linear gradient begins. + pub start: Point, + /// The point where the linear gradient ends. + pub end: Point, + /// [`ColorStop`]s along the linear gradient path. + pub color_stops: Vec, + } + + /// A [`Linear`] builder. + #[derive(Debug)] + pub struct Builder { + start: Point, + end: Point, + stops: Vec<(f32, Color)>, + valid: bool, + } + + impl Builder { + /// Creates a new [`Builder`]. + pub fn new(start: Point, end: Point) -> Self { + Self { + start, + end, + stops: vec![], + valid: true, + } + } + + /// Adds a new stop, defined by an offset and a color, to the gradient. + /// + /// `offset` must be between `0.0` and `1.0`. + pub fn add_stop(mut self, offset: f32, color: Color) -> Self { + if !(0.0..=1.0).contains(&offset) { + self.valid = false; + } + + self.stops.push((offset, color)); + self + } + + /// Builds the linear [`Gradient`] of this [`Builder`]. + /// + /// Returns `None` if no stops were added to the builder or + /// if stops not between 0.0 and 1.0 were added. + pub fn build(self) -> Option { + if self.stops.is_empty() || !self.valid { + return None; + } + + let mut stops: Vec = self.stops.clone().into_iter().map(|f| ColorStop { + offset: f.0, + color: f.1 + }).collect(); + + stops.sort_by(|a, b| a.offset.partial_cmp(&b.offset).unwrap()); + + Some(Gradient::Linear(Linear { + start: self.start, + end: self.end, + color_stops: stops + })) + } + } +} + -- 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/gradient.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index fa57842b..0c394e8b 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -1,6 +1,6 @@ //! For creating a Gradient. use iced_native::Color; -use crate::gradient::linear::Linear; +pub use crate::gradient::linear::Linear; use crate::Point; #[derive(Debug, Clone, PartialEq)] -- 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/gradient.rs | 75 ++---------------------------------------------- 1 file changed, 2 insertions(+), 73 deletions(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 0c394e8b..33453c67 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -1,4 +1,6 @@ //! For creating a Gradient. +mod linear; + use iced_native::Color; pub use crate::gradient::linear::Linear; use crate::Point; @@ -28,76 +30,3 @@ impl Gradient { } } -/// Linear gradient builder & definition. -pub mod linear { - use crate::gradient::{ColorStop, Gradient}; - use crate::{Color, Point}; - - /// A linear gradient that can be used in the style of [`super::Fill`] or [`super::Stroke`]. - #[derive(Debug, Clone, PartialEq)] - pub struct Linear { - /// The point where the linear gradient begins. - pub start: Point, - /// The point where the linear gradient ends. - pub end: Point, - /// [`ColorStop`]s along the linear gradient path. - pub color_stops: Vec, - } - - /// A [`Linear`] builder. - #[derive(Debug)] - pub struct Builder { - start: Point, - end: Point, - stops: Vec<(f32, Color)>, - valid: bool, - } - - impl Builder { - /// Creates a new [`Builder`]. - pub fn new(start: Point, end: Point) -> Self { - Self { - start, - end, - stops: vec![], - valid: true, - } - } - - /// Adds a new stop, defined by an offset and a color, to the gradient. - /// - /// `offset` must be between `0.0` and `1.0`. - pub fn add_stop(mut self, offset: f32, color: Color) -> Self { - if !(0.0..=1.0).contains(&offset) { - self.valid = false; - } - - self.stops.push((offset, color)); - self - } - - /// Builds the linear [`Gradient`] of this [`Builder`]. - /// - /// Returns `None` if no stops were added to the builder or - /// if stops not between 0.0 and 1.0 were added. - pub fn build(self) -> Option { - if self.stops.is_empty() || !self.valid { - return None; - } - - let mut stops: Vec = self.stops.clone().into_iter().map(|f| ColorStop { - offset: f.0, - color: f.1 - }).collect(); - - stops.sort_by(|a, b| a.offset.partial_cmp(&b.offset).unwrap()); - - Some(Gradient::Linear(Linear { - start: self.start, - end: self.end, - color_stops: stops - })) - } - } -} - -- cgit From cb7c4676543cd508dfae8d4dcbd9cc8b61b1a94e Mon Sep 17 00:00:00 2001 From: shan Date: Thu, 6 Oct 2022 07:28:05 -0700 Subject: Fixed lint issues & cleaned up some documentation. --- graphics/src/gradient.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 33453c67..13600fb9 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -29,4 +29,3 @@ impl Gradient { linear::Builder::new(start, end) } } - -- 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/gradient.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'graphics/src/gradient.rs') 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 + } } -- 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/gradient.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 683a7413..d785fb2a 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -3,8 +3,7 @@ mod linear; pub use crate::gradient::linear::Linear; use crate::widget::canvas::frame::Transform; -use crate::Point; -use iced_native::Color; +use crate::{Point, Color}; #[derive(Debug, Clone, PartialEq)] /// A fill which transitions colors progressively along a direction, either linearly, radially (TBD), -- cgit From 12a87c54eb68b992676060c80e518ffb29445cfc Mon Sep 17 00:00:00 2001 From: shan Date: Fri, 7 Oct 2022 11:41:50 -0700 Subject: Added support for relative positioning of gradient fills. Addressed some PR feedback. --- graphics/src/gradient.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index d785fb2a..e1984854 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -1,9 +1,10 @@ //! For creating a Gradient. mod linear; -pub use crate::gradient::linear::Linear; +pub use crate::gradient::linear::{Linear, Position, Location}; use crate::widget::canvas::frame::Transform; -use crate::{Point, Color}; +use crate::Color; +use crate::widget::canvas::{Fill, fill}; #[derive(Debug, Clone, PartialEq)] /// A fill which transitions colors progressively along a direction, either linearly, radially (TBD), @@ -25,18 +26,27 @@ pub struct ColorStop { impl Gradient { /// Creates a new linear [`linear::Builder`]. - pub fn linear(start: Point, end: Point) -> linear::Builder { - linear::Builder::new(start, end) + pub fn linear(position: impl Into) -> linear::Builder { + linear::Builder::new(position.into()) } /// 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); + linear.start = transform.transform_point(linear.start); + linear.end = transform.transform_point(linear.end); } } self } } + +impl<'a> Into> for &'a Gradient { + fn into(self) -> Fill<'a> { + Fill { + style: fill::Style::Gradient(self), + .. Default::default() + } + } +} -- 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/gradient.rs | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index e1984854..1446d7cf 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -1,10 +1,8 @@ //! For creating a Gradient. mod linear; -pub use crate::gradient::linear::{Linear, Position, Location}; -use crate::widget::canvas::frame::Transform; -use crate::Color; -use crate::widget::canvas::{Fill, fill}; +pub use crate::gradient::linear::{Linear, Location, Position}; +use crate::{Color, Point}; #[derive(Debug, Clone, PartialEq)] /// A fill which transitions colors progressively along a direction, either linearly, radially (TBD), @@ -30,23 +28,9 @@ impl Gradient { linear::Builder::new(position.into()) } - /// 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.transform_point(linear.start); - linear.end = transform.transform_point(linear.end); - } - } - self - } -} - -impl<'a> Into> for &'a Gradient { - fn into(self) -> Fill<'a> { - Fill { - style: fill::Style::Gradient(self), - .. Default::default() + pub(crate) fn coords(&mut self) -> (&mut Point, &mut Point) { + match self { + Gradient::Linear(gradient) => (&mut gradient.start, &mut gradient.end) } } } -- 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/gradient.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 1446d7cf..da0f911d 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -2,7 +2,7 @@ mod linear; pub use crate::gradient::linear::{Linear, Location, Position}; -use crate::{Color, Point}; +use crate::Color; #[derive(Debug, Clone, PartialEq)] /// A fill which transitions colors progressively along a direction, either linearly, radially (TBD), @@ -27,10 +27,4 @@ impl Gradient { pub fn linear(position: impl Into) -> linear::Builder { linear::Builder::new(position.into()) } - - pub(crate) fn coords(&mut self) -> (&mut Point, &mut Point) { - match self { - Gradient::Linear(gradient) => (&mut gradient.start, &mut gradient.end) - } - } } -- cgit From d8045e2dc30633553e087ad6f7748235223017d7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Nov 2022 05:15:32 +0100 Subject: Move `Position` and `Location` to `gradient` module --- graphics/src/gradient.rs | 96 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 89 insertions(+), 7 deletions(-) (limited to 'graphics/src/gradient.rs') diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index da0f911d..64f9e4b3 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -1,8 +1,9 @@ //! For creating a Gradient. -mod linear; +pub mod linear; -pub use crate::gradient::linear::{Linear, Location, Position}; -use crate::Color; +pub use linear::Linear; + +use crate::{Color, Point, Size}; #[derive(Debug, Clone, PartialEq)] /// A fill which transitions colors progressively along a direction, either linearly, radially (TBD), @@ -13,6 +14,13 @@ pub enum Gradient { Linear(Linear), } +impl Gradient { + /// Creates a new linear [`linear::Builder`]. + pub fn linear(position: impl Into) -> linear::Builder { + linear::Builder::new(position.into()) + } +} + #[derive(Debug, Clone, Copy, PartialEq)] /// A point along the gradient vector where the specified [`color`] is unmixed. pub struct ColorStop { @@ -22,9 +30,83 @@ pub struct ColorStop { pub color: Color, } -impl Gradient { - /// Creates a new linear [`linear::Builder`]. - pub fn linear(position: impl Into) -> linear::Builder { - linear::Builder::new(position.into()) +#[derive(Debug)] +/// The position of the gradient within its bounds. +pub enum Position { + /// The gradient will be positioned with respect to two points. + Absolute { + /// The starting point of the gradient. + start: Point, + /// The ending point of the gradient. + end: Point, + }, + /// The gradient will be positioned relative to the provided bounds. + Relative { + /// The top left position of the bounds. + top_left: Point, + /// The width & height of the bounds. + size: Size, + /// The start [Location] of the gradient. + start: Location, + /// The end [Location] of the gradient. + end: Location, + }, +} + +impl From<(Point, Point)> for Position { + fn from((start, end): (Point, Point)) -> Self { + Self::Absolute { start, end } + } +} + +#[derive(Debug)] +/// The location of a relatively-positioned gradient. +pub enum Location { + /// Top left. + TopLeft, + /// Top. + Top, + /// Top right. + TopRight, + /// Right. + Right, + /// Bottom right. + BottomRight, + /// Bottom. + Bottom, + /// Bottom left. + BottomLeft, + /// Left. + Left, +} + +impl Location { + fn to_absolute(&self, top_left: Point, size: Size) -> Point { + match self { + Location::TopLeft => top_left, + Location::Top => { + Point::new(top_left.x + size.width / 2.0, top_left.y) + } + Location::TopRight => { + Point::new(top_left.x + size.width, top_left.y) + } + Location::Right => Point::new( + top_left.x + size.width, + top_left.y + size.height / 2.0, + ), + Location::BottomRight => { + Point::new(top_left.x + size.width, top_left.y + size.height) + } + Location::Bottom => Point::new( + top_left.x + size.width / 2.0, + top_left.y + size.height, + ), + Location::BottomLeft => { + Point::new(top_left.x, top_left.y + size.height) + } + Location::Left => { + Point::new(top_left.x, top_left.y + size.height / 2.0) + } + } } } -- cgit