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 relative position reporting to UI picking #17681

Merged
merged 5 commits into from
Feb 11, 2025

Conversation

bytemunch
Copy link
Contributor

Objective

Add position reporting to HitData sent from the UI picking backend.

Solution

Add the computed normalized relative cursor position to hit_data alongside the Entity.

The position reported in HitData is normalized relative to the node, with (0.,0.,0.) at the top left and (1., 1., 0.) in the bottom right. Coordinates are relative to the entire node, not just the visible region.

HitData needs a Vec3 so I just extended with 0.0. I considered inserting the depth here but thought it would be redundant.

I also considered putting the screen space position in the normal field of HitData, but that would require renaming of the field or a separate data structure.

Testing

Tested with mouse on X11 with entities that have Node components.


Showcase

// Get click position relative to node
fn hit_position(trigger: Trigger<Pointer<Click>>) {
    let hit_pos = trigger.event.hit.position.expect("no position");
    info!("{}", hit_pos);
}

Copy link
Contributor

github-actions bot commented Feb 4, 2025

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@ickshonpe ickshonpe added C-Feature A new feature, making something new possible A-UI Graphical user interfaces, styles, layouts, and widgets D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward A-Picking Pointing at and selecting objects of all sorts labels Feb 4, 2025
Copy link
Contributor

@ickshonpe ickshonpe left a comment

Choose a reason for hiding this comment

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

Works fine, added

                .observe(|trigger: Trigger<Pointer<Click>>| {
                    let hit_pos = trigger.event.hit.position.expect("no position");
                    info!("{}", hit_pos);
                });

to the interactable node in the relative_cursor_position example and the position reported by info! matches the position reported by the example's position text.

I think not repurposing the normal field is a good decision as it's potentially confusing.

@bytemunch bytemunch mentioned this pull request Feb 10, 2025
@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 10, 2025
@alice-i-cecile
Copy link
Member

@bytemunch I think I didn't resolve the merge conflicts quite correctly. Can you take a quick look and ping me once CI is passing again?

@bytemunch
Copy link
Contributor Author

bytemunch commented Feb 11, 2025

No worries, I think it was a change in how UI nodes now have target cameras, doing tests now letting github do tests, I need to learn more about local CI runs >.<

auto-merge was automatically disabled February 11, 2025 10:50

Head branch was pushed to by a user without write access

@bytemunch
Copy link
Contributor Author

@alice-i-cecile CI ping :)

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Feb 11, 2025
@alice-i-cecile
Copy link
Member

cargo run -p ci in the future :)

Merged via the queue into bevyengine:main with commit 5eff6e8 Feb 11, 2025
29 checks passed
@notmd
Copy link
Contributor

notmd commented Feb 12, 2025

This seems really confusing with docs The position of the intersection in the world, if the data is available from the backend.. In the web, this is the mouse location https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/x

@bytemunch
Copy link
Contributor Author

Good point, I overlooked that! Perhaps docs could say something along the lines of The position reported by the backend, if the data is available., as giving mouse coords a la web would still be in screen space rather than world space. Maybe a mention of different spaces in docs would be useful too?

I guess I was thinking solely of my own use-case for having position data in relative local space, and knowing that the pointer position is accessible elsewhere in an observer trigger opted for this space.

@alice-i-cecile
Copy link
Member

Good point, I overlooked that! Perhaps docs could say something along the lines of The position reported by the backend, if the data is available., as giving mouse coords a la web would still be in screen space rather than world space. Maybe a mention of different spaces in docs would be useful too?

This would be ideal :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Picking Pointing at and selecting objects of all sorts A-UI Graphical user interfaces, styles, layouts, and widgets C-Feature A new feature, making something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples 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