Skip to content

Commit 3cf94e7

Browse files
Aceeriickshonpe
andauthored
Check cursor position for out of bounds of the window (#8855)
# Objective Fixes #8840 Make the cursor position more consistent, right now the cursor position is *sometimes* outside of the window and returns the position and *sometimes* `None`. Even in the cases where someone might be using that position that is outside of the window, it'll probably require some manual transformations for it to actually be useful. ## Solution Check the windows width and height for out of bounds positions. --- ## Changelog - Cursor position is now always `None` when outside of the window. --------- Co-authored-by: ickshonpe <[email protected]>
1 parent 474b55a commit 3cf94e7

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

crates/bevy_window/src/window.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bevy_ecs::{
22
entity::{Entity, EntityMapper, MapEntities},
33
prelude::{Component, ReflectComponent},
44
};
5-
use bevy_math::{DVec2, IVec2, Vec2};
5+
use bevy_math::{DVec2, IVec2, Rect, Vec2};
66
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
77

88
#[cfg(feature = "serialize")]
@@ -316,9 +316,8 @@ impl Window {
316316
/// See [`WindowResolution`] for an explanation about logical/physical sizes.
317317
#[inline]
318318
pub fn cursor_position(&self) -> Option<Vec2> {
319-
self.internal
320-
.physical_cursor_position
321-
.map(|position| (position / self.scale_factor()).as_vec2())
319+
self.physical_cursor_position()
320+
.map(|position| (position.as_dvec2() / self.scale_factor()).as_vec2())
322321
}
323322

324323
/// The cursor position in this window in physical pixels.
@@ -328,9 +327,17 @@ impl Window {
328327
/// See [`WindowResolution`] for an explanation about logical/physical sizes.
329328
#[inline]
330329
pub fn physical_cursor_position(&self) -> Option<Vec2> {
331-
self.internal
332-
.physical_cursor_position
333-
.map(|position| position.as_vec2())
330+
match self.internal.physical_cursor_position {
331+
Some(position) => {
332+
let position = position.as_vec2();
333+
if Rect::new(0., 0., self.width(), self.height()).contains(position) {
334+
Some(position)
335+
} else {
336+
None
337+
}
338+
}
339+
None => None,
340+
}
334341
}
335342

336343
/// Set the cursor position in this window in logical pixels.

0 commit comments

Comments
 (0)