Implement key handling (#65)
This commit is contained in:
parent
599d478a5c
commit
0a112be97b
|
|
@ -109,6 +109,19 @@ export default defineComponent({
|
||||||
const { on_mouse_move } = await wasm;
|
const { on_mouse_move } = await wasm;
|
||||||
on_mouse_move(e.offsetX, e.offsetY);
|
on_mouse_move(e.offsetX, e.offsetY);
|
||||||
},
|
},
|
||||||
|
async keyDown(e: KeyboardEvent) {
|
||||||
|
const { on_key_down } = await wasm;
|
||||||
|
on_key_down(e.key);
|
||||||
|
},
|
||||||
|
async keyUp(e: KeyboardEvent) {
|
||||||
|
const { on_key_up } = await wasm;
|
||||||
|
on_key_up(e.key);
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
mounted() {
|
||||||
|
let self = this;
|
||||||
|
window.addEventListener("keyup", (evt: KeyboardEvent) => {self.keyUp(evt)})
|
||||||
|
window.addEventListener("keydown", (evt: KeyboardEvent) => {self.keyDown(evt)})
|
||||||
|
},
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::shims::Error;
|
use crate::wrappers::{translate_key, translate_tool, Color};
|
||||||
use crate::wrappers::{translate_tool, Color};
|
|
||||||
use crate::EDITOR_STATE;
|
use crate::EDITOR_STATE;
|
||||||
|
use crate::{shims::Error, utils};
|
||||||
use editor_core::events;
|
use editor_core::events;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
|
@ -46,6 +46,24 @@ pub fn on_mouse_up(x: u32, y: u32, mouse_keys: u8) -> Result<(), JsValue> {
|
||||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
|
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A keyboard button depressed within screenspace the bounds of the viewport
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn on_key_down(name: String) -> Result<(), JsValue> {
|
||||||
|
let key = translate_key(&name);
|
||||||
|
log::trace!("key down {:?}, name: {}", key, name);
|
||||||
|
let ev = events::Event::KeyDown(key);
|
||||||
|
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A keyboard button released
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn on_key_up(name: String) -> Result<(), JsValue> {
|
||||||
|
let key = translate_key(&name);
|
||||||
|
log::trace!("key up {:?}, name: {}", key, name);
|
||||||
|
let ev = events::Event::KeyUp(key);
|
||||||
|
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(|err| Error::new(&err.to_string()).into())
|
||||||
|
}
|
||||||
|
|
||||||
/// Update primary color
|
/// Update primary color
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn update_primary_color(primary_color: Color) -> Result<(), JsValue> {
|
pub fn update_primary_color(primary_color: Color) -> Result<(), JsValue> {
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ pub fn set_panic_hook() {
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#[wasm_bindgen(js_namespace = console)]
|
#[wasm_bindgen(js_namespace = console)]
|
||||||
fn debug(msg: &str, format: &str);
|
fn log(msg: &str, format: &str);
|
||||||
#[wasm_bindgen(js_namespace = console)]
|
#[wasm_bindgen(js_namespace = console)]
|
||||||
fn info(msg: &str, format: &str);
|
fn info(msg: &str, format: &str);
|
||||||
#[wasm_bindgen(js_namespace = console)]
|
#[wasm_bindgen(js_namespace = console)]
|
||||||
|
|
@ -32,13 +32,13 @@ impl log::Log for WasmLog {
|
||||||
|
|
||||||
fn log(&self, record: &log::Record) {
|
fn log(&self, record: &log::Record) {
|
||||||
let (log, name, color): (fn(&str, &str), &str, &str) = match record.level() {
|
let (log, name, color): (fn(&str, &str), &str, &str) = match record.level() {
|
||||||
log::Level::Trace => (debug, "trace", "color:plum"),
|
log::Level::Trace => (log, "trace", "color:plum"),
|
||||||
log::Level::Debug => (debug, "debug", "color:plum"),
|
log::Level::Debug => (log, "debug", "color:blue"),
|
||||||
log::Level::Warn => (warn, "warn", "color:#1b8"),
|
log::Level::Warn => (warn, "warn", "color:#fa2"),
|
||||||
log::Level::Info => (info, "info", "color:#fa2"),
|
log::Level::Info => (info, "info", "color:#1b8"),
|
||||||
log::Level::Error => (error, "error", "color:red"),
|
log::Level::Error => (error, "error", "color:red"),
|
||||||
};
|
};
|
||||||
let msg = &format!("{}", format_args!("%c{}%c\t{}", name, record.args()));
|
let msg = &format!("{}", format_args!("%c{}\t{}", name, record.args()));
|
||||||
log(msg, color)
|
log(msg, color)
|
||||||
}
|
}
|
||||||
fn flush(&self) {}
|
fn flush(&self) {}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::shims::Error;
|
use crate::shims::Error;
|
||||||
|
use editor_core::events;
|
||||||
use editor_core::tools::{SelectAppendMode, ToolType};
|
use editor_core::tools::{SelectAppendMode, ToolType};
|
||||||
use editor_core::Color as InnerColor;
|
use editor_core::Color as InnerColor;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
@ -48,3 +49,24 @@ pub fn translate_append_mode(name: &str) -> Option<SelectAppendMode> {
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn translate_key(name: &str) -> events::Key {
|
||||||
|
use events::Key as K;
|
||||||
|
match name {
|
||||||
|
"e" => K::KeyE,
|
||||||
|
"r" => K::KeyR,
|
||||||
|
"m" => K::KeyM,
|
||||||
|
"x" => K::KeyX,
|
||||||
|
"0" => K::Key0,
|
||||||
|
"1" => K::Key1,
|
||||||
|
"2" => K::Key2,
|
||||||
|
"3" => K::Key3,
|
||||||
|
"4" => K::Key4,
|
||||||
|
"5" => K::Key5,
|
||||||
|
"6" => K::Key6,
|
||||||
|
"7" => K::Key7,
|
||||||
|
"8" => K::Key8,
|
||||||
|
"9" => K::Key9,
|
||||||
|
_ => K::UnknownKey,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -91,6 +91,20 @@ impl MouseState {
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
pub enum Key {
|
pub enum Key {
|
||||||
UnknownKey,
|
UnknownKey,
|
||||||
|
KeyR,
|
||||||
|
KeyM,
|
||||||
|
KeyE,
|
||||||
|
KeyX,
|
||||||
|
Key0,
|
||||||
|
Key1,
|
||||||
|
Key2,
|
||||||
|
Key3,
|
||||||
|
Key4,
|
||||||
|
Key5,
|
||||||
|
Key6,
|
||||||
|
Key7,
|
||||||
|
Key8,
|
||||||
|
Key9,
|
||||||
}
|
}
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
pub mod events;
|
pub mod events;
|
||||||
use crate::{Color, Document, EditorError, EditorState};
|
use crate::{tools::ToolType, Color, Document, EditorError, EditorState};
|
||||||
use document_core::Operation;
|
use document_core::Operation;
|
||||||
use events::{Event, Response};
|
use events::{Event, Key, Response};
|
||||||
|
|
||||||
pub type Callback = Box<dyn Fn(Response)>;
|
pub type Callback = Box<dyn Fn(Response)>;
|
||||||
pub struct Dispatcher {
|
pub struct Dispatcher {
|
||||||
|
|
@ -23,7 +23,7 @@ impl Dispatcher {
|
||||||
editor_state.tool_state.secondary_color = *color;
|
editor_state.tool_state.secondary_color = *color;
|
||||||
}
|
}
|
||||||
Event::SwapColors => {
|
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 => {
|
Event::ResetColors => {
|
||||||
editor_state.tool_state.primary_color = Color::BLACK;
|
editor_state.tool_state.primary_color = Color::BLACK;
|
||||||
|
|
@ -38,8 +38,36 @@ impl Dispatcher {
|
||||||
Event::MouseMove(pos) => {
|
Event::MouseMove(pos) => {
|
||||||
editor_state.tool_state.mouse_state.position = *pos;
|
editor_state.tool_state.mouse_state.position = *pos;
|
||||||
}
|
}
|
||||||
Event::KeyUp(key) => todo!(),
|
Event::KeyUp(key) => (),
|
||||||
Event::KeyDown(key) => todo!(),
|
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);
|
let (responses, operations) = editor_state.tool_state.active_tool()?.handle_input(event, &editor_state.document);
|
||||||
|
|
|
||||||
|
|
@ -77,6 +77,10 @@ impl ToolFsmState {
|
||||||
pub fn active_tool(&mut self) -> Result<&mut Box<dyn Tool>, EditorError> {
|
pub fn active_tool(&mut self) -> Result<&mut Box<dyn Tool>, EditorError> {
|
||||||
self.tools.get_mut(&self.active_tool_type).ok_or(EditorError::UnknownTool)
|
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> {
|
fn default_tool_settings() -> HashMap<ToolType, ToolSettings> {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue