iced_graphics/geometry/
path.rs

1//! Build different kinds of 2D shapes.
2pub mod arc;
3
4mod builder;
5
6#[doc(no_inline)]
7pub use arc::Arc;
8pub use builder::Builder;
9
10pub use lyon_path;
11
12use crate::core::border;
13use crate::core::{Point, Size};
14
15/// An immutable set of points that may or may not be connected.
16///
17/// A single [`Path`] can represent different kinds of 2D shapes!
18#[derive(Debug, Clone)]
19pub struct Path {
20    raw: lyon_path::Path,
21}
22
23impl Path {
24    /// Creates a new [`Path`] with the provided closure.
25    ///
26    /// Use the [`Builder`] to configure your [`Path`].
27    pub fn new(f: impl FnOnce(&mut Builder)) -> Self {
28        let mut builder = Builder::new();
29
30        // TODO: Make it pure instead of side-effect-based (?)
31        f(&mut builder);
32
33        builder.build()
34    }
35
36    /// Creates a new [`Path`] representing a line segment given its starting
37    /// and end points.
38    pub fn line(from: Point, to: Point) -> Self {
39        Self::new(|p| {
40            p.move_to(from);
41            p.line_to(to);
42        })
43    }
44
45    /// Creates a new [`Path`] representing a rectangle given its top-left
46    /// corner coordinate and its `Size`.
47    pub fn rectangle(top_left: Point, size: Size) -> Self {
48        Self::new(|p| p.rectangle(top_left, size))
49    }
50
51    /// Creates a new [`Path`] representing a rounded rectangle given its top-left
52    /// corner coordinate, its [`Size`] and [`border::Radius`].
53    pub fn rounded_rectangle(top_left: Point, size: Size, radius: border::Radius) -> Self {
54        Self::new(|p| p.rounded_rectangle(top_left, size, radius))
55    }
56
57    /// Creates a new [`Path`] representing a circle given its center
58    /// coordinate and its radius.
59    pub fn circle(center: Point, radius: f32) -> Self {
60        Self::new(|p| {
61            p.circle(center, radius);
62            p.close();
63        })
64    }
65
66    /// Returns the internal [`lyon_path::Path`].
67    #[inline]
68    pub fn raw(&self) -> &lyon_path::Path {
69        &self.raw
70    }
71
72    /// Returns the current [`Path`] with the given transform applied to it.
73    #[inline]
74    pub fn transform(&self, transform: &lyon_path::math::Transform) -> Path {
75        Path {
76            raw: self.raw.clone().transformed(transform),
77        }
78    }
79}