Implement Line tool (#71)

* Add Line tool
This commit is contained in:
Edwin Cheng
2021-04-12 21:43:05 -07:00
committed by GitHub
parent 8c957e572b
commit 1fd298cf9e
7 changed files with 92 additions and 5 deletions
+10 -1
View File
@@ -1,6 +1,6 @@
pub mod operation;
pub use kurbo::{Circle, Point, Rect};
pub use kurbo::{Circle, Line, Point, Rect};
pub use operation::Operation;
#[derive(Debug, Clone, PartialEq)]
@@ -8,6 +8,7 @@ pub enum LayerType {
Folder(Folder),
Circle(Circle),
Rect(Rect),
Line(Line),
}
impl LayerType {
@@ -20,6 +21,9 @@ impl LayerType {
Self::Rect(r) => {
format!(r#"<rect x="{}" y="{}" width="{}" height="{}" style="fill: #fff;" />"#, r.min_x(), r.min_y(), r.width(), r.height())
}
Self::Line(l) => {
format!(r#"<line x1="{}" y1="{}" x2="{}" y2="{}" style="stroke: #fff;" />"#, l.p0.x, l.p0.y, l.p1.x, l.p1.y)
}
}
}
}
@@ -211,6 +215,11 @@ impl Document {
update_frontend(self.render());
}
Operation::AddLine { path, insert_index, x0, y0, x1, y1 } => {
self.add_layer(&path, Layer::new(LayerType::Line(Line::new(Point::new(x0, y0), Point::new(x1, y1)))), insert_index)?;
update_frontend(self.render());
}
Operation::DeleteLayer { path } => {
self.delete(&path)?;
+8
View File
@@ -16,6 +16,14 @@ pub enum Operation {
x1: f64,
y1: f64,
},
AddLine {
path: Vec<LayerId>,
insert_index: isize,
x0: f64,
y0: f64,
x1: f64,
y1: f64,
},
DeleteLayer {
path: Vec<LayerId>,
},
+1
View File
@@ -111,6 +111,7 @@ pub enum Key {
KeyR,
KeyM,
KeyE,
KeyL,
KeyV,
KeyX,
KeyZ,
+6
View File
@@ -63,6 +63,12 @@ impl Dispatcher {
tool_name: ToolType::Select.to_string(),
});
}
Key::KeyL => {
editor_state.tool_state.active_tool_type = ToolType::Line;
self.dispatch_response(Response::SetActiveTool {
tool_name: ToolType::Line.to_string(),
});
}
Key::KeyM => {
editor_state.tool_state.active_tool_type = ToolType::Rectangle;
self.dispatch_response(Response::SetActiveTool {
+65 -3
View File
@@ -1,13 +1,75 @@
use crate::events::{Event, Response};
use crate::tools::Tool;
use crate::events::{Key, MouseKeys, ViewportPosition};
use crate::tools::{Fsm, Tool};
use crate::Document;
use document_core::Operation;
#[derive(Default)]
pub struct Line;
pub struct Line {
fsm_state: LineToolFsmState,
data: LineToolData,
}
impl Tool for Line {
fn handle_input(&mut self, event: &Event, document: &Document) -> (Vec<Response>, Vec<Operation>) {
todo!();
let mut responses = Vec::new();
let mut operations = Vec::new();
self.fsm_state = self.fsm_state.transition(event, document, &mut self.data, &mut responses, &mut operations);
(responses, operations)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum LineToolFsmState {
Ready,
LmbDown,
}
impl Default for LineToolFsmState {
fn default() -> Self {
LineToolFsmState::Ready
}
}
#[derive(Clone, Debug, Default)]
struct LineToolData {
drag_start: ViewportPosition,
}
impl Fsm for LineToolFsmState {
type ToolData = LineToolData;
fn transition(self, event: &Event, document: &Document, data: &mut Self::ToolData, responses: &mut Vec<Response>, operations: &mut Vec<Operation>) -> Self {
match (self, event) {
(LineToolFsmState::Ready, Event::MouseDown(mouse_state)) if mouse_state.mouse_keys.contains(MouseKeys::LEFT) => {
data.drag_start = mouse_state.position;
LineToolFsmState::LmbDown
}
(LineToolFsmState::Ready, Event::KeyDown(Key::KeyZ)) => {
if let Some(id) = document.root.list_layers().last() {
operations.push(Operation::DeleteLayer { path: vec![*id] })
}
LineToolFsmState::Ready
}
// TODO - Check for left mouse button
(LineToolFsmState::LmbDown, Event::MouseUp(mouse_state)) => {
let distance = data.drag_start.distance(&mouse_state.position);
log::info!("draw Line with distance: {:.2}", distance);
let start = data.drag_start;
let end = mouse_state.position;
operations.push(Operation::AddLine {
path: vec![],
insert_index: -1,
x0: start.x as f64,
y0: start.y as f64,
x1: end.x as f64,
y1: end.y as f64,
});
LineToolFsmState::Ready
}
_ => self,
}
}
}