summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar shan <shankern@protonmail.com>2022-09-30 10:27:00 -0700
committerLibravatar shan <shankern@protonmail.com>2022-09-30 10:27:00 -0700
commit5d0fffc626928177239336757507b986b081b878 (patch)
tree03c6d1c8ab957f3cb5115fe6c75df65110a0ad64
parente25f3d3dea2d2c1ba7d2778948443df2745084be (diff)
downloadiced-5d0fffc626928177239336757507b986b081b878.tar.gz
iced-5d0fffc626928177239336757507b986b081b878.tar.bz2
iced-5d0fffc626928177239336757507b986b081b878.zip
Fixed some importing issues since you can use a Shader::Gradient outside a Canvas widget, where it was previously only accessible.
-rw-r--r--examples/modern_art/Cargo.toml1
-rw-r--r--examples/modern_art/src/main.rs3
-rw-r--r--graphics/src/gradient.rs92
-rw-r--r--graphics/src/shader.rs25
-rw-r--r--graphics/src/triangle.rs10
-rw-r--r--graphics/src/widget/canvas.rs16
-rw-r--r--graphics/src/widget/canvas/fill.rs13
-rw-r--r--graphics/src/widget/canvas/frame.rs7
-rw-r--r--graphics/src/widget/canvas/gradient.rs21
-rw-r--r--graphics/src/widget/canvas/gradient/linear.rs73
-rw-r--r--graphics/src/widget/canvas/stroke.rs13
11 files changed, 126 insertions, 148 deletions
diff --git a/examples/modern_art/Cargo.toml b/examples/modern_art/Cargo.toml
index 4995e9a7..8242f7e4 100644
--- a/examples/modern_art/Cargo.toml
+++ b/examples/modern_art/Cargo.toml
@@ -7,4 +7,5 @@ publish = false
[dependencies]
iced = { path = "../..", features = ["canvas", "tokio", "debug"] }
+iced_graphics = { path = "../../graphics" }
rand = "0.8.5" \ No newline at end of file
diff --git a/examples/modern_art/src/main.rs b/examples/modern_art/src/main.rs
index 7b04b581..c7945012 100644
--- a/examples/modern_art/src/main.rs
+++ b/examples/modern_art/src/main.rs
@@ -1,5 +1,5 @@
use rand::{Rng, thread_rng};
-use crate::canvas::{Cursor, FillStyle, Geometry, Gradient};
+use crate::canvas::{Cursor, FillStyle, Geometry};
use iced::widget::canvas::{Cache, Fill, Frame};
use iced::widget::{canvas, Canvas};
use iced::Settings;
@@ -7,6 +7,7 @@ use iced::{
executor, Application, Color, Command, Element, Length, Point, Rectangle,
Renderer, Size, Theme,
};
+use iced_graphics::gradient::Gradient;
fn main() -> iced::Result {
ModernArt::run(Settings {
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<ColorStop>,
+ }
+
+ /// 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<Gradient> {
+ if self.stops.is_empty() || !self.valid {
+ return None;
+ }
+
+ let mut stops: Vec<ColorStop> = 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
+ }))
+ }
+ }
+}
+
diff --git a/graphics/src/shader.rs b/graphics/src/shader.rs
index b9071c74..69679e9b 100644
--- a/graphics/src/shader.rs
+++ b/graphics/src/shader.rs
@@ -1,8 +1,7 @@
//! Supported shaders;
-use crate::{Color, widget};
+use crate::Color;
use crate::gradient::Gradient;
-use crate::widget::canvas::{FillStyle, StrokeStyle};
#[derive(Debug, Clone)]
/// Supported shaders for primitives.
@@ -13,28 +12,10 @@ pub enum Shader {
Gradient(Gradient)
}
-impl <'a> Into<Shader> for StrokeStyle<'a> {
+impl <'a> Into<Shader> for Gradient {
fn into(self) -> Shader {
match self {
- StrokeStyle::Solid(color) => Shader::Solid(color),
- StrokeStyle::Gradient(gradient) => gradient.clone().into()
- }
- }
-}
-
-impl <'a> Into<Shader> for FillStyle<'a> {
- fn into(self) -> Shader {
- match self {
- FillStyle::Solid(color) => Shader::Solid(color),
- FillStyle::Gradient(gradient) => gradient.clone().into()
- }
- }
-}
-
-impl <'a> Into<Shader> for widget::canvas::Gradient {
- fn into(self) -> Shader {
- match self {
- widget::canvas::Gradient::Linear(linear) => {
+ Gradient::Linear(linear) => {
Shader::Gradient(Gradient::Linear(linear))
}
}
diff --git a/graphics/src/triangle.rs b/graphics/src/triangle.rs
index 92709fe2..8f82c5df 100644
--- a/graphics/src/triangle.rs
+++ b/graphics/src/triangle.rs
@@ -16,12 +16,4 @@ pub struct Mesh2D {
pub struct Vertex2D {
/// The vertex position in 2D space.
pub position: [f32; 2],
-}
-
-/// Convert from lyon's position data to Iced's Vertex2D type.
-impl Vertex2D {
- /// Converts from [`lyon::math::Point`] to [`Vertex2D`]. Used for generating primitives.
- pub fn from(points: Vec<lyon::math::Point>) -> Vec<Vertex2D> {
- points.iter().map(|p| Vertex2D { position: [p.x, p.y]}).collect()
- }
-}
+} \ No newline at end of file
diff --git a/graphics/src/widget/canvas.rs b/graphics/src/widget/canvas.rs
index 09aad98d..95c962af 100644
--- a/graphics/src/widget/canvas.rs
+++ b/graphics/src/widget/canvas.rs
@@ -5,7 +5,6 @@
//! and more!
pub mod event;
-pub mod gradient;
pub mod path;
mod cache;
@@ -23,7 +22,6 @@ pub use event::Event;
pub use fill::{Fill, FillRule, FillStyle};
pub use frame::Frame;
pub use geometry::Geometry;
-pub use gradient::Gradient;
pub use path::Path;
pub use program::Program;
pub use stroke::{LineCap, LineDash, LineJoin, Stroke, StrokeStyle};
@@ -47,16 +45,12 @@ use std::marker::PhantomData;
/// If you want to get a quick overview, here's how we can draw a simple circle:
///
/// ```no_run
-/// # mod iced {
-/// # pub mod widget {
-/// # pub use iced_graphics::widget::canvas;
-/// # }
-/// # pub use iced_native::{Color, Rectangle, Theme};
-/// # }
-/// use iced::widget::canvas::{self, Canvas, Cursor, Fill, Frame, Geometry, Path, Program};
-/// use iced::{Color, Rectangle, Theme};
-///
/// // First, we define the data we need for drawing
+/// use iced_graphics::{Color, Rectangle};
+/// use iced_graphics::widget::Canvas;
+/// use iced_graphics::widget::canvas::{Cursor, Frame, Geometry, Path, Program};
+/// use iced_style::Theme;
+///
/// #[derive(Debug)]
/// struct Circle {
/// radius: f32,
diff --git a/graphics/src/widget/canvas/fill.rs b/graphics/src/widget/canvas/fill.rs
index 02d2311f..60029e03 100644
--- a/graphics/src/widget/canvas/fill.rs
+++ b/graphics/src/widget/canvas/fill.rs
@@ -1,6 +1,6 @@
use iced_native::Color;
-
-use crate::widget::canvas::Gradient;
+use crate::gradient::Gradient;
+use crate::shader::Shader;
/// The style used to fill geometry.
#[derive(Debug, Clone)]
@@ -48,6 +48,15 @@ pub enum FillStyle<'a> {
Gradient(&'a Gradient),
}
+impl <'a> Into<Shader> for FillStyle<'a> {
+ fn into(self) -> Shader {
+ match self {
+ FillStyle::Solid(color) => Shader::Solid(color),
+ FillStyle::Gradient(gradient) => gradient.clone().into()
+ }
+ }
+}
+
/// The fill rule defines how to determine what is inside and what is outside of
/// a shape.
///
diff --git a/graphics/src/widget/canvas/frame.rs b/graphics/src/widget/canvas/frame.rs
index 8845bc6a..30239b2a 100644
--- a/graphics/src/widget/canvas/frame.rs
+++ b/graphics/src/widget/canvas/frame.rs
@@ -335,7 +335,7 @@ impl Frame {
if !buffer.indices.is_empty() {
self.primitives.push(Primitive::Mesh2D {
buffers: triangle::Mesh2D {
- vertices: Vertex2D::from(buffer.vertices),
+ vertices: vertices_from(buffer.vertices),
indices: buffer.indices,
},
size: self.size,
@@ -347,3 +347,8 @@ impl Frame {
self.primitives
}
}
+
+/// Converts from [`lyon::math::Point`] to [`Vertex2D`]. Used for generating primitives.
+fn vertices_from(points: Vec<lyon::math::Point>) -> Vec<Vertex2D> {
+ points.iter().map(|p| Vertex2D { position: [p.x, p.y]}).collect()
+} \ No newline at end of file
diff --git a/graphics/src/widget/canvas/gradient.rs b/graphics/src/widget/canvas/gradient.rs
deleted file mode 100644
index 7d2daabc..00000000
--- a/graphics/src/widget/canvas/gradient.rs
+++ /dev/null
@@ -1,21 +0,0 @@
-//! Define a color gradient.
-use iced_native::Point;
-
-pub mod linear;
-
-pub use linear::Linear;
-
-/// A gradient that can be used in the style of [`super::Fill`] or [`super::Stroke`].
-#[derive(Debug, Clone)]
-pub enum Gradient {
- /// A linear gradient
- Linear(Linear),
- //TODO: radial, conical
-}
-
-impl Gradient {
- /// Creates a new linear [`linear::Builder`].
- pub fn linear(start: Point, end: Point) -> linear::Builder {
- linear::Builder::new(start, end)
- }
-} \ No newline at end of file
diff --git a/graphics/src/widget/canvas/gradient/linear.rs b/graphics/src/widget/canvas/gradient/linear.rs
deleted file mode 100644
index 1dc7e3a8..00000000
--- a/graphics/src/widget/canvas/gradient/linear.rs
+++ /dev/null
@@ -1,73 +0,0 @@
-//! A linear color gradient.
-use iced_native::{Color, Point};
-
-use crate::gradient::ColorStop;
-
-use super::Gradient;
-
-/// 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<ColorStop>,
-}
-
-/// 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<Gradient> {
- if self.stops.is_empty() || !self.valid {
- return None;
- }
-
- let mut stops: Vec<ColorStop> = 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
- }))
- }
-}
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<Shader> 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 {