-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()); | ||
} | ||
} | ||
}; | ||
} | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to add an explanation for this assertion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||
} | ||
|
||
#[test] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean it already reflects the
Ended
/Canceled
phase? I haven't used touches in Bevy before but I assume the event returned fromjust_released
would be one of those phases.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.