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.
102 lines
3.2 KiB
Rust
102 lines
3.2 KiB
Rust
//! 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_list, setup_nav_tree))
|
|
.add_systems(Update, run_tree.run_if(any_component_changed::<NavTree>));
|
|
app.run();
|
|
}
|
|
|
|
fn setup_list(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(RED.into()),
|
|
))
|
|
.with_children(|parent| {
|
|
// List items
|
|
(0..250).for_each(|i| {
|
|
parent.spawn(Text(format!("Item {i}")));
|
|
});
|
|
})
|
|
.observe(scroll);
|
|
}
|
|
|
|
fn setup_nav_tree(mut commands: Commands) {
|
|
// Nav Tree
|
|
commands
|
|
.spawn((
|
|
Node {
|
|
position_type: PositionType::Absolute,
|
|
right: Val::Px(0.0),
|
|
width: Val::Px(100.0),
|
|
height: Val::Px(100.0),
|
|
..default()
|
|
},
|
|
BackgroundColor(RED.into()),
|
|
NavTree::Closed,
|
|
))
|
|
.with_children(|parent| {
|
|
parent
|
|
.spawn((
|
|
Node {
|
|
position_type: PositionType::Absolute,
|
|
right: Val::Px(100.0),
|
|
width: Val::Px(100.0),
|
|
height: Val::Px(100.0),
|
|
..default()
|
|
},
|
|
BackgroundColor(ORANGE.into()),
|
|
NavTree::Closed,
|
|
))
|
|
.with_children(|parent| {
|
|
parent.spawn((
|
|
Node {
|
|
position_type: PositionType::Absolute,
|
|
right: Val::Px(100.0),
|
|
width: Val::Px(100.0),
|
|
height: Val::Px(100.0),
|
|
..default()
|
|
},
|
|
BackgroundColor(YELLOW.into()),
|
|
NavTree::Closed,
|
|
));
|
|
});
|
|
});
|
|
}
|
|
|
|
fn run_tree(
|
|
q: Query<(Entity, &NavTree), Changed<NavTree>>,
|
|
children: Query<&Children>,
|
|
mut query: Query<(&ChildOf, &mut Visibility)>,
|
|
) {
|
|
q.iter().for_each(|(e, nav)| {
|
|
// todo!("Clean this code up please!");
|
|
match nav {
|
|
NavTree::Open => children.iter_descendants(e).for_each(|child| {
|
|
let (co, mut v) = query.get_mut(child).unwrap();
|
|
if ChildOf(e) == *co {
|
|
*v = Visibility::Inherited;
|
|
}
|
|
}),
|
|
NavTree::Closed => children.iter_descendants(e).for_each(|child| {
|
|
let (co, mut v) = query.get_mut(child).unwrap();
|
|
if ChildOf(e) == *co {
|
|
*v = Visibility::Hidden;
|
|
}
|
|
}),
|
|
}
|
|
});
|
|
}
|