Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A better solution to fix incorrect axis data in Chord #433

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 7 additions & 27 deletions src/input_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,20 +373,16 @@ impl<A: Actionlike> InputMap<A> {
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());

Expand All @@ -406,22 +402,6 @@ impl<A: Actionlike> InputMap<A> {
}
}

/// 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<A: Actionlike> InputMap<A> {
/// Returns an iterator over actions with their inputs
Expand Down
20 changes: 12 additions & 8 deletions src/input_streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,16 +421,20 @@ impl<'a> InputStreams<'a> {
/// be sure to clamp the returned data.
pub fn input_axis_pair(&self, input: &UserInput) -> Option<DualAxisData> {
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))
}
Expand Down
Loading