mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-24 03:58:12 +08:00
Add Active Tool Router (#58)
* Add Active Tool Router * Remove commented out import
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
use crate::events::Event;
|
||||
use crate::tools::Tool;
|
||||
use document_core::Operation;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Crop;
|
||||
|
||||
impl Tool for Crop {
|
||||
fn handle_input(&mut self, event: Event) -> Vec<Operation> {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
use crate::events::Event;
|
||||
use crate::tools::Tool;
|
||||
use document_core::Operation;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Ellipse;
|
||||
|
||||
impl Tool for Ellipse {
|
||||
fn handle_input(&mut self, event: Event) -> Vec<Operation> {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
use crate::events::Event;
|
||||
use crate::tools::Tool;
|
||||
use document_core::Operation;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Line;
|
||||
|
||||
impl Tool for Line {
|
||||
fn handle_input(&mut self, event: Event) -> Vec<Operation> {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,32 @@
|
||||
use crate::events::{ModKeys, MouseState, TracePoint};
|
||||
use crate::{events::Trace, Color};
|
||||
mod crop;
|
||||
mod ellipse;
|
||||
mod line;
|
||||
mod navigate;
|
||||
mod path;
|
||||
mod pen;
|
||||
mod rectangle;
|
||||
mod sample;
|
||||
mod select;
|
||||
mod shape;
|
||||
|
||||
use crate::events::{Event, ModKeys, MouseState, Trace, TracePoint};
|
||||
use crate::Color;
|
||||
use crate::EditorError;
|
||||
use document_core::Operation;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub trait Tool {
|
||||
fn handle_input(&mut self, event: Event) -> Vec<Operation>;
|
||||
}
|
||||
|
||||
pub struct ToolState {
|
||||
pub mouse_state: MouseState,
|
||||
pub mod_keys: ModKeys,
|
||||
pub trace: Trace,
|
||||
pub primary_color: Color,
|
||||
pub secondary_color: Color,
|
||||
pub active_tool: ToolType,
|
||||
pub active_tool_type: ToolType,
|
||||
pub tools: HashMap<ToolType, Box<dyn Tool>>,
|
||||
tool_settings: HashMap<ToolType, ToolSettings>,
|
||||
}
|
||||
|
||||
@@ -20,7 +38,19 @@ impl Default for ToolState {
|
||||
trace: Trace::new(),
|
||||
primary_color: Color::BLACK,
|
||||
secondary_color: Color::WHITE,
|
||||
active_tool: ToolType::Select,
|
||||
active_tool_type: ToolType::Select,
|
||||
tools: gen_tools_hash_map! {
|
||||
Select => select::Select,
|
||||
Crop => crop::Crop,
|
||||
Navigate => navigate::Navigate,
|
||||
Sample => sample::Sample,
|
||||
Path => path::Path,
|
||||
Pen => pen::Pen,
|
||||
Line => line::Line,
|
||||
Rectangle => rectangle::Rectangle,
|
||||
Ellipse => ellipse::Ellipse,
|
||||
Shape => shape::Shape,
|
||||
},
|
||||
tool_settings: default_tool_settings(),
|
||||
}
|
||||
}
|
||||
@@ -37,6 +67,10 @@ impl ToolState {
|
||||
mod_keys: self.mod_keys,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn active_tool(&mut self) -> Result<&mut Box<dyn Tool>, EditorError> {
|
||||
self.tools.get_mut(&self.active_tool_type).ok_or(EditorError::UnknownTool)
|
||||
}
|
||||
}
|
||||
|
||||
fn default_tool_settings() -> HashMap<ToolType, ToolSettings> {
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
use crate::events::Event;
|
||||
use crate::tools::Tool;
|
||||
use document_core::Operation;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Navigate;
|
||||
|
||||
impl Tool for Navigate {
|
||||
fn handle_input(&mut self, event: Event) -> Vec<Operation> {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
use crate::events::Event;
|
||||
use crate::tools::Tool;
|
||||
use document_core::Operation;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Path;
|
||||
|
||||
impl Tool for Path {
|
||||
fn handle_input(&mut self, event: Event) -> Vec<Operation> {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
use crate::events::Event;
|
||||
use crate::tools::Tool;
|
||||
use document_core::Operation;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Pen;
|
||||
|
||||
impl Tool for Pen {
|
||||
fn handle_input(&mut self, event: Event) -> Vec<Operation> {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
use crate::events::Event;
|
||||
use crate::tools::Tool;
|
||||
use document_core::Operation;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Rectangle;
|
||||
|
||||
impl Tool for Rectangle {
|
||||
fn handle_input(&mut self, event: Event) -> Vec<Operation> {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
use crate::events::Event;
|
||||
use crate::tools::Tool;
|
||||
use document_core::Operation;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Sample;
|
||||
|
||||
impl Tool for Sample {
|
||||
fn handle_input(&mut self, event: Event) -> Vec<Operation> {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
use crate::events::Event;
|
||||
use crate::events::MouseKeys;
|
||||
use crate::tools::Tool;
|
||||
use document_core::Operation;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Select(Fsm);
|
||||
|
||||
impl Tool for Select {
|
||||
fn handle_input(&mut self, event: Event) -> Vec<Operation> {
|
||||
match event {
|
||||
Event::MouseDown(state) => {
|
||||
if state.mouse_keys.contains(MouseKeys::LEFT) {
|
||||
self.0 = Fsm::LmbDown;
|
||||
}
|
||||
}
|
||||
Event::MouseUp(state) => {
|
||||
if self.0 == Fsm::LmbDown && state.mouse_keys.contains(MouseKeys::LEFT) {
|
||||
self.0 = Fsm::SelectedObject;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum Fsm {
|
||||
Ready,
|
||||
LmbDown,
|
||||
SelectedObject,
|
||||
}
|
||||
|
||||
impl Default for Fsm {
|
||||
fn default() -> Self {
|
||||
Fsm::Ready
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
use crate::events::Event;
|
||||
use crate::tools::Tool;
|
||||
use document_core::Operation;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Shape;
|
||||
|
||||
impl Tool for Shape {
|
||||
fn handle_input(&mut self, event: Event) -> Vec<Operation> {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user