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(
54 top_left: Point,
55 size: Size,
56 radius: border::Radius,
57 ) -> Self {
58 Self::new(|p| p.rounded_rectangle(top_left, size, radius))
59 }
60
61 /// Creates a new [`Path`] representing a circle given its center
62 /// coordinate and its radius.
63 pub fn circle(center: Point, radius: f32) -> Self {
64 Self::new(|p| p.circle(center, radius))
65 }
66
67 /// Returns the internal [`lyon_path::Path`].
68 #[inline]
69 pub fn raw(&self) -> &lyon_path::Path {
70 &self.raw
71 }
72
73 /// Returns the current [`Path`] with the given transform applied to it.
74 #[inline]
75 pub fn transform(&self, transform: &lyon_path::math::Transform) -> Path {
76 Path {
77 raw: self.raw.clone().transformed(transform),
78 }
79 }
80}