2022-10-05 10:49:58 -07:00
|
|
|
//! Fill [crate::widget::canvas::Geometry] with a certain style.
|
|
|
|
|
|
2022-09-30 10:27:00 -07:00
|
|
|
use crate::gradient::Gradient;
|
2022-10-05 10:49:58 -07:00
|
|
|
use crate::layer::mesh;
|
|
|
|
|
use iced_native::Color;
|
2022-10-06 16:57:38 -07:00
|
|
|
use crate::widget::canvas::frame::Transform;
|
2022-09-29 10:52:58 -07:00
|
|
|
|
2020-02-18 08:48:54 +01:00
|
|
|
/// The style used to fill geometry.
|
2022-09-29 10:52:58 -07:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct Fill<'a> {
|
|
|
|
|
/// The color or gradient of the fill.
|
2020-06-02 02:21:07 +02:00
|
|
|
///
|
2022-09-29 10:52:58 -07:00
|
|
|
/// By default, it is set to [`FillStyle::Solid`] `BLACK`.
|
2022-10-04 18:24:46 -07:00
|
|
|
pub style: Style<'a>,
|
2020-06-02 02:21:07 +02:00
|
|
|
|
|
|
|
|
/// The fill rule defines how to determine what is inside and what is
|
|
|
|
|
/// outside of a shape.
|
|
|
|
|
///
|
|
|
|
|
/// See the [SVG specification][1] for more details.
|
|
|
|
|
///
|
|
|
|
|
/// By default, it is set to `NonZero`.
|
|
|
|
|
///
|
|
|
|
|
/// [1]: https://www.w3.org/TR/SVG/painting.html#FillRuleProperty
|
|
|
|
|
pub rule: FillRule,
|
2020-02-12 09:12:35 +01:00
|
|
|
}
|
|
|
|
|
|
2022-10-05 10:49:58 -07:00
|
|
|
impl<'a> Default for Fill<'a> {
|
2022-09-29 10:52:58 -07:00
|
|
|
fn default() -> Fill<'a> {
|
2020-06-02 02:21:07 +02:00
|
|
|
Fill {
|
2022-10-04 18:24:46 -07:00
|
|
|
style: Style::Solid(Color::BLACK),
|
2020-06-02 02:21:07 +02:00
|
|
|
rule: FillRule::NonZero,
|
|
|
|
|
}
|
2020-02-12 09:12:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-04-14 06:38:06 +02:00
|
|
|
|
2022-09-29 10:52:58 -07:00
|
|
|
impl<'a> From<Color> for Fill<'a> {
|
|
|
|
|
fn from(color: Color) -> Fill<'a> {
|
2020-06-02 02:21:07 +02:00
|
|
|
Fill {
|
2022-10-04 18:24:46 -07:00
|
|
|
style: Style::Solid(color),
|
2020-06-02 02:21:07 +02:00
|
|
|
..Fill::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-07 16:28:13 -07:00
|
|
|
impl<'a> Into<Fill<'a>> for &'a Gradient {
|
|
|
|
|
fn into(self) -> Fill<'a> {
|
|
|
|
|
Fill {
|
|
|
|
|
style: Style::Gradient(self),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 19:41:00 -07:00
|
|
|
/// The style of a [`Fill`].
|
2022-09-29 10:52:58 -07:00
|
|
|
#[derive(Debug, Clone)]
|
2022-10-04 18:24:46 -07:00
|
|
|
pub enum Style<'a> {
|
2022-09-29 10:52:58 -07:00
|
|
|
/// A solid color
|
|
|
|
|
Solid(Color),
|
|
|
|
|
/// A color gradient
|
|
|
|
|
Gradient(&'a Gradient),
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 16:57:38 -07:00
|
|
|
impl<'a> Style<'a> {
|
|
|
|
|
/// Converts a fill's [Style] to a [mesh::Style] for use in the renderer's shader.
|
|
|
|
|
pub(crate) fn as_mesh_style(&self, transform: &Transform) -> mesh::Style {
|
2022-09-30 10:27:00 -07:00
|
|
|
match self {
|
2022-10-06 16:57:38 -07:00
|
|
|
Style::Solid(color) => {
|
|
|
|
|
mesh::Style::Solid(*color)
|
|
|
|
|
},
|
2022-10-07 16:55:55 -07:00
|
|
|
Style::Gradient(gradient) => mesh::Style::Gradient(
|
|
|
|
|
transform.transform_gradient((*gradient).clone()),
|
|
|
|
|
),
|
2022-09-30 10:27:00 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 02:21:07 +02:00
|
|
|
/// The fill rule defines how to determine what is inside and what is outside of
|
|
|
|
|
/// a shape.
|
|
|
|
|
///
|
|
|
|
|
/// See the [SVG specification][1].
|
|
|
|
|
///
|
|
|
|
|
/// [1]: https://www.w3.org/TR/SVG/painting.html#FillRuleProperty
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
#[allow(missing_docs)]
|
|
|
|
|
pub enum FillRule {
|
|
|
|
|
NonZero,
|
|
|
|
|
EvenOdd,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<FillRule> for lyon::tessellation::FillRule {
|
|
|
|
|
fn from(rule: FillRule) -> lyon::tessellation::FillRule {
|
|
|
|
|
match rule {
|
|
|
|
|
FillRule::NonZero => lyon::tessellation::FillRule::NonZero,
|
|
|
|
|
FillRule::EvenOdd => lyon::tessellation::FillRule::EvenOdd,
|
|
|
|
|
}
|
2020-04-14 06:38:06 +02:00
|
|
|
}
|
|
|
|
|
}
|