Fixed some importing issues since you can use a Shader::Gradient outside a Canvas widget, where it was previously only accessible.
This commit is contained in:
parent
e25f3d3dea
commit
5d0fffc626
11 changed files with 126 additions and 148 deletions
|
|
@ -7,4 +7,5 @@ publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
iced = { path = "../..", features = ["canvas", "tokio", "debug"] }
|
iced = { path = "../..", features = ["canvas", "tokio", "debug"] }
|
||||||
|
iced_graphics = { path = "../../graphics" }
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use rand::{Rng, thread_rng};
|
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::{Cache, Fill, Frame};
|
||||||
use iced::widget::{canvas, Canvas};
|
use iced::widget::{canvas, Canvas};
|
||||||
use iced::Settings;
|
use iced::Settings;
|
||||||
|
|
@ -7,6 +7,7 @@ use iced::{
|
||||||
executor, Application, Color, Command, Element, Length, Point, Rectangle,
|
executor, Application, Color, Command, Element, Length, Point, Rectangle,
|
||||||
Renderer, Size, Theme,
|
Renderer, Size, Theme,
|
||||||
};
|
};
|
||||||
|
use iced_graphics::gradient::Gradient;
|
||||||
|
|
||||||
fn main() -> iced::Result {
|
fn main() -> iced::Result {
|
||||||
ModernArt::run(Settings {
|
ModernArt::run(Settings {
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,17 @@
|
||||||
//! For creating a Gradient.
|
//! For creating a Gradient.
|
||||||
|
|
||||||
use iced_native::Color;
|
use iced_native::Color;
|
||||||
use crate::widget::canvas::gradient::Linear;
|
use crate::gradient::linear::Linear;
|
||||||
|
use crate::Point;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
/// A fill which transitions colors progressively along a direction, either linearly, radially,
|
/// A fill which transitions colors progressively along a direction, either linearly, radially (TBD),
|
||||||
/// or conically.
|
/// or conically (TBD).
|
||||||
pub enum Gradient {
|
pub enum Gradient {
|
||||||
/// A linear gradient interpolates colors along a direction from its [`start`] to its [`end`]
|
/// A linear gradient interpolates colors along a direction from its [`start`] to its [`end`]
|
||||||
/// point.
|
/// point.
|
||||||
Linear(Linear),
|
Linear(Linear),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
/// A point along the gradient vector where the specified [`color`] is unmixed.
|
/// A point along the gradient vector where the specified [`color`] is unmixed.
|
||||||
pub struct ColorStop {
|
pub struct ColorStop {
|
||||||
|
|
@ -20,4 +19,85 @@ pub struct ColorStop {
|
||||||
pub offset: f32,
|
pub offset: f32,
|
||||||
/// The color of the gradient at the specified [`offset`].
|
/// The color of the gradient at the specified [`offset`].
|
||||||
pub color: Color,
|
pub color: Color,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,7 @@
|
||||||
//! Supported shaders;
|
//! Supported shaders;
|
||||||
|
|
||||||
use crate::{Color, widget};
|
use crate::Color;
|
||||||
use crate::gradient::Gradient;
|
use crate::gradient::Gradient;
|
||||||
use crate::widget::canvas::{FillStyle, StrokeStyle};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
/// Supported shaders for primitives.
|
/// Supported shaders for primitives.
|
||||||
|
|
@ -13,28 +12,10 @@ pub enum Shader {
|
||||||
Gradient(Gradient)
|
Gradient(Gradient)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'a> Into<Shader> for StrokeStyle<'a> {
|
impl <'a> Into<Shader> for Gradient {
|
||||||
fn into(self) -> Shader {
|
fn into(self) -> Shader {
|
||||||
match self {
|
match self {
|
||||||
StrokeStyle::Solid(color) => Shader::Solid(color),
|
Gradient::Linear(linear) => {
|
||||||
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) => {
|
|
||||||
Shader::Gradient(Gradient::Linear(linear))
|
Shader::Gradient(Gradient::Linear(linear))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,4 @@ pub struct Mesh2D {
|
||||||
pub struct Vertex2D {
|
pub struct Vertex2D {
|
||||||
/// The vertex position in 2D space.
|
/// The vertex position in 2D space.
|
||||||
pub position: [f32; 2],
|
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -5,7 +5,6 @@
|
||||||
//! and more!
|
//! and more!
|
||||||
|
|
||||||
pub mod event;
|
pub mod event;
|
||||||
pub mod gradient;
|
|
||||||
pub mod path;
|
pub mod path;
|
||||||
|
|
||||||
mod cache;
|
mod cache;
|
||||||
|
|
@ -23,7 +22,6 @@ pub use event::Event;
|
||||||
pub use fill::{Fill, FillRule, FillStyle};
|
pub use fill::{Fill, FillRule, FillStyle};
|
||||||
pub use frame::Frame;
|
pub use frame::Frame;
|
||||||
pub use geometry::Geometry;
|
pub use geometry::Geometry;
|
||||||
pub use gradient::Gradient;
|
|
||||||
pub use path::Path;
|
pub use path::Path;
|
||||||
pub use program::Program;
|
pub use program::Program;
|
||||||
pub use stroke::{LineCap, LineDash, LineJoin, Stroke, StrokeStyle};
|
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:
|
/// If you want to get a quick overview, here's how we can draw a simple circle:
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```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
|
/// // 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)]
|
/// #[derive(Debug)]
|
||||||
/// struct Circle {
|
/// struct Circle {
|
||||||
/// radius: f32,
|
/// radius: f32,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use iced_native::Color;
|
use iced_native::Color;
|
||||||
|
use crate::gradient::Gradient;
|
||||||
use crate::widget::canvas::Gradient;
|
use crate::shader::Shader;
|
||||||
|
|
||||||
/// The style used to fill geometry.
|
/// The style used to fill geometry.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
|
@ -48,6 +48,15 @@ pub enum FillStyle<'a> {
|
||||||
Gradient(&'a Gradient),
|
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
|
/// The fill rule defines how to determine what is inside and what is outside of
|
||||||
/// a shape.
|
/// a shape.
|
||||||
///
|
///
|
||||||
|
|
|
||||||
|
|
@ -335,7 +335,7 @@ impl Frame {
|
||||||
if !buffer.indices.is_empty() {
|
if !buffer.indices.is_empty() {
|
||||||
self.primitives.push(Primitive::Mesh2D {
|
self.primitives.push(Primitive::Mesh2D {
|
||||||
buffers: triangle::Mesh2D {
|
buffers: triangle::Mesh2D {
|
||||||
vertices: Vertex2D::from(buffer.vertices),
|
vertices: vertices_from(buffer.vertices),
|
||||||
indices: buffer.indices,
|
indices: buffer.indices,
|
||||||
},
|
},
|
||||||
size: self.size,
|
size: self.size,
|
||||||
|
|
@ -347,3 +347,8 @@ impl Frame {
|
||||||
self.primitives
|
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()
|
||||||
|
}
|
||||||
|
|
@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -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
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use iced_native::Color;
|
use iced_native::Color;
|
||||||
|
use crate::gradient::Gradient;
|
||||||
use crate::widget::canvas::Gradient;
|
use crate::shader::Shader;
|
||||||
|
|
||||||
/// The style of a stroke.
|
/// The style of a stroke.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
|
@ -66,6 +66,15 @@ pub enum StrokeStyle<'a> {
|
||||||
Gradient(&'a Gradient),
|
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.
|
/// The shape used at the end of open subpaths when they are stroked.
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum LineCap {
|
pub enum LineCap {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue