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

Move cursor position to internal state #7988

Merged
merged 2 commits into from
Mar 9, 2023
Merged
Changes from all 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: 10 additions & 11 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,27 +261,28 @@ impl Window {
/// The cursor position in this window
#[inline]
pub fn cursor_position(&self) -> Option<Vec2> {
self.cursor
.physical_position
self.internal
.physical_cursor_position
.map(|position| (position / self.scale_factor()).as_vec2())
}

/// The physical cursor position in this window
#[inline]
pub fn physical_cursor_position(&self) -> Option<Vec2> {
self.cursor
.physical_position
self.internal
.physical_cursor_position
.map(|position| position.as_vec2())
}

/// Set the cursor position in this window
pub fn set_cursor_position(&mut self, position: Option<Vec2>) {
self.cursor.physical_position = position.map(|p| p.as_dvec2() * self.scale_factor());
self.internal.physical_cursor_position =
position.map(|p| p.as_dvec2() * self.scale_factor());
}

/// Set the physical cursor position in this window
pub fn set_physical_cursor_position(&mut self, position: Option<DVec2>) {
self.cursor.physical_position = position;
self.internal.physical_cursor_position = position;
}
}

Expand Down Expand Up @@ -397,9 +398,6 @@ pub struct Cursor {
///
/// - iOS / Android / Web / X11: Unsupported.
pub hit_test: bool,

/// The position of this window's cursor.
physical_position: Option<DVec2>,
}

impl Default for Cursor {
Expand All @@ -409,7 +407,6 @@ impl Default for Cursor {
visible: true,
grab_mode: CursorGrabMode::None,
hit_test: true,
physical_position: None,
}
}
}
Expand Down Expand Up @@ -648,7 +645,7 @@ pub enum CursorGrabMode {
}

/// Stores internal state that isn't directly accessible.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Reflect, FromReflect)]
#[derive(Default, Debug, Copy, Clone, PartialEq, Reflect, FromReflect)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
Expand All @@ -660,6 +657,8 @@ pub struct InternalWindowState {
minimize_request: Option<bool>,
/// If this is true then next frame we will ask to maximize/un-maximize the window depending on `maximized`.
maximize_request: Option<bool>,
/// Unscaled cursor position.
physical_cursor_position: Option<DVec2>,
}

impl InternalWindowState {
Expand Down