From 9e41e0c4b89e76df3071c688076cc1639316dbf8 Mon Sep 17 00:00:00 2001 From: grzi Date: Fri, 2 Apr 2021 20:54:48 +0200 Subject: [PATCH] feat: Add basic keyboard inputs FIXES #52 --- examples/tetris/main.rs | 9 +++++++++ src/application.rs | 28 ++++++++++++++++++++++++++++ src/core/inputs/keycode.rs | 24 ++++++++++++++++++++++++ src/core/inputs/mod.rs | 2 +- src/core/resources/inputs.rs | 10 ++++++++++ src/core/resources/keyboard.rs | 31 +++++++++++++++++++++++++++++++ src/core/resources/mod.rs | 1 + 7 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/core/inputs/keycode.rs create mode 100644 src/core/resources/keyboard.rs diff --git a/examples/tetris/main.rs b/examples/tetris/main.rs index 93691a9..7110766 100644 --- a/examples/tetris/main.rs +++ b/examples/tetris/main.rs @@ -13,7 +13,15 @@ use scion::{ }; use scion::core::components::ui::font::Font; use scion::core::components::ui::ui_text::UiText; +use scion::core::resources::inputs::Inputs; +use scion::core::inputs::keycode::KeyCode; +#[system] +fn test(#[resource] inputs: &Inputs){ + if inputs.keyboard().key_event(&KeyCode::Right) { + log::info!("right events pressed"); + } +} #[derive(Default)] struct LayerA; @@ -61,5 +69,6 @@ impl SimpleGameLayer for LayerA { fn main() { Scion::app() .with_game_layer(GameLayer::weak::()) + .with_system(test_system()) .run(); } diff --git a/src/application.rs b/src/application.rs index a248f72..28c35a8 100644 --- a/src/application.rs +++ b/src/application.rs @@ -23,6 +23,7 @@ use crate::{ }, state::GameState, systems::{hierarchy_system::children_manager_system, ui_text_system::ui_text_bitmap_update_system}, + inputs::keycode::KeyCode }, rendering::{renderer_state::RendererState, RendererType}, }; @@ -153,6 +154,28 @@ impl Scion { ElementState::Released => {} } } + WindowEvent::KeyboardInput { + input, .. + } => { + if let Some(keycode)= input.virtual_keycode { + match input.state { + ElementState::Pressed => { + self.resources + .get_mut::() + .expect("Missing mandatory ressource : Inputs") + .keyboard_mut() + .press(KeyCode::from(keycode)); + } + ElementState::Released => { + self.resources + .get_mut::() + .expect("Missing mandatory ressource : Inputs") + .keyboard_mut() + .release(KeyCode::from(keycode)); + } + } + } + } _ => {} } } @@ -211,6 +234,11 @@ impl Scion { &mut self.world, &mut self.resources, ); + self.resources + .get_mut::() + .expect("Missing mandatory ressource : Inputs") + .keyboard_mut() + .clear_events(); } } diff --git a/src/core/inputs/keycode.rs b/src/core/inputs/keycode.rs new file mode 100644 index 0000000..7415b2c --- /dev/null +++ b/src/core/inputs/keycode.rs @@ -0,0 +1,24 @@ +use winit::event::VirtualKeyCode; + +#[derive(Eq, PartialEq, Hash, Clone)] +pub enum KeyCode { + Escape, + Left, + Up, + Right, + Down, + Any +} + +impl From for KeyCode { + fn from(vkc: VirtualKeyCode) -> Self { + match vkc{ + VirtualKeyCode::Escape => KeyCode::Escape, + VirtualKeyCode::Left => KeyCode::Left, + VirtualKeyCode::Up => KeyCode::Up, + VirtualKeyCode::Right => KeyCode::Right, + VirtualKeyCode::Down => KeyCode::Down, + _ => KeyCode::Any + } + } +} \ No newline at end of file diff --git a/src/core/inputs/mod.rs b/src/core/inputs/mod.rs index 8b13789..26c35ca 100644 --- a/src/core/inputs/mod.rs +++ b/src/core/inputs/mod.rs @@ -1 +1 @@ - +pub mod keycode; \ No newline at end of file diff --git a/src/core/resources/inputs.rs b/src/core/resources/inputs.rs index 0788e40..687d26c 100644 --- a/src/core/resources/inputs.rs +++ b/src/core/resources/inputs.rs @@ -1,11 +1,13 @@ //! Everything that is relatives to the core.inputs. use crate::core::resources::mouse::Mouse; +use crate::core::resources::keyboard::Keyboard; /// A resource updated by `Scion` to keep track of the core.inputs /// Can be used in any system. #[derive(Default)] pub struct Inputs { mouse: Mouse, + keyboard: Keyboard } impl Inputs { @@ -16,4 +18,12 @@ impl Inputs { pub fn mouse_mut(&mut self) -> &mut Mouse { &mut self.mouse } + + pub fn keyboard(&self) -> &Keyboard { + &self.keyboard + } + + pub fn keyboard_mut(&mut self) -> &mut Keyboard { + &mut self.keyboard + } } diff --git a/src/core/resources/keyboard.rs b/src/core/resources/keyboard.rs new file mode 100644 index 0000000..d0a3b4f --- /dev/null +++ b/src/core/resources/keyboard.rs @@ -0,0 +1,31 @@ +use std::collections::HashSet; +use crate::core::inputs::keycode::KeyCode; + +#[derive(Default)] +pub struct Keyboard { + pressed_keys: HashSet, + event_keys: HashSet +} + +impl Keyboard { + pub fn key_pressed(&self, key: &KeyCode) -> bool{ + self.pressed_keys.contains(key) + } + + pub fn key_event(&self, key: &KeyCode) -> bool{ + self.event_keys.contains(key) + } + + pub(crate) fn clear_events(&mut self){ + self.event_keys.clear() + } + + pub(crate) fn press(&mut self, key: KeyCode){ + self.pressed_keys.insert(key.clone()); + self.event_keys.insert(key); + } + + pub(crate) fn release(&mut self, key: KeyCode){ + self.pressed_keys.remove(&key); + } +} diff --git a/src/core/resources/mod.rs b/src/core/resources/mod.rs index 21fbd47..76206dd 100644 --- a/src/core/resources/mod.rs +++ b/src/core/resources/mod.rs @@ -2,3 +2,4 @@ pub mod inputs; pub mod mouse; pub mod time; pub mod window; +pub mod keyboard;