Skip to content

Commit

Permalink
bevy_input: Fix process touch event
Browse files Browse the repository at this point in the history
  • Loading branch information
light4 committed Mar 30, 2022
1 parent 63fee25 commit dfeaa8d
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions crates/bevy_input/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,18 @@ impl Touches {
}
}
TouchPhase::Ended => {
self.just_released.insert(event.id, event.into());
self.pressed.remove_entry(&event.id);
if let Some((_, v)) = self.pressed.remove_entry(&event.id) {
self.just_released.insert(event.id, v);
} else {
self.just_released.insert(event.id, event.into());
}
}
TouchPhase::Cancelled => {
self.just_cancelled.insert(event.id, event.into());
self.pressed.remove_entry(&event.id);
if let Some((_, v)) = self.pressed.remove_entry(&event.id) {
self.just_cancelled.insert(event.id, v);
} else {
self.just_cancelled.insert(event.id, event.into());
}
}
};
}
Expand Down Expand Up @@ -321,24 +327,28 @@ mod test {
touches.update();
touches.process_touch_event(&cancel_touch_event);

assert!(touches.just_cancelled.get(&cancel_touch_event.id).is_some());
assert!(touches.pressed.get(&cancel_touch_event.id).is_none());
assert!(touches.just_cancelled.get(&touch_event.id).is_some());
assert!(touches.pressed.get(&touch_event.id).is_none());

// Test ending an event

let end_touch_event = TouchInput {
phase: TouchPhase::Ended,
position: Vec2::new(4.0, 4.0),
position: Vec2::new(5.0, 5.0),
force: None,
id: 4,
id: touch_event.id,
};

touches.update();
touches.process_touch_event(&touch_event);
touches.process_touch_event(&moved_touch_event);
touches.process_touch_event(&end_touch_event);

assert!(touches.just_released.get(&touch_event.id).is_some());
assert!(touches.pressed.get(&touch_event.id).is_none());
let touch = touches.just_released.get(&touch_event.id).unwrap();
// Make sure the position is changed
assert!(touch.previous_position != touch.position);
}

#[test]
Expand Down

0 comments on commit dfeaa8d

Please sign in to comment.