Skip to content

Commit

Permalink
fix: Fix incorrect direction when mouse drag goes outside window
Browse files Browse the repository at this point in the history
Simply ignore mouse drag events when we don't know the position of the
cursor.
  • Loading branch information
johanhelsing committed Dec 5, 2023
1 parent d56bc27 commit 10fc952
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,27 @@ pub(crate) fn send_drag_events_from_mouse(
primary_window: Query<&Window, With<PrimaryWindow>>,
) {
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 {
drag_events.send(DragEvent::End { id: 0 });
}

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 });
}
}
}

0 comments on commit 10fc952

Please sign in to comment.