-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Change focused widget with arrow keys #3272
Change focused widget with arrow keys #3272
Conversation
737f617
to
866ba78
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exciting!
The navigation works okish, but I left some ideas on how to improve it
examples/hello_world/src/main.rs
Outdated
name2: String, | ||
age: u32, | ||
age2: u32, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please revert the changes in this file. Use egui_demo_app
(cargo r
) for a better test of your code!
Thanks a lot for the feedback! I updated the selection metric to use the ratio and rectangles but it is still not perfect.
Not sorting by dot product afterward results in close-by widgets being picked even while they have a big angle. But sorting by dot product sometimes results in widgets further away but higher dot products being selected. I wonder if other context needs to be brought in to make a better decision. I don't know how perfect this feature is to be. It mostly works well. For our use case, we probably design the UI to be simple so that this algorithm works fine. Another issue is that sometimes it's hard to tell where the focus went as it might be a window behind the current window or some widget in a different panel. The rectangle thing improves the overall behavior so that was a nice change. |
// The rect is updated at the end of the frame. | ||
self.focus_widgets_cache | ||
.entry(id) | ||
.or_insert(Rect::EVERYTHING); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, no that makes sense 👍
There is still plenty of room for improvement, but this is a good start! |
|
Gotta make some custom logic to achieve that. E.g detect the cardinal directions of the joystick and map them to key combo's |
This feature was introduced in emilk/egui#3272. Since malakal implements its own keyboard navigation it conflicts with egui's built-in arrow navigation and causes the focus to jump around unexpectedly.
We have the use case of gamepad control for a user-facing game menu. This PR makes it possible to change widget focus with the keyboard arrows. It allows integrations to convert joysticks to arrow keys in order to toggle between menu items. It does not add support for gamepad events in egui as this seems like a bigger topic that I desire not to touch.
Initially, I examine the currently focused widget to determine the one with the highest dot product when compared to the desired focus direction. Subsequently, I identify the widget that is nearest to the current one. This process enables the selection of the widget in the desired direction which not only possesses the smallest angle relative to the current widget but also maintains the closest proximity.
The widget selection code can be improved over time ofcourse, there are some potential issues right now:
Focus::interested_in_focus
is not called and thus I do not know what the interactable widgets and their rectangles are. I only know this after using a key like arrow or tab (+shift).Closes #1250