Skip to content

Commit

Permalink
Use shorter names
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Oct 17, 2024
1 parent ed131e9 commit 5a05a96
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 45 deletions.
16 changes: 5 additions & 11 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = [KeyCode; 2]> {
pub fn iter_keys(self) -> impl Iterator<Item = [KeyCode; 2]> {
self.iter_names().map(|(_, modifier)| match modifier {
Modifiers::ALT => [KeyCode::AltLeft, KeyCode::AltRight],
Modifiers::CONTROL => [KeyCode::ControlLeft, KeyCode::ControlRight],
Expand Down Expand Up @@ -57,10 +57,7 @@ impl From<KeyCode> 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 {
Expand Down Expand Up @@ -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 },
Expand All @@ -131,9 +125,9 @@ impl Input {
}

impl From<KeyCode> for Input {
fn from(key_code: KeyCode) -> Self {
fn from(key: KeyCode) -> Self {
Self::Keyboard {
key_code,
key,
modifiers: Default::default(),
}
}
Expand Down
63 changes: 30 additions & 33 deletions src/input/input_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyCode>>,
keys: Res<'w, ButtonInput<KeyCode>>,
mouse_buttons: Res<'w, ButtonInput<MouseButton>>,
mouse_motion_events: EventReader<'w, 's, MouseMotion>,
mouse_wheel_events: EventReader<'w, 's, MouseWheel>,
Expand Down Expand Up @@ -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<Input>) -> 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);
}

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -234,7 +231,7 @@ impl InputReader<'_, '_> {
struct ConsumedInput {
ui_wants_keyboard: bool,
ui_wants_mouse: bool,
key_codes: HashSet<KeyCode>,
keys: HashSet<KeyCode>,
modifiers: Modifiers,
mouse_buttons: HashSet<MouseButton>,
gamepad_buttons: HashSet<GamepadInput<GamepadButtonType>>,
Expand All @@ -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();
Expand Down Expand Up @@ -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::<ButtonInput<KeyCode>>().press(key_code);
let key = KeyCode::Space;
world.resource_mut::<ButtonInput<KeyCode>>().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]
Expand Down Expand Up @@ -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::<ButtonInput<KeyCode>>();
key_codes.press(modifier);
key_codes.press(key_code);
let mut keys = world.resource_mut::<ButtonInput<KeyCode>>();
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)
Expand All @@ -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::<ButtonInput<KeyCode>>()
.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]
Expand Down Expand Up @@ -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::<ButtonInput<KeyCode>>().press(key_code);
world.resource_mut::<ButtonInput<KeyCode>>().press(key);
world
.resource_mut::<ButtonInput<MouseButton>>()
.press(button);
Expand All @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion src/input_context/context_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl ActionBind {
/// # use bevy_enhanced_input::prelude::*;
/// # let mut ctx = ContextInstance::default();
/// ctx.bind::<Jump>().with(Input::Keyboard {
/// key_code: KeyCode::Space,
/// key: KeyCode::Space,
/// modifiers: Modifiers::CONTROL,
/// });
/// # #[derive(Debug, InputAction)]
Expand Down

0 comments on commit 5a05a96

Please sign in to comment.