From 5a05a96dca7901a6bb1552a762031aff81399232 Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Thu, 17 Oct 2024 21:25:18 +0300 Subject: [PATCH] Use shorter names --- src/input.rs | 16 +++---- src/input/input_reader.rs | 63 +++++++++++++-------------- src/input_context/context_instance.rs | 2 +- 3 files changed, 36 insertions(+), 45 deletions(-) diff --git a/src/input.rs b/src/input.rs index 7fadd64..7b11e13 100644 --- a/src/input.rs +++ b/src/input.rs @@ -25,7 +25,7 @@ impl Modifiers { /// Returns an iterator over the key codes corresponding to the set modifier bits. /// /// Each item contains left and right key codes. - pub fn iter_key_codes(self) -> impl Iterator { + pub fn iter_keys(self) -> impl Iterator { self.iter_names().map(|(_, modifier)| match modifier { Modifiers::ALT => [KeyCode::AltLeft, KeyCode::AltRight], Modifiers::CONTROL => [KeyCode::ControlLeft, KeyCode::ControlRight], @@ -57,10 +57,7 @@ impl From for Modifiers { pub enum Input { /// Keyboard button, will be captured as /// [`ActionValue::Bool`](crate::action_value::ActionValue). - Keyboard { - key_code: KeyCode, - modifiers: Modifiers, - }, + Keyboard { key: KeyCode, modifiers: Modifiers }, /// Mouse button, will be captured as /// [`ActionValue::Bool`](crate::action_value::ActionValue). MouseButton { @@ -116,10 +113,7 @@ impl Input { #[must_use] pub const fn with_modifiers(self, modifiers: Modifiers) -> Self { match self { - Input::Keyboard { key_code, .. } => Self::Keyboard { - key_code, - modifiers, - }, + Input::Keyboard { key, .. } => Self::Keyboard { key, modifiers }, Input::MouseButton { button, .. } => Self::MouseButton { button, modifiers }, Input::MouseMotion { .. } => Self::MouseMotion { modifiers }, Input::MouseWheel { .. } => Self::MouseWheel { modifiers }, @@ -131,9 +125,9 @@ impl Input { } impl From for Input { - fn from(key_code: KeyCode) -> Self { + fn from(key: KeyCode) -> Self { Self::Keyboard { - key_code, + key, modifiers: Default::default(), } } diff --git a/src/input/input_reader.rs b/src/input/input_reader.rs index cdbb4ac..f42ad06 100644 --- a/src/input/input_reader.rs +++ b/src/input/input_reader.rs @@ -15,7 +15,7 @@ use crate::action_value::ActionValue; /// Reads input from multiple sources. #[derive(SystemParam)] pub(crate) struct InputReader<'w, 's> { - key_codes: Res<'w, ButtonInput>, + keys: Res<'w, ButtonInput>, mouse_buttons: Res<'w, ButtonInput>, mouse_motion_events: EventReader<'w, 's, MouseMotion>, mouse_wheel_events: EventReader<'w, 's, MouseWheel>, @@ -92,17 +92,14 @@ impl InputReader<'_, '_> { /// See also [`Self::set_consume_input`] and [`Self::set_gamepad`]. pub(crate) fn value(&mut self, input: impl Into) -> ActionValue { match input.into() { - Input::Keyboard { - key_code, - modifiers, - } => { + Input::Keyboard { key, modifiers } => { let pressed = !self.consumed.ui_wants_keyboard - && self.key_codes.pressed(key_code) - && !self.consumed.key_codes.contains(&key_code) + && self.keys.pressed(key) + && !self.consumed.keys.contains(&key) && self.modifiers_pressed(modifiers); if pressed && self.params.consume_input { - self.consumed.key_codes.insert(key_code); + self.consumed.keys.insert(key); self.consumed.modifiers.insert(modifiers); } @@ -217,8 +214,8 @@ impl InputReader<'_, '_> { return false; } - for key_codes in modifiers.iter_key_codes() { - if !self.key_codes.any_pressed(key_codes) { + for modifier_keys in modifiers.iter_keys() { + if !self.keys.any_pressed(modifier_keys) { return false; } } @@ -234,7 +231,7 @@ impl InputReader<'_, '_> { struct ConsumedInput { ui_wants_keyboard: bool, ui_wants_mouse: bool, - key_codes: HashSet, + keys: HashSet, modifiers: Modifiers, mouse_buttons: HashSet, gamepad_buttons: HashSet>, @@ -245,7 +242,7 @@ impl ConsumedInput { fn reset(&mut self) { self.ui_wants_keyboard = false; self.ui_wants_mouse = false; - self.key_codes.clear(); + self.keys.clear(); self.modifiers = Modifiers::empty(); self.mouse_buttons.clear(); self.gamepad_buttons.clear(); @@ -279,26 +276,26 @@ mod tests { use crate::Input; #[test] - fn key_code() { + fn keyboard() { let (mut world, mut state) = init_world(); - let key_code = KeyCode::Space; - world.resource_mut::>().press(key_code); + let key = KeyCode::Space; + world.resource_mut::>().press(key); let mut reader = state.get(&world); - assert_eq!(reader.value(key_code), ActionValue::Bool(true)); + assert_eq!(reader.value(key), ActionValue::Bool(true)); assert_eq!(reader.value(KeyCode::Escape), ActionValue::Bool(false)); assert_eq!( reader.value(Input::Keyboard { - key_code, + key, modifiers: Modifiers::ALT }), ActionValue::Bool(false) ); reader.set_consume_input(true); - assert_eq!(reader.value(key_code), ActionValue::Bool(true)); - assert_eq!(reader.value(key_code), ActionValue::Bool(false)); + assert_eq!(reader.value(key), ActionValue::Bool(true)); + assert_eq!(reader.value(key), ActionValue::Bool(false)); } #[test] @@ -444,22 +441,22 @@ mod tests { } #[test] - fn key_code_with_modifier() { + fn keyboard_with_modifier() { let (mut world, mut state) = init_world(); - let key_code = KeyCode::Space; + let key = KeyCode::Space; let modifier = KeyCode::ControlLeft; - let mut key_codes = world.resource_mut::>(); - key_codes.press(modifier); - key_codes.press(key_code); + let mut keys = world.resource_mut::>(); + keys.press(modifier); + keys.press(key); let input = Input::Keyboard { - key_code, + key, modifiers: modifier.into(), }; let mut reader = state.get(&world); assert_eq!(reader.value(input), ActionValue::Bool(true)); - assert_eq!(reader.value(key_code), ActionValue::Bool(true)); + assert_eq!(reader.value(key), ActionValue::Bool(true)); assert_eq!( reader.value(input.with_modifiers(Modifiers::ALT)), ActionValue::Bool(false) @@ -474,17 +471,17 @@ mod tests { assert_eq!(reader.value(input), ActionValue::Bool(false)); // Try another key, but with the same modifier that was consumed. - let other_key_code = KeyCode::Enter; + let other_key = KeyCode::Enter; world .resource_mut::>() - .press(other_key_code); + .press(other_key); let other_input = Input::Keyboard { - key_code: other_key_code, + key: other_key, modifiers: modifier.into(), }; let mut reader = state.get(&world); assert_eq!(reader.value(other_input), ActionValue::Bool(false)); - assert_eq!(reader.value(other_key_code), ActionValue::Bool(true)); + assert_eq!(reader.value(other_key), ActionValue::Bool(true)); } #[test] @@ -594,9 +591,9 @@ mod tests { fn ui_input() { let (mut world, mut state) = init_world(); - let key_code = KeyCode::Space; + let key = KeyCode::Space; let button = MouseButton::Left; - world.resource_mut::>().press(key_code); + world.resource_mut::>().press(key); world .resource_mut::>() .press(button); @@ -613,7 +610,7 @@ mod tests { reader.consumed.ui_wants_keyboard = true; reader.consumed.ui_wants_mouse = true; - assert_eq!(reader.value(key_code), ActionValue::Bool(false)); + assert_eq!(reader.value(key), ActionValue::Bool(false)); assert_eq!(reader.value(button), ActionValue::Bool(false)); assert_eq!( reader.value(Input::mouse_motion()), diff --git a/src/input_context/context_instance.rs b/src/input_context/context_instance.rs index 87f9736..5d7f36a 100644 --- a/src/input_context/context_instance.rs +++ b/src/input_context/context_instance.rs @@ -220,7 +220,7 @@ impl ActionBind { /// # use bevy_enhanced_input::prelude::*; /// # let mut ctx = ContextInstance::default(); /// ctx.bind::().with(Input::Keyboard { - /// key_code: KeyCode::Space, + /// key: KeyCode::Space, /// modifiers: Modifiers::CONTROL, /// }); /// # #[derive(Debug, InputAction)]