mirror of
https://github.com/lisk77/comet.git
synced 2025-10-24 05:48:50 +00:00
feat: added a camera with orthographic projection and did some work restructuring the comet_app to make the setup system optional. Input handling is moved to the app
This commit is contained in:
parent
780365aeb8
commit
5a9f632e3a
22 changed files with 1173 additions and 349 deletions
|
|
@ -1,7 +1,8 @@
|
|||
[package]
|
||||
name = "comet_input"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
winit = { version = "0.29", features = ["rwh_05"] }
|
||||
winit = { version = "0.29", features = ["rwh_05"] }
|
||||
winit_input_helper = "0.16.0"
|
||||
|
|
|
|||
73
crates/comet_input/src/input_handler.rs
Normal file
73
crates/comet_input/src/input_handler.rs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
use winit::event::{ElementState, WindowEvent, KeyEvent, Event};
|
||||
use std::collections::HashSet;
|
||||
use winit::event::WindowEvent::KeyboardInput;
|
||||
use winit::keyboard::PhysicalKey;
|
||||
use crate::keyboard::Key;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct InputHandler {
|
||||
keys_pressed: Vec<PhysicalKey>,
|
||||
keys_held: Vec<PhysicalKey>,
|
||||
keys_released: Vec<PhysicalKey>
|
||||
}
|
||||
|
||||
impl InputHandler {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
keys_pressed: Vec::new(),
|
||||
keys_held: Vec::new(),
|
||||
keys_released: Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update<T>(&mut self, event: &Event<T>) {
|
||||
match event {
|
||||
Event::WindowEvent {
|
||||
event: WindowEvent::KeyboardInput {
|
||||
event: KeyEvent {
|
||||
state,
|
||||
physical_key: PhysicalKey::Code(keycode),
|
||||
..
|
||||
},
|
||||
..
|
||||
},
|
||||
..
|
||||
} =>
|
||||
{
|
||||
match state {
|
||||
ElementState::Pressed => {
|
||||
if self.keys_pressed.contains(&PhysicalKey::Code(keycode.clone())) {
|
||||
self.keys_held.push(PhysicalKey::Code(keycode.clone()));
|
||||
} else {
|
||||
self.keys_pressed.push(PhysicalKey::Code(keycode.clone()));
|
||||
}
|
||||
self.keys_pressed.push(PhysicalKey::Code(keycode.clone()));
|
||||
}
|
||||
ElementState::Released => {
|
||||
self.keys_released = vec![];
|
||||
if let Some(index) = self.keys_pressed.iter().position(|&x| x == PhysicalKey::Code(keycode.clone())) {
|
||||
self.keys_pressed.remove(index);
|
||||
}
|
||||
if let Some(index) = self.keys_held.iter().position(|&x| x == PhysicalKey::Code(keycode.clone())) {
|
||||
self.keys_held.remove(index);
|
||||
}
|
||||
self.keys_released.push(PhysicalKey::Code(keycode.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn key_pressed(&self, key: Key) -> bool {
|
||||
self.keys_pressed.contains(&PhysicalKey::Code(key))
|
||||
}
|
||||
|
||||
pub fn key_held(&self, key: Key) -> bool {
|
||||
self.keys_held.contains(&PhysicalKey::Code(key))
|
||||
}
|
||||
|
||||
pub fn key_released(&self, key: Key) -> bool {
|
||||
self.keys_released.contains(&PhysicalKey::Code(key))
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
use winit::event::{ElementState, KeyEvent, WindowEvent};
|
||||
use winit::keyboard::{ KeyCode, PhysicalKey};
|
||||
use winit::keyboard::{ KeyCode, PhysicalKey };
|
||||
|
||||
pub type Key = KeyCode;
|
||||
|
||||
|
|
@ -29,4 +29,18 @@ pub fn key_released(event: &WindowEvent, key_code: Key) -> bool {
|
|||
} => *code == key_code,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn key_press(event: &WindowEvent, key_code: Key) -> bool {
|
||||
match event {
|
||||
WindowEvent::KeyboardInput {
|
||||
event: KeyEvent {
|
||||
state: ElementState::Pressed,
|
||||
physical_key: PhysicalKey::Code(code),
|
||||
..
|
||||
},
|
||||
..
|
||||
} => *code == key_code,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
|
@ -1,2 +1,3 @@
|
|||
pub mod keyboard;
|
||||
pub mod mouse;
|
||||
pub mod input_handler;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue