Skip to content

Commit

Permalink
Change bevy_input::Touch API to match similar APIs (#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
ambeeeeee authored Dec 1, 2020
1 parent c7b9ad5 commit 4c1bc02
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
44 changes: 37 additions & 7 deletions crates/bevy_input/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ pub struct TouchSystemState {

#[derive(Debug, Clone, Copy)]
pub struct Touch {
pub id: u64,
pub start_position: Vec2,
pub start_force: Option<ForceTouch>,
pub previous_position: Vec2,
pub previous_force: Option<ForceTouch>,
pub position: Vec2,
pub force: Option<ForceTouch>,
id: u64,
start_position: Vec2,
start_force: Option<ForceTouch>,
previous_position: Vec2,
previous_force: Option<ForceTouch>,
position: Vec2,
force: Option<ForceTouch>,
}

impl Touch {
Expand All @@ -102,6 +102,36 @@ impl Touch {
pub fn distance(&self) -> Vec2 {
self.position - self.start_position
}

#[inline]
pub fn id(&self) -> u64 {
self.id
}

#[inline]
pub fn start_position(&self) -> Vec2 {
self.start_position
}

#[inline]
pub fn start_force(&self) -> Option<ForceTouch> {
self.start_force
}

#[inline]
pub fn previous_position(&self) -> Vec2 {
self.previous_position
}

#[inline]
pub fn position(&self) -> Vec2 {
self.position
}

#[inline]
pub fn force(&self) -> Option<ForceTouch> {
self.force
}
}

impl From<&TouchInput> for Touch {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn ui_focus_system(
state.cursor_position = cursor_moved.position;
}
if let Some(touch) = touches_input.get_pressed(0) {
state.cursor_position = touch.position;
state.cursor_position = touch.position();
}

if mouse_button_input.just_released(MouseButton::Left) || touches_input.just_released(0) {
Expand Down
10 changes: 6 additions & 4 deletions examples/input/touch_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@ fn touch_system(touches: Res<Touches>) {
for touch in touches.iter_just_pressed() {
println!(
"just pressed touch with id: {:?}, at: {:?}",
touch.id, touch.position
touch.id(),
touch.position()
);
}

for touch in touches.iter_just_released() {
println!(
"just released touch with id: {:?}, at: {:?}",
touch.id, touch.position
touch.id(),
touch.position()
);
}

for touch in touches.iter_just_cancelled() {
println!("cancelled touch with id: {:?}", touch.id);
println!("cancelled touch with id: {:?}", touch.id());
}

// you can also iterate all current touches and retrieve their state like this:
for touch in touches.iter() {
println!("active touch: {:?}", touch);
println!(" just_pressed: {}", touches.just_pressed(touch.id));
println!(" just_pressed: {}", touches.just_pressed(touch.id()));
}
}

0 comments on commit 4c1bc02

Please sign in to comment.