Skip to content

Commit

Permalink
Keep updating disabled actions (#583)
Browse files Browse the repository at this point in the history
* Remove action consuming

* Keep updating the values of disabled actions
  • Loading branch information
alice-i-cecile authored Aug 8, 2024
1 parent e8d4da4 commit c343479
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ Input processors allow you to create custom logic for axis-like input manipulati
- Actions can now be disabled at both the individual action and `ActionState` level
- Disabling actions now resets their value, exposed via the `ActionState::reset` method
- `ActionState::release_all` has been renamed to `ActionState::reset_all` and now resets the values of `Axislike` and `DualAxislike` actions
- the state of actions now continues to be updated while they are disabled. However, when checked, their value is always released / zero.
- this ensures that holding down an action, disabling it and then re-enabling it does not trigger just-pressed
- the values of disabled actions can be accessed by checking their `ActionData` directly

### Usability (0.15)

Expand Down
23 changes: 18 additions & 5 deletions src/action_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub use action_data::*;
///
/// Can be used as either a resource or as a [`Component`] on entities that you wish to control directly from player input.
///
/// # Disabling actions
///
/// Actions can be disabled in four different ways, with increasing granularity:
///
/// 1. By disabling updates to all actions using a run condition on [`InputManagerSystem::Update`](crate::plugin::InputManagerSystem::Update).
Expand All @@ -30,7 +32,13 @@ pub use action_data::*;
/// More general mechanisms of disabling actions will cause specific mechanisms to be ignored.
/// For example, if an entire action state is disabled, then enabling or disabling individual actions will have no effect.
///
/// Actions that are disabled will report as released (but not just released), and their values will be zero.
/// Under the hood, their values are still updated to avoid surprising behavior when re-enabled,
/// but they are not reported to the user using standard methods like [`ActionState::pressed`].
/// To check the underlying values, access their [`ActionData`] directly.
///
/// # Example
///
/// ```rust
/// use bevy::reflect::Reflect;
/// use leafwing_input_manager::prelude::*;
Expand Down Expand Up @@ -121,12 +129,9 @@ impl<A: Actionlike> ActionState<A> {
/// The `action_data` is typically constructed from [`InputMap::process_actions`](crate::input_map::InputMap::process_actions),
/// which reads from the assorted [`ButtonInput`](bevy::input::ButtonInput) resources.
///
/// If this [`ActionState`] is disabled, it will not be updated.
/// Actions that are disabled will still be updated: instead, their values will be read as released / zero.
/// You can see their underlying values by checking their [`ActionData`] directly.
pub fn update(&mut self, updated_actions: UpdatedActions<A>) {
if self.disabled {
return;
}

for (action, updated_value) in updated_actions.iter() {
match updated_value {
UpdatedValue::Button(pressed) => {
Expand Down Expand Up @@ -471,6 +476,10 @@ impl<A: Actionlike> ActionState<A> {
pub fn value(&self, action: &A) -> f32 {
debug_assert_eq!(action.input_control_kind(), InputControlKind::Axis);

if self.action_disabled(action) {
return 0.0;
}

match self.axis_data(action) {
Some(axis_data) => axis_data.value,
None => 0.0,
Expand Down Expand Up @@ -514,6 +523,10 @@ impl<A: Actionlike> ActionState<A> {
pub fn axis_pair(&self, action: &A) -> Vec2 {
debug_assert_eq!(action.input_control_kind(), InputControlKind::DualAxis);

if self.action_disabled(action) {
return Vec2::ZERO;
}

let action_data = self.dual_axis_data(action);
action_data.map_or(Vec2::ZERO, |action_data| action_data.pair)
}
Expand Down

0 comments on commit c343479

Please sign in to comment.