Skip to content

Commit

Permalink
Set gamepad in a separate function for InputReader
Browse files Browse the repository at this point in the history
It needs to be set once per context and much nicer for the future tests.
  • Loading branch information
Shatur committed Oct 13, 2024
1 parent 0648f6b commit 52c259e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
6 changes: 2 additions & 4 deletions src/input_context/context_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down Expand Up @@ -251,22 +251,20 @@ 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,
) {
trace!("updating action `{}`", self.action_name);

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 {
Expand Down
32 changes: 19 additions & 13 deletions src/input_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(super) struct InputReader<'w, 's> {
gamepad_axis: Res<'w, Axis<GamepadAxis>>,
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")]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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()
Expand Down

0 comments on commit 52c259e

Please sign in to comment.