-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
base: main
Are you sure you want to change the base?
Conversation
crates/bevy_picking/src/events.rs
Outdated
@@ -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 |
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.
this comment looks like a copy paste error
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.
I removed that and added an appropriate comment mirroring the other events' implementations lower down in that code block.
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.
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.
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.
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. |
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.
/// 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.
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.
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.
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.
Does that mean we should leave it as is?
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.
Yep it's fine, aervrie's explanation makes sense.
Done! |
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
I haven't tested these changes, if the reviewers can advise me how to do so I'd appreciate it!