diff options
Diffstat (limited to 'engine/src/input.rs')
-rw-r--r-- | engine/src/input.rs | 56 |
1 files changed, 44 insertions, 12 deletions
diff --git a/engine/src/input.rs b/engine/src/input.rs index e292b76..d1fb336 100644 --- a/engine/src/input.rs +++ b/engine/src/input.rs @@ -4,7 +4,11 @@ use ecs::extension::Collector as ExtensionCollector; use ecs::sole::Single; use ecs::Sole; -use crate::event::{PreUpdate as PreUpdateEvent, Start as StartEvent}; +use crate::event::{ + PostPresent as PostPresentEvent, + PreUpdate as PreUpdateEvent, + Start as StartEvent, +}; use crate::vector::Vec2; use crate::window::Window; @@ -15,10 +19,10 @@ mod reexports pub use reexports::*; -#[derive(Debug, Clone, Sole)] +#[derive(Debug, Sole)] pub struct Keys { - map: HashMap<Key, KeyState>, + map: HashMap<Key, KeyData>, } impl Keys @@ -29,7 +33,15 @@ impl Keys Self { map: Key::KEYS .iter() - .map(|key| (*key, KeyState::Released)) + .map(|key| { + ( + *key, + KeyData { + state: KeyState::Released, + prev_tick_state: KeyState::Released, + }, + ) + }) .collect(), } } @@ -37,7 +49,16 @@ impl Keys #[must_use] pub fn get_key_state(&self, key: Key) -> KeyState { - self.map.get(&key).copied().expect("Key not in key map") + self.map.get(&key).expect("Key not in key map").state + } + + #[must_use] + pub fn get_prev_key_state(&self, key: Key) -> KeyState + { + self.map + .get(&key) + .expect("Key not in key map") + .prev_tick_state } pub fn set_key_state(&mut self, key: Key, new_key_state: KeyState) @@ -46,9 +67,9 @@ impl Keys return; } - let key_state = self.map.get_mut(&key).expect("Key not in key map"); + let key_data = self.map.get_mut(&key).expect("Key not in key map"); - *key_state = new_key_state; + key_data.state = new_key_state; } #[must_use] @@ -56,7 +77,7 @@ impl Keys { self.map .values() - .any(|key_state| matches!(key_state, KeyState::Pressed)) + .any(|key_data| matches!(key_data.state, KeyState::Pressed)) } } @@ -123,6 +144,7 @@ impl ecs::extension::Extension for Extension { collector.add_system(StartEvent, initialize); collector.add_system(PreUpdateEvent, maybe_clear_cursor_is_first_move); + collector.add_system(PostPresentEvent, set_keys_prev_tick_state); collector.add_sole(Keys::default()).ok(); collector.add_sole(Cursor::default()).ok(); @@ -140,8 +162,6 @@ fn initialize( let keys_weak_ref = keys.to_weak_ref(); window.set_key_callback(move |key, _scancode, key_state, _modifiers| { - let keys_weak_ref = keys_weak_ref.clone(); - let keys_ref = keys_weak_ref.access().expect("No world"); let mut keys = keys_ref.to_single(); @@ -152,8 +172,6 @@ fn initialize( let cursor_weak_ref = cursor.to_weak_ref(); window.set_cursor_pos_callback(move |cursor_position| { - let cursor_weak_ref = cursor_weak_ref.clone(); - let cursor_ref = cursor_weak_ref.access().expect("No world"); let mut cursor = cursor_ref.to_single(); @@ -201,3 +219,17 @@ fn maybe_clear_cursor_is_first_move( cursor_flags.is_first_move.pending_clear = true; } } + +fn set_keys_prev_tick_state(mut keys: Single<Keys>) +{ + for key_data in keys.map.values_mut() { + key_data.prev_tick_state = key_data.state; + } +} + +#[derive(Debug)] +struct KeyData +{ + state: KeyState, + prev_tick_state: KeyState, +} |