mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
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
1 changed files with 14 additions and 7 deletions
|
@ -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")]
|
||||
|
@ -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.
|
||||
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue