Add a movable canvas with matricies (#175)

* Convert polygon and rectangle tool to kurbo::BezPath

* Add glam

* Add affine transform to elipse and remove circle

* Format

* Add svg group and add matrix for group

* Convert all operations to use matricies

* Work uses same transform as root

* Format

* Frontend fixed to render changes to working colors when changed from backend (#180)

* Backend and Frontend modification to show working color mods

* Remove comments & change precedence for tool and doc actions

* Add keybind for resetting work colors

* Minor Frontend changes

* Remove early sample "greet" code

* Add a contributing section to the project README

* Add moving document around

* Add document transform for tools

* Update to GraphiteEditor's fork

* Use write in foreach for rendering group / folder

* Add missing TranslateDown action

* Use points for line operation

* Format

* Add todo to change to shape's aspect ratio

* Remove empty if

* Initial pass at refactor

* Fix polyline test

* Use document message to modify document transform

* Messages -> Operations

* Transform layer

* Format

* Use DAffine2::IDENTITY

* Clean up kurbo generation for line and rect

* Use .into for rectangle points

* Rename cols to transform

* Rename other cols to transform

* Add todo for into_iter

* Remove unnecessary clone

Co-authored-by: akshay1992kalbhor <akshay1992kalbhor@gmail.com>
Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
0HyperCube
2021-06-26 21:44:48 +01:00
committed by Keavon Chambers
co-authored by akshay1992kalbhor Keavon Chambers
parent 923e63c045
commit bb3293af43
25 changed files with 401 additions and 454 deletions
+61 -13
View File
@@ -1,12 +1,10 @@
pub mod style;
pub mod circle;
pub use circle::Circle;
pub mod ellipse;
pub use ellipse::Ellipse;
pub mod line;
use kurbo::BezPath;
pub use line::Line;
pub mod rect;
@@ -21,14 +19,16 @@ pub use shape::Shape;
pub mod folder;
pub use folder::Folder;
use crate::DocumentError;
pub trait LayerData {
fn render(&mut self, svg: &mut String);
fn render(&mut self, svg: &mut String, transform: glam::DAffine2, style: style::PathStyle);
fn to_kurbo_path(&mut self, transform: glam::DAffine2, style: style::PathStyle) -> BezPath;
}
#[derive(Debug, Clone, PartialEq)]
pub enum LayerDataTypes {
Folder(Folder),
Circle(Circle),
Ellipse(Ellipse),
Rect(Rect),
Line(Line),
@@ -37,19 +37,36 @@ pub enum LayerDataTypes {
}
macro_rules! call_render {
($self:ident.render($svg:ident) { $($variant:ident),* }) => {
($self:ident.render($svg:ident, $transform:ident, $style:ident) { $($variant:ident),* }) => {
match $self {
$(Self::$variant(x) => x.render($svg)),*
$(Self::$variant(x) => x.render($svg, $transform, $style)),*
}
};
}
macro_rules! call_kurbo_path {
($self:ident.to_kurbo_path($transform:ident, $style:ident) { $($variant:ident),* }) => {
match $self {
$(Self::$variant(x) => x.to_kurbo_path($transform, $style)),*
}
};
}
impl LayerDataTypes {
pub fn render(&mut self, svg: &mut String) {
pub fn render(&mut self, svg: &mut String, transform: glam::DAffine2, style: style::PathStyle) {
call_render! {
self.render(svg) {
self.render(svg, transform, style) {
Folder,
Ellipse,
Rect,
Line,
PolyLine,
Shape
}
}
}
pub fn to_kurbo_path(&mut self, transform: glam::DAffine2, style: style::PathStyle) -> BezPath {
call_kurbo_path! {
self.to_kurbo_path(transform, style) {
Folder,
Circle,
Ellipse,
Rect,
Line,
@@ -65,16 +82,20 @@ pub struct Layer {
pub visible: bool,
pub name: Option<String>,
pub data: LayerDataTypes,
pub transform: glam::DAffine2,
pub style: style::PathStyle,
pub cache: String,
pub cache_dirty: bool,
}
impl Layer {
pub fn new(data: LayerDataTypes) -> Self {
pub fn new(data: LayerDataTypes, transform: [f64; 6], style: style::PathStyle) -> Self {
Self {
visible: true,
name: None,
data,
transform: glam::DAffine2::from_cols_array(&transform),
style: style,
cache: String::new(),
cache_dirty: true,
}
@@ -86,9 +107,36 @@ impl Layer {
}
if self.cache_dirty {
self.cache.clear();
self.data.render(&mut self.cache);
self.data.render(&mut self.cache, self.transform, self.style);
self.cache_dirty = false;
}
self.cache.as_str()
}
pub fn render_on(&mut self, svg: &mut String) {
*svg += self.render();
}
pub fn to_kurbo_path(&mut self) -> BezPath {
self.data.to_kurbo_path(self.transform, self.style)
}
pub fn as_folder_mut(&mut self) -> Result<&mut Folder, DocumentError> {
match &mut self.data {
LayerDataTypes::Folder(f) => Ok(f),
_ => Err(DocumentError::NotAFolder),
}
}
pub fn as_folder(&self) -> Result<&Folder, DocumentError> {
match &self.data {
LayerDataTypes::Folder(f) => Ok(&f),
_ => Err(DocumentError::NotAFolder),
}
}
pub fn render_as_folder(&mut self, svg: &mut String) {
match &mut self.data {
LayerDataTypes::Folder(f) => f.render(svg, self.transform, self.style),
_ => {}
}
}
}