Skip to content
Open
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
192 changes: 96 additions & 96 deletions crates/bevy_input/src/button_input.rs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions crates/bevy_input/src/common_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ where
{
let mut active = default;
move |inputs: Res<ButtonInput<T>>| {
active ^= inputs.just_pressed(input);
active ^= inputs.just_pressed(&input);
active
}
}
Expand All @@ -68,7 +68,7 @@ pub fn input_pressed<T>(input: T) -> impl FnMut(Res<ButtonInput<T>>) -> bool + C
where
T: Copy + Eq + Hash + Send + Sync + 'static,
{
move |inputs: Res<ButtonInput<T>>| inputs.pressed(input)
move |inputs: Res<ButtonInput<T>>| inputs.pressed(&input)
}

/// Run condition that is active if [`ButtonInput::just_pressed`] is true for the given input.
Expand All @@ -90,15 +90,15 @@ pub fn input_just_pressed<T>(input: T) -> impl FnMut(Res<ButtonInput<T>>) -> boo
where
T: Copy + Eq + Hash + Send + Sync + 'static,
{
move |inputs: Res<ButtonInput<T>>| inputs.just_pressed(input)
move |inputs: Res<ButtonInput<T>>| inputs.just_pressed(&input)
}

/// 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<ButtonInput<T>>| inputs.just_released(input)
move |inputs: Res<ButtonInput<T>>| inputs.just_released(&input)
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_input/src/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,7 @@ pub fn gamepad_button_event_system(

if button_property.is_released(value) {
// Check if button was previously pressed
if button_input.pressed(button) {
if button_input.pressed(&button) {
button_input_events.send(GamepadButtonInput {
button,
state: ButtonState::Released,
Expand All @@ -1199,7 +1199,7 @@ pub fn gamepad_button_event_system(
button_input.release(button);
} else if button_property.is_pressed(value) {
// Check if button was previously not pressed
if !button_input.pressed(button) {
if !button_input.pressed(&button) {
button_input_events.send(GamepadButtonInput {
button,
state: ButtonState::Pressed,
Expand Down
102 changes: 51 additions & 51 deletions crates/bevy_input/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ impl Touches {
}

/// Returns the [`Touch`] input corresponding to the `id` if it is being pressed.
pub fn get_pressed(&self, id: u64) -> Option<&Touch> {
self.pressed.get(&id)
pub fn get_pressed(&self, id: &u64) -> Option<&Touch> {
self.pressed.get(id)
}

/// Checks if any touch input was just pressed.
Expand All @@ -268,15 +268,15 @@ impl Touches {
}

/// Returns `true` if the input corresponding to the `id` has just been pressed.
pub fn just_pressed(&self, id: u64) -> bool {
self.just_pressed.contains_key(&id)
pub fn just_pressed(&self, id: &u64) -> bool {
self.just_pressed.contains_key(id)
}

/// Clears the `just_pressed` state of the touch input and returns `true` if the touch input has just been pressed.
///
/// Future calls to [`Touches::just_pressed`] for the given touch input will return false until a new press event occurs.
pub fn clear_just_pressed(&mut self, id: u64) -> bool {
self.just_pressed.remove(&id).is_some()
pub fn clear_just_pressed(&mut self, id: &u64) -> bool {
self.just_pressed.remove(id).is_some()
}

/// An iterator visiting every just pressed [`Touch`] input in arbitrary order.
Expand All @@ -285,8 +285,8 @@ impl Touches {
}

/// Returns the [`Touch`] input corresponding to the `id` if it has just been released.
pub fn get_released(&self, id: u64) -> Option<&Touch> {
self.just_released.get(&id)
pub fn get_released(&self, id: &u64) -> Option<&Touch> {
self.just_released.get(id)
}

/// Checks if any touch input was just released.
Expand All @@ -295,15 +295,15 @@ impl Touches {
}

/// Returns `true` if the input corresponding to the `id` has just been released.
pub fn just_released(&self, id: u64) -> bool {
self.just_released.contains_key(&id)
pub fn just_released(&self, id: &u64) -> bool {
self.just_released.contains_key(id)
}

/// Clears the `just_released` state of the touch input and returns `true` if the touch input has just been released.
///
/// Future calls to [`Touches::just_released`] for the given touch input will return false until a new release event occurs.
pub fn clear_just_released(&mut self, id: u64) -> bool {
self.just_released.remove(&id).is_some()
pub fn clear_just_released(&mut self, id: &u64) -> bool {
self.just_released.remove(id).is_some()
}

/// An iterator visiting every just released [`Touch`] input in arbitrary order.
Expand All @@ -317,8 +317,8 @@ impl Touches {
}

/// Returns `true` if the input corresponding to the `id` has just been canceled.
pub fn just_canceled(&self, id: u64) -> bool {
self.just_canceled.contains_key(&id)
pub fn just_canceled(&self, id: &u64) -> bool {
self.just_canceled.contains_key(id)
}

/// Clears the `just_canceled` state of the touch input and returns `true` if the touch input has just been canceled.
Expand Down Expand Up @@ -602,7 +602,7 @@ mod test {
touches.process_touch_event(&moved_touch_event2);

{
let touch = touches.get_pressed(started_touch_event.id).unwrap();
let touch = touches.get_pressed(&started_touch_event.id).unwrap();
assert_eq!(touch.previous_position, started_touch_event.position);
assert_eq!(touch.position, moved_touch_event2.position);
}
Expand All @@ -616,7 +616,7 @@ mod test {
touches.process_touch_event(&moved_touch_event1);

{
let touch = touches.get_pressed(started_touch_event.id).unwrap();
let touch = touches.get_pressed(&started_touch_event.id).unwrap();
assert_eq!(touch.previous_position, moved_touch_event2.position);
assert_eq!(touch.position, moved_touch_event1.position);
}
Expand All @@ -641,12 +641,12 @@ mod test {
// Register the touch and test that it was registered correctly
touches.process_touch_event(&touch_event);

assert!(touches.get_pressed(touch_event.id).is_some());
assert!(touches.just_pressed(touch_event.id));
assert!(touches.get_pressed(&touch_event.id).is_some());
assert!(touches.just_pressed(&touch_event.id));
assert_eq!(touches.iter().count(), 1);

touches.clear_just_pressed(touch_event.id);
assert!(!touches.just_pressed(touch_event.id));
touches.clear_just_pressed(&touch_event.id);
assert!(!touches.just_pressed(&touch_event.id));
}

#[test]
Expand All @@ -668,12 +668,12 @@ mod test {
// Register the touch and test that it was registered correctly
touches.process_touch_event(&touch_event);

assert!(touches.get_released(touch_event.id).is_some());
assert!(touches.just_released(touch_event.id));
assert!(touches.get_released(&touch_event.id).is_some());
assert!(touches.just_released(&touch_event.id));
assert_eq!(touches.iter_just_released().count(), 1);

touches.clear_just_released(touch_event.id);
assert!(!touches.just_released(touch_event.id));
touches.clear_just_released(&touch_event.id);
assert!(!touches.just_released(&touch_event.id));
}

#[test]
Expand All @@ -695,11 +695,11 @@ mod test {
// Register the touch and test that it was registered correctly
touches.process_touch_event(&touch_event);

assert!(touches.just_canceled(touch_event.id));
assert!(touches.just_canceled(&touch_event.id));
assert_eq!(touches.iter_just_canceled().count(), 1);

touches.clear_just_canceled(touch_event.id);
assert!(!touches.just_canceled(touch_event.id));
assert!(!touches.just_canceled(&touch_event.id));
}

#[test]
Expand All @@ -721,11 +721,11 @@ mod test {
// Register the touch and test that it was registered correctly
touches.process_touch_event(&touch_event);

assert!(touches.get_pressed(touch_event.id).is_some());
assert!(touches.get_pressed(&touch_event.id).is_some());

touches.release(touch_event.id);
assert!(touches.get_pressed(touch_event.id).is_none());
assert!(touches.just_released(touch_event.id));
assert!(touches.get_pressed(&touch_event.id).is_none());
assert!(touches.just_released(&touch_event.id));
}

#[test]
Expand Down Expand Up @@ -755,15 +755,15 @@ mod test {
touches.process_touch_event(&touch_pressed_event);
touches.process_touch_event(&touch_moved_event);

assert!(touches.get_pressed(touch_pressed_event.id).is_some());
assert!(touches.get_pressed(touch_moved_event.id).is_some());
assert!(touches.get_pressed(&touch_pressed_event.id).is_some());
assert!(touches.get_pressed(&touch_moved_event.id).is_some());

touches.release_all();

assert!(touches.get_pressed(touch_pressed_event.id).is_none());
assert!(touches.just_released(touch_pressed_event.id));
assert!(touches.get_pressed(touch_moved_event.id).is_none());
assert!(touches.just_released(touch_moved_event.id));
assert!(touches.get_pressed(&touch_pressed_event.id).is_none());
assert!(touches.just_released(&touch_pressed_event.id));
assert!(touches.get_pressed(&touch_moved_event.id).is_none());
assert!(touches.just_released(&touch_moved_event.id));
}

#[test]
Expand Down Expand Up @@ -803,17 +803,17 @@ mod test {
touches.process_touch_event(&touch_canceled_event);
touches.process_touch_event(&touch_released_event);

assert!(touches.get_pressed(touch_press_event.id).is_some());
assert!(touches.just_pressed(touch_press_event.id));
assert!(touches.just_canceled(touch_canceled_event.id));
assert!(touches.just_released(touch_released_event.id));
assert!(touches.get_pressed(&touch_press_event.id).is_some());
assert!(touches.just_pressed(&touch_press_event.id));
assert!(touches.just_canceled(&touch_canceled_event.id));
assert!(touches.just_released(&touch_released_event.id));

touches.clear();

assert!(touches.get_pressed(touch_press_event.id).is_some());
assert!(!touches.just_pressed(touch_press_event.id));
assert!(!touches.just_canceled(touch_canceled_event.id));
assert!(!touches.just_released(touch_released_event.id));
assert!(touches.get_pressed(&touch_press_event.id).is_some());
assert!(!touches.just_pressed(&touch_press_event.id));
assert!(!touches.just_canceled(&touch_canceled_event.id));
assert!(!touches.just_released(&touch_released_event.id));
}

#[test]
Expand Down Expand Up @@ -853,17 +853,17 @@ mod test {
touches.process_touch_event(&touch_canceled_event);
touches.process_touch_event(&touch_released_event);

assert!(touches.get_pressed(touch_press_event.id).is_some());
assert!(touches.just_pressed(touch_press_event.id));
assert!(touches.just_canceled(touch_canceled_event.id));
assert!(touches.just_released(touch_released_event.id));
assert!(touches.get_pressed(&touch_press_event.id).is_some());
assert!(touches.just_pressed(&touch_press_event.id));
assert!(touches.just_canceled(&touch_canceled_event.id));
assert!(touches.just_released(&touch_released_event.id));

touches.reset_all();

assert!(touches.get_pressed(touch_press_event.id).is_none());
assert!(!touches.just_pressed(touch_press_event.id));
assert!(!touches.just_canceled(touch_canceled_event.id));
assert!(!touches.just_released(touch_released_event.id));
assert!(touches.get_pressed(&touch_press_event.id).is_none());
assert!(!touches.just_pressed(&touch_press_event.id));
assert!(!touches.just_canceled(&touch_canceled_event.id));
assert!(!touches.just_released(&touch_released_event.id));
}

fn clear_all(touch_state: &mut Touches) {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub fn ui_focus_system(
}

let mouse_released =
mouse_button_input.just_released(MouseButton::Left) || touches_input.any_just_released();
mouse_button_input.just_released(&MouseButton::Left) || touches_input.any_just_released();
if mouse_released {
for node in &mut node_query {
if let Some(mut interaction) = node.interaction {
Expand All @@ -185,7 +185,7 @@ pub fn ui_focus_system(
}

let mouse_clicked =
mouse_button_input.just_pressed(MouseButton::Left) || touches_input.any_just_pressed();
mouse_button_input.just_pressed(&MouseButton::Left) || touches_input.any_just_pressed();

let camera_cursor_positions: HashMap<Entity, Vec2> = camera_query
.iter()
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/2d_shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn toggle_wireframe(
mut wireframe_config: ResMut<Wireframe2dConfig>,
keyboard: Res<ButtonInput<KeyCode>>,
) {
if keyboard.just_pressed(KeyCode::Space) {
if keyboard.just_pressed(&KeyCode::Space) {
wireframe_config.global = !wireframe_config.global;
}
}
32 changes: 16 additions & 16 deletions examples/2d/bloom_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,65 +120,65 @@ fn update_bloom_settings(
bloom_settings.prefilter_settings.threshold_softness
));

if keycode.just_pressed(KeyCode::Space) {
if keycode.just_pressed(&KeyCode::Space) {
commands.entity(entity).remove::<BloomSettings>();
}

let dt = time.delta_seconds();

if keycode.pressed(KeyCode::KeyA) {
if keycode.pressed(&KeyCode::KeyA) {
bloom_settings.intensity -= dt / 10.0;
}
if keycode.pressed(KeyCode::KeyQ) {
if keycode.pressed(&KeyCode::KeyQ) {
bloom_settings.intensity += dt / 10.0;
}
bloom_settings.intensity = bloom_settings.intensity.clamp(0.0, 1.0);

if keycode.pressed(KeyCode::KeyS) {
if keycode.pressed(&KeyCode::KeyS) {
bloom_settings.low_frequency_boost -= dt / 10.0;
}
if keycode.pressed(KeyCode::KeyW) {
if keycode.pressed(&KeyCode::KeyW) {
bloom_settings.low_frequency_boost += dt / 10.0;
}
bloom_settings.low_frequency_boost = bloom_settings.low_frequency_boost.clamp(0.0, 1.0);

if keycode.pressed(KeyCode::KeyD) {
if keycode.pressed(&KeyCode::KeyD) {
bloom_settings.low_frequency_boost_curvature -= dt / 10.0;
}
if keycode.pressed(KeyCode::KeyE) {
if keycode.pressed(&KeyCode::KeyE) {
bloom_settings.low_frequency_boost_curvature += dt / 10.0;
}
bloom_settings.low_frequency_boost_curvature =
bloom_settings.low_frequency_boost_curvature.clamp(0.0, 1.0);

if keycode.pressed(KeyCode::KeyF) {
if keycode.pressed(&KeyCode::KeyF) {
bloom_settings.high_pass_frequency -= dt / 10.0;
}
if keycode.pressed(KeyCode::KeyR) {
if keycode.pressed(&KeyCode::KeyR) {
bloom_settings.high_pass_frequency += dt / 10.0;
}
bloom_settings.high_pass_frequency = bloom_settings.high_pass_frequency.clamp(0.0, 1.0);

if keycode.pressed(KeyCode::KeyG) {
if keycode.pressed(&KeyCode::KeyG) {
bloom_settings.composite_mode = BloomCompositeMode::Additive;
}
if keycode.pressed(KeyCode::KeyT) {
if keycode.pressed(&KeyCode::KeyT) {
bloom_settings.composite_mode = BloomCompositeMode::EnergyConserving;
}

if keycode.pressed(KeyCode::KeyH) {
if keycode.pressed(&KeyCode::KeyH) {
bloom_settings.prefilter_settings.threshold -= dt;
}
if keycode.pressed(KeyCode::KeyY) {
if keycode.pressed(&KeyCode::KeyY) {
bloom_settings.prefilter_settings.threshold += dt;
}
bloom_settings.prefilter_settings.threshold =
bloom_settings.prefilter_settings.threshold.max(0.0);

if keycode.pressed(KeyCode::KeyJ) {
if keycode.pressed(&KeyCode::KeyJ) {
bloom_settings.prefilter_settings.threshold_softness -= dt / 10.0;
}
if keycode.pressed(KeyCode::KeyU) {
if keycode.pressed(&KeyCode::KeyU) {
bloom_settings.prefilter_settings.threshold_softness += dt / 10.0;
}
bloom_settings.prefilter_settings.threshold_softness = bloom_settings
Expand All @@ -190,7 +190,7 @@ fn update_bloom_settings(
(entity, None) => {
*text = "Bloom: Off (Toggle: Space)".to_string();

if keycode.just_pressed(KeyCode::Space) {
if keycode.just_pressed(&KeyCode::Space) {
commands.entity(entity).insert(BloomSettings::default());
}
}
Expand Down
Loading