Commit 70664afb authored by Rahix's avatar Rahix 🦀
Browse files

state: Add keyboard event handling

parent e76aee14
......@@ -47,4 +47,5 @@ features = [
"NodeList",
"SvgElement",
"CssStyleDeclaration",
"KeyboardEvent",
]
......@@ -32,6 +32,8 @@ impl Transition {
#[derive(Debug, Clone)]
pub enum Event {
MouseClick(nalgebra::Point2<f32>),
KeyDown(String),
KeyUp(String),
}
pub trait State {
......@@ -63,6 +65,53 @@ impl StateInitializer {
}
}
pub fn register_keyevents(&self) {
utils::event_handler(
|fun| utils::document().body().unwrap().set_onkeydown(Some(fun)),
{
let state_machine = self.state_machine.clone();
let current_generation = self.generation;
move |event| {
if current_generation != StateMachine::current_generation(&state_machine) {
crate::console_warn!("Wrong generation in event handler, ignoring ...");
return;
}
let event = event
.dyn_ref::<web_sys::KeyboardEvent>()
.expect("not a keyboard event?");
StateMachine::handle_event(
state_machine.clone(),
"",
Event::KeyDown(event.key()),
);
}
},
);
utils::event_handler(
|fun| utils::document().body().unwrap().set_onkeyup(Some(fun)),
{
let state_machine = self.state_machine.clone();
let current_generation = self.generation;
move |event| {
if current_generation != StateMachine::current_generation(&state_machine) {
crate::console_warn!("Wrong generation in event handler, ignoring ...");
return;
}
let event = event
.dyn_ref::<web_sys::KeyboardEvent>()
.expect("not a keyboard event?");
StateMachine::handle_event(
state_machine.clone(),
"",
Event::KeyUp(event.key()),
);
}
},
);
}
pub fn register_onclick(&self, id: &str) {
utils::event_handler(
|fun| {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment