Skip to content

Commit

Permalink
Rename Input to ButtonInput (#10859)
Browse files Browse the repository at this point in the history
# Objective

- Resolves #10853 

## Solution

- ~~Changed the name of `Input` struct to `PressableInput`.~~
- Changed the name of `Input` struct to `ButtonInput`.

## Migration Guide

- Breaking Change: Users need to rename `Input` to `ButtonInput` in
their projects.
  • Loading branch information
matiqo15 authored Dec 6, 2023
1 parent d9aac88 commit 1f97717
Show file tree
Hide file tree
Showing 66 changed files with 200 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ use bevy_ecs::schedule::State;
/// This type can be used as a resource to keep the current state of an input, by reacting to
/// events from the input. For a given input value:
///
/// * [`Input::pressed`] will return `true` between a press and a release event.
/// * [`Input::just_pressed`] will return `true` for one frame after a press event.
/// * [`Input::just_released`] will return `true` for one frame after a release event.
/// * [`ButtonInput::pressed`] will return `true` between a press and a release event.
/// * [`ButtonInput::just_pressed`] will return `true` for one frame after a press event.
/// * [`ButtonInput::just_released`] will return `true` for one frame after a release event.
///
/// ## Multiple systems
///
/// In case multiple systems are checking for [`Input::just_pressed`] or [`Input::just_released`]
/// In case multiple systems are checking for [`ButtonInput::just_pressed`] or [`ButtonInput::just_released`]
/// but only one should react, for example in the case of triggering
/// [`State`] change, you should consider clearing the input state, either by:
///
/// * Using [`Input::clear_just_pressed`] or [`Input::clear_just_released`] instead.
/// * Calling [`Input::clear`] or [`Input::reset`] immediately after the state change.
/// * Using [`ButtonInput::clear_just_pressed`] or [`ButtonInput::clear_just_released`] instead.
/// * Calling [`ButtonInput::clear`] or [`ButtonInput::reset`] immediately after the state change.
///
/// ## Note
///
/// When adding this resource for a new input type, you should:
///
/// * Call the [`Input::press`] method for each press event.
/// * Call the [`Input::release`] method for each release event.
/// * Call the [`Input::clear`] method at each frame start, before processing events.
/// * Call the [`ButtonInput::press`] method for each press event.
/// * Call the [`ButtonInput::release`] method for each release event.
/// * Call the [`ButtonInput::clear`] method at each frame start, before processing events.
///
/// Note: Calling `clear` from a [`ResMut`] will trigger change detection.
/// It may be preferable to use [`DetectChangesMut::bypass_change_detection`]
Expand All @@ -45,7 +45,7 @@ use bevy_ecs::schedule::State;
///[`DetectChangesMut::bypass_change_detection`]: bevy_ecs::change_detection::DetectChangesMut::bypass_change_detection
#[derive(Debug, Clone, Resource, Reflect)]
#[reflect(Default)]
pub struct Input<T: Copy + Eq + Hash + Send + Sync + 'static> {
pub struct ButtonInput<T: Copy + Eq + Hash + Send + Sync + 'static> {
/// A collection of every button that is currently being pressed.
pressed: HashSet<T>,
/// A collection of every button that has just been pressed.
Expand All @@ -54,7 +54,7 @@ pub struct Input<T: Copy + Eq + Hash + Send + Sync + 'static> {
just_released: HashSet<T>,
}

impl<T: Copy + Eq + Hash + Send + Sync + 'static> Default for Input<T> {
impl<T: Copy + Eq + Hash + Send + Sync + 'static> Default for ButtonInput<T> {
fn default() -> Self {
Self {
pressed: Default::default(),
Expand All @@ -64,7 +64,7 @@ impl<T: Copy + Eq + Hash + Send + Sync + 'static> Default for Input<T> {
}
}

