diff options
Diffstat (limited to 'engine/src/input.rs')
-rw-r--r-- | engine/src/input.rs | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/engine/src/input.rs b/engine/src/input.rs index e847702..95de048 100644 --- a/engine/src/input.rs +++ b/engine/src/input.rs @@ -1,16 +1,13 @@ use std::collections::HashMap; use ecs::extension::Collector as ExtensionCollector; +use ecs::phase::{Phase, PRE_UPDATE as PRE_UPDATE_PHASE, START as START_PHASE}; +use ecs::relationship::{ChildOf, Relationship}; use ecs::sole::Single; -use ecs::Sole; +use ecs::{static_entity, Sole}; -use crate::event::{ - PostPresent as PostPresentEvent, - PreUpdate as PreUpdateEvent, - Start as StartEvent, -}; use crate::vector::Vec2; -use crate::window::Window; +use crate::window::{Window, UPDATE_PHASE as WINDOW_UPDATE_PHASE}; mod reexports { @@ -19,10 +16,19 @@ mod reexports pub use reexports::*; +static_entity!( + SET_PREV_KEY_STATE_PHASE, + ( + Phase, + <Relationship<ChildOf, Phase>>::new(*WINDOW_UPDATE_PHASE) + ) +); + #[derive(Debug, Sole)] pub struct Keys { map: HashMap<Key, KeyData>, + pending: Vec<(Key, KeyState)>, } impl Keys @@ -43,6 +49,7 @@ impl Keys ) }) .collect(), + pending: Vec::with_capacity(Key::KEYS.len()), } } @@ -145,9 +152,9 @@ impl ecs::extension::Extension for Extension { fn collect(self, mut collector: ExtensionCollector<'_>) { - 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_system(*START_PHASE, initialize); + collector.add_system(*PRE_UPDATE_PHASE, maybe_clear_cursor_is_first_move); + collector.add_system(*SET_PREV_KEY_STATE_PHASE, set_pending_key_states); collector.add_sole(Keys::default()).ok(); collector.add_sole(Cursor::default()).ok(); @@ -169,7 +176,7 @@ fn initialize( let mut keys = keys_ref.to_single(); - keys.set_key_state(key, key_state); + keys.pending.push((key, key_state)); }); let cursor_weak_ref = cursor.to_weak_ref(); @@ -220,11 +227,21 @@ fn maybe_clear_cursor_is_first_move( } } -fn set_keys_prev_tick_state(mut keys: Single<Keys>) +fn set_pending_key_states(mut keys: Single<Keys>) { - for key_data in keys.map.values_mut() { + let Keys { map, pending } = &mut *keys; + + for key_data in map.values_mut() { key_data.prev_tick_state = key_data.state; } + + for (key, key_state) in pending { + let Some(key_data) = map.get_mut(key) else { + unreachable!(); + }; + + key_data.state = *key_state; + } } #[derive(Debug)] |