Merge pull request #1225 from tarkah/feat/canvas-line-dash
Add line dash API
This commit is contained in:
commit
c15701581e
5 changed files with 80 additions and 10 deletions
|
|
@ -163,6 +163,10 @@ impl<Message> canvas::Program<Message> for State {
|
||||||
Stroke {
|
Stroke {
|
||||||
width: 1.0,
|
width: 1.0,
|
||||||
color: Color::from_rgba8(0, 153, 255, 0.1),
|
color: Color::from_rgba8(0, 153, 255, 0.1),
|
||||||
|
line_dash: canvas::LineDash {
|
||||||
|
offset: 0,
|
||||||
|
segments: &[3.0, 6.0],
|
||||||
|
},
|
||||||
..Stroke::default()
|
..Stroke::default()
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ pub use frame::Frame;
|
||||||
pub use geometry::Geometry;
|
pub use geometry::Geometry;
|
||||||
pub use path::Path;
|
pub use path::Path;
|
||||||
pub use program::Program;
|
pub use program::Program;
|
||||||
pub use stroke::{LineCap, LineJoin, Stroke};
|
pub use stroke::{LineCap, LineDash, LineJoin, Stroke};
|
||||||
pub use text::Text;
|
pub use text::Text;
|
||||||
|
|
||||||
/// A widget capable of drawing 2D graphics.
|
/// A widget capable of drawing 2D graphics.
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use iced_native::{Point, Rectangle, Size, Vector};
|
use iced_native::{Point, Rectangle, Size, Vector};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
canvas::path,
|
||||||
canvas::{Fill, Geometry, Path, Stroke, Text},
|
canvas::{Fill, Geometry, Path, Stroke, Text},
|
||||||
triangle, Primitive,
|
triangle, Primitive,
|
||||||
};
|
};
|
||||||
|
|
@ -150,7 +153,7 @@ impl Frame {
|
||||||
|
|
||||||
/// Draws the stroke of the given [`Path`] on the [`Frame`] with the
|
/// Draws the stroke of the given [`Path`] on the [`Frame`] with the
|
||||||
/// provided style.
|
/// provided style.
|
||||||
pub fn stroke(&mut self, path: &Path, stroke: impl Into<Stroke>) {
|
pub fn stroke<'a>(&mut self, path: &Path, stroke: impl Into<Stroke<'a>>) {
|
||||||
let stroke = stroke.into();
|
let stroke = stroke.into();
|
||||||
|
|
||||||
let mut buffers = tessellation::BuffersBuilder::new(
|
let mut buffers = tessellation::BuffersBuilder::new(
|
||||||
|
|
@ -164,6 +167,12 @@ impl Frame {
|
||||||
options.end_cap = stroke.line_cap.into();
|
options.end_cap = stroke.line_cap.into();
|
||||||
options.line_join = stroke.line_join.into();
|
options.line_join = stroke.line_join.into();
|
||||||
|
|
||||||
|
let path = if stroke.line_dash.segments.is_empty() {
|
||||||
|
Cow::Borrowed(path)
|
||||||
|
} else {
|
||||||
|
Cow::Owned(path::dashed(path, stroke.line_dash))
|
||||||
|
};
|
||||||
|
|
||||||
let result = if self.transforms.current.is_identity {
|
let result = if self.transforms.current.is_identity {
|
||||||
self.stroke_tessellator.tessellate_path(
|
self.stroke_tessellator.tessellate_path(
|
||||||
path.raw(),
|
path.raw(),
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,11 @@ mod builder;
|
||||||
pub use arc::Arc;
|
pub use arc::Arc;
|
||||||
pub use builder::Builder;
|
pub use builder::Builder;
|
||||||
|
|
||||||
|
use crate::canvas::LineDash;
|
||||||
|
|
||||||
use iced_native::{Point, Size};
|
use iced_native::{Point, Size};
|
||||||
|
use lyon::algorithms::walk::{walk_along_path, RepeatedPattern};
|
||||||
|
use lyon::path::iterator::PathIterator;
|
||||||
|
|
||||||
/// An immutable set of points that may or may not be connected.
|
/// An immutable set of points that may or may not be connected.
|
||||||
///
|
///
|
||||||
|
|
@ -66,3 +70,43 @@ impl Path {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(super) fn dashed(path: &Path, line_dash: LineDash<'_>) -> Path {
|
||||||
|
Path::new(|builder| {
|
||||||
|
let segments_odd = (line_dash.segments.len() % 2 == 1).then(|| {
|
||||||
|
[&line_dash.segments[..], &line_dash.segments[..]].concat()
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut draw_line = false;
|
||||||
|
|
||||||
|
walk_along_path(
|
||||||
|
path.raw().iter().flattened(0.01),
|
||||||
|
0.0,
|
||||||
|
&mut RepeatedPattern {
|
||||||
|
callback: |position: lyon::algorithms::math::Point,
|
||||||
|
_tangent,
|
||||||
|
_distance| {
|
||||||
|
let point = Point {
|
||||||
|
x: position.x,
|
||||||
|
y: position.y,
|
||||||
|
};
|
||||||
|
|
||||||
|
if draw_line {
|
||||||
|
builder.line_to(point);
|
||||||
|
} else {
|
||||||
|
builder.move_to(point);
|
||||||
|
}
|
||||||
|
|
||||||
|
draw_line = !draw_line;
|
||||||
|
|
||||||
|
true
|
||||||
|
},
|
||||||
|
index: line_dash.offset,
|
||||||
|
intervals: segments_odd
|
||||||
|
.as_ref()
|
||||||
|
.map(Vec::as_slice)
|
||||||
|
.unwrap_or(line_dash.segments),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use iced_native::Color;
|
||||||
|
|
||||||
/// The style of a stroke.
|
/// The style of a stroke.
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct Stroke {
|
pub struct Stroke<'a> {
|
||||||
/// The color of the stroke.
|
/// The color of the stroke.
|
||||||
pub color: Color,
|
pub color: Color,
|
||||||
/// The distance between the two edges of the stroke.
|
/// The distance between the two edges of the stroke.
|
||||||
|
|
@ -12,37 +12,40 @@ pub struct Stroke {
|
||||||
/// The shape to be used at the corners of paths or basic shapes when they
|
/// The shape to be used at the corners of paths or basic shapes when they
|
||||||
/// are stroked.
|
/// are stroked.
|
||||||
pub line_join: LineJoin,
|
pub line_join: LineJoin,
|
||||||
|
/// The dash pattern used when stroking the line.
|
||||||
|
pub line_dash: LineDash<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Stroke {
|
impl<'a> Stroke<'a> {
|
||||||
/// Sets the color of the [`Stroke`].
|
/// Sets the color of the [`Stroke`].
|
||||||
pub fn with_color(self, color: Color) -> Stroke {
|
pub fn with_color(self, color: Color) -> Self {
|
||||||
Stroke { color, ..self }
|
Stroke { color, ..self }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the width of the [`Stroke`].
|
/// Sets the width of the [`Stroke`].
|
||||||
pub fn with_width(self, width: f32) -> Stroke {
|
pub fn with_width(self, width: f32) -> Self {
|
||||||
Stroke { width, ..self }
|
Stroke { width, ..self }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the [`LineCap`] of the [`Stroke`].
|
/// Sets the [`LineCap`] of the [`Stroke`].
|
||||||
pub fn with_line_cap(self, line_cap: LineCap) -> Stroke {
|
pub fn with_line_cap(self, line_cap: LineCap) -> Self {
|
||||||
Stroke { line_cap, ..self }
|
Stroke { line_cap, ..self }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the [`LineJoin`] of the [`Stroke`].
|
/// Sets the [`LineJoin`] of the [`Stroke`].
|
||||||
pub fn with_line_join(self, line_join: LineJoin) -> Stroke {
|
pub fn with_line_join(self, line_join: LineJoin) -> Self {
|
||||||
Stroke { line_join, ..self }
|
Stroke { line_join, ..self }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Stroke {
|
impl<'a> Default for Stroke<'a> {
|
||||||
fn default() -> Stroke {
|
fn default() -> Self {
|
||||||
Stroke {
|
Stroke {
|
||||||
color: Color::BLACK,
|
color: Color::BLACK,
|
||||||
width: 1.0,
|
width: 1.0,
|
||||||
line_cap: LineCap::default(),
|
line_cap: LineCap::default(),
|
||||||
line_join: LineJoin::default(),
|
line_join: LineJoin::default(),
|
||||||
|
line_dash: LineDash::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -103,3 +106,13 @@ impl From<LineJoin> for lyon::tessellation::LineJoin {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The dash pattern used when stroking the line.
|
||||||
|
#[derive(Debug, Clone, Copy, Default)]
|
||||||
|
pub struct LineDash<'a> {
|
||||||
|
/// The alternating lengths of lines and gaps which describe the pattern.
|
||||||
|
pub segments: &'a [f32],
|
||||||
|
|
||||||
|
/// The offset of [`LineDash::segments`] to start the pattern.
|
||||||
|
pub offset: usize,
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue