Skip to content

Commit

Permalink
Implement Line tool (#71)
Browse files Browse the repository at this point in the history
* Add Line tool
  • Loading branch information
edwin0cheng authored and Keavon committed Apr 13, 2021
1 parent 6511f5a commit f12db37
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 5 deletions.
2 changes: 1 addition & 1 deletion client/web/src/components/panels/Document.vue
Expand Up @@ -75,7 +75,7 @@
<ShelfItem title="Pen Tool" :active="activeTool === 'Pen'" @click="'tool not implemented' || selectTool('Pen')"><PenTool /></ShelfItem>
<ShelfItem title="Freehand Tool" :active="activeTool === 'Freehand'" @click="'tool not implemented' || selectTool('Freehand')"><FreehandTool /></ShelfItem>
<ShelfItem title="Spline Tool" :active="activeTool === 'Spline'" @click="'tool not implemented' || selectTool('Spline')"><SplineTool /></ShelfItem>
<ShelfItem title="Line Tool" :active="activeTool === 'Line'" @click="'tool not implemented' || selectTool('Line')"><LineTool /></ShelfItem>
<ShelfItem title="Line Tool" :active="activeTool === 'Line'" @click="selectTool('Line')"><LineTool /></ShelfItem>
<ShelfItem title="Rectangle Tool (M)" :active="activeTool === 'Rectangle'" @click="selectTool('Rectangle')"><RectangleTool /></ShelfItem>
<ShelfItem title="Ellipse Tool (E)" :active="activeTool === 'Ellipse'" @click="selectTool('Ellipse')"><EllipseTool /></ShelfItem>
<ShelfItem title="Shape Tool" :active="activeTool === 'Shape'" @click="'tool not implemented' || selectTool('Shape')"><ShapeTool /></ShelfItem>
Expand Down
1 change: 1 addition & 0 deletions client/web/wasm/src/wrappers.rs
Expand Up @@ -79,6 +79,7 @@ pub fn translate_key(name: &str) -> events::Key {
match name {
"e" => K::KeyE,
"v" => K::KeyV,
"l" => K::KeyL,
"r" => K::KeyR,
"m" => K::KeyM,
"x" => K::KeyX,
Expand Down
11 changes: 10 additions & 1 deletion core/document/src/lib.rs
@@ -1,13 +1,14 @@
pub mod operation;

pub use kurbo::{Circle, Point, Rect};
pub use kurbo::{Circle, Line, Point, Rect};
pub use operation::Operation;

#[derive(Debug, Clone, PartialEq)]
pub enum LayerType {
Folder(Folder),
Circle(Circle),
Rect(Rect),
Line(Line),
}

impl LayerType {
Expand All @@ -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)
}
}
}
}
Expand Down Expand Up @@ -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)?;

Expand Down
8 changes: 8 additions & 0 deletions core/document/src/operation.rs
Expand Up @@ -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>,
},
Expand Down
1 change: 1 addition & 0 deletions core/editor/src/dispatcher/events.rs
Expand Up @@ -111,6 +111,7 @@ pub enum Key {
KeyR,
KeyM,
KeyE,
KeyL,
KeyV,
KeyX,
KeyZ,
Expand Down
6 changes: 6 additions & 0 deletions core/editor/src/dispatcher/mod.rs
Expand Up @@ -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 {
Expand Down
68 changes: 65 additions & 3 deletions core/editor/src/tools/line.rs
@@ -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,
}
}
}

0 comments on commit f12db37

Please sign in to comment.