Improve Ellipse Tool (#98)

This commit is contained in:
Paul Kupper
2021-05-04 10:53:00 +02:00
committed by Keavon Chambers
parent 76eb944233
commit c9fea54ec5
7 changed files with 161 additions and 30 deletions

View File

@@ -193,7 +193,21 @@ impl Document {
self.work_operations.push(operation.clone());
let responses = match operation {
Operation::AddCircle { path, insert_index, cx, cy, r, style } => {
self.add_layer(&path, Layer::new(LayerDataTypes::Circle(layers::Circle::new(kurbo::Point::new(cx, cy), r, style))), insert_index)?;
self.add_layer(&path, Layer::new(LayerDataTypes::Circle(layers::Circle::new((cx, cy), r, style))), insert_index)?;
Some(vec![DocumentResponse::DocumentChanged])
}
Operation::AddEllipse {
path,
insert_index,
cx,
cy,
rx,
ry,
rot,
style,
} => {
self.add_layer(&path, Layer::new(LayerDataTypes::Ellipse(layers::Ellipse::new((cx, cy), (rx, ry), rot, style))), insert_index)?;
Some(vec![DocumentResponse::DocumentChanged])
}
@@ -206,11 +220,7 @@ impl Document {
y1,
style,
} => {
self.add_layer(
&path,
Layer::new(LayerDataTypes::Rect(Rect::new(kurbo::Point::new(x0, y0), kurbo::Point::new(x1, y1), style))),
insert_index,
)?;
self.add_layer(&path, Layer::new(LayerDataTypes::Rect(Rect::new((x0, y0), (x1, y1), style))), insert_index)?;
Some(vec![DocumentResponse::DocumentChanged])
}
@@ -223,11 +233,7 @@ impl Document {
y1,
style,
} => {
self.add_layer(
&path,
Layer::new(LayerDataTypes::Line(Line::new(kurbo::Point::new(x0, y0), kurbo::Point::new(x1, y1), style))),
insert_index,
)?;
self.add_layer(&path, Layer::new(LayerDataTypes::Line(Line::new((x0, y0), (x1, y1), style))), insert_index)?;
Some(vec![DocumentResponse::DocumentChanged])
}
@@ -247,7 +253,7 @@ impl Document {
sides,
style,
} => {
let s = Shape::new(kurbo::Point::new(x0, y0), kurbo::Vec2 { x: x0 - x1, y: y0 - y1 }, sides, style);
let s = Shape::new((x0, y0), (x0 - x1, y0 - y1), sides, style);
self.add_layer(&path, Layer::new(LayerDataTypes::Shape(s)), insert_index)?;
Some(vec![DocumentResponse::DocumentChanged])

View File

@@ -0,0 +1,37 @@
use super::style;
use super::LayerData;
use std::fmt::Write;
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Ellipse {
shape: kurbo::Ellipse,
style: style::PathStyle,
}
impl Ellipse {
pub fn new(center: impl Into<kurbo::Point>, radii: impl Into<kurbo::Vec2>, rotation: f64, style: style::PathStyle) -> Ellipse {
Ellipse {
shape: kurbo::Ellipse::new(center, radii, rotation),
style,
}
}
}
impl LayerData for Ellipse {
fn render(&mut self, svg: &mut String) {
let kurbo::Vec2 { x: rx, y: ry } = self.shape.radii();
let kurbo::Point { x: cx, y: cy } = self.shape.center();
let _ = write!(
svg,
r#"<ellipse cx="0" cy="0" rx="{}" ry="{}" transform="translate({} {}) rotate({})" {} />"#,
rx,
ry,
cx,
cy,
self.shape.rotation().to_degrees(),
self.style.render(),
);
}
}

View File

@@ -3,6 +3,9 @@ pub mod style;
pub mod circle;
pub use circle::Circle;
pub mod ellipse;
pub use ellipse::Ellipse;
pub mod line;
pub use line::Line;
@@ -26,6 +29,7 @@ pub trait LayerData {
pub enum LayerDataTypes {
Folder(Folder),
Circle(Circle),
Ellipse(Ellipse),
Rect(Rect),
Line(Line),
PolyLine(PolyLine),
@@ -37,6 +41,7 @@ impl LayerDataTypes {
match self {
Self::Folder(f) => f.render(svg),
Self::Circle(c) => c.render(svg),
Self::Ellipse(e) => e.render(svg),
Self::Rect(r) => r.render(svg),
Self::Line(l) => l.render(svg),
Self::PolyLine(pl) => pl.render(svg),

View File

@@ -12,6 +12,16 @@ pub enum Operation {
r: f64,
style: style::PathStyle,
},
AddEllipse {
path: Vec<LayerId>,
insert_index: isize,
cx: f64,
cy: f64,
rx: f64,
ry: f64,
rot: f64,
style: style::PathStyle,
},
AddRect {
path: Vec<LayerId>,
insert_index: isize,