summaryrefslogtreecommitdiffstats
path: root/graphics/src/widget
diff options
context:
space:
mode:
Diffstat (limited to 'graphics/src/widget')
-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
6 files changed, 33 insertions, 110 deletions
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 {