Skip to content

Commit

Permalink
bevy_input: Fix process touch event (bevyengine#4352)
Browse files Browse the repository at this point in the history
# Objective

- `process_touch_event` in `bevy_input` don't update position info. `TouchPhase::Ended` and `TouchPhase::Cancelled` should use the position info from `pressed`. Otherwise, it'll not update. The position info is updated from `TouchPhase::Moved`.

## Solution

- Use updated touch info. 

---

## Changelog

> This section is optional. If this was a trivial fix, or has no externally-visible impact, feel free to skip this section.

- Fixed: bevy_input, fix process touch event, update touch info
  • Loading branch information
light4 authored and james7132 committed Oct 19, 2022
1 parent d103067 commit 9d18b26
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions crates/bevy_input/src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,22 @@ impl Touches {
}
}
TouchPhase::Ended => {
self.just_released.insert(event.id, event.into());
self.pressed.remove_entry(&event.id);
// if touch `just_released`, add related event to it
// the event position info is inside `pressed`, so use it unless not found
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 touch `just_cancelled`, add related event to it
// the event position info is inside `pressed`, so use it unless not found
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 @@ -427,24 +437,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::splat(4.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 updated from TouchPhase::Moved and TouchPhase::Ended
assert!(touch.previous_position != touch.position);
}

#[test]
Expand Down

0 comments on commit 9d18b26

Please sign in to comment.