mirror of
https://github.com/bevyengine/bevy
synced 2025-02-16 14:08:32 +00:00
Fix scale factor for cursor position (#2932)
# Objective - Fixes #2501 - Builds up on #2639 taking https://github.com/bevyengine/bevy/pull/2639#issuecomment-898701047 into account ## Solution - keep the physical cursor position in `Window`, and expose it. - still convert to logical position in event, and when getting `cursor_position` Co-authored-by: Ahmed Charles <acharles@outlook.com>
This commit is contained in:
parent
099386f184
commit
d65fbd7b9d
2 changed files with 23 additions and 18 deletions
|
@ -1,4 +1,4 @@
|
|||
use bevy_math::{IVec2, Vec2};
|
||||
use bevy_math::{DVec2, IVec2, Vec2};
|
||||
use bevy_utils::{tracing::warn, Uuid};
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
||||
|
@ -122,7 +122,7 @@ pub struct Window {
|
|||
decorations: bool,
|
||||
cursor_visible: bool,
|
||||
cursor_locked: bool,
|
||||
cursor_position: Option<Vec2>,
|
||||
physical_cursor_position: Option<DVec2>,
|
||||
focused: bool,
|
||||
mode: WindowMode,
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
|
@ -215,7 +215,7 @@ impl Window {
|
|||
decorations: window_descriptor.decorations,
|
||||
cursor_visible: window_descriptor.cursor_visible,
|
||||
cursor_locked: window_descriptor.cursor_locked,
|
||||
cursor_position: None,
|
||||
physical_cursor_position: None,
|
||||
focused: true,
|
||||
mode: window_descriptor.mode,
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
|
@ -466,10 +466,18 @@ impl Window {
|
|||
});
|
||||
}
|
||||
|
||||
/// The current mouse position, in physical pixels.
|
||||
#[inline]
|
||||
pub fn physical_cursor_position(&self) -> Option<DVec2> {
|
||||
self.physical_cursor_position
|
||||
}
|
||||
|
||||
/// The current mouse position, in logical pixels, taking into account the screen scale factor.
|
||||
#[inline]
|
||||
#[doc(alias = "mouse position")]
|
||||
pub fn cursor_position(&self) -> Option<Vec2> {
|
||||
self.cursor_position
|
||||
self.physical_cursor_position
|
||||
.map(|p| (p / self.scale_factor()).as_vec2())
|
||||
}
|
||||
|
||||
pub fn set_cursor_position(&mut self, position: Vec2) {
|
||||
|
@ -485,8 +493,8 @@ impl Window {
|
|||
|
||||
#[allow(missing_docs)]
|
||||
#[inline]
|
||||
pub fn update_cursor_position_from_backend(&mut self, cursor_position: Option<Vec2>) {
|
||||
self.cursor_position = cursor_position;
|
||||
pub fn update_cursor_physical_position_from_backend(&mut self, cursor_position: Option<DVec2>) {
|
||||
self.physical_cursor_position = cursor_position;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
|
|
@ -12,7 +12,7 @@ pub use winit_windows::*;
|
|||
|
||||
use bevy_app::{App, AppExit, CoreStage, Events, ManualEventReader, Plugin};
|
||||
use bevy_ecs::{system::IntoExclusiveSystem, world::World};
|
||||
use bevy_math::{ivec2, Vec2};
|
||||
use bevy_math::{ivec2, DVec2, Vec2};
|
||||
use bevy_utils::tracing::{error, trace, warn};
|
||||
use bevy_window::{
|
||||
CreateWindow, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter,
|
||||
|
@ -303,20 +303,18 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
|
|||
let mut cursor_moved_events =
|
||||
world.get_resource_mut::<Events<CursorMoved>>().unwrap();
|
||||
let winit_window = winit_windows.get_window(window_id).unwrap();
|
||||
let position = position.to_logical(winit_window.scale_factor());
|
||||
let inner_size = winit_window
|
||||
.inner_size()
|
||||
.to_logical::<f32>(winit_window.scale_factor());
|
||||
let inner_size = winit_window.inner_size();
|
||||
|
||||
// move origin to bottom left
|
||||
let y_position = inner_size.height - position.y;
|
||||
let y_position = inner_size.height as f64 - position.y;
|
||||
|
||||
let position = Vec2::new(position.x, y_position);
|
||||
window.update_cursor_position_from_backend(Some(position));
|
||||
let physical_position = DVec2::new(position.x, y_position);
|
||||
window
|
||||
.update_cursor_physical_position_from_backend(Some(physical_position));
|
||||
|
||||
cursor_moved_events.send(CursorMoved {
|
||||
id: window_id,
|
||||
position,
|
||||
position: (physical_position / window.scale_factor()).as_vec2(),
|
||||
});
|
||||
}
|
||||
WindowEvent::CursorEntered { .. } => {
|
||||
|
@ -327,7 +325,7 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
|
|||
WindowEvent::CursorLeft { .. } => {
|
||||
let mut cursor_left_events =
|
||||
world.get_resource_mut::<Events<CursorLeft>>().unwrap();
|
||||
window.update_cursor_position_from_backend(None);
|
||||
window.update_cursor_physical_position_from_backend(None);
|
||||
cursor_left_events.send(CursorLeft { id: window_id });
|
||||
}
|
||||
WindowEvent::MouseInput { state, button, .. } => {
|
||||
|
@ -363,8 +361,7 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) {
|
|||
let mut touch_input_events =
|
||||
world.get_resource_mut::<Events<TouchInput>>().unwrap();
|
||||
|
||||
let winit_window = winit_windows.get_window(window_id).unwrap();
|
||||
let mut location = touch.location.to_logical(winit_window.scale_factor());
|
||||
let mut location = touch.location.to_logical(window.scale_factor());
|
||||
|
||||
// On a mobile window, the start is from the top while on PC/Linux/OSX from
|
||||
// bottom
|
||||
|
|
Loading…
Add table
Reference in a new issue