Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
92 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,7 @@ pub enum Key { | |
KeyR, | ||
KeyM, | ||
KeyE, | ||
KeyL, | ||
KeyV, | ||
KeyX, | ||
KeyZ, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
} | ||
} | ||
} |