Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - bevy_input: Fix process touch event #4352

Closed
wants to merge 1 commit into from
Closed
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
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we update the touch phase before inserting? (Same on canceled)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The touch info is from self.pressed or event, so I think this is fine.
I can change this if you prefer to update it first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the touch info should come from self.pressed.
If not found in self.pressed, there may be some error occurred, set it to default just to get rid of panic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The touch info is from self.pressed or event, so I think this is fine.

You mean it already reflects the Ended/Canceled phase? I haven't used touches in Bevy before but I assume the event returned from just_released would be one of those phases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The info is updated from phase TouchPhase::Moved

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting. See I'd think that one should also update the phase to Moved. 🤔

But maybe I'm just mistaken in how the touch system is supposed to work.

} 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add an explanation for this assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

}

#[test]
Expand Down