diff options
Diffstat (limited to 'engine')
| -rw-r--r-- | engine/src/windowing.rs | 22 | ||||
| -rw-r--r-- | engine/src/windowing/keyboard.rs | 12 |
2 files changed, 34 insertions, 0 deletions
diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs index 3163869..53e7bc3 100644 --- a/engine/src/windowing.rs +++ b/engine/src/windowing.rs @@ -58,6 +58,8 @@ const MESSAGE_FROM_APP_QUEUE_SIZE: usize = 512; const MESSAGE_TO_APP_QUEUE_SIZE: usize = 16; // Increase if more messages are added +const TEXT_KEY_QUEUE_SIZE: usize = 255; + static CONTEXT_CREATED: AtomicBool = AtomicBool::new(false); declare_entity!( @@ -181,6 +183,8 @@ fn update_stuff( .load(Ordering::Relaxed) .into(); + keyboard.set_text_keys(iter_array_queue(&context.shared_state.text_keys)); + let Context { ref message_from_app_queue, ref mut display_handle, @@ -542,6 +546,7 @@ struct SharedState { relative_mouse_pos_delta: AtomicTwoF64, absolute_mouse_pos: AtomicTwoF64, + text_keys: ArrayQueue<char>, is_dropped: AtomicBool, } @@ -552,6 +557,7 @@ impl Default for SharedState Self { relative_mouse_pos_delta: AtomicTwoF64::new((0.0, 0.0)), absolute_mouse_pos: AtomicTwoF64::new((0.0, 0.0)), + text_keys: ArrayQueue::new(TEXT_KEY_QUEUE_SIZE), is_dropped: AtomicBool::new(false), } } @@ -692,6 +698,22 @@ impl ApplicationHandler for App event: keyboard_event, is_synthetic: _, } => { + if let Some(key_text) = keyboard_event + .text + .filter(|_| keyboard_event.state.is_pressed()) + { + for character in key_text.chars() { + if self.shared_state.text_keys.is_full() { + cold_path(); + tracing::warn!( + "Text key queue is full. Dropping oldest character" + ); + } + + self.shared_state.text_keys.force_push(character); + } + } + if keyboard_event.repeat { return; } diff --git a/engine/src/windowing/keyboard.rs b/engine/src/windowing/keyboard.rs index 5a31e7c..4515d8a 100644 --- a/engine/src/windowing/keyboard.rs +++ b/engine/src/windowing/keyboard.rs @@ -6,6 +6,7 @@ use crate::ecs::Sole; pub struct Keyboard { map: HashMap<Key, KeyData>, + text_keys: String, } impl Keyboard @@ -71,6 +72,11 @@ impl Keyboard key_data.previous_state } + pub fn text_keys(&self) -> &str + { + &self.text_keys + } + pub fn set_key_state(&mut self, key: Key, key_state: KeyState) { let key_data = self.map.entry(key).or_default(); @@ -84,6 +90,12 @@ impl Keyboard key_data.previous_state = key_data.curr_state; } } + + pub fn set_text_keys(&mut self, text_keys: impl IntoIterator<Item = char>) + { + self.text_keys.clear(); + self.text_keys.extend(text_keys); + } } #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
