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
+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,
}
}
}