//! This example illustrates scrolling in Bevy UI. use games::*; fn main() { let mut app = App::new(); app.add_plugins(BaseGamePlugin::default()) .add_systems(Startup, setup); app.run(); } const LINE_HEIGHT: f32 = 21.; fn setup(mut commands: Commands) { // Scrolling list commands .spawn(( Node { flex_direction: FlexDirection::Column, // If height is not set, we need both align_self: Stetch and overflow: scroll() height: Val::Percent(50.0), // align_self: AlignSelf::Stretch, overflow: Overflow::scroll(), ..default() }, BackgroundColor(Color::srgb(0.10, 0.10, 0.10)), )) .with_children(|parent| { // List items (0..250).for_each(|i| { parent.spawn(Text(format!("Item {i}"))); }); }) .observe(scroll); } /// Updates the scroll position of scrollable nodes in response to mouse input pub fn scroll( trigger: Trigger>, mut scrollers: Query<&mut ScrollPosition>, ) { let Pointer { event: Scroll { unit, x, y, .. }, .. } = trigger.event(); let (dx, dy) = match unit { MouseScrollUnit::Line => (x * LINE_HEIGHT, y * LINE_HEIGHT), MouseScrollUnit::Pixel => (x * 1., y * 1.), }; if let Ok(mut pos) = scrollers.get_mut(trigger.target()) { pos.offset_x -= dx; pos.offset_y -= dy; } }