You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.3 KiB
Rust

use bevy::{input::mouse::MouseWheel, prelude::*};
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "UI WTF".into(),
resolution: (640., 480.).into(),
..default()
}),
..default()
}))
// Only run when window is active to reduce cpu cycles
// .insert_resource(WinitSettings::desktop_app())
.add_startup_system(init)
.add_system(scroll)
.run();
}
#[derive(Component)]
struct ScrollingList;
fn init(mut commands: Commands) {
info!("Spawning camera");
commands.spawn(Camera2dBundle { ..default() });
info!("Initializing UI");
commands
.spawn(NodeBundle {
background_color: BackgroundColor(Color::WHITE),
style: Style {
justify_content: JustifyContent::Center,
size: Size::all(Val::Percent(90.0)),
overflow: Overflow::Hidden,
..default()
},
..default()
})
.with_children(|parent| {
parent
.spawn((
NodeBundle {
background_color: BackgroundColor(Color::OLIVE),
style: Style {
flex_wrap: FlexWrap::Wrap,
flex_direction: FlexDirection::Row,
justify_content: JustifyContent::SpaceAround,
size: Size::AUTO,
max_size: Size::UNDEFINED,
..default()
},
..default()
},
ScrollingList,
))
.with_children(|parent| {
info!("Initializing Child Element");
let colors = [
Color::ORANGE,
Color::BLUE,
Color::GREEN,
Color::SALMON,
Color::SEA_GREEN,
Color::MAROON,
Color::ORANGE,
Color::BLUE,
Color::GREEN,
Color::SALMON,
Color::SEA_GREEN,
Color::MAROON,
];
for i in 0..12 {
parent.spawn(NodeBundle {
background_color: BackgroundColor(colors[i]),
style: Style {
size: Size::all(Val::Px(256.0)),
padding: UiRect::all(Val::Px(5.0)),
..default()
},
..default()
});
}
});
});
}
fn scroll(
mut scroll_evr: EventReader<MouseWheel>,
mut query: Query<&mut Style, With<ScrollingList>>,
) {
for ev in scroll_evr.iter() {
for mut s in query.iter_mut() {
s.position.top = match s.position.top {
Val::Px(current) => Val::Px(current + (ev.y * 5.0)),
_ => Val::Px(0.0),
};
}
}
}