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

Fix check that cursor position is within window bounds #9662

Merged
merged 2 commits into from
Sep 3, 2023
Merged

Fix check that cursor position is within window bounds #9662

merged 2 commits into from
Sep 3, 2023

Conversation

mdickopp
Copy link
Contributor

@mdickopp mdickopp commented Aug 31, 2023

Objective

The recently introduced check that the cursor position returned by Window::cursor_position() is within the bounds of the window (3cf94e7) has the following issue:

If w is the window width, points within the window satisfy the condition 0 ≤ x < w, but the code assumes the condition 0 ≤ xw. In other words, if x = w, the point is not within the window bounds. Likewise for the height. This program demonstrates the issue:

use bevy::{prelude::*, window::WindowResolution};

fn main() {
    let mut window = Window {
        resolution: WindowResolution::new(100.0, 100.0),
        ..default()
    };

    window.set_cursor_position(Some(Vec2::new(100.0, 0.0)));

    println!("{:?}", window.cursor_position());
}

It prints Some(Vec2(100.0, 0.0)) instead of the expected None.

Solution

  • Exclude the upper bound, i.e., the window width for the x position and the window height for the y position.

@rparrett
Copy link
Contributor

rparrett commented Aug 31, 2023

A fix (for the first issue you mentioned) was just merged: #9657

@mdickopp
Copy link
Contributor Author

Should I open a new PR for the second issue (and close this one)?

@rparrett
Copy link
Contributor

Feel free to modify this PR. Just making sure you know that the code just changed underneath you.

@mdickopp
Copy link
Contributor Author

I modified the commit and the PR description accordingly.

@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-Input Player input via keyboard, mouse, gamepad, and more A-Windowing Platform-agnostic interface layer to run your app in labels Aug 31, 2023
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like a quick regression test (or a handful of tests) for this :)

Good fix though.

@mdickopp
Copy link
Contributor Author

mdickopp commented Sep 1, 2023

Tests added.

@alice-i-cecile alice-i-cecile added this to the 0.12 milestone Sep 1, 2023
Copy link
Contributor

@NoahShomette NoahShomette left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Sep 3, 2023
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Sep 3, 2023
Merged via the queue into bevyengine:main with commit a166b65 Sep 3, 2023
22 checks passed
rdrpenguin04 pushed a commit to rdrpenguin04/bevy that referenced this pull request Jan 9, 2024
# Objective

The recently introduced check that the cursor position returned by
`Window::cursor_position()` is within the bounds of the window
(bevyengine@3cf94e7)
has the following issue:

If *w* is the window width, points within the window satisfy the
condition 0 ≤ *x* < *w*, but the code assumes the condition 0 ≤ *x* ≤
*w*. In other words, if *x* = *w*, the point is not within the window
bounds. Likewise for the height. This program demonstrates the issue:

```rust
use bevy::{prelude::*, window::WindowResolution};

fn main() {
    let mut window = Window {
        resolution: WindowResolution::new(100.0, 100.0),
        ..default()
    };

    window.set_cursor_position(Some(Vec2::new(100.0, 0.0)));

    println!("{:?}", window.cursor_position());
}
```

It prints `Some(Vec2(100.0, 0.0))` instead of the expected `None`.

## Solution

- Exclude the upper bound, i.e., the window width for the *x* position
and the window height for the *y* position.
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 A-Windowing Platform-agnostic interface layer to run your app in C-Bug An unexpected or incorrect behavior S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants