2023-09-09 21:08:23 +02:00
|
|
|
//! Create lines from a [`Path`] and assigns them various attributes/styles.
|
|
|
|
|
//!
|
|
|
|
|
//! [`Path`]: super::Path
|
2023-03-03 04:57:55 +01:00
|
|
|
pub use crate::geometry::Style;
|
2022-10-05 10:49:58 -07:00
|
|
|
|
2023-03-04 05:37:11 +01:00
|
|
|
use iced_core::Color;
|
2022-09-29 10:52:58 -07:00
|
|
|
|
2020-02-18 08:48:54 +01:00
|
|
|
/// The style of a stroke.
|
2024-06-21 11:55:42 +01:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2022-02-03 17:18:05 +07:00
|
|
|
pub struct Stroke<'a> {
|
2022-09-29 10:52:58 -07:00
|
|
|
/// The color or gradient of the stroke.
|
|
|
|
|
///
|
2022-11-10 00:10:53 +01:00
|
|
|
/// By default, it is set to a [`Style::Solid`] with [`Color::BLACK`].
|
2022-11-03 05:50:53 +01:00
|
|
|
pub style: Style,
|
2020-02-18 08:48:54 +01:00
|
|
|
/// The distance between the two edges of the stroke.
|
2020-02-12 09:12:35 +01:00
|
|
|
pub width: f32,
|
2020-02-18 08:48:54 +01:00
|
|
|
/// The shape to be used at the end of open subpaths when they are stroked.
|
2020-02-12 09:12:35 +01:00
|
|
|
pub line_cap: LineCap,
|
2020-02-18 08:48:54 +01:00
|
|
|
/// The shape to be used at the corners of paths or basic shapes when they
|
|
|
|
|
/// are stroked.
|
2020-02-12 09:12:35 +01:00
|
|
|
pub line_join: LineJoin,
|
2022-01-27 09:40:52 -08:00
|
|
|
/// The dash pattern used when stroking the line.
|
2022-02-03 17:18:05 +07:00
|
|
|
pub line_dash: LineDash<'a>,
|
2020-02-12 09:12:35 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-03 17:18:05 +07:00
|
|
|
impl<'a> Stroke<'a> {
|
2020-04-30 07:38:46 +02:00
|
|
|
/// Sets the color of the [`Stroke`].
|
2022-02-03 17:18:05 +07:00
|
|
|
pub fn with_color(self, color: Color) -> Self {
|
2022-09-29 10:52:58 -07:00
|
|
|
Stroke {
|
2022-10-04 18:24:46 -07:00
|
|
|
style: Style::Solid(color),
|
2022-09-29 10:52:58 -07:00
|
|
|
..self
|
|
|
|
|
}
|
2020-04-28 03:14:05 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-30 07:38:46 +02:00
|
|
|
/// Sets the width of the [`Stroke`].
|
2022-02-03 17:18:05 +07:00
|
|
|
pub fn with_width(self, width: f32) -> Self {
|
2020-04-28 03:14:05 +02:00
|
|
|
Stroke { width, ..self }
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-30 07:38:46 +02:00
|
|
|
/// Sets the [`LineCap`] of the [`Stroke`].
|
2022-02-03 17:18:05 +07:00
|
|
|
pub fn with_line_cap(self, line_cap: LineCap) -> Self {
|
2020-04-28 03:14:05 +02:00
|
|
|
Stroke { line_cap, ..self }
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-30 07:38:46 +02:00
|
|
|
/// Sets the [`LineJoin`] of the [`Stroke`].
|
2022-02-03 17:18:05 +07:00
|
|
|
pub fn with_line_join(self, line_join: LineJoin) -> Self {
|
2020-04-28 03:14:05 +02:00
|
|
|
Stroke { line_join, ..self }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-03 17:18:05 +07:00
|
|
|
impl<'a> Default for Stroke<'a> {
|
|
|
|
|
fn default() -> Self {
|
2020-02-12 09:12:35 +01:00
|
|
|
Stroke {
|
2022-10-04 18:24:46 -07:00
|
|
|
style: Style::Solid(Color::BLACK),
|
2020-02-12 09:12:35 +01:00
|
|
|
width: 1.0,
|
|
|
|
|
line_cap: LineCap::default(),
|
|
|
|
|
line_join: LineJoin::default(),
|
2022-01-27 09:40:52 -08:00
|
|
|
line_dash: LineDash::default(),
|
2020-02-12 09:12:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 08:48:54 +01:00
|
|
|
/// The shape used at the end of open subpaths when they are stroked.
|
2023-03-14 11:11:17 +01:00
|
|
|
#[derive(Debug, Clone, Copy, Default)]
|
2020-02-12 09:12:35 +01:00
|
|
|
pub enum LineCap {
|
2020-02-18 08:48:54 +01:00
|
|
|
/// The stroke for each sub-path does not extend beyond its two endpoints.
|
2023-03-14 11:11:17 +01:00
|
|
|
#[default]
|
2020-02-12 09:12:35 +01:00
|
|
|
Butt,
|
2020-02-18 08:48:54 +01:00
|
|
|
/// At the end of each sub-path, the shape representing the stroke will be
|
|
|
|
|
/// extended by a square.
|
2020-02-12 09:12:35 +01:00
|
|
|
Square,
|
2020-02-18 08:48:54 +01:00
|
|
|
/// At the end of each sub-path, the shape representing the stroke will be
|
|
|
|
|
/// extended by a semicircle.
|
2020-02-12 09:12:35 +01:00
|
|
|
Round,
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 08:48:54 +01:00
|
|
|
/// The shape used at the corners of paths or basic shapes when they are
|
|
|
|
|
/// stroked.
|
2023-03-14 11:11:17 +01:00
|
|
|
#[derive(Debug, Clone, Copy, Default)]
|
2020-02-12 09:12:35 +01:00
|
|
|
pub enum LineJoin {
|
2020-02-18 08:48:54 +01:00
|
|
|
/// A sharp corner.
|
2023-03-14 11:11:17 +01:00
|
|
|
#[default]
|
2020-02-12 09:12:35 +01:00
|
|
|
Miter,
|
2020-02-18 08:48:54 +01:00
|
|
|
/// A round corner.
|
2020-02-12 09:12:35 +01:00
|
|
|
Round,
|
2020-02-18 08:48:54 +01:00
|
|
|
/// A bevelled corner.
|
2020-02-12 09:12:35 +01:00
|
|
|
Bevel,
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-27 09:40:52 -08:00
|
|
|
/// The dash pattern used when stroking the line.
|
2022-02-03 17:18:05 +07:00
|
|
|
#[derive(Debug, Clone, Copy, Default)]
|
|
|
|
|
pub struct LineDash<'a> {
|
2022-01-27 09:40:52 -08:00
|
|
|
/// The alternating lengths of lines and gaps which describe the pattern.
|
2022-02-03 17:18:05 +07:00
|
|
|
pub segments: &'a [f32],
|
|
|
|
|
|
2022-01-27 09:40:52 -08:00
|
|
|
/// The offset of [`LineDash::segments`] to start the pattern.
|
|
|
|
|
pub offset: usize,
|
|
|
|
|
}
|