Skip to content

Commit

Permalink
Merge pull request #11 from johanhelsing/dragging-outside-window
Browse files Browse the repository at this point in the history
fix: Fix incorrect direction when mouse drag goes outside window
  • Loading branch information
johanhelsing authored Dec 5, 2023
2 parents d56bc27 + 10fc952 commit d72fe88
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 d72fe88

Please sign in to comment.