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

Add delta to CursorMoved event #11710

Merged
merged 9 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 7 additions & 1 deletion crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,13 @@ pub struct WindowDestroyed {
/// An event reporting that the mouse cursor has moved inside a window.
///
/// The event is sent only if the cursor is over one of the application's windows.
/// It is the translated version of [`WindowEvent::CursorMoved`] from the `winit` crate.
/// It is the translated version of [`WindowEvent::CursorMoved`] from the `winit` crate with the addition of `delta`.
///
/// Not to be confused with the [`MouseMotion`] event from `bevy_input`.
///
/// Because the range of data is limited by the window area and it may have been transformed by the OS to implement certain effects like acceleration,
/// you should not use it for non-cursor-like behaviour such as 3D camera control. Please see [`MouseMotion`] instead.
///
/// [`WindowEvent::CursorMoved`]: https://docs.rs/winit/latest/winit/event/enum.WindowEvent.html#variant.CursorMoved
/// [`MouseMotion`]: bevy_input::mouse::MouseMotion
#[derive(Event, Debug, Clone, PartialEq, Reflect)]
Expand All @@ -133,6 +136,9 @@ pub struct CursorMoved {
pub window: Entity,
/// The cursor position in logical pixels.
pub position: Vec2,
/// The change in the position of the cursor since the last event was sent.
/// This value is `None` if the cursor was outside the window area during the last frame.
pub delta: Option<Vec2>,
lynn-lumen marked this conversation as resolved.
Show resolved Hide resolved
}

/// An event that is sent whenever the user's cursor enters a window.
Expand Down
12 changes: 11 additions & 1 deletion crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,20 @@ fn handle_winit_event(
}
WindowEvent::CursorMoved { position, .. } => {
let physical_position = DVec2::new(position.x, position.y);

let last_position = win.physical_cursor_position();
let delta = last_position.map(|last_pos| {
(physical_position.as_vec2() - last_pos) / win.resolution.scale_factor()
});

win.set_physical_cursor_position(Some(physical_position));
let position =
(physical_position / win.resolution.scale_factor() as f64).as_vec2();
app.send_event(CursorMoved { window, position });
app.send_event(CursorMoved {
window,
position,
delta,
});
}
WindowEvent::CursorEntered { .. } => {
app.send_event(CursorEntered { window });
Expand Down