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 #11695

Closed
Sebbl0508 opened this issue Feb 4, 2024 · 3 comments · Fixed by #11710
Closed

Add delta to CursorMoved event #11695

Sebbl0508 opened this issue Feb 4, 2024 · 3 comments · Fixed by #11710
Labels
A-Input Player input via keyboard, mouse, gamepad, and more C-Feature A new feature, making something new possible

Comments

@Sebbl0508
Copy link

What problem does this solve or what need does it fill?

Since MouseMotion (afaik) intentionally doesn't take mouse acceleration or (maybe also?) scaling into account, one would have to use CursorMoved to stay "up-to-date" to the actual cursor position(s).
Unfortunately the CursorMoved event doesn't contain deltas. That means if one needs that, they would need to implement that themselves in their code.

What solution would you like?

If possible and/or wanted delta to be added to the CursorMoved event.

What alternative(s) have you considered?

Implementing the delta calculation in my own game code or trying to solve my problem without using deltas.

Additional context

If this is nothing against this addition, i'd like to try implementing this myself. I haven't gone into the bevy internals so idk how possible this is, but i'd like to try :)

For additionaly context why i want this change: Added is a video of my WIP app, where i'm trying to implement a mouse-dragging camera controller. At the moment i'm using the deltas from MouseMotion, which causes issues since apparently on my laptop there's some kind of mouse acceleration or something.

2024-02-04.14-08-08.mp4
@Sebbl0508 Sebbl0508 added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Feb 4, 2024
@Sebbl0508
Copy link
Author

For the moment, this is the approach i took in my own game code:

Code
use bevy::prelude::*;

pub struct CursorMovedDeltaPlugin;

impl Plugin for CursorMovedDeltaPlugin {
    fn build(&self, app: &mut App) {
        app.add_event::<CursorMovedWithDelta>()
            .init_resource::<LastCursorPosition>()
            .add_systems(PreUpdate, translate_cursor_moved_events);
    }
}

#[derive(Event, Debug)]
pub struct CursorMovedWithDelta {
    pub window: Entity,
    pub position: Vec2,
    pub delta: Vec2,
}

/// `None` if not set yet
#[derive(Resource, Debug, Default)]
pub struct LastCursorPosition(Option<Vec2>);

fn translate_cursor_moved_events(
    mut last_cursor_position: ResMut<LastCursorPosition>,
    mut cursor_motion: EventReader<CursorMoved>,
    mut cursor_motion_with_delta: EventWriter<CursorMovedWithDelta>,
) {
    for evt in cursor_motion.read() {
        if let Some(last_cursor_position) = last_cursor_position.0 {
            let delta = evt.position - last_cursor_position;
            cursor_motion_with_delta.send(CursorMovedWithDelta {
                window: evt.window,
                position: evt.position,
                delta,
            });
        }

        last_cursor_position.0 = Some(evt.position);
    }
}

@Kanabenki Kanabenki added A-Input Player input via keyboard, mouse, gamepad, and more and removed S-Needs-Triage This issue needs to be labelled labels Feb 4, 2024
@lynn-lumen
Copy link
Contributor

I agree that this might be useful, but since CursorMoved is a translated version of the winit::CursorMoved struct which does not include delta, I don't know if this is the place to add it.

@Sebbl0508
Copy link
Author

I agree that this might be useful, but since CursorMoved is a translated version of the winit::CursorMoved struct which does not include delta, I don't know if this is the place to add it.

That's what i thought too.
Nice PR btw. Thanks a lot. Hope it gets merged!

github-merge-queue bot pushed a commit that referenced this issue Feb 12, 2024
# Objective

- Fixes #11695

## Solution

- Added `delta: Option<Vec2>` to `bevy_window::CursorMoved`. `delta` is
an `Option` because the `CursorMoved` event does get fired when the
cursor was outside the window area in the last frame. In that case there
is no cursor position from the last frame to compare with the current
cursor position.

---

## Changelog

- Added `delta: Option<Vec2>` to `bevy_window::CursorMoved`. 

## Migration Guide

- You need to add `delta` to any manually created `CursorMoved` struct.

---------

Co-authored-by: Kanabenki <lucien.menassol@gmail.com>
Co-authored-by: James Liu <contact@jamessliu.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Input Player input via keyboard, mouse, gamepad, and more C-Feature A new feature, making something new possible
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants