Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Fix scale factor for cursor position #2932

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 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,19 @@ impl Window {
});
}

/// The current mouse position, in physical pixels.
#[inline]
#[doc(alias = "mouse position")]
mockersf marked this conversation as resolved.
Show resolved Hide resolved
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 +494,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