Implement canvas::Path::rectangle helper method

This commit is contained in:
Héctor Ramón Jiménez 2020-04-14 06:49:15 +02:00
parent 5c923fce48
commit c545af3577
3 changed files with 23 additions and 17 deletions

View File

@ -133,9 +133,7 @@ impl canvas::Drawable for State {
let center = frame.center(); let center = frame.center();
let space = Path::new(|path| { let space = Path::rectangle(Point::new(0.0, 0.0), frame.size());
path.rectangle(Point::new(0.0, 0.0), frame.size())
});
let stars = Path::new(|path| { let stars = Path::new(|path| {
for (p, size) in &self.stars { for (p, size) in &self.stars {
@ -174,15 +172,10 @@ impl canvas::Drawable for State {
path.circle(Point::ORIGIN, Self::EARTH_RADIUS) path.circle(Point::ORIGIN, Self::EARTH_RADIUS)
}); });
let shadow = Path::new(|path| { let shadow = Path::rectangle(
path.rectangle(
Point::new(0.0, -Self::EARTH_RADIUS), Point::new(0.0, -Self::EARTH_RADIUS),
Size::new( Size::new(Self::EARTH_RADIUS * 4.0, Self::EARTH_RADIUS * 2.0),
Self::EARTH_RADIUS * 4.0, );
Self::EARTH_RADIUS * 2.0,
),
)
});
frame.fill(&earth, Color::from_rgb8(0x6B, 0x93, 0xD6)); frame.fill(&earth, Color::from_rgb8(0x6B, 0x93, 0xD6));

View File

@ -7,6 +7,8 @@ mod builder;
pub use arc::Arc; pub use arc::Arc;
pub use builder::Builder; pub use builder::Builder;
use iced_native::{Point, Size};
/// An immutable set of points that may or may not be connected. /// An immutable set of points that may or may not be connected.
/// ///
/// A single [`Path`] can represent different kinds of 2D shapes! /// A single [`Path`] can represent different kinds of 2D shapes!
@ -33,6 +35,14 @@ impl Path {
builder.build() builder.build()
} }
/// Creates a new [`Path`] representing a rectangle given its top-left
/// corner coordinate and its `Size`.
///
/// [`Path`]: struct.Path.html
pub fn rectangle(top_left: Point, size: Size) -> Self {
Self::new(|p| p.rectangle(top_left, size))
}
#[inline] #[inline]
pub(crate) fn raw(&self) -> &lyon::path::Path { pub(crate) fn raw(&self) -> &lyon::path::Path {
&self.raw &self.raw

View File

@ -133,11 +133,14 @@ impl Builder {
/// ///
/// [`Path`]: struct.Path.html /// [`Path`]: struct.Path.html
#[inline] #[inline]
pub fn rectangle(&mut self, p: Point, size: Size) { pub fn rectangle(&mut self, top_left: Point, size: Size) {
self.move_to(p); self.move_to(top_left);
self.line_to(Point::new(p.x + size.width, p.y)); self.line_to(Point::new(top_left.x + size.width, top_left.y));
self.line_to(Point::new(p.x + size.width, p.y + size.height)); self.line_to(Point::new(
self.line_to(Point::new(p.x, p.y + size.height)); top_left.x + size.width,
top_left.y + size.height,
));
self.line_to(Point::new(top_left.x, top_left.y + size.height));
self.close(); self.close();
} }