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 a simple directional UI navigation example #17224

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

Conversation

alice-i-cecile
Copy link
Member

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

Objective

Gamepad / directional navigation needs an example, for both teaching and testing purposes.

Solution

  • Add a simple grid-based example.
  • Fix an intermittent panic caused by a race condition with bevy_a11y
  • Clean up small issues noticed in bevy_input_focus

image

To do: this PR

  • figure out why "enter" isn't doing anything
  • change button color on interaction rather than printing
  • add on-screen directions
  • move to an asymmetric grid to catch bugs
  • fix colors not resetting on button press lol this is mostly just a problem with hacking Interaction for this
  • swap to using observers + bubbling, rather than Interaction

To do: future work

  • when I increase the button size, such that there is no line break, the text on the buttons is no longer centered :( EDIT: this is Text2d and TextSpan is not center-aligned when JustifyText::Center is specified. #16783
  • add gamepad stick navigation
  • add tools to find the nearest populated quadrant to make diagonal inputs work
  • add a add_edges method to DirectionalNavigationMap
  • add a add_grid method to DirectionalNavigationMap
  • make the example's layout more complex and realistic
  • add tools to automatically generate this list
  • add button shake on failed navigation rather than printing an error
  • make Pressed events easier to mock: default fields, PointerId::Focus

Testing

cargo run --example directional_navigation

thread 'main' panicked at C:\Users\alice\.cargo\registry\src\index.crates.io-Panic was: 6f17d22bba15001f\accesskit_consumer-0.26.0\src\tree.rs:34:13:
Focused id #4294967304 is not in the node list
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior C-Examples An addition or correction to our examples A-Input Player input via keyboard, mouse, gamepad, and more A-Accessibility A problem that prevents users with disabilities from using Bevy A-UI Graphical user interfaces, styles, layouts, and widgets labels Jan 7, 2025
@rparrett rparrett self-requested a review January 7, 2025 22:51
Copy link
Contributor

github-actions bot commented Jan 7, 2025

The generated examples/README.md is out of sync with the example metadata in Cargo.toml or the example readme template. Please run cargo run -p build-templated-pages -- update examples to update it, and commit the file change.

cargo run -p build-templated-pages -- update examples
@alice-i-cecile alice-i-cecile added this to the 0.16 milestone Jan 7, 2025
@alice-i-cecile alice-i-cecile added the S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged label Jan 7, 2025
@rparrett
Copy link
Contributor

rparrett commented Jan 8, 2025

Sent a couple tiny PRs your way

@alice-i-cecile alice-i-cecile changed the title Add a simple directional navigation example Add a simple directional UI navigation example Jan 8, 2025
@alice-i-cecile alice-i-cecile added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Jan 8, 2025
@alice-i-cecile alice-i-cecile marked this pull request as ready for review January 8, 2025 21:49
}
}

// We're spawning a simple 3x3 grid of buttons
Copy link
Contributor

@rparrett rparrett Jan 8, 2025

Choose a reason for hiding this comment

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

Suggested change
// We're spawning a simple 3x3 grid of buttons
// We're spawning a simple 3x5 grid of buttons

Comment on lines 142 to 143
// Allow the grid to take up the full height and width of the window
width: Val::Percent(60.),
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO

  • Make instructions node wider, maybe Px(300.0)
  • Leave this at 100.0.
  • Adjust this comment to say "take up the full height and the rest of the width."

Seems okay for the grid to not be centered in the screen to me.

examples/ui/directional_navigation.rs Outdated Show resolved Hide resolved
// to capture the actual intent of the user
fn universal_button_click_behavior(
// We're using a on-mouse-down trigger to improve responsiveness;
// Clicked is better when you want roll-off cancellation
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be nicer to have separate effects that get triggered on Pressed and Clicked rather than explain why it looks like we're doing the "wrong" thing.

But that could be a future improvement.

width: Val::Px(200.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
margin: UiRect::all(Val::Px(16.0)),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
margin: UiRect::all(Val::Px(16.0)),
margin: UiRect::all(Val::Px(12.0)),

For consistency with other examples with top-left instructions

@bas-ie
Copy link
Contributor

bas-ie commented Jan 8, 2025

Checked and working with an 8Bitdo Ultimate on Linux. D-pad and left stick both navigate, I presume because of whatever mode the controller is in. B and A both trigger buttons.

@rparrett
Copy link
Contributor

rparrett commented Jan 9, 2025

Would you mind rewriting that last commit message without the tag?

Approving, but continuing to nitpick to clarify the reasoning for a previous review.

image

I think that either

  • The grid node should be width: 100% so that it takes up the rest of the space and the buttons are centered within the non-instruction space
  • The instructions node should given a percentage width of (100% - grid%) / 2 so that things are centered
  • The grid node should be width: 60% or whatever, but the text node absolutely positioned.

My eyes are just drawn to the "implicit green box" in the layout and it looks off-center.

@alice-i-cecile alice-i-cecile force-pushed the directional-navigation-example branch from b00e303 to e5d7cda Compare January 9, 2025 02:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Accessibility A problem that prevents users with disabilities from using Bevy A-Input Player input via keyboard, mouse, gamepad, and more A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior C-Examples An addition or correction to our examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants