From 1c3286270f085d5b0b7823293cceb449ff73253f Mon Sep 17 00:00:00 2001 From: Ahmed Charles Date: Thu, 12 Aug 2021 03:46:13 -0700 Subject: [PATCH 1/3] Fix scale_factor_override with cursor position. Sometimes the wrong scale factor was used (not accounting for overrides). Fixes #2501 --- crates/bevy_winit/src/lib.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 294ba67cd9fc7..4e3c0eaf16768 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -303,10 +303,10 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) { let mut cursor_moved_events = world.get_resource_mut::>().unwrap(); let winit_window = winit_windows.get_window(window_id).unwrap(); - let position = position.to_logical(winit_window.scale_factor()); + let position = position.to_logical(window.scale_factor()); let inner_size = winit_window .inner_size() - .to_logical::(winit_window.scale_factor()); + .to_logical::(window.scale_factor()); // move origin to bottom left let y_position = inner_size.height - position.y; @@ -363,8 +363,7 @@ pub fn winit_runner_with(mut app: App, mut event_loop: EventLoop<()>) { let mut touch_input_events = world.get_resource_mut::>().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 From 4599dda51fd260a09dd227a122fb6e0f81eed430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Fri, 8 Oct 2021 01:39:34 +0200 Subject: [PATCH 2/3] keep physical cursor position in Window --- crates/bevy_window/src/window.rs | 21 +++++++++++++++------ crates/bevy_winit/src/lib.rs | 18 ++++++++---------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index be3a6c0f99230..b22ebe32d6b24 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -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, + physical_cursor_position: Option, 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,19 @@ impl Window { }); } + /// The current mouse position, in physical pixels. + #[inline] + #[doc(alias = "mouse position")] + pub fn physical_cursor_position(&self) -> Option { + 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 { - 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 +494,8 @@ impl Window { #[allow(missing_docs)] #[inline] - pub fn update_cursor_position_from_backend(&mut self, cursor_position: Option) { - self.cursor_position = cursor_position; + pub fn update_cursor_physical_position_from_backend(&mut self, cursor_position: Option) { + self.physical_cursor_position = cursor_position; } #[inline] diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 4e3c0eaf16768..97b79131cadc4 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -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::>().unwrap(); let winit_window = winit_windows.get_window(window_id).unwrap(); - let position = position.to_logical(window.scale_factor()); - let inner_size = winit_window - .inner_size() - .to_logical::(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::>().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, .. } => { From ab8606e411cfe979cd9f0245bb6a72f9a79d56c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Fri, 15 Oct 2021 00:36:00 +0200 Subject: [PATCH 3/3] remove doc alias Co-authored-by: bjorn3 --- crates/bevy_window/src/window.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index b22ebe32d6b24..f8e2e249da76e 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -468,7 +468,6 @@ impl Window { /// The current mouse position, in physical pixels. #[inline] - #[doc(alias = "mouse position")] pub fn physical_cursor_position(&self) -> Option { self.physical_cursor_position }