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 <david.curthoys@googlemail.com>
This commit is contained in:
parent
474b55a29c
commit
3cf94e7c9d
@ -2,7 +2,7 @@ use bevy_ecs::{
|
|||||||
entity::{Entity, EntityMapper, MapEntities},
|
entity::{Entity, EntityMapper, MapEntities},
|
||||||
prelude::{Component, ReflectComponent},
|
prelude::{Component, ReflectComponent},
|
||||||
};
|
};
|
||||||
use bevy_math::{DVec2, IVec2, Vec2};
|
use bevy_math::{DVec2, IVec2, Rect, Vec2};
|
||||||
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
|
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
|
||||||
|
|
||||||
#[cfg(feature = "serialize")]
|
#[cfg(feature = "serialize")]
|
||||||
@ -316,9 +316,8 @@ impl Window {
|
|||||||
/// See [`WindowResolution`] for an explanation about logical/physical sizes.
|
/// See [`WindowResolution`] for an explanation about logical/physical sizes.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn cursor_position(&self) -> Option<Vec2> {
|
pub fn cursor_position(&self) -> Option<Vec2> {
|
||||||
self.internal
|
self.physical_cursor_position()
|
||||||
.physical_cursor_position
|
.map(|position| (position.as_dvec2() / self.scale_factor()).as_vec2())
|
||||||
.map(|position| (position / self.scale_factor()).as_vec2())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The cursor position in this window in physical pixels.
|
/// The cursor position in this window in physical pixels.
|
||||||
@ -328,9 +327,17 @@ impl Window {
|
|||||||
/// See [`WindowResolution`] for an explanation about logical/physical sizes.
|
/// See [`WindowResolution`] for an explanation about logical/physical sizes.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn physical_cursor_position(&self) -> Option<Vec2> {
|
pub fn physical_cursor_position(&self) -> Option<Vec2> {
|
||||||
self.internal
|
match self.internal.physical_cursor_position {
|
||||||
.physical_cursor_position
|
Some(position) => {
|
||||||
.map(|position| position.as_vec2())
|
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.
|
/// Set the cursor position in this window in logical pixels.
|
||||||
|
Loading…
Reference in New Issue
Block a user