diff --git a/src/input_context/context_instance.rs b/src/input_context/context_instance.rs index df50855..e160957 100644 --- a/src/input_context/context_instance.rs +++ b/src/input_context/context_instance.rs @@ -83,13 +83,13 @@ impl ContextInstance { entities: &[Entity], delta: f32, ) { + reader.set_gamepad(self.gamepad); for action_map in &mut self.actions { action_map.update( world, commands, reader, &mut self.actions_data, - self.gamepad, entities, delta, ); @@ -251,14 +251,12 @@ impl ActionMap { self } - #[allow(clippy::too_many_arguments)] fn update( &mut self, world: &World, commands: &mut Commands, reader: &mut InputReader, actions_data: &mut ActionsData, - gamepad: GamepadDevice, entities: &[Entity], delta: f32, ) { @@ -266,7 +264,7 @@ impl ActionMap { let mut tracker = TriggerTracker::new(ActionValue::zero(self.dim)); for input_map in &mut self.inputs { - let mut value = reader.value(input_map.input, gamepad, self.consumes_input); + let mut value = reader.value(input_map.input, self.consumes_input); value = value.convert(self.dim); if input_map.ignored { diff --git a/src/input_reader.rs b/src/input_reader.rs index 5499aa2..dc8e55d 100644 --- a/src/input_reader.rs +++ b/src/input_reader.rs @@ -22,6 +22,7 @@ pub(super) struct InputReader<'w, 's> { gamepad_axis: Res<'w, Axis>, gamepads: Res<'w, Gamepads>, consumed: Local<'s, ConsumedInput>, + gamepad: Local<'s, GamepadDevice>, #[cfg(feature = "ui_priority")] interactions: Query<'w, 's, &'static Interaction>, #[cfg(feature = "egui_priority")] @@ -57,16 +58,15 @@ impl InputReader<'_, '_> { } } + /// Assignes a gamepad from which [`Self::value`] should read input. + pub(super) fn set_gamepad(&mut self, gamepad: GamepadDevice) { + *self.gamepad = gamepad; + } + /// Returns the [`ActionValue`] for the given [`Input`] if exists. /// - /// For gamepad input, it exclusively reads from the specified `gamepad`. /// If `consume` is `true`, the value will be consumed and unavailable for subsequent calls. - pub(super) fn value( - &mut self, - input: Input, - gamepad: GamepadDevice, - consume: bool, - ) -> ActionValue { + pub(super) fn value(&mut self, input: Input, consume: bool) -> ActionValue { match input { Input::Keyboard { key_code, @@ -126,11 +126,15 @@ impl InputReader<'_, '_> { value.into() } Input::GamepadButton { button } => { - if self.consumed.gamepad_buttons.contains(&(gamepad, button)) { + if self + .consumed + .gamepad_buttons + .contains(&(*self.gamepad, button)) + { return false.into(); } - let pressed = match gamepad { + let pressed = match *self.gamepad { GamepadDevice::Any => self.gamepads.iter().any(|gamepad| { self.gamepad_buttons.pressed(GamepadButton { gamepad, @@ -144,17 +148,19 @@ impl InputReader<'_, '_> { }; if pressed && consume { - self.consumed.gamepad_buttons.insert((gamepad, button)); + self.consumed + .gamepad_buttons + .insert((*self.gamepad, button)); } pressed.into() } Input::GamepadAxis { axis } => { - if self.consumed.gamepad_axes.contains(&(gamepad, axis)) { + if self.consumed.gamepad_axes.contains(&(*self.gamepad, axis)) { return 0.0.into(); } - let value = match gamepad { + let value = match *self.gamepad { GamepadDevice::Any => self.gamepads.iter().find_map(|gamepad| { self.gamepad_axis.get_unclamped(GamepadAxis { gamepad, @@ -170,7 +176,7 @@ impl InputReader<'_, '_> { let value = value.unwrap_or_default(); if value != 0.0 && consume { - self.consumed.gamepad_axes.insert((gamepad, axis)); + self.consumed.gamepad_axes.insert((*self.gamepad, axis)); } value.into()