diff options
author | 2022-02-04 14:44:08 +0700 | |
---|---|---|
committer | 2022-02-04 14:44:08 +0700 | |
commit | c15701581e52ae838f8e7153ae7e639dd86b1ef6 (patch) | |
tree | 27be3d4b7fa56b3b72b9265c07907b1349d07c37 /graphics/src/widget/canvas/path.rs | |
parent | 74a64d88e1b5b4173fa15c30506aece19ea368d0 (diff) | |
parent | bace264bfe5e3cb5046867bd411e54969a637c79 (diff) | |
download | iced-c15701581e52ae838f8e7153ae7e639dd86b1ef6.tar.gz iced-c15701581e52ae838f8e7153ae7e639dd86b1ef6.tar.bz2 iced-c15701581e52ae838f8e7153ae7e639dd86b1ef6.zip |
Merge pull request #1225 from tarkah/feat/canvas-line-dash
Add line dash API
Diffstat (limited to 'graphics/src/widget/canvas/path.rs')
-rw-r--r-- | graphics/src/widget/canvas/path.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/graphics/src/widget/canvas/path.rs b/graphics/src/widget/canvas/path.rs index 4e4fd734..1728f060 100644 --- a/graphics/src/widget/canvas/path.rs +++ b/graphics/src/widget/canvas/path.rs @@ -7,7 +7,11 @@ mod builder; pub use arc::Arc; pub use builder::Builder; +use crate::canvas::LineDash; + 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. /// @@ -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), + }, + ); + }) +} |