impl<T> Input<T>
impl<T> ButtonInput<T>
where
T: Copy + Eq + Hash + Send + Sync + 'static,
{
Expand Down Expand Up @@ -112,7 +112,7 @@ where

/// Clears the `just_pressed` state of the `input` and returns `true` if the `input` has just been pressed.
///
/// Future calls to [`Input::just_pressed`] for the given input will return false until a new press event occurs.
/// Future calls to [`ButtonInput::just_pressed`] for the given input will return false until a new press event occurs.
pub fn clear_just_pressed(&mut self, input: T) -> bool {
self.just_pressed.remove(&input)
}
Expand All @@ -129,7 +129,7 @@ where

/// Clears the `just_released` state of the `input` and returns `true` if the `input` has just been released.
///
/// Future calls to [`Input::just_released`] for the given input will return false until a new release event occurs.
/// Future calls to [`ButtonInput::just_released`] for the given input will return false until a new release event occurs.
pub fn clear_just_released(&mut self, input: T) -> bool {
self.just_released.remove(&input)
}
Expand All @@ -143,7 +143,7 @@ where

/// Clears the `pressed`, `just_pressed`, and `just_released` data for every input.
///
/// See also [`Input::clear`] for simulating elapsed time steps.
/// See also [`ButtonInput::clear`] for simulating elapsed time steps.
pub fn reset_all(&mut self) {
self.pressed.clear();
self.just_pressed.clear();
Expand All @@ -152,7 +152,7 @@ where

/// Clears the `just pressed` and `just released` data for every input.
///
/// See also [`Input::reset_all`] for a full reset.
/// See also [`ButtonInput::reset_all`] for a full reset.
pub fn clear(&mut self) {
self.just_pressed.clear();
self.just_released.clear();
Expand All @@ -178,9 +178,9 @@ where
mod test {
use bevy_reflect::TypePath;

use crate::Input;
use crate::ButtonInput;

/// Used for testing the functionality of [`Input`].
/// Used for testing the functionality of [`ButtonInput`].
#[derive(TypePath, Copy, Clone, Eq, PartialEq, Hash)]
enum DummyInput {
Input1,
Expand All @@ -189,7 +189,7 @@ mod test {

#[test]
fn test_press() {
let mut input = Input::default();
let mut input = ButtonInput::default();
assert!(!input.pressed.contains(&DummyInput::Input1));
assert!(!input.just_pressed.contains(&DummyInput::Input1));
input.press(DummyInput::Input1);
Expand All @@ -199,15 +199,15 @@ mod test {

#[test]
fn test_pressed() {
let mut input = Input::default();
let mut input = ButtonInput::default();
assert!(!input.pressed(DummyInput::Input1));
input.press(DummyInput::Input1);
assert!(input.pressed(DummyInput::Input1));
}

#[test]
fn test_any_pressed() {
let mut input = Input::default();
let mut input = ButtonInput::default();
assert!(!input.any_pressed([DummyInput::Input1]));
assert!(!input.any_pressed([DummyInput::Input2]));
assert!(!input.any_pressed([DummyInput::Input1, DummyInput::Input2]));
Expand All @@ -219,7 +219,7 @@ mod test {

#[test]
fn test_release() {
let mut input = Input::default();
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
assert!(input.pressed.contains(&DummyInput::Input1));
assert!(!input.just_released.contains(&DummyInput::Input1));
Expand All @@ -230,7 +230,7 @@ mod test {

#[test]
fn test_release_all() {
let mut input = Input::default();
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
input.press(DummyInput::Input2);
input.release_all();
Expand All @@ -241,15 +241,15 @@ mod test {

#[test]
fn test_just_pressed() {
let mut input = Input::default();
let mut input = ButtonInput::default();
assert!(!input.just_pressed(DummyInput::Input1));
input.press(DummyInput::Input1);
assert!(input.just_pressed(DummyInput::Input1));
}

#[test]
fn test_any_just_pressed() {
let mut input = Input::default();
let mut input = ButtonInput::default();
assert!(!input.any_just_pressed([DummyInput::Input1]));
assert!(!input.any_just_pressed([DummyInput::Input2]));
assert!(!input.any_just_pressed([DummyInput::Input1, DummyInput::Input2]));
Expand All @@ -261,7 +261,7 @@ mod test {

#[test]
fn test_clear_just_pressed() {
let mut input = Input::default();
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
assert!(input.just_pressed(DummyInput::Input1));
input.clear_just_pressed(DummyInput::Input1);
Expand All @@ -270,7 +270,7 @@ mod test {

#[test]
fn test_just_released() {
let mut input = Input::default();
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
assert!(!input.just_released(DummyInput::Input1));
input.release(DummyInput::Input1);
Expand All @@ -279,7 +279,7 @@ mod test {

#[test]
fn test_any_just_released() {
let mut input = Input::default();
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
assert!(!input.any_just_released([DummyInput::Input1]));
assert!(!input.any_just_released([DummyInput::Input2]));
Expand All @@ -292,7 +292,7 @@ mod test {

#[test]
fn test_clear_just_released() {
let mut input = Input::default();
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
input.release(DummyInput::Input1);
assert!(input.just_released(DummyInput::Input1));
Expand All @@ -302,7 +302,7 @@ mod test {

#[test]
fn test_reset() {
let mut input = Input::default();
let mut input = ButtonInput::default();

// Pressed
input.press(DummyInput::Input1);
Expand All @@ -328,7 +328,7 @@ mod test {

#[test]
fn test_reset_all() {
let mut input = Input::default();
let mut input = ButtonInput::default();

input.press(DummyInput::Input1);
input.press(DummyInput::Input2);
Expand All @@ -344,7 +344,7 @@ mod test {

#[test]
fn test_clear() {
let mut input = Input::default();
let mut input = ButtonInput::default();

// Pressed
input.press(DummyInput::Input1);
Expand All @@ -370,7 +370,7 @@ mod test {

#[test]
fn test_get_pressed() {
let mut input = Input::default();
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
input.press(DummyInput::Input2);
let pressed = input.get_pressed();
Expand All @@ -382,7 +382,7 @@ mod test {

#[test]
fn test_get_just_pressed() {
let mut input = Input::default();
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
input.press(DummyInput::Input2);
let just_pressed = input.get_just_pressed();
Expand All @@ -394,7 +394,7 @@ mod test {

#[test]
fn test_get_just_released() {
let mut input = Input::default();
let mut input = ButtonInput::default();
input.press(DummyInput::Input1);
input.press(DummyInput::Input2);
input.release(DummyInput::Input1);
Expand All @@ -408,7 +408,7 @@ mod test {

#[test]
fn test_general_input_handling() {
let mut input = Input::default();
let mut input = ButtonInput::default();

// Test pressing
input.press(DummyInput::Input1);
Expand Down Expand Up @@ -453,7 +453,7 @@ mod test {
assert!(!input.just_released(DummyInput::Input2));

// Set up an `Input` to test resetting
let mut input = Input::default();
let mut input = ButtonInput::default();

input.press(DummyInput::Input1);
input.release(DummyInput::Input2);
Expand Down
29 changes: 16 additions & 13 deletions crates/bevy_input/src/common_conditions.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::Input;
use crate::ButtonInput;
use bevy_ecs::system::Res;
use std::hash::Hash;

/// Stateful run condition that can be toggled via a input press using [`Input::just_pressed`].
/// Stateful run condition that can be toggled via a input press using [`ButtonInput::just_pressed`].
///
/// ```rust,no_run
/// use bevy::prelude::*;
Expand Down Expand Up @@ -47,26 +47,29 @@ use std::hash::Hash;
/// }
///
/// ```
pub fn input_toggle_active<T>(default: bool, input: T) -> impl FnMut(Res<Input<T>>) -> bool + Clone
pub fn input_toggle_active<T>(
default: bool,
input: T,
) -> impl FnMut(Res<ButtonInput<T>>) -> bool + Clone
where
T: Copy + Eq + Hash + Send + Sync + 'static,
{
let mut active = default;
move |inputs: Res<Input<T>>| {
move |inputs: Res<ButtonInput<T>>| {
active ^= inputs.just_pressed(input);
active
}
}

/// Run condition that is active if [`Input::pressed`] is true for the given input.
pub fn input_pressed<T>(input: T) -> impl FnMut(Res<Input<T>>) -> bool + Clone
/// Run condition that is active if [`ButtonInput::pressed`] is true for the given input.
pub fn input_pressed<T>(input: T) -> impl FnMut(Res<ButtonInput<T>>) -> bool + Clone
where
T: Copy + Eq + Hash + Send + Sync + 'static,
{
move |inputs: Res<Input<T>>| inputs.pressed(input)
move |inputs: Res<ButtonInput<T>>| inputs.pressed(input)
}

/// Run condition that is active if [`Input::just_pressed`] is true for the given input.
/// Run condition that is active if [`ButtonInput::just_pressed`] is true for the given input.
///
/// ```rust,no_run
/// use bevy::prelude::*;
Expand All @@ -80,19 +83,19 @@ where
///
/// # fn jump() {}
/// ```
pub fn input_just_pressed<T>(input: T) -> impl FnMut(Res<Input<T>>) -> bool + Clone
pub fn input_just_pressed<T>(input: T) -> impl FnMut(Res<ButtonInput<T>>) -> bool + Clone
where
T: Copy + Eq + Hash + Send + Sync + 'static,
{
move |inputs: Res<Input<T>>| inputs.just_pressed(input)
move |inputs: Res<ButtonInput<T>>| inputs.just_pressed(input)
}

/// Run condition that is active if [`Input::just_released`] is true for the given input.
pub fn input_just_released<T>(input: T) -> impl FnMut(Res<Input<T>>) -> bool + Clone
/// Run condition that is active if [`ButtonInput::just_released`] is true for the given input.
pub fn input_just_released<T>(input: T) -> impl FnMut(Res<ButtonInput<T>>) -> bool + Clone
where
T: Copy + Eq + Hash + Send + Sync + 'static,
{
move |inputs: Res<Input<T>>| inputs.just_released(input)
move |inputs: Res<ButtonInput<T>>| inputs.just_released(input)
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 1f97717

Please sign in to comment.