buggy but working collapse button + title work

main
Elijah Voigt 2 years ago
parent b26517e721
commit fd1ee92d5f

@ -123,6 +123,21 @@ fn initialize_ui(mut commands: Commands) {
}; };
commands commands
.spawn(NodeBundle {
style: Style {
border: UiRect::all(Val::Px(1.0)),
margin: UiRect::all(Val::Px(5.0)),
padding: UiRect::all(Val::Px(5.0)),
flex_direction: FlexDirection::Column,
overflow: Overflow::clip(),
..default()
},
background_color: Color::WHITE.into(),
border_color: Color::BLACK.into(),
..default()
})
.with_children(|parent| {
let container = parent
.spawn((NodeBundle { .spawn((NodeBundle {
style: Style { style: Style {
border: UiRect::all(Val::Px(1.0)), border: UiRect::all(Val::Px(1.0)),
@ -229,14 +244,34 @@ fn initialize_ui(mut commands: Commands) {
content_containers.iter().for_each(|(name, target)| { content_containers.iter().for_each(|(name, target)| {
parent.spawn(( parent.spawn((
b.clone(), b.clone(),
ui::Title { ui::Title { text: name.clone() },
name: name.clone(),
..default()
},
ui::Collapse { target: *target }, ui::Collapse { target: *target },
ui::Active,
)); ));
}); });
}); });
})
.id();
parent.spawn((
NodeBundle {
style: Style {
border: UiRect::all(Val::Px(1.0)),
margin: UiRect::all(Val::Px(5.0)),
padding: UiRect::all(Val::Px(5.0)),
flex_direction: FlexDirection::Row,
overflow: Overflow::clip(),
..default()
},
background_color: Color::ALICE_BLUE.into(),
border_color: Color::BLACK.into(),
..default()
},
ui::Title {
text: "Assets".into(),
},
ui::Minimize { target: container },
ui::Sorting(0),
));
}); });
} }
@ -261,10 +296,7 @@ fn spawn_tab_container<T: Default + Component>(
z_index: ZIndex::Local(100), z_index: ZIndex::Local(100),
..default() ..default()
}, },
ui::Title { ui::Title { text: title.into() },
name: title.into(),
..default()
},
T::default(), T::default(),
ui::Scroll, ui::Scroll,
Interaction::default(), Interaction::default(),
@ -436,7 +468,7 @@ mod assets {
border_color: Color::BLACK.into(), border_color: Color::BLACK.into(),
..default() ..default()
}, },
ui::Title { name, ..default() }, ui::Title { text: name },
)) ))
.set_parent(root.single()) .set_parent(root.single())
.id() .id()
@ -461,7 +493,7 @@ mod assets {
border_color: Color::BLACK.into(), border_color: Color::BLACK.into(),
..default() ..default()
}, },
ui::Title { name, ..default() }, ui::Title { text: name },
)) ))
.set_parent(root.single()) .set_parent(root.single())
.id() .id()

@ -33,12 +33,13 @@ impl Plugin for GameUiPlugin {
Update, Update,
( (
init_titles, init_titles,
manage_titles,
manage_button_interaction, manage_button_interaction,
manage_select_active, manage_select_active,
manage_cursor, manage_cursor,
manage_scroll, manage_scroll,
manage_collapse_active,
manage_collapse_hiding, manage_collapse_hiding,
manage_collapse_active,
show_child_number, show_child_number,
manage_sort, manage_sort,
), ),
@ -50,48 +51,48 @@ pub use title::*;
mod title { mod title {
use super::*; use super::*;
#[derive(Debug, Component, Default)] #[derive(Debug, Component)]
pub struct Title { pub struct Title {
pub name: String, pub text: String,
pub note: Option<String>, }
#[derive(Debug, Component)]
pub struct Note {
pub text: String,
}
#[derive(Debug, Component)]
pub struct TitleText;
#[derive(Debug, Component)]
pub struct Minimize {
pub target: Entity,
} }
pub fn init_titles( pub fn init_titles(
events: Query<(Entity, &Title), Or<(Changed<Title>, Added<Title>)>>, events: Query<(Entity, &Title, Option<&Note>, Option<&Minimize>), Added<Title>>,
mut commands: Commands, mut commands: Commands,
) { ) {
events.for_each(|(entity, Title { name, note })| { events.for_each(|(entity, title, note, minimize)| {
commands commands.entity(entity).with_children(|parent| {
.entity(entity) parent.spawn((
.despawn_descendants() TextBundle {
.with_children(|parent| {
parent
.spawn((
NodeBundle {
style: Style {
padding: UiRect::all(Val::Px(5.0)),
margin: UiRect::all(Val::Px(5.0)),
border: UiRect::all(Val::Px(1.0)),
flex_direction: FlexDirection::Row,
..default()
},
..default()
},
Sorting(0),
))
.with_children(|parent| {
parent.spawn(TextBundle {
text: Text { text: Text {
sections: vec![ sections: vec![
TextSection { TextSection {
value: name.clone(), value: title.text.clone(),
style: TextStyle { style: TextStyle {
color: Color::BLACK, color: Color::BLACK,
..default() ..default()
}, },
}, },
TextSection { TextSection {
value: note.clone().unwrap_or(String::new()), value: note
.unwrap_or(&Note {
text: String::new(),
})
.text
.clone(),
style: TextStyle { style: TextStyle {
color: Color::BLACK, color: Color::BLACK,
..default() ..default()
@ -106,11 +107,68 @@ mod title {
..default() ..default()
}, },
..default() ..default()
}); },
}); Sorting(0),
TitleText,
));
if let Some(target) = minimize {
parent.spawn((
ButtonBundle {
style: Style {
border: UiRect::all(Val::Px(1.0)),
margin: UiRect::all(Val::Px(5.0)),
padding: UiRect::all(Val::Px(5.0)),
right: Val::Px(0.0),
..default()
},
background_color: Color::WHITE.into(),
border_color: Color::BLACK.into(),
..default()
},
Collapse {
target: target.target,
},
));
}
}); });
}); });
} }
pub fn manage_titles(
events: Query<(&Title, Option<&Note>, &Children), Changed<Title>>,
mut texts: Query<&mut Text, With<TitleText>>,
) {
events.iter().for_each(|(title, note, children)| {
children.iter().for_each(|child| {
if let Ok(mut text) = texts.get_mut(*child) {
*text = Text {
sections: vec![
TextSection {
value: title.text.clone(),
style: TextStyle {
color: Color::BLACK,
..default()
},
},
TextSection {
value: note
.unwrap_or(&Note {
text: String::new(),
})
.text
.clone(),
style: TextStyle {
color: Color::BLACK,
..default()
},
},
],
..default()
}
}
})
})
}
} }
pub use collapse::*; pub use collapse::*;
@ -171,22 +229,26 @@ mod collapse {
pub fn show_child_number( pub fn show_child_number(
events: Query<(Entity, &Children), (Changed<Children>, With<Node>)>, events: Query<(Entity, &Children), (Changed<Children>, With<Node>)>,
mut tabs: Query<(&Collapse, &mut Title)>, mut tabs: Query<(Entity, &Collapse)>,
mut commands: Commands,
) { ) {
// Any time a widget changes // Any time a widget changes
events.iter().for_each(|(entity, children)| { events.iter().for_each(|(entity, children)| {
// Find any tabs which have this as a target // Find any tabs which have this as a target
tabs.iter_mut() tabs.iter_mut()
.find_map(|(collapse, title)| { .find_map(|(button, collapse)| {
if entity == collapse.target { if entity == collapse.target {
Some(title) Some(button)
} else { } else {
None None
} }
}) })
.iter_mut() .iter()
.for_each(|title| { .for_each(|button| {
title.note = Some(format!("({})", children.len().saturating_sub(1))) let num_children = children.len();
commands.entity(*button).insert(Note {
text: format!("({})", num_children),
});
}); });
}); });
} }

Loading…
Cancel
Save