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 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
21 changes: 14 additions & 7 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy_ecs::{
entity::{Entity, EntityMapper, MapEntities},
prelude::{Component, ReflectComponent},
};
use bevy_math::{DVec2, IVec2, Vec2};
use bevy_math::{DVec2, IVec2, Rect, Vec2};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};

#[cfg(feature = "serialize")]
Expand Down Expand Up @@ -305,9 +305,8 @@ impl Window {
/// See [`WindowResolution`] for an explanation about logical/physical sizes.
#[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 cursor position in this window in physical pixels.
Expand All @@ -317,9 +316,17 @@ impl Window {
/// See [`WindowResolution`] for an explanation about logical/physical sizes.
#[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 Rect::new(0., 0., self.width(), self.height()).contains(position) {
Some(position)
} else {
None
}
}
None => None,
}
}

/// Set the cursor position in this window in logical pixels.
Expand Down