Skip to content

Commit

Permalink
feat: Add basic keyboard inputs
Browse files Browse the repository at this point in the history
FIXES #52
  • Loading branch information
suspistew committed Apr 2, 2021
1 parent e59ae42 commit 9e41e0c
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 1 deletion.
9 changes: 9 additions & 0 deletions examples/tetris/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,5 +69,6 @@ impl SimpleGameLayer for LayerA {
fn main() {
Scion::app()
.with_game_layer(GameLayer::weak::<LayerA>())
.with_system(test_system())
.run();
}
28 changes: 28 additions & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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::<Inputs>()
.expect("Missing mandatory ressource : Inputs")
.keyboard_mut()
.press(KeyCode::from(keycode));
}
ElementState::Released => {
self.resources
.get_mut::<Inputs>()
.expect("Missing mandatory ressource : Inputs")
.keyboard_mut()
.release(KeyCode::from(keycode));
}
}
}
}
_ => {}
}
}
Expand Down Expand Up @@ -211,6 +234,11 @@ impl Scion {
&mut self.world,
&mut self.resources,
);
self.resources
.get_mut::<Inputs>()
.expect("Missing mandatory ressource : Inputs")
.keyboard_mut()
.clear_events();
}
}

Expand Down
24 changes: 24 additions & 0 deletions src/core/inputs/keycode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use winit::event::VirtualKeyCode;

#[derive(Eq, PartialEq, Hash, Clone)]
pub enum KeyCode {
Escape,
Left,
Up,
Right,
Down,
Any
}

impl From<VirtualKeyCode> 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
}
}
}
2 changes: 1 addition & 1 deletion src/core/inputs/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@

pub mod keycode;
10 changes: 10 additions & 0 deletions src/core/resources/inputs.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
}
31 changes: 31 additions & 0 deletions src/core/resources/keyboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::collections::HashSet;
use crate::core::inputs::keycode::KeyCode;

#[derive(Default)]
pub struct Keyboard {
pressed_keys: HashSet<KeyCode>,
event_keys: HashSet<KeyCode>
}

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);
}
}
1 change: 1 addition & 0 deletions src/core/resources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod inputs;
pub mod mouse;
pub mod time;
pub mod window;
pub mod keyboard;

0 comments on commit 9e41e0c

Please # to comment.