mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-21 07:18:11 +08:00
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:
committed by
Keavon Chambers
co-authored by
akshay1992kalbhor
Keavon Chambers
parent
60cf084684
commit
3b5cc535ee
@@ -1,12 +1,14 @@
|
||||
use glam::DAffine2;
|
||||
|
||||
use crate::{
|
||||
layers::{self, Folder, Layer, LayerData, LayerDataTypes, Line, PolyLine, Rect, Shape},
|
||||
layers::{self, style::PathStyle, Folder, Layer, LayerDataTypes, Line, PolyLine, Rect, Shape},
|
||||
DocumentError, DocumentResponse, LayerId, Operation,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Document {
|
||||
pub root: layers::Folder,
|
||||
pub work: Folder,
|
||||
pub root: Layer,
|
||||
pub work: Layer,
|
||||
pub work_mount_path: Vec<LayerId>,
|
||||
pub work_operations: Vec<Operation>,
|
||||
pub work_mounted: bool,
|
||||
@@ -15,8 +17,8 @@ pub struct Document {
|
||||
impl Default for Document {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
root: Folder::default(),
|
||||
work: Folder::default(),
|
||||
root: Layer::new(LayerDataTypes::Folder(Folder::default()), DAffine2::IDENTITY.to_cols_array(), PathStyle::default()),
|
||||
work: Layer::new(LayerDataTypes::Folder(Folder::default()), DAffine2::IDENTITY.to_cols_array(), PathStyle::default()),
|
||||
work_mount_path: Vec::new(),
|
||||
work_operations: Vec::new(),
|
||||
work_mounted: false,
|
||||
@@ -41,8 +43,11 @@ impl Document {
|
||||
return;
|
||||
}
|
||||
if path.as_slice() == self.work_mount_path {
|
||||
self.document_folder_mut(path).unwrap().render(svg);
|
||||
self.work.render(svg);
|
||||
// TODO: Handle if mounted in nested folders
|
||||
let transform = self.document_folder(path).unwrap().transform;
|
||||
self.document_folder_mut(path).unwrap().render_as_folder(svg);
|
||||
self.work.transform = transform;
|
||||
self.work.render_as_folder(svg);
|
||||
path.pop();
|
||||
}
|
||||
let ids = self.folder(path).unwrap().layer_ids.clone();
|
||||
@@ -68,10 +73,10 @@ impl Document {
|
||||
/// This function respects mounted folders and will thus not contain the layers already
|
||||
/// present in the document if a temporary folder is mounted on top.
|
||||
pub fn folder(&self, mut path: &[LayerId]) -> Result<&Folder, DocumentError> {
|
||||
let mut root = &self.root;
|
||||
let mut root = self.root.as_folder()?;
|
||||
if self.is_mounted(self.work_mount_path.as_slice(), path) {
|
||||
path = &path[self.work_mount_path.len()..];
|
||||
root = &self.work;
|
||||
root = self.work.as_folder()?;
|
||||
}
|
||||
for id in path {
|
||||
root = root.folder(*id).ok_or(DocumentError::LayerNotFound)?;
|
||||
@@ -87,9 +92,9 @@ impl Document {
|
||||
pub fn folder_mut(&mut self, mut path: &[LayerId]) -> Result<&mut Folder, DocumentError> {
|
||||
let mut root = if self.is_mounted(self.work_mount_path.as_slice(), path) {
|
||||
path = &path[self.work_mount_path.len()..];
|
||||
&mut self.work
|
||||
self.work.as_folder_mut()?
|
||||
} else {
|
||||
&mut self.root
|
||||
self.root.as_folder_mut()?
|
||||
};
|
||||
for id in path {
|
||||
root = root.folder_mut(*id).ok_or(DocumentError::LayerNotFound)?;
|
||||
@@ -101,10 +106,10 @@ impl Document {
|
||||
/// or if the requested layer is not of type folder.
|
||||
/// This function does **not** respect mounted folders and will always return the current
|
||||
/// state of the document, disregarding any temporary modifications.
|
||||
pub fn document_folder(&self, path: &[LayerId]) -> Result<&Folder, DocumentError> {
|
||||
pub fn document_folder(&self, path: &[LayerId]) -> Result<&Layer, DocumentError> {
|
||||
let mut root = &self.root;
|
||||
for id in path {
|
||||
root = root.folder(*id).ok_or(DocumentError::LayerNotFound)?;
|
||||
root = root.as_folder()?.layer(*id).ok_or(DocumentError::LayerNotFound)?;
|
||||
}
|
||||
Ok(root)
|
||||
}
|
||||
@@ -114,10 +119,10 @@ impl Document {
|
||||
/// This function does **not** respect mounted folders and will always return the current
|
||||
/// state of the document, disregarding any temporary modifications.
|
||||
/// If you manually edit the folder you have to set the cache_dirty flag yourself.
|
||||
pub fn document_folder_mut(&mut self, path: &[LayerId]) -> Result<&mut Folder, DocumentError> {
|
||||
pub fn document_folder_mut(&mut self, path: &[LayerId]) -> Result<&mut Layer, DocumentError> {
|
||||
let mut root = &mut self.root;
|
||||
for id in path {
|
||||
root = root.folder_mut(*id).ok_or(DocumentError::LayerNotFound)?;
|
||||
root = root.as_folder_mut()?.layer_mut(*id).ok_or(DocumentError::LayerNotFound)?;
|
||||
}
|
||||
Ok(root)
|
||||
}
|
||||
@@ -137,7 +142,7 @@ impl Document {
|
||||
|
||||
/// Replaces the layer at the specified `path` with `layer`.
|
||||
pub fn set_layer(&mut self, path: &[LayerId], layer: Layer) -> Result<(), DocumentError> {
|
||||
let mut folder = &mut self.root;
|
||||
let mut folder = self.root.as_folder_mut()?;
|
||||
if let Ok((path, id)) = split_path(path) {
|
||||
self.layer_mut(path)?.cache_dirty = true;
|
||||
folder = self.folder_mut(path)?;
|
||||
@@ -163,7 +168,7 @@ impl Document {
|
||||
pub fn delete(&mut self, path: &[LayerId]) -> Result<(), DocumentError> {
|
||||
let (path, id) = split_path(path)?;
|
||||
let _ = self.layer_mut(path).map(|x| x.cache_dirty = true);
|
||||
self.document_folder_mut(path)?.remove_layer(id)?;
|
||||
self.document_folder_mut(path)?.as_folder_mut()?.remove_layer(id)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -171,73 +176,46 @@ impl Document {
|
||||
/// reaction from the frontend, responses may be returned.
|
||||
pub fn handle_operation(&mut self, operation: Operation) -> Result<Option<Vec<DocumentResponse>>, DocumentError> {
|
||||
let responses = match &operation {
|
||||
Operation::AddCircle { path, insert_index, cx, cy, r, style } => {
|
||||
let id = self.add_layer(&path, Layer::new(LayerDataTypes::Circle(layers::Circle::new((*cx, *cy), *r, *style))), *insert_index)?;
|
||||
Operation::AddEllipse { path, insert_index, transform, style } => {
|
||||
let id = self.add_layer(&path, Layer::new(LayerDataTypes::Ellipse(layers::Ellipse::new()), *transform, *style), *insert_index)?;
|
||||
let path = [path.clone(), vec![id]].concat();
|
||||
|
||||
Some(vec![DocumentResponse::DocumentChanged, DocumentResponse::SelectLayer { path }])
|
||||
}
|
||||
Operation::AddEllipse {
|
||||
Operation::AddRect { path, insert_index, transform, style } => {
|
||||
let id = self.add_layer(&path, Layer::new(LayerDataTypes::Rect(Rect::new()), *transform, *style), *insert_index)?;
|
||||
let path = [path.clone(), vec![id]].concat();
|
||||
|
||||
Some(vec![DocumentResponse::DocumentChanged, DocumentResponse::SelectLayer { path }])
|
||||
}
|
||||
Operation::AddLine { path, insert_index, transform, style } => {
|
||||
let id = self.add_layer(&path, Layer::new(LayerDataTypes::Line(Line::new()), *transform, *style), *insert_index)?;
|
||||
let path = [path.clone(), vec![id]].concat();
|
||||
|
||||
Some(vec![DocumentResponse::DocumentChanged, DocumentResponse::SelectLayer { path }])
|
||||
}
|
||||
Operation::AddPen {
|
||||
path,
|
||||
insert_index,
|
||||
cx,
|
||||
cy,
|
||||
rx,
|
||||
ry,
|
||||
rot,
|
||||
points,
|
||||
transform,
|
||||
style,
|
||||
} => {
|
||||
let id = self.add_layer(&path, Layer::new(LayerDataTypes::Ellipse(layers::Ellipse::new((*cx, *cy), (*rx, *ry), *rot, *style))), *insert_index)?;
|
||||
let path = [path.clone(), vec![id]].concat();
|
||||
|
||||
Some(vec![DocumentResponse::DocumentChanged, DocumentResponse::SelectLayer { path }])
|
||||
}
|
||||
Operation::AddRect {
|
||||
path,
|
||||
insert_index,
|
||||
x0,
|
||||
y0,
|
||||
x1,
|
||||
y1,
|
||||
style,
|
||||
} => {
|
||||
let id = self.add_layer(&path, Layer::new(LayerDataTypes::Rect(Rect::new((*x0, *y0), (*x1, *y1), *style))), *insert_index)?;
|
||||
let path = [path.clone(), vec![id]].concat();
|
||||
|
||||
Some(vec![DocumentResponse::DocumentChanged, DocumentResponse::SelectLayer { path }])
|
||||
}
|
||||
Operation::AddLine {
|
||||
path,
|
||||
insert_index,
|
||||
x0,
|
||||
y0,
|
||||
x1,
|
||||
y1,
|
||||
style,
|
||||
} => {
|
||||
let id = self.add_layer(&path, Layer::new(LayerDataTypes::Line(Line::new((*x0, *y0), (*x1, *y1), *style))), *insert_index)?;
|
||||
let path = [path.clone(), vec![id]].concat();
|
||||
|
||||
Some(vec![DocumentResponse::DocumentChanged, DocumentResponse::SelectLayer { path }])
|
||||
}
|
||||
Operation::AddPen { path, insert_index, points, style } => {
|
||||
let points: Vec<kurbo::Point> = points.iter().map(|&it| it.into()).collect();
|
||||
let polyline = PolyLine::new(points, *style);
|
||||
self.add_layer(&path, Layer::new(LayerDataTypes::PolyLine(polyline)), *insert_index)?;
|
||||
let points: Vec<glam::DVec2> = points.iter().map(|&it| it.into()).collect();
|
||||
let polyline = PolyLine::new(points);
|
||||
self.add_layer(&path, Layer::new(LayerDataTypes::PolyLine(polyline), *transform, *style), *insert_index)?;
|
||||
Some(vec![DocumentResponse::DocumentChanged])
|
||||
}
|
||||
Operation::AddShape {
|
||||
path,
|
||||
insert_index,
|
||||
x0,
|
||||
y0,
|
||||
x1,
|
||||
y1,
|
||||
transform,
|
||||
equal_sides,
|
||||
sides,
|
||||
style,
|
||||
} => {
|
||||
let s = Shape::new((*x0, *y0), (*x1, *y1), *sides, *style);
|
||||
let id = self.add_layer(&path, Layer::new(LayerDataTypes::Shape(s)), *insert_index)?;
|
||||
let s = Shape::new(*equal_sides, *sides);
|
||||
let id = self.add_layer(&path, Layer::new(LayerDataTypes::Shape(s), *transform, *style), *insert_index)?;
|
||||
let path = [path.clone(), vec![id]].concat();
|
||||
|
||||
Some(vec![DocumentResponse::DocumentChanged, DocumentResponse::SelectLayer { path }])
|
||||
@@ -256,27 +234,34 @@ impl Document {
|
||||
Some(vec![DocumentResponse::DocumentChanged, DocumentResponse::FolderChanged { path: folder_path.to_vec() }])
|
||||
}
|
||||
Operation::AddFolder { path } => {
|
||||
self.set_layer(&path, Layer::new(LayerDataTypes::Folder(Folder::default())))?;
|
||||
self.set_layer(&path, Layer::new(LayerDataTypes::Folder(Folder::default()), DAffine2::IDENTITY.to_cols_array(), PathStyle::default()))?;
|
||||
|
||||
Some(vec![DocumentResponse::DocumentChanged, DocumentResponse::FolderChanged { path: path.clone() }])
|
||||
}
|
||||
Operation::MountWorkingFolder { path } => {
|
||||
self.work_mount_path = path.clone();
|
||||
self.work_operations.clear();
|
||||
self.work = Folder::default();
|
||||
self.work = Layer::new(LayerDataTypes::Folder(Folder::default()), DAffine2::IDENTITY.to_cols_array(), PathStyle::default());
|
||||
self.work_mounted = true;
|
||||
None
|
||||
}
|
||||
Operation::TransformLayer { path, transform } => {
|
||||
let transform = self.root.transform * DAffine2::from_cols_array(&transform);
|
||||
let layer = self.document_folder_mut(path).unwrap();
|
||||
layer.transform = transform;
|
||||
layer.cache_dirty = true;
|
||||
Some(vec![DocumentResponse::DocumentChanged])
|
||||
}
|
||||
Operation::DiscardWorkingFolder => {
|
||||
self.work_operations.clear();
|
||||
self.work_mount_path = vec![];
|
||||
self.work = Folder::default();
|
||||
self.work = Layer::new(LayerDataTypes::Folder(Folder::default()), DAffine2::IDENTITY.to_cols_array(), PathStyle::default());
|
||||
self.work_mounted = false;
|
||||
Some(vec![DocumentResponse::DocumentChanged])
|
||||
}
|
||||
Operation::ClearWorkingFolder => {
|
||||
self.work_operations.clear();
|
||||
self.work = Folder::default();
|
||||
self.work = Layer::new(LayerDataTypes::Folder(Folder::default()), DAffine2::IDENTITY.to_cols_array(), PathStyle::default());
|
||||
Some(vec![DocumentResponse::DocumentChanged])
|
||||
}
|
||||
Operation::CommitTransaction => {
|
||||
@@ -286,7 +271,7 @@ impl Document {
|
||||
std::mem::swap(&mut ops, &mut self.work_operations);
|
||||
self.work_mounted = false;
|
||||
self.work_mount_path = vec![];
|
||||
self.work = Folder::default();
|
||||
self.work = Layer::new(LayerDataTypes::Folder(Folder::default()), DAffine2::IDENTITY.to_cols_array(), PathStyle::default());
|
||||
let mut responses = vec![];
|
||||
for operation in ops.into_iter() {
|
||||
if let Some(mut op_responses) = self.handle_operation(operation)? {
|
||||
|
||||
Reference in New Issue
Block a user