Add Shape Tool for drawing polygons (#75)

* ⬠ Add polygon drawing tool

* 🔤 Minor fix of variable and function names

*  Remove stroke

* ⌨️ Use N key as polygo tool shortcut.

* ⌨️ Now using key Y for polygons.

* ⌨️ The tooltip for the shortcut is fixed
This commit is contained in:
0HyperCube
2021-04-17 19:08:44 +01:00
committed by Keavon Chambers
parent 0ca4b9fe7c
commit 90df412aab
9 changed files with 324 additions and 95 deletions

View File

@@ -115,6 +115,7 @@ pub enum Key {
KeyV,
KeyX,
KeyZ,
KeyY,
Key0,
Key1,
Key2,

View File

@@ -75,6 +75,12 @@ impl Dispatcher {
tool_name: ToolType::Rectangle.to_string(),
});
}
Key::KeyY => {
editor_state.tool_state.active_tool_type = ToolType::Shape;
self.dispatch_response(Response::SetActiveTool {
tool_name: ToolType::Shape.to_string(),
});
}
Key::KeyE => {
editor_state.tool_state.active_tool_type = ToolType::Ellipse;
self.dispatch_response(Response::SetActiveTool {

View File

@@ -1,13 +1,80 @@
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 Shape;
pub struct Shape {
fsm_state: ShapeToolFsmState,
data: ShapeToolData,
}
impl Tool for Shape {
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 ShapeToolFsmState {
Ready,
LmbDown,
}
impl Default for ShapeToolFsmState {
fn default() -> Self {
ShapeToolFsmState::Ready
}
}
#[derive(Clone, Debug, Default)]
struct ShapeToolData {
drag_start: ViewportPosition,
sides: u8,
}
impl Fsm for ShapeToolFsmState {
type ToolData = ShapeToolData;
fn transition(self, event: &Event, document: &Document, data: &mut Self::ToolData, responses: &mut Vec<Response>, operations: &mut Vec<Operation>) -> Self {
match (self, event) {
(ShapeToolFsmState::Ready, Event::MouseDown(mouse_state)) if mouse_state.mouse_keys.contains(MouseKeys::LEFT) => {
data.drag_start = mouse_state.position;
ShapeToolFsmState::LmbDown
}
(ShapeToolFsmState::Ready, Event::KeyDown(Key::KeyZ)) => {
if let Some(id) = document.root.list_layers().last() {
operations.push(Operation::DeleteLayer { path: vec![*id] })
}
ShapeToolFsmState::Ready
}
// TODO - Check for left mouse button
(ShapeToolFsmState::LmbDown, Event::MouseUp(mouse_state)) => {
let r = data.drag_start.distance(&mouse_state.position);
log::info!("Draw Shape with radius: {:.2}", r);
let start = data.drag_start;
let end = mouse_state.position;
let sides = data.sides;
operations.push(Operation::AddShape {
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,
sides: 6,
});
ShapeToolFsmState::Ready
}
_ => self,
}
}
}