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

MouseButton and KeyBoard events should contain WindowID #4649

Closed
coderedart opened this issue May 3, 2022 · 2 comments · Fixed by #8852
Closed

MouseButton and KeyBoard events should contain WindowID #4649

coderedart opened this issue May 3, 2022 · 2 comments · Fixed by #8852
Labels
A-Input Player input via keyboard, mouse, gamepad, and more C-Usability A targeted quality-of-life change that makes Bevy easier to use
Milestone

Comments

@coderedart
Copy link

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

if i have Primary and Secondary windows for my app/game, and lets say Primary window is in focus. if i click on Secondary window, we will get a CursorLeft / FocusLost for Primary window, CursorEntered / FocusGain for Secondary Window. and in the global event reader for MouseButtonInput, we will get a Press/Release events due to the click. The issue is that I have no way of knowing which window the click belongs to?

The usecase is integration of bevy_egui mvlabat/bevy_egui#61 .

a simple alternative use case would be PrimaryWindow containing a 3d game player, and W pressed on primary window might mean to move player forward. but on Secondary Window, we might have a Map or something, and pressing W might mean to move the map center towards south so that we can browser at the further north part of the map. W pressed event means different things for different windows.

What solution would you like?

The easiest would be to have WindowID attached to the MouseButtonInput and Keyboard events just like CursorEntered/Left or Focus events. winit provides that. glfw provides that. sdl2 provides that. so, its the easiest workaround at the moment to just let people deal with manual events. but to go above and beyond, bevy can provide alternative functions like https://docs.rs/bevy/latest/bevy/input/struct.Input.html#method.pressed which take the windowID as another argument to query the input state of Secondary windows, rather than just the primary or global keyboard state.

What alternative(s) have you considered?

timestamps

glfw and sdl2 provide timestamps for events which can be used to order the events and check whether the click happened after the cursor Left or before it. then, based on that whether a specific click should be sent to secondary or primary window. well, there might be a little more work to do like checking if there's a release before press event, in which case the release might belong to the previous window's press etc... but idk what you would do with winit as it doesn't provide timestamps.

ignore corner cases

during the frame that focus shifts from one window to another, just consider ALL clicks or other events to belong to the second window. it will lead to some corner case bugs, but time is scarce resource and this might not be worth the effort.

Additional context

There's two problems mentioned here:

  1. not knowing which window a particular event belongs to in a certain frame (especially when frame times are high due to performance bottlenecks). this is what i want.
  2. not knowing whether the W key is pressed in primary or other windows, as the input state is currently global to all windows from what i see. I do not care about this, but it is kinda related, so might as well mention its usecase of having a 3d world and map on different windows.
@coderedart coderedart added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels May 3, 2022
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Trivial Nice and easy! A great choice to get started with Bevy A-Input Player input via keyboard, mouse, gamepad, and more and removed C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled A-ECS Entities, components, systems, and events labels May 3, 2022
@coderedart
Copy link
Author

coderedart commented May 3, 2022

an alternative way to get the latest position of the cursor is https://docs.rs/bevy/latest/bevy/window/struct.Window.html#method.update_cursor_physical_position_from_backend

though we can check the latest position of cursor per window, if the click happened in the primary window, but the cursor left the window in the same frame, the latest position would be reset to None (atleast in winit backend), so the click is still ambiguous.

again, these kinds of things only happen when there's multiple events within a single frame (or game loop iteration? idk the word). they are rare when we have high fps, but more often when our fps drops.

I almost thought alice was a bot with how fast the issue got tagged with labels / badges :D

@alice-i-cecile
Copy link
Member

I almost thought alice was a bot with how fast the issue got tagged with labels / badges :D

That's what the #github channel is for on Discord :)

I like this analysis (and it's a desirable feature). We could actually use this feature to mirror the GamepadButton API, and solve #3224 by making the mouse and keyboard APIs more complex to match 🤔

@alice-i-cecile alice-i-cecile removed the D-Trivial Nice and easy! A great choice to get started with Bevy label May 3, 2022
@alice-i-cecile alice-i-cecile added this to the 0.11 milestone Feb 27, 2023
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-Usability A targeted quality-of-life change that makes Bevy easier to use
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants