mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 06:30:19 +00:00
b995827013
# Objective Make `bevy_ui` "root" nodes more intuitive to use/style by: - Removing the implicit flexbox styling (such as stretch alignment) that is applied to them, and replacing it with more intuitive CSS Grid styling (notably with stretch alignment disabled in both axes). - Making root nodes layout independently of each other. Instead of there being a single implicit "viewport" node that all root nodes are children of, there is now an implicit "viewport" node *per root node*. And layout of each tree is computed separately. ## Solution - Remove the global implicit viewport node, and instead create an implicit viewport node for each user-specified root node. - Keep track of both the user-specified root nodes and the implicit viewport nodes in a separate `Vec`. - Use the window's size as the `available_space` parameter to `Taffy.compute_layout` rather than setting it on the implicit viewport node (and set the viewport to `height: 100%; width: 100%` to make this "just work"). --- ## Changelog - Bevy UI now lays out root nodes independently of each other in separate layout contexts. - The implicit viewport node (which contains each user-specified root node) is now `Display::Grid` with `align_items` and `justify_items` both set to `Start`. ## Migration Guide - Bevy UI now lays out root nodes independently of each other in separate layout contexts. If you were relying on your root nodes being able to affect each other's layouts, then you may need to wrap them in a single root node. - The implicit viewport node (which contains each user-specified root node) is now `Display::Grid` with `align_items` and `justify_items` both set to `Start`. You may need to add `height: Val::Percent(100.)` to your root nodes if you were previously relying on being implicitly set.
126 lines
5.2 KiB
Rust
126 lines
5.2 KiB
Rust
//! Demonstrates how to use the z-index component on UI nodes to control their relative depth
|
|
//!
|
|
//! It uses colored boxes with different z-index values to demonstrate how it can affect the order of
|
|
//! depth of nodes compared to their siblings, but also compared to the entire UI.
|
|
|
|
use bevy::prelude::*;
|
|
|
|
fn main() {
|
|
App::new()
|
|
.insert_resource(ClearColor(Color::BLACK))
|
|
.add_plugins(DefaultPlugins)
|
|
.add_systems(Startup, setup)
|
|
.run();
|
|
}
|
|
|
|
fn setup(mut commands: Commands) {
|
|
commands.spawn(Camera2dBundle::default());
|
|
|
|
// spawn the container with default z-index.
|
|
// the default z-index value is `ZIndex::Local(0)`.
|
|
// because this is a root UI node, using local or global values will do the same thing.
|
|
commands
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
width: Val::Percent(100.),
|
|
height: Val::Percent(100.),
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..default()
|
|
},
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
parent
|
|
.spawn(NodeBundle {
|
|
background_color: Color::GRAY.into(),
|
|
style: Style {
|
|
width: Val::Px(180.0),
|
|
height: Val::Px(100.0),
|
|
..default()
|
|
},
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
// spawn a node with default z-index.
|
|
parent.spawn(NodeBundle {
|
|
background_color: Color::RED.into(),
|
|
style: Style {
|
|
position_type: PositionType::Absolute,
|
|
left: Val::Px(10.0),
|
|
bottom: Val::Px(40.0),
|
|
width: Val::Px(100.0),
|
|
height: Val::Px(50.0),
|
|
..default()
|
|
},
|
|
..default()
|
|
});
|
|
|
|
// spawn a node with a positive local z-index of 2.
|
|
// it will show above other nodes in the gray container.
|
|
parent.spawn(NodeBundle {
|
|
z_index: ZIndex::Local(2),
|
|
background_color: Color::BLUE.into(),
|
|
style: Style {
|
|
position_type: PositionType::Absolute,
|
|
left: Val::Px(45.0),
|
|
bottom: Val::Px(30.0),
|
|
width: Val::Px(100.),
|
|
height: Val::Px(50.),
|
|
..default()
|
|
},
|
|
..default()
|
|
});
|
|
|
|
// spawn a node with a negative local z-index.
|
|
// it will show under other nodes in the gray container.
|
|
parent.spawn(NodeBundle {
|
|
z_index: ZIndex::Local(-1),
|
|
background_color: Color::GREEN.into(),
|
|
style: Style {
|
|
position_type: PositionType::Absolute,
|
|
left: Val::Px(70.0),
|
|
bottom: Val::Px(20.0),
|
|
width: Val::Px(100.),
|
|
height: Val::Px(75.),
|
|
..default()
|
|
},
|
|
..default()
|
|
});
|
|
|
|
// spawn a node with a positive global z-index of 1.
|
|
// it will show above all other nodes, because it's the highest global z-index in this example.
|
|
// by default, boxes all share the global z-index of 0 that the gray container is added to.
|
|
parent.spawn(NodeBundle {
|
|
z_index: ZIndex::Global(1),
|
|
background_color: Color::PURPLE.into(),
|
|
style: Style {
|
|
position_type: PositionType::Absolute,
|
|
left: Val::Px(15.0),
|
|
bottom: Val::Px(10.0),
|
|
width: Val::Px(100.),
|
|
height: Val::Px(60.),
|
|
..default()
|
|
},
|
|
..default()
|
|
});
|
|
|
|
// spawn a node with a negative global z-index of -1.
|
|
// this will show under all other nodes including its parent, because it's the lowest global z-index
|
|
// in this example.
|
|
parent.spawn(NodeBundle {
|
|
z_index: ZIndex::Global(-1),
|
|
background_color: Color::YELLOW.into(),
|
|
style: Style {
|
|
position_type: PositionType::Absolute,
|
|
left: Val::Px(-15.0),
|
|
bottom: Val::Px(-15.0),
|
|
width: Val::Px(100.),
|
|
height: Val::Px(125.),
|
|
..default()
|
|
},
|
|
..default()
|
|
});
|
|
});
|
|
});
|
|
}
|