From bfd8f31854b660a9ba82e0af303280c74adfd9d4 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 27 Oct 2024 15:06:49 +0100 Subject: feat(engine): add Keys function to get previous key state --- engine/src/event.rs | 5 +++++ engine/src/input.rs | 56 +++++++++++++++++++++++++++++++++++++++++----------- engine/src/lib.rs | 9 ++++++++- engine/src/window.rs | 4 ++-- 4 files changed, 59 insertions(+), 15 deletions(-) diff --git a/engine/src/event.rs b/engine/src/event.rs index 8d5f805..e5ae486 100644 --- a/engine/src/event.rs +++ b/engine/src/event.rs @@ -20,3 +20,8 @@ impl Event for Present {} pub struct PostPresent; impl Event for PostPresent {} + +#[derive(Debug)] +pub struct Conclude; + +impl Event for Conclude {} 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, + map: HashMap, } 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) +{ + 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, +} diff --git a/engine/src/lib.rs b/engine/src/lib.rs index 36e9285..5df2292 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -13,6 +13,7 @@ use ecs::{SoleAlreadyExistsError, World}; use crate::delta_time::{update as update_delta_time, DeltaTime, LastUpdate}; use crate::event::{ + Conclude as ConcludeEvent, PostPresent as PostPresentEvent, PreUpdate as PreUpdateEvent, Present as PresentEvent, @@ -48,7 +49,13 @@ pub extern crate ecs; pub(crate) use crate::data_types::matrix; pub use crate::data_types::{color, vector}; -type EventOrder = (PreUpdateEvent, UpdateEvent, PresentEvent, PostPresentEvent); +type EventOrder = ( + PreUpdateEvent, + UpdateEvent, + PresentEvent, + PostPresentEvent, + ConcludeEvent, +); #[derive(Debug)] pub struct Engine diff --git a/engine/src/window.rs b/engine/src/window.rs index b308539..ccc1b8d 100644 --- a/engine/src/window.rs +++ b/engine/src/window.rs @@ -8,7 +8,7 @@ use ecs::Sole; use glfw::WindowSize; use crate::data_types::dimens::Dimens; -use crate::event::{PostPresent as PostPresentEvent, Start as StartEvent}; +use crate::event::{Conclude as ConcludeEvent, Start as StartEvent}; use crate::vector::Vec2; mod reexports @@ -258,7 +258,7 @@ impl ecs::extension::Extension for Extension fn collect(self, mut collector: ExtensionCollector<'_>) { collector.add_system(StartEvent, initialize); - collector.add_system(PostPresentEvent, update); + collector.add_system(ConcludeEvent, update); let window = self .window_builder -- cgit v1.2.3-18-g5258