|
|
|
@ -5,13 +5,12 @@ use games::*;
|
|
|
|
fn main() {
|
|
|
|
fn main() {
|
|
|
|
let mut app = App::new();
|
|
|
|
let mut app = App::new();
|
|
|
|
app.add_plugins(BaseGamePlugin::default())
|
|
|
|
app.add_plugins(BaseGamePlugin::default())
|
|
|
|
.add_systems(Startup, setup);
|
|
|
|
.add_systems(Startup, (setup_list, setup_nav_tree))
|
|
|
|
|
|
|
|
.add_systems(Update, run_tree.run_if(any_component_changed::<NavTree>));
|
|
|
|
app.run();
|
|
|
|
app.run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const LINE_HEIGHT: f32 = 21.;
|
|
|
|
fn setup_list(mut commands: Commands) {
|
|
|
|
|
|
|
|
|
|
|
|
fn setup(mut commands: Commands) {
|
|
|
|
|
|
|
|
// Scrolling list
|
|
|
|
// Scrolling list
|
|
|
|
commands
|
|
|
|
commands
|
|
|
|
.spawn((
|
|
|
|
.spawn((
|
|
|
|
@ -23,7 +22,7 @@ fn setup(mut commands: Commands) {
|
|
|
|
overflow: Overflow::scroll(),
|
|
|
|
overflow: Overflow::scroll(),
|
|
|
|
..default()
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
BackgroundColor(Color::srgb(0.10, 0.10, 0.10)),
|
|
|
|
BackgroundColor(RED.into()),
|
|
|
|
))
|
|
|
|
))
|
|
|
|
.with_children(|parent| {
|
|
|
|
.with_children(|parent| {
|
|
|
|
// List items
|
|
|
|
// List items
|
|
|
|
@ -34,20 +33,69 @@ fn setup(mut commands: Commands) {
|
|
|
|
.observe(scroll);
|
|
|
|
.observe(scroll);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Updates the scroll position of scrollable nodes in response to mouse input
|
|
|
|
fn setup_nav_tree(mut commands: Commands) {
|
|
|
|
pub fn scroll(
|
|
|
|
// Nav Tree
|
|
|
|
trigger: Trigger<Pointer<Scroll>>,
|
|
|
|
commands
|
|
|
|
mut scrollers: Query<&mut ScrollPosition>,
|
|
|
|
.spawn((
|
|
|
|
) {
|
|
|
|
Node {
|
|
|
|
let Pointer { event: Scroll { unit, x, y, .. }, .. } = trigger.event();
|
|
|
|
position_type: PositionType::Absolute,
|
|
|
|
|
|
|
|
right: Val::Px(0.0),
|
|
|
|
let (dx, dy) = match unit {
|
|
|
|
width: Val::Px(100.0),
|
|
|
|
MouseScrollUnit::Line => (x * LINE_HEIGHT, y * LINE_HEIGHT),
|
|
|
|
height: Val::Px(100.0),
|
|
|
|
MouseScrollUnit::Pixel => (x * 1., y * 1.),
|
|
|
|
..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,
|
|
|
|
|
|
|
|
));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Ok(mut pos) = scrollers.get_mut(trigger.target()) {
|
|
|
|
fn run_tree(
|
|
|
|
pos.offset_x -= dx;
|
|
|
|
q: Query<(Entity, &NavTree), Changed<NavTree>>,
|
|
|
|
pos.offset_y -= dy;
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|