Skip to content

Commit

Permalink
Add release_all in Touches and test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
matiqo15 committed Dec 12, 2023
1 parent 6ebcea6 commit 2730654
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions crates/bevy_input/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ impl Touches {
}
}

/// Registers a release for all currently pressed touch inputs.
pub fn release_all(&mut self) {
self.just_released.extend(self.pressed.drain());
}

/// 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)
Expand Down Expand Up @@ -629,6 +634,41 @@ mod test {
assert!(touches.just_released(touch_event.id));
}

#[test]
fn release_all_touches() {
use crate::{touch::TouchPhase, TouchInput, Touches};
use bevy_math::Vec2;

let mut touches = Touches::default();

let touch_pressed_event = TouchInput {
phase: TouchPhase::Started,
position: Vec2::splat(4.0),
force: None,
id: 4,
};

let touch_moved_event = TouchInput {
phase: TouchPhase::Moved,
position: Vec2::splat(4.0),
force: None,
id: 4,
};

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());

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));
}

#[test]
fn clear_touches() {
use crate::{touch::TouchPhase, TouchInput, Touches};
Expand Down

0 comments on commit 2730654

Please sign in to comment.