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

Check cursor position for out of bounds of the window #8855

Merged
merged 5 commits into from
Aug 28, 2023
Merged
Changes from 2 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
31 changes: 23 additions & 8 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,20 +266,35 @@ impl Window {
self.resolution.scale_factor()
}

/// The cursor position in this window
/// The cursor position in this window.
///
/// Returns `None` if the cursor is out of bounds of the window.
#[inline]
pub fn cursor_position(&self) -> Option<Vec2> {
self.internal
.physical_cursor_position
.map(|position| (position / self.scale_factor()).as_vec2())
self.physical_cursor_position()
.map(|position| (position.as_dvec2() / self.scale_factor()).as_vec2())
}

/// The physical cursor position in this window
/// The physical cursor position in this window.
///
/// Returns `None` if the cursor is out of bounds of the window.
#[inline]
pub fn physical_cursor_position(&self) -> Option<Vec2> {
self.internal
.physical_cursor_position
.map(|position| position.as_vec2())
match self.internal.physical_cursor_position {
Some(position) => {
let position = position.as_vec2();
if position.x > self.width()
|| position.x < 0.0
|| position.y > self.height()
|| position.y < 0.0
{
None
} else {
Some(position)
}
}
None => None,
}
}

/// Set the cursor position in this window
Expand Down