Improved Rectangle, Line and Shape Tools (#106)

This commit is contained in:
Paul Kupper
2021-05-07 20:05:03 +02:00
committed by GitHub
parent 5bf0452b9c
commit 1230e9d829
8 changed files with 320 additions and 87 deletions

View File

@@ -247,7 +247,7 @@ impl Document {
sides,
style,
} => {
let s = Shape::new((x0, y0), (x0 - x1, y0 - y1), sides, style);
let s = Shape::new((x0, y0), (x1, y1), sides, style);
self.add_layer(&path, Layer::new(LayerDataTypes::Shape(s)), insert_index)?;
Some(vec![DocumentResponse::DocumentChanged])

View File

@@ -7,14 +7,16 @@ use std::fmt::Write;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Shape {
bounding_rect: kurbo::Rect,
shape: shape_points::ShapePoints,
style: style::PathStyle,
}
impl Shape {
pub fn new(center: impl Into<kurbo::Point>, extent: impl Into<kurbo::Vec2>, sides: u8, style: style::PathStyle) -> Shape {
pub fn new(p0: impl Into<kurbo::Point>, p1: impl Into<kurbo::Point>, sides: u8, style: style::PathStyle) -> Shape {
Shape {
shape: shape_points::ShapePoints::new(center, extent, sides),
bounding_rect: kurbo::Rect::from_points(p0, p1),
shape: shape_points::ShapePoints::new(kurbo::Point::new(0.5, 0.5), kurbo::Vec2::new(0.5, 0.0), sides),
style,
}
}
@@ -22,6 +24,15 @@ impl Shape {
impl LayerData for Shape {
fn render(&mut self, svg: &mut String) {
let _ = write!(svg, r#"<polygon points="{}" {} />"#, self.shape, self.style.render(),);
let _ = write!(
svg,
r#"<polygon points="{}" transform="translate({} {}) scale({} {})" {} />"#,
self.shape,
self.bounding_rect.origin().x,
self.bounding_rect.origin().y,
self.bounding_rect.width(),
self.bounding_rect.height(),
self.style.render(),
);
}
}