-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Despawned Node Entity Continues to Affect Layout #351
Comments
Suggested labels: |
karroffel
added
C-Bug
An unexpected or incorrect behavior
A-UI
Graphical user interfaces, styles, layouts, and widgets
labels
Aug 26, 2020
Open
In the new version of bevy, I’ve replicated your code, and it’s working exactly as you’d expect. use bevy::prelude::*;
/// This example illustrates how to create a button that changes color and text based on its interaction state.
fn main() {
App::build()
.add_default_plugins()
.init_resource::<ButtonMaterials>()
.init_resource::<ButtonCount>()
.add_startup_system(setup.system())
.add_system(button_system.system())
.run();
}
fn button_system(
mut commands: Commands,
button_materials: Res<ButtonMaterials>,
mut button_count: ResMut<ButtonCount>,
asset_server: Res<AssetServer>,
mut interaction_query: Query<(
Entity,
&Button,
Mutated<Interaction>,
&mut Handle<ColorMaterial>,
&Children,
)>,
mut root_query: Query<(Entity, &Root, &Children)>,
) {
for (_, _, interaction, mut material, _) in &mut interaction_query.iter() {
match *interaction {
Interaction::Clicked => {
// we're going to remvoe the button
// commands.despawn_recursive(e);
let font = asset_server.get_handle("assets/fonts/FiraCode-Light.ttf").unwrap_or_default();
// spawn a new button in the same spot
for (parent, _, children) in &mut root_query.iter() {
println!("Now have: {} children", children.0.len());
let e = spawn_button_node(
&mut commands,
font,
&button_materials,
&(format!("Button {}", button_count.0))[..],
);
button_count.0 = button_count.0 + 1;
commands.push_children(parent, &[e]);
break;
}
}
Interaction::Hovered => {
*material = button_materials.hovered;
}
Interaction::None => {
*material = button_materials.normal;
}
}
}
}
fn setup(
mut commands: Commands,
mut font_manager:ResMut<Assets<Font>>,
asset_server: Res<AssetServer>,
button_materials: Res<ButtonMaterials>,
) {
let font = asset_server
.load_sync(&mut font_manager,"assets/fonts/FiraCode-Light.ttf")
.unwrap();
let root = commands
.spawn(UiCameraComponents::default())
.spawn(NodeComponents {
style: Style {
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
flex_direction: FlexDirection::ColumnReverse,
justify_content: JustifyContent::FlexStart,
align_items: AlignItems::Center,
..Default::default()
},
..Default::default()
}
)
.with(Root{})
.current_entity().unwrap();
let button = spawn_button_node(&mut commands, font,&button_materials, "First button");
commands.push_children(root, &[button]);
}
struct ButtonMaterials {
normal: Handle<ColorMaterial>,
hovered: Handle<ColorMaterial>,
}
impl FromResources for ButtonMaterials {
fn from_resources(resources: &Resources) -> Self {
let mut materials = resources.get_mut::<Assets<ColorMaterial>>().unwrap();
ButtonMaterials {
normal: materials.add(Color::rgb(0.02, 0.02, 0.02).into()),
hovered: materials.add(Color::rgb(0.05, 0.05, 0.05).into()),
}
}
}
#[derive(Default)]
struct ButtonCount(u32);
struct Root;
fn spawn_button_node(
commands: &mut Commands,
font: Handle<Font>,
mats: &Res<ButtonMaterials>,
label: &str,
) -> Entity {
commands
.spawn(ButtonComponents {
style: Style {
size: Size::new(Val::Px(300.0), Val::Px(65.0)),
// center button
margin: Rect::all(Val::Auto),
// horizontally center child text
justify_content: JustifyContent::Center,
// vertically center child text
align_items: AlignItems::Center,
..Default::default()
},
material: mats.normal,
..Default::default()
}
)
.with_children(|parent| {
parent.spawn(TextComponents {
text: Text {
value: label.to_string(),
style: TextStyle {
font_size: 28.0,
color: Color::rgb(1.0, 0.1, 0.1),
},
font:font,
},
..Default::default()
});
})
.current_entity().unwrap()
} |
Closed by #386. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An UI Node that is despawned continues to impact the layout of it's parent Node as if it were still present. I think it's likely that the despawn does not cause the underlying Stretch node to be removed.
Example Reproduction
In this example everytime the button is clicked we despawn the existing button, and then spawn a new button inside the same container. My expectation is that since the container only ever holds one child, that child remains in the same position.
Demo
For comparison, this is the behaviour without despawning the button
The text was updated successfully, but these errors were encountered: