kinda fixed scene/animation buttons -- also kinda reworked asset/button layout a lot...

main
Elijah Voigt 2 years ago
parent a2b3d73fa8
commit 0d374dc45e

1
Cargo.lock generated

@ -2226,6 +2226,7 @@ version = "0.1.0"
dependencies = [
"bevy",
"bevy_rapier3d",
"serde",
]
[[package]]

@ -43,6 +43,7 @@ path = "bin/chars.rs"
[dependencies]
bevy = "0.11"
bevy_rapier3d = "*"
serde = "1"
# From rapier docs
[profile.dev.package.bevy_rapier3d]

File diff suppressed because it is too large Load Diff

@ -1,21 +1,32 @@
/// TODO:
/// * Text box w/ clear button
/// * Names/Labels management
/// * Button color management
/// * Move code to submodules
/// * Generic Minimize/Close Components
/// * Notice/Warning/Error Popups
/// * Textbox (ReadOnly)
/// * Textbox (ReadWrite)
///
/// BUGS:
/// * When selecting one tree, possible to select another without the first closing.
/// * Active::Multi (ex: Audio) vs Active::Single (ex: Assets, Gltf) for different tabs
///
use bevy::{
input::{
keyboard::KeyboardInput,
mouse::{MouseScrollUnit, MouseWheel},
ButtonState,
},
asset::Asset,
input::mouse::{MouseScrollUnit, MouseWheel},
prelude::*,
window::PrimaryWindow,
};
use std::cmp::PartialEq;
#[derive(Debug, Component, PartialEq)]
pub struct TargetAsset<T: Asset> {
pub handle: Handle<T>,
}
#[derive(Debug, Component, PartialEq)]
pub struct TargetEntity {
pub entity: Entity,
}
pub struct GameUiPlugin;
@ -28,7 +39,9 @@ impl Plugin for GameUiPlugin {
manage_button_interaction,
manage_cursor,
manage_scroll,
manage_collapse,
manage_collapse_active,
manage_collapse_hiding,
show_child_number,
manage_sort,
),
);
@ -39,33 +52,49 @@ pub use title::*;
mod title {
use super::*;
#[derive(Debug, Component)]
#[derive(Debug, Component, Default)]
pub struct Title {
pub name: String,
pub note: Option<String>,
}
pub fn init_titles(events: Query<(Entity, &Title), Added<Title>>, mut commands: Commands) {
events.for_each(|(entity, Title { name })| {
commands.entity(entity).with_children(|parent| {
parent.spawn(TextBundle {
text: Text {
sections: vec![TextSection {
value: name.clone(),
style: TextStyle {
color: Color::BLACK,
..default()
},
}],
..default()
},
style: Style {
margin: UiRect::all(Val::Px(5.0)),
padding: UiRect::all(Val::Px(5.0)),
pub fn init_titles(
events: Query<(Entity, &Title), Or<(Changed<Title>, Added<Title>)>>,
mut commands: Commands,
) {
events.for_each(|(entity, Title { name, note })| {
commands
.entity(entity)
.despawn_descendants()
.with_children(|parent| {
parent.spawn(TextBundle {
text: Text {
sections: vec![
TextSection {
value: name.clone(),
style: TextStyle {
color: Color::BLACK,
..default()
},
},
TextSection {
value: note.clone().unwrap_or(String::new()),
style: TextStyle {
color: Color::BLACK,
..default()
},
},
],
..default()
},
style: Style {
margin: UiRect::all(Val::Px(5.0)),
padding: UiRect::all(Val::Px(5.0)),
..default()
},
..default()
},
..default()
});
});
});
});
}
}
@ -79,22 +108,80 @@ mod collapse {
pub target: Entity,
}
pub fn manage_collapse(
events: Query<(Entity, &Collapse, &Interaction), (Changed<Interaction>, With<Button>)>,
mut styles: Query<&mut Style>,
pub fn manage_collapse_active(
events: Query<
(Entity, Option<&Active>, &Parent, &Interaction),
(Changed<Interaction>, With<Button>, With<Collapse>),
>,
mut commands: Commands,
active_children: Query<&Children>,
) {
events
.iter()
.filter(|(_, _, &interaction)| interaction == Interaction::Pressed)
.for_each(|(entity, collapse, _)| {
info!("Collapse press on {:?}", entity);
if let Ok(mut style) = styles.get_mut(collapse.target) {
style.display = match style.display {
Display::None => Display::Flex,
Display::Flex | Display::Grid => Display::None,
};
.filter(|(_, _, _, &interaction)| interaction == Interaction::Pressed)
.for_each(|(entity, active, parent, _)| {
match active {
Some(_) => {
commands.entity(entity).remove::<Active>();
}
None => {
// Set all other buttons to inactive
if let Ok(children) = active_children.get(parent.get()) {
children.iter().for_each(|child| {
commands.entity(*child).remove::<Active>();
});
}
// Set this butotn to active
commands.entity(entity).insert(Active);
}
};
});
}
pub fn manage_collapse_hiding(
added: Query<Entity, (Added<Active>, With<Collapse>)>,
mut removed: RemovedComponents<Active>,
collapses: Query<&Collapse, With<Button>>,
mut styles: Query<&mut Style>,
) {
// Added collapse, display the target entity
added.iter().for_each(|e| {
if let Ok(Collapse { target }) = collapses.get(e) {
if let Ok(mut style) = styles.get_mut(*target) {
style.display = Display::Flex;
}
}
});
// Removed collapse, hide the target entity
removed.iter().for_each(|e| {
if let Ok(Collapse { target }) = collapses.get(e) {
if let Ok(mut style) = styles.get_mut(*target) {
style.display = Display::None;
}
})
}
});
}
pub fn show_child_number(
events: Query<(Entity, &Children), (Changed<Children>, With<Node>)>,
mut tabs: Query<(&Collapse, &mut Title)>,
) {
// Any time a widget changes
events.iter().for_each(|(entity, children)| {
// Find any tabs which have this as a target
tabs.iter_mut()
.find_map(|(collapse, title)| {
if entity == collapse.target {
Some(title)
} else {
None
}
})
.iter_mut()
.for_each(|title| {
title.note = Some(format!("({})", children.len().saturating_sub(1)))
});
});
}
}
@ -106,22 +193,39 @@ mod buttons {
pub struct Active;
pub fn manage_button_interaction(
mut events: Query<
(&Interaction, &mut BackgroundColor, Option<&Active>),
events: Query<
(Entity, &Interaction, Option<&Active>),
(Changed<Interaction>, With<Button>),
>,
added_active: Query<Entity, Added<Active>>,
mut removed_active: RemovedComponents<Active>,
mut bg_colors: Query<&mut BackgroundColor>,
) {
events.for_each_mut(|(interaction, mut bg_color, active)| match active {
Some(_) => match interaction {
Interaction::None => *bg_color = Color::ORANGE.into(),
Interaction::Pressed => *bg_color = Color::YELLOW.into(),
Interaction::Hovered => *bg_color = Color::RED.into(),
},
None => match interaction {
Interaction::None => *bg_color = Color::GRAY.into(),
Interaction::Pressed => *bg_color = Color::WHITE.into(),
Interaction::Hovered => *bg_color = Color::DARK_GRAY.into(),
},
added_active.for_each(|e| {
if let Ok(mut bg_color) = bg_colors.get_mut(e) {
*bg_color = Color::ORANGE.into();
}
});
removed_active.iter().for_each(|e| {
if let Ok(mut bg_color) = bg_colors.get_mut(e) {
*bg_color = Color::WHITE.into();
}
});
events.for_each(|(entity, interaction, active)| {
if let Ok(mut bg_color) = bg_colors.get_mut(entity) {
match active {
Some(_) => match interaction {
Interaction::None => *bg_color = Color::ORANGE.into(),
Interaction::Pressed => *bg_color = Color::YELLOW.into(),
Interaction::Hovered => *bg_color = Color::RED.into(),
},
None => match interaction {
Interaction::None => *bg_color = Color::WHITE.into(),
Interaction::Pressed => *bg_color = Color::WHITE.into(),
Interaction::Hovered => *bg_color = Color::GRAY.into(),
},
}
}
})
}
}

Loading…
Cancel
Save