Skip to content

Commit

Permalink
Don't merge non-active trackers
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Oct 29, 2024
1 parent fb68cfb commit c16ba1a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/input_context/context_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ use crate::{
/// 1.1. Apply input-level [`InputModifier`]s.
/// 1.2. Evaluate input-level [`InputCondition`]s, combining their results based on their [`InputCondition::kind`].
/// 2. Select all [`ActionValue`]s with the most significant [`ActionState`] and combine based on [`InputAction::ACCUMULATION`].
/// The value will also be converted using [`ActionValue::convert`] into [`InputAction::DIM`].
/// Combined value be converted into [`InputAction::DIM`] dimention using [`ActionValue::convert`].
/// 3. Apply action level [`InputModifier`]s.
/// 4. Evaluate action level [`InputCondition`]s, combining their results according to [`InputCondition::kind`].
/// 5. Set the final [`ActionState`] based on the results.
/// The value will also be converted using [`ActionValue::convert`] into [`InputAction::DIM`].
/// Final value be converted into [`InputAction::DIM`] dimention using [`ActionValue::convert`].
///
/// New instances won't react to currently held inputs until they are released.
/// This prevents unintended behavior where switching contexts using the same key
Expand Down
3 changes: 2 additions & 1 deletion src/input_context/input_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ pub trait InputAction: Debug + Send + Sync + 'static {
const ACCUMULATION: Accumulation = Accumulation::Cumulative;
}

/// Defines how [`ActionValue`] is calculated when multiple inputs are evaluated with the same [`ActionState`].
/// Defines how [`ActionValue`] is calculated when multiple inputs are evaluated with the
/// same most significant [`ActionState`] (excluding [`ActionState::None`]).
#[derive(Default, Clone, Copy, Debug)]
pub enum Accumulation {
/// Cumulatively add the key values for each mapping.
Expand Down
10 changes: 9 additions & 1 deletion src/input_context/trigger_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,15 @@ impl TriggerTracker {

/// Merges input-level tracker into an action-level tracker.
pub(super) fn merge(&mut self, other: Self, accumulation: Accumulation) {
match self.state().cmp(&other.state()) {
let other_state = other.state();
if other_state == ActionState::None {
// Don't merge non-active trackers to allow the action to fire even if all
// input-level conditions return `ActionState::None`. This ensures that an
// action-level condition or modifier can still trigger the action.
return;
}

match self.state().cmp(&other_state) {
Ordering::Less => {
let dim = self.value.dim();
*self = other;
Expand Down
2 changes: 1 addition & 1 deletion tests/state_and_value_merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn input_level() {
let recorded = app.world().resource::<RecordedActions>();
let events = recorded.get::<InputLevel>(entity).unwrap();
let event = events.last().unwrap();
assert_eq!(event.value, Vec2::Y.into());
assert_eq!(event.value, Vec2::ZERO.into());
assert_eq!(
event.state,
ActionState::None,
Expand Down

0 comments on commit c16ba1a

Please sign in to comment.