From 10fc952a4482aee9d02a1dc966910e0ef6ccb090 Mon Sep 17 00:00:00 2001 From: Johan Klokkhammer Helsing Date: Tue, 5 Dec 2023 14:42:32 +0100 Subject: [PATCH] fix: Fix incorrect direction when mouse drag goes outside window Simply ignore mouse drag events when we don't know the position of the cursor. --- src/input.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/input.rs b/src/input.rs index 25b9b86..b6e21c1 100644 --- a/src/input.rs +++ b/src/input.rs @@ -119,7 +119,7 @@ pub(crate) fn send_drag_events_from_mouse( primary_window: Query<&Window, With>, ) { let primary_window = primary_window.single(); - let position = primary_window.cursor_position().unwrap_or(Vec2::ZERO); + let position = primary_window.cursor_position(); for mouse_event in mouse_events.read() { if mouse_event.button == MouseButton::Left && mouse_event.state == ButtonState::Released { @@ -127,11 +127,19 @@ pub(crate) fn send_drag_events_from_mouse( } if mouse_event.button == MouseButton::Left && mouse_event.state == ButtonState::Pressed { - drag_events.send(DragEvent::Start { id: 0, position }); + drag_events.send(DragEvent::Start { + id: 0, + position: position.unwrap_or_default(), + }); } } if mouse_buttons.pressed(MouseButton::Left) { - drag_events.send(DragEvent::Drag { id: 0, position }); + // if the mouse is outside the window, we'll still get pressed state, + // but we won't get the position. So in that case, we stop sending drag + // events. + if let Some(position) = position { + drag_events.send(DragEvent::Drag { id: 0, position }); + } } }