use serde::{Deserialize, Serialize};
use crate::Direction;
use super::{EventSource, KeyModifiers};
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct KeyEvent {
pub source: EventSource,
pub down: bool,
pub repeat: bool,
pub code: String, pub modifiers: KeyModifiers,
}
impl KeyEvent {
pub fn direction(&self) -> Option<Direction> {
self.wasd_direction().or_else(|| self.arrow_direction())
}
pub fn wasd_direction(&self) -> Option<Direction> {
match self.code.as_str() {
"KeyW" => Some(Direction::Up),
"KeyA" => Some(Direction::Left),
"KeyS" => Some(Direction::Down),
"KeyD" => Some(Direction::Right),
_ => None,
}
}
pub fn arrow_direction(&self) -> Option<Direction> {
match self.code.as_str() {
"ArrowUp" => Some(Direction::Up),
"ArrowLeft" => Some(Direction::Left),
"ArrowDown" => Some(Direction::Down),
"ArrowRight" => Some(Direction::Right),
_ => None,
}
}
}