Skip to content

Commit

Permalink
Fix scale factor for cursor position (#2932)
Browse files Browse the repository at this point in the history
# Objective

- Fixes #2501 
- Builds up on #2639 taking #2639 (comment) 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>
  • Loading branch information
mockersf and ahmedcharles committed Oct 15, 2021
1 parent 099386f commit d65fbd7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
20 changes: 14 additions & 6 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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")]
Expand Down Expand Up @@ -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) {
Expand All @@ -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]
Expand Down
21 changes: 9 additions & 12 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 { .. } => {
Expand All @@ -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, .. } => {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d65fbd7

Please sign in to comment.