Skip to content

Commit

Permalink
core: Move key_code_to_button_key_code into ButtonKeyCode
Browse files Browse the repository at this point in the history
  • Loading branch information
kjarosh authored and Dinnerbone committed Oct 15, 2024
1 parent 7cc61a2 commit ba191b3
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,26 @@ impl ButtonKeyCode {
num_traits::FromPrimitive::from_u8(n)
}

pub fn from_key_code(key_code: KeyCode) -> Option<Self> {
Some(match key_code {
KeyCode::LEFT => ButtonKeyCode::Left,
KeyCode::RIGHT => ButtonKeyCode::Right,
KeyCode::HOME => ButtonKeyCode::Home,
KeyCode::END => ButtonKeyCode::End,
KeyCode::INSERT => ButtonKeyCode::Insert,
KeyCode::DELETE => ButtonKeyCode::Delete,
KeyCode::BACKSPACE => ButtonKeyCode::Backspace,
KeyCode::RETURN => ButtonKeyCode::Return,
KeyCode::UP => ButtonKeyCode::Up,
KeyCode::DOWN => ButtonKeyCode::Down,
KeyCode::PG_UP => ButtonKeyCode::PgUp,
KeyCode::PG_DOWN => ButtonKeyCode::PgDown,
KeyCode::ESCAPE => ButtonKeyCode::Escape,
KeyCode::TAB => ButtonKeyCode::Tab,
_ => return None,
})
}

pub fn from_player_event(event: PlayerEvent) -> Option<Self> {
match event {
// ASCII characters convert directly to keyPress button events.
Expand All @@ -753,9 +773,7 @@ impl ButtonKeyCode {
}

// Special keys have custom values for keyPress.
PlayerEvent::KeyDown { key_code, .. } => {
crate::events::key_code_to_button_key_code(key_code)
}
PlayerEvent::KeyDown { key_code, .. } => Self::from_key_code(key_code),
_ => None,
}
}
Expand All @@ -765,27 +783,6 @@ impl ButtonKeyCode {
}
}

pub fn key_code_to_button_key_code(key_code: KeyCode) -> Option<ButtonKeyCode> {
let out = match key_code {
KeyCode::LEFT => ButtonKeyCode::Left,
KeyCode::RIGHT => ButtonKeyCode::Right,
KeyCode::HOME => ButtonKeyCode::Home,
KeyCode::END => ButtonKeyCode::End,
KeyCode::INSERT => ButtonKeyCode::Insert,
KeyCode::DELETE => ButtonKeyCode::Delete,
KeyCode::BACKSPACE => ButtonKeyCode::Backspace,
KeyCode::RETURN => ButtonKeyCode::Return,
KeyCode::UP => ButtonKeyCode::Up,
KeyCode::DOWN => ButtonKeyCode::Down,
KeyCode::PG_UP => ButtonKeyCode::PgUp,
KeyCode::PG_DOWN => ButtonKeyCode::PgDown,
KeyCode::ESCAPE => ButtonKeyCode::Escape,
KeyCode::TAB => ButtonKeyCode::Tab,
_ => return None,
};
Some(out)
}

#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
pub enum GamepadButton {
Expand Down

0 comments on commit ba191b3

Please sign in to comment.