Skip to content

Commit

Permalink
Restore the ButtonState::Pressed state when re-enabling `ToggleActi…
Browse files Browse the repository at this point in the history
…ons`
  • Loading branch information
Shute052 committed Jan 28, 2024
1 parent 7c0117c commit c8f8cf2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- registered types in the reflection system
- added `InputMap::clear`
- fixed [a bug](https://github.com/Leafwing-Studios/leafwing-input-manager/issues/430) related to incorrect axis data in `Chord` when not all buttons are pressed.
- fixed [a bug](https://github.com/Leafwing-Studios/leafwing-input-manager/issues/446) related to missing restoration for `ButtonState::Pressed` state when re-enabling `ToggleActions`.

### Code Quality

Expand Down
34 changes: 34 additions & 0 deletions src/action_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ impl<A: Actionlike> ActionState<A> {
ButtonState::Pressed => entry.state.press(),
ButtonState::JustReleased => entry.state.release(),
ButtonState::Released => entry.state.release(),
ButtonState::PressedWhenDisabling => {}
}

entry.axis_pair = action_datum.axis_pair;
Expand Down Expand Up @@ -364,6 +365,32 @@ impl<A: Actionlike> ActionState<A> {
action_data.state.release();
}

/// Used to release the `action` when disabling the [`ToggleActions<A>`](crate::plugin::ToggleActions) resource.
/// If the `action` is pressed, sets its [`ButtonState`] to [`ButtonState::PressedWhenDisabling`].
///
/// No initial instant will be recorded
/// Instead, this is set through [`ActionState::tick()`]
#[inline]
pub(crate) fn release_when_disabling(&mut self, action: &A) {
let action_data = match self.action_data_mut(action) {
Some(action_data) => action_data,
None => {
self.set_action_data(action.clone(), ActionData::default());
self.action_data_mut(action).unwrap()
}
};

// Once released, consumed actions can be pressed again
action_data.consumed = false;

if action_data.state.pressed() {
action_data.timing.flip();
action_data.state = ButtonState::PressedWhenDisabling;
} else {
action_data.state.release();
}
}

/// Consumes the `action`
///
/// The action will be released, and will not be able to be pressed again
Expand Down Expand Up @@ -434,6 +461,13 @@ impl<A: Actionlike> ActionState<A> {
}
}

/// Used to release all actions when disabling the [`ToggleActions<A>`](crate::plugin::ToggleActions) resource.
pub(crate) fn release_all_when_disabling(&mut self) {
for action in self.keys() {
self.release_when_disabling(&action);
}
}

/// Is this `action` currently consumed?
#[inline]
#[must_use]
Expand Down
7 changes: 6 additions & 1 deletion src/buttonlike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ pub enum ButtonState {
/// This button is currently released (and was released before the most recent tick)
#[default]
Released,
/// This button was pressed before disabling the [`ToggleActions<A>`](crate::plugin::ToggleActions) resource,
/// used for restoring the pressed state on re-enabling the resource.
PressedWhenDisabling,
}

impl ButtonState {
/// Causes [`just_pressed`](ButtonState::just_pressed) and [`just_released`](ButtonState::just_released) to become false
///
/// [`JustPressed`](ButtonState::JustPressed) becomes [`Pressed`](ButtonState::Pressed) and
/// [`JustPressed`](ButtonState::JustPressed) becomes [`Pressed`](ButtonState::Pressed),
/// [`PressedWhenDisabling`](ButtonState::PressedWhenDisabling) becomes [`Pressed`](ButtonState::Pressed), and
/// [`JustReleased`](ButtonState::JustReleased) becomes [`Released`](ButtonState::Released)
pub fn tick(&mut self) {
use ButtonState::*;
Expand All @@ -32,6 +36,7 @@ impl ButtonState {
Pressed => Pressed,
JustReleased => Released,
Released => Released,
PressedWhenDisabling => Pressed,
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ pub fn release_on_disable<A: Actionlike>(
) {
if toggle_actions.is_changed() && !toggle_actions.enabled {
for mut action_state in query.iter_mut() {
action_state.release_all();
action_state.release_all_when_disabling();
}
if let Some(mut action_state) = resource {
action_state.release_all();
action_state.release_all_when_disabling();
}
}
}
Expand Down

0 comments on commit c8f8cf2

Please sign in to comment.