Skip to content

Commit

Permalink
Check cursor position for out of bounds of the window (#8855)
Browse files Browse the repository at this point in the history
# 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 <david.curthoys@googlemail.com>
  • Loading branch information
Aceeri and ickshonpe committed Aug 28, 2023
1 parent 474b55a commit 3cf94e7
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions crates/bevy_window/src/window.rs
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 @@ -316,9 +316,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 @@ -328,9 +327,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

0 comments on commit 3cf94e7

Please sign in to comment.