Skip to content

Add scroll functionality to bevy_picking#17704

Merged
alice-i-cecile merged 6 commits intobevyengine:mainfrom
colepoirier:picking_add_scroll
Feb 10, 2025
Merged

Add scroll functionality to bevy_picking#17704
alice-i-cecile merged 6 commits intobevyengine:mainfrom
colepoirier:picking_add_scroll

Conversation

@colepoirier
Copy link
Copy Markdown
Contributor

@colepoirier colepoirier commented Feb 6, 2025

Objective

bevy_picking currently does not support scroll events.

Solution

This pr adds a new event type for scroll, and updates the default input system for mouse pointers to read and emit this event.

Testing

  • Did you test these changes? If so, how?
  • Are there any parts that need more testing?
  • How can other people (reviewers) test your changes? Is there anything specific they need to know?
  • If relevant, what platforms did you test these changes on, and are there any important ones you can't test?

I haven't tested these changes, if the reviewers can advise me how to do so I'd appreciate it!

@colepoirier colepoirier self-assigned this Feb 6, 2025
@ickshonpe ickshonpe added A-Picking Pointing at and selecting objects of all sorts S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 6, 2025
Copy link
Copy Markdown
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.

You need to add

 .add_event::<Pointer<Scroll>>()

to InteractionPlugin's Plugin impl, otherwise you get a panic because it can't find the scroll event resource.

Copy link
Copy Markdown
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.

With the event registered, there don't seem to be any other problems.

To test the changes, I removed the update_scroll_position from the testbed_ui example (in examples/testbed/ui.rs) and added an observer to the scrollable node:

// Scrolling list
parent
    .spawn((
        Node {
            flex_direction: FlexDirection::Column,
            align_self: AlignSelf::Stretch,
            height: Val::Percent(50.),
            overflow: Overflow::scroll_y(),
            ..default()
        },
        BackgroundColor(Color::srgb(0.10, 0.10, 0.10)),
    ))
    .observe(|
        trigger: Trigger<Pointer<Scroll>>,
        mut scroll_position_query: Query<&mut ScrollPosition>,
    | {
        if let Ok(mut scroll_position) = scroll_position_query.get_mut(trigger.target) {
            let (dx, dy) = match trigger.unit {
                MouseScrollUnit::Line => (trigger.x * 20., trigger.y * 20.),
                MouseScrollUnit::Pixel => (trigger.x, trigger.y),
            };
    
            scroll_position.offset_x -= dx;
            scroll_position.offset_y -= dy;
        }
    })
    .with_children(|parent| {
        // List items
        for i in 0..25 {
            parent
                .spawn((
                    Text(format!("Item {i}")),
                    TextFont {
                        font: asset_server.load("fonts/FiraSans-Bold.ttf"),
                        ..default()
                    },
                    Label,
                    AccessibilityNode(Accessible::new(Role::ListItem)),
                ))
                .insert(Pickable {
                    should_block_lower: false,
                    ..default()
                });
        }
    });

scrolling working as before.

@ickshonpe ickshonpe added C-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 6, 2025
@colepoirier
Copy link
Copy Markdown
Contributor Author

You need to add

 .add_event::<Pointer<Scroll>>()

to InteractionPlugin's Plugin impl, otherwise you get a panic because it can't find the scroll event resource.

Done!

@ickshonpe ickshonpe 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 Feb 7, 2025
Copy link
Copy Markdown
Member

@aevyrie aevyrie left a comment

Choose a reason for hiding this comment

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

Nice work, thank you!

@ickshonpe ickshonpe 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 8, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Feb 10, 2025
Merged via the queue into bevyengine:main with commit 8435951 Feb 10, 2025
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 C-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes 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