Implement key handling (#65)

This commit is contained in:
TrueDoctor
2021-04-07 11:02:56 +02:00
committed by Keavon Chambers
parent 599d478a5c
commit 0a112be97b
7 changed files with 112 additions and 13 deletions

View File

@@ -91,6 +91,20 @@ impl MouseState {
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Key {
UnknownKey,
KeyR,
KeyM,
KeyE,
KeyX,
Key0,
Key1,
Key2,
Key3,
Key4,
Key5,
Key6,
Key7,
Key8,
Key9,
}
bitflags! {

View File

@@ -1,7 +1,7 @@
pub mod events;
use crate::{Color, Document, EditorError, EditorState};
use crate::{tools::ToolType, Color, Document, EditorError, EditorState};
use document_core::Operation;
use events::{Event, Response};
use events::{Event, Key, Response};
pub type Callback = Box<dyn Fn(Response)>;
pub struct Dispatcher {
@@ -23,7 +23,7 @@ impl Dispatcher {
editor_state.tool_state.secondary_color = *color;
}
Event::SwapColors => {
std::mem::swap(&mut editor_state.tool_state.primary_color, &mut editor_state.tool_state.secondary_color);
editor_state.tool_state.swap_colors();
}
Event::ResetColors => {
editor_state.tool_state.primary_color = Color::BLACK;
@@ -38,8 +38,36 @@ impl Dispatcher {
Event::MouseMove(pos) => {
editor_state.tool_state.mouse_state.position = *pos;
}
Event::KeyUp(key) => todo!(),
Event::KeyDown(key) => todo!(),
Event::KeyUp(key) => (),
Event::KeyDown(key) => {
log::trace!("pressed key {:?}", key);
log::debug!("pressed key {:?}", key);
match key {
Key::Key0 => {
log::set_max_level(log::LevelFilter::Info);
log::debug!("set log verbosity to info");
}
Key::Key1 => {
log::set_max_level(log::LevelFilter::Debug);
log::debug!("set log verbosity to debug");
}
Key::Key2 => {
log::set_max_level(log::LevelFilter::Trace);
log::debug!("set log verbosity to trace");
}
Key::KeyM => {
editor_state.tool_state.active_tool_type = ToolType::Rectangle;
}
Key::KeyE => {
editor_state.tool_state.active_tool_type = ToolType::Ellipse;
}
Key::KeyX => {
editor_state.tool_state.swap_colors();
}
_ => (),
}
}
}
let (responses, operations) = editor_state.tool_state.active_tool()?.handle_input(event, &editor_state.document);

View File

@@ -77,6 +77,10 @@ impl ToolFsmState {
pub fn active_tool(&mut self) -> Result<&mut Box<dyn Tool>, EditorError> {
self.tools.get_mut(&self.active_tool_type).ok_or(EditorError::UnknownTool)
}
pub fn swap_colors(&mut self) {
std::mem::swap(&mut self.primary_color, &mut self.secondary_color);
}
}
fn default_tool_settings() -> HashMap<ToolType, ToolSettings> {