Fixed import issue with canvas in the gradient mod for situations where canvas feature is not enabled.

This commit is contained in:
shan 2022-10-07 16:28:13 -07:00
parent 215e6c95be
commit a4a1262fa2
5 changed files with 27 additions and 33 deletions

View file

@ -11,7 +11,7 @@ use iced::executor;
use iced::theme::{self, Theme}; use iced::theme::{self, Theme};
use iced::time; use iced::time;
use iced::widget::canvas; use iced::widget::canvas;
use iced::widget::canvas::{Cursor, Path, Stroke, Fill, fill, Gradient, stroke, gradient::Position}; use iced::widget::canvas::{Cursor, Path, Stroke, Gradient, stroke, gradient::Position};
use iced::window; use iced::window;
use iced::{ use iced::{
Application, Color, Command, Element, Length, Point, Rectangle, Settings, Application, Color, Command, Element, Length, Point, Rectangle, Settings,
@ -212,13 +212,7 @@ impl<Message> canvas::Program<Message> for State {
.build() .build()
.unwrap(); .unwrap();
frame.fill( frame.fill(&earth, &earth_fill);
&earth,
Fill {
style: fill::Style::Gradient(&earth_fill),
..Default::default()
},
);
frame.with_save(|frame| { frame.with_save(|frame| {
frame.rotate(rotation * 10.0); frame.rotate(rotation * 10.0);

View file

@ -1,10 +1,8 @@
//! For creating a Gradient. //! For creating a Gradient.
mod linear; mod linear;
pub use crate::gradient::linear::{Linear, Position, Location}; pub use crate::gradient::linear::{Linear, Location, Position};
use crate::widget::canvas::frame::Transform; use crate::{Color, Point};
use crate::Color;
use crate::widget::canvas::{Fill, fill};
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
/// A fill which transitions colors progressively along a direction, either linearly, radially (TBD), /// A fill which transitions colors progressively along a direction, either linearly, radially (TBD),
@ -30,23 +28,9 @@ impl Gradient {
linear::Builder::new(position.into()) linear::Builder::new(position.into())
} }
/// Modifies the start & end stops of the gradient to have a proper transform value. pub(crate) fn coords(&mut self) -> (&mut Point, &mut Point) {
pub(crate) fn transform(mut self, transform: &Transform) -> Self { match self {
match &mut self { Gradient::Linear(gradient) => (&mut gradient.start, &mut gradient.end)
Gradient::Linear(linear) => {
linear.start = transform.transform_point(linear.start);
linear.end = transform.transform_point(linear.end);
}
}
self
}
}
impl<'a> Into<Fill<'a>> for &'a Gradient {
fn into(self) -> Fill<'a> {
Fill {
style: fill::Style::Gradient(self),
.. Default::default()
} }
} }
} }

View file

@ -42,6 +42,15 @@ impl<'a> From<Color> for Fill<'a> {
} }
} }
impl<'a> Into<Fill<'a>> for &'a Gradient {
fn into(self) -> Fill<'a> {
Fill {
style: Style::Gradient(self),
..Default::default()
}
}
}
/// The style of a [`Fill`]. /// The style of a [`Fill`].
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Style<'a> { pub enum Style<'a> {
@ -59,7 +68,11 @@ impl<'a> Style<'a> {
mesh::Style::Solid(*color) mesh::Style::Solid(*color)
}, },
Style::Gradient(gradient) => { 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)
} }
} }
} }

View file

@ -38,12 +38,11 @@ pub(crate) struct Transform {
impl Transform { impl Transform {
/// Transforms the given [Point] by the transformation matrix. /// Transforms the given [Point] by the transformation matrix.
pub(crate) fn transform_point(&self, mut point: Point) -> Point { pub(crate) fn transform_point(&self, point: &mut Point) {
let transformed = let transformed =
self.raw.transform_point(Point2D::new(point.x, point.y)); self.raw.transform_point(Point2D::new(point.x, point.y));
point.x = transformed.x; point.x = transformed.x;
point.y = transformed.y; point.y = transformed.y;
point
} }
} }

View file

@ -77,7 +77,11 @@ impl<'a> Style<'a> {
mesh::Style::Solid(*color) mesh::Style::Solid(*color)
}, },
Style::Gradient(gradient) => { 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)
} }
} }
} }