mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-20 03:18:06 +08:00
Implement Pen Tool (#79)
This commit is contained in:
committed by
Keavon Chambers
parent
b556dd6bfd
commit
e40727e914
@@ -1,3 +1,5 @@
|
||||
use layers::PolyLine;
|
||||
|
||||
use crate::{
|
||||
layers::{self, Folder, Layer, LayerData, LayerDataTypes, Line, Rect, Shape},
|
||||
DocumentError, LayerId, Operation,
|
||||
@@ -178,6 +180,12 @@ impl Document {
|
||||
|
||||
update_frontend(self.render(&mut vec![]));
|
||||
}
|
||||
Operation::AddPen { path, insert_index, points, style } => {
|
||||
let points: Vec<kurbo::Point> = points.into_iter().map(|it| it.into()).collect();
|
||||
let polyline = PolyLine::new(points, style);
|
||||
self.add_layer(&path, Layer::new(LayerDataTypes::PolyLine(polyline)), insert_index)?;
|
||||
update_frontend(self.render(&mut vec![]));
|
||||
}
|
||||
Operation::AddShape {
|
||||
path,
|
||||
insert_index,
|
||||
|
||||
@@ -9,6 +9,9 @@ pub use line::Line;
|
||||
pub mod rect;
|
||||
pub use rect::Rect;
|
||||
|
||||
pub mod polyline;
|
||||
pub use polyline::PolyLine;
|
||||
|
||||
pub mod shape;
|
||||
pub use shape::Shape;
|
||||
|
||||
@@ -25,6 +28,7 @@ pub enum LayerDataTypes {
|
||||
Circle(Circle),
|
||||
Rect(Rect),
|
||||
Line(Line),
|
||||
PolyLine(PolyLine),
|
||||
Shape(Shape),
|
||||
}
|
||||
|
||||
@@ -35,6 +39,7 @@ impl LayerDataTypes {
|
||||
Self::Circle(c) => c.render(),
|
||||
Self::Rect(r) => r.render(),
|
||||
Self::Line(l) => l.render(),
|
||||
Self::PolyLine(pl) => pl.render(),
|
||||
Self::Shape(s) => s.render(),
|
||||
}
|
||||
}
|
||||
|
||||
24
core/document/src/layers/polyline.rs
Normal file
24
core/document/src/layers/polyline.rs
Normal file
@@ -0,0 +1,24 @@
|
||||
use super::style;
|
||||
use super::LayerData;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct PolyLine {
|
||||
points: Vec<kurbo::Point>,
|
||||
style: style::PathStyle,
|
||||
}
|
||||
|
||||
impl PolyLine {
|
||||
pub fn new(points: Vec<impl Into<kurbo::Point>>, style: style::PathStyle) -> PolyLine {
|
||||
PolyLine {
|
||||
points: points.into_iter().map(|it| it.into()).collect(),
|
||||
style,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LayerData for PolyLine {
|
||||
fn render(&self) -> String {
|
||||
let points = self.points.iter().map(|p| format!("{},{}", p.x, p.y)).collect::<Vec<_>>().join(" ");
|
||||
format!(r#"<polyline points="{}" {}" />"#, points, self.style.render())
|
||||
}
|
||||
}
|
||||
@@ -3,14 +3,20 @@ use crate::color::Color;
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Default)]
|
||||
pub struct Fill {
|
||||
color: Color,
|
||||
color: Option<Color>,
|
||||
}
|
||||
impl Fill {
|
||||
pub fn new(color: Color) -> Self {
|
||||
Self { color }
|
||||
Self { color: Some(color) }
|
||||
}
|
||||
pub fn none() -> Self {
|
||||
Self { color: None }
|
||||
}
|
||||
pub fn render(&self) -> String {
|
||||
format!("fill: #{};", self.color.as_hex())
|
||||
match self.color {
|
||||
Some(c) => format!("fill: #{};", c.as_hex()),
|
||||
None => format!("fill: none;"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,12 @@ pub enum Operation {
|
||||
y1: f64,
|
||||
style: style::PathStyle,
|
||||
},
|
||||
AddPen {
|
||||
path: Vec<LayerId>,
|
||||
insert_index: isize,
|
||||
points: Vec<(f64, f64)>,
|
||||
style: style::PathStyle,
|
||||
},
|
||||
AddShape {
|
||||
path: Vec<LayerId>,
|
||||
insert_index: isize,
|
||||
|
||||
Reference in New Issue
Block a user