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

Make mocking picking events for bevy_ui easier #17399

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

alice-i-cecile
Copy link
Member

@alice-i-cecile alice-i-cecile commented Jan 16, 2025

Objective

As discussed in #17267, we'd like to unify input-focus driven widget activations (e.g. press enter to click the focused button) with mouse-driven picking interactions by going through the bevy_picking infrastructure.

While you can fake this with observer triggers, it is:

  • inaccurate: you're feeding in garbage information
  • extremely verbose
  • rather arcane

Solution

As @aevyrie points out, we have access to everything we need to send realistic pointer events! Doing this correctly requires rather a lot of information. To make users lives easier, I've introduced a system param with all of the required information, which simulates pointer events at the center of the UI node specified.

The directional_navigation example has been updated to demonstrate / test this change.

I've also removed the Component impl for bevy_picking::Location as this is unused and a serious footgun: I wasted quite a bit of time trying to debug what had gone wrong. See #17390.

Discussion

  • Some small improvements to this general area of the code have been split out into Small improvements for directional navigation #17395. They'd be nice here, but I wanted to reduce complexity here.
  • While PointerId::Custom exists, it is challenging to both generate a UUID and to match against it if you want to respond differently if the Pointer event is from a focused element. While I could have used sentinel values here, I think it's more idiomatic and nicer to use if we just add an enum arm for a common use.
  • We should probably rename bevy_ui/picking_backend.rs, but I've opted to defer that to make the diff easier to read.
  • This was originally a custom command, but has been refactored to a SystemParam for flexibility, easier error handling and performance.
  • As follow-up, we could add some simple helper methods to the SystemParam to make certain actions even easier.
  • In theory, other backends could do the same thing! The logic required is slightly specialized, so I stuck to just UI for now.

Testing

I've tested this with the directional_navigation example. While I hope that this works properly with more exotic windowing / rendering target setups (viewports, portals, multiple windows), we're not equipped to do that effectively right now.

Migration Guide

bevy_picking::Location is no longer a component. This was a bug: you wanted PickingLocation.

PointerId has a new variant, PointerId::Focus that will need to be exhaustively matched against.

@alice-i-cecile alice-i-cecile added A-Input Player input via keyboard, mouse, gamepad, and more A-UI Graphical user interfaces, styles, layouts, and widgets C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward A-Picking Pointing at and selecting objects of all sorts labels Jan 16, 2025
@alice-i-cecile alice-i-cecile added this to the 0.16 milestone Jan 16, 2025
@viridia
Copy link
Contributor

viridia commented Jan 17, 2025

A concern I have is that the motivation, use case, and overall theory of operation won't be apparent to people who are looking at the docs for Pointer. I would like to see a more detailed note, and possibly link to the emulated pointer documentation.

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-Picking Pointing at and selecting objects of all sorts A-UI Graphical user interfaces, styles, layouts, and widgets C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants