From d3d0c00719556ac67c04f69856a6bd96ddd9c3a2 Mon Sep 17 00:00:00 2001 From: Djzudieng Date: Sat, 30 Dec 2023 02:41:20 +0800 Subject: [PATCH] Refactor previous fix --- src/input_map.rs | 34 +++++++--------------------------- src/input_streams.rs | 20 ++++++++++++-------- 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/input_map.rs b/src/input_map.rs index 62c39b33..6623cf24 100644 --- a/src/input_map.rs +++ b/src/input_map.rs @@ -373,20 +373,16 @@ impl InputMap { for input in input_vec { let action = &mut action_data[action.index()]; - // Exclude chord combining both button-like and axis-like inputs unless all buttons are pressed. - if let UserInput::Chord(buttons) = input { - if input_streams.all_buttons_pressed(buttons) { - inputs.push(input.clone()); - - action.value += input_streams.input_value(input, true); - - merge_axis_pair_into_action(action, input_streams, input); + // Merge axis pair into action data + let axis_pair = input_streams.input_axis_pair(input); + if let Some(axis_pair) = axis_pair { + if let Some(current_axis_pair) = &mut action.axis_pair { + *current_axis_pair = current_axis_pair.merged_with(axis_pair); + } else { + action.axis_pair = Some(axis_pair); } - continue; } - merge_axis_pair_into_action(action, input_streams, input); - if input_streams.input_pressed(input) { inputs.push(input.clone()); @@ -406,22 +402,6 @@ impl InputMap { } } -/// Merges axis pair into action data. -#[inline] -fn merge_axis_pair_into_action( - action: &mut ActionData, - input_streams: &InputStreams, - input: &UserInput, -) { - if let Some(axis_pair) = input_streams.input_axis_pair(input) { - if let Some(current_axis_pair) = &mut action.axis_pair { - *current_axis_pair = current_axis_pair.merged_with(axis_pair); - } else { - action.axis_pair = Some(axis_pair); - } - } -} - // Utilities impl InputMap { /// Returns an iterator over actions with their inputs diff --git a/src/input_streams.rs b/src/input_streams.rs index c6167e92..dfbf85bc 100644 --- a/src/input_streams.rs +++ b/src/input_streams.rs @@ -421,16 +421,20 @@ impl<'a> InputStreams<'a> { /// be sure to clamp the returned data. pub fn input_axis_pair(&self, input: &UserInput) -> Option { match input { - UserInput::Chord(inputs) => inputs - .iter() - .flat_map(|input_kind| { + UserInput::Chord(inputs) => { + for input_kind in inputs.iter() { + // Exclude chord combining both button-like and axis-like inputs unless all buttons are pressed. + if !self.button_pressed(*input_kind) { + return None; + } + + // Return result of the first dual axis in the chord. if let InputKind::DualAxis(dual_axis) = input_kind { - Some(self.extract_dual_axis_data(dual_axis)) - } else { - None + return Some(self.extract_dual_axis_data(dual_axis)); } - }) - .next(), + } + None + } UserInput::Single(InputKind::DualAxis(dual_axis)) => { Some(self.extract_dual_axis_data(dual_axis)) }