Starting on ui example
parent
9643ff94f4
commit
c349ce08d0
@ -0,0 +1,53 @@
|
||||
//! 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<Pointer<Scroll>>,
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
[toolchain]
|
||||
channel = "nightly-2025-06-16"
|
||||
channel = "nightly-2025-07-16"
|
||||
components = [ "rustfmt", "rustc-dev", "cargo", "clippy", "rust-analyzer", "rustc-codegen-cranelift" ]
|
||||
targets = [ "x86_64-unknown-linux-gnu", "wasm32-unknown-unknown" ]
|
||||
|
||||
Loading…
Reference in New Issue