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 scroll functionality to bevy_picking #17704

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

Conversation

colepoirier
Copy link
Contributor

Objective

bevy_picking currently does 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
@@ -750,6 +765,28 @@ pub fn pointer_events(
event_writers.move_events.send(move_event);
}
}
PointerAction::Scroll { x, y, unit } => {
// If it's a press, emit a Pressed event and mark the hovered entities as pressed
Copy link
Member

Choose a reason for hiding this comment

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

this comment looks like a copy paste error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed that and added an appropriate comment mirroring the other events' implementations lower down in that code block.

@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
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
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.

@@ -285,6 +286,19 @@ pub struct DragEntry {
pub latest_pos: Vec2,
}

/// Fires while a pointer is scrolling over the `target` entity.
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
/// Fires while a pointer is scrolling over the `target` entity.
/// Fires while a pointer is scrolling over the `target` entity.

The way this is written seems a bit odd, does a pointer scroll? Maybe I'm being fussy, I'm not sure.

Copy link
Member

Choose a reason for hiding this comment

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

This is just mirroring the language used in the other event docs. I think it's a valid description. The pointing device sends the scroll, but it is definitely tied to the pointer. That is, pointer scrolling events depend on where the pointer is located w.r.t. picking, it's not just an input.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does that mean we should leave it as is?

Copy link
Contributor

@ickshonpe ickshonpe Feb 7, 2025

Choose a reason for hiding this comment

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

Yep it's fine, aervrie's explanation makes sense.

@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
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
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-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