Implement svg layer structure (#59)

* Add folder struct

* Add file system like structure to interface with svgs

* Add primitive undo functionality

* Restructure layer system
This commit is contained in:
TrueDoctor
2021-04-12 00:57:39 +02:00
committed by Keavon Chambers
parent b19a9ab6fd
commit 6511f5a628
8 changed files with 245 additions and 31 deletions
+1
View File
@@ -113,6 +113,7 @@ pub enum Key {
KeyE,
KeyV,
KeyX,
KeyZ,
Key0,
Key1,
Key2,
+10 -10
View File
@@ -85,28 +85,28 @@ impl Dispatcher {
let (responses, operations) = editor_state.tool_state.active_tool()?.handle_input(event, &editor_state.document);
self.dispatch_operations(&mut editor_state.document, &operations);
self.dispatch_operations(&mut editor_state.document, operations);
// TODO - Dispatch Responses
Ok(())
}
fn dispatch_operations(&self, document: &mut Document, operations: &[Operation]) {
fn dispatch_operations<I: IntoIterator<Item = Operation>>(&self, document: &mut Document, operations: I) {
for operation in operations {
self.dispatch_operation(document, operation);
if let Err(error) = self.dispatch_operation(document, operation) {
log::error!("{}", error);
}
}
}
fn dispatch_operation(&self, document: &mut Document, operation: &Operation) {
document.handle_operation(operation, |svg: String| {
self.dispatch_response(Response::UpdateCanvas { document: svg });
});
fn dispatch_operation(&self, document: &mut Document, operation: Operation) -> Result<(), EditorError> {
document.handle_operation(operation, |svg: String| self.dispatch_response(Response::UpdateCanvas { document: svg }))?;
Ok(())
}
pub fn dispatch_responses(&self, responses: &[Response]) {
pub fn dispatch_responses<I: IntoIterator<Item = Response>>(&self, responses: I) {
for response in responses {
// TODO - Remove clone when Response is Copy
self.dispatch_response(response.clone());
self.dispatch_response(response);
}
}
+4
View File
@@ -1,5 +1,6 @@
use crate::events::Event;
use crate::Color;
use document_core::DocumentError;
use thiserror::Error;
/// The error type used by the Graphite editor.
@@ -15,6 +16,8 @@ pub enum EditorError {
Color(String),
#[error("The requested tool does not exist")]
UnknownTool,
#[error("The operation caused a document error {0:?}")]
Document(String),
}
macro_rules! derive_from {
@@ -31,3 +34,4 @@ derive_from!(&str, Misc);
derive_from!(String, Misc);
derive_from!(Color, Color);
derive_from!(Event, InvalidEvent);
derive_from!(DocumentError, Document);
+9 -1
View File
@@ -1,5 +1,5 @@
use crate::events::{Event, Response};
use crate::events::{MouseKeys, ViewportPosition};
use crate::events::{Key, MouseKeys, ViewportPosition};
use crate::tools::{Fsm, Tool};
use crate::Document;
use document_core::Operation;
@@ -45,12 +45,20 @@ impl Fsm for EllipseToolFsmState {
data.drag_start = mouse_state.position;
EllipseToolFsmState::LmbDown
}
(EllipseToolFsmState::Ready, Event::KeyDown(Key::KeyZ)) => {
if let Some(id) = document.root.list_layers().last() {
operations.push(Operation::DeleteLayer { path: vec![*id] })
}
EllipseToolFsmState::Ready
}
// TODO - Check for left mouse button
(EllipseToolFsmState::LmbDown, Event::MouseUp(mouse_state)) => {
let r = data.drag_start.distance(&mouse_state.position);
log::info!("draw ellipse with radius: {:.2}", r);
operations.push(Operation::AddCircle {
path: vec![],
insert_index: -1,
cx: data.drag_start.x as f64,
cy: data.drag_start.y as f64,
r: data.drag_start.distance(&mouse_state.position),
+9 -1
View File
@@ -1,5 +1,5 @@
use crate::events::{Event, Response};
use crate::events::{MouseKeys, ViewportPosition};
use crate::events::{Key, MouseKeys, ViewportPosition};
use crate::tools::{Fsm, Tool};
use crate::Document;
use document_core::Operation;
@@ -45,6 +45,12 @@ impl Fsm for RectangleToolFsmState {
data.drag_start = mouse_state.position;
RectangleToolFsmState::LmbDown
}
(RectangleToolFsmState::Ready, Event::KeyDown(Key::KeyZ)) => {
if let Some(id) = document.root.list_layers().last() {
operations.push(Operation::DeleteLayer { path: vec![*id] })
}
RectangleToolFsmState::Ready
}
// TODO - Check for left mouse button
(RectangleToolFsmState::LmbDown, Event::MouseUp(mouse_state)) => {
@@ -53,6 +59,8 @@ impl Fsm for RectangleToolFsmState {
let start = data.drag_start;
let end = mouse_state.position;
operations.push(Operation::AddRect {
path: vec![],
insert_index: -1,
x0: start.x as f64,
y0: start.y as f64,
x1: end.x as f64,