mirror of
https://github.com/bevyengine/bevy
synced 2024-12-11 22:02:34 +00:00
015f2c69ca
# Objective Continue improving the user experience of our UI Node API in the direction specified by [Bevy's Next Generation Scene / UI System](https://github.com/bevyengine/bevy/discussions/14437) ## Solution As specified in the document above, merge `Style` fields into `Node`, and move "computed Node fields" into `ComputedNode` (I chose this name over something like `ComputedNodeLayout` because it currently contains more than just layout info. If we want to break this up / rename these concepts, lets do that in a separate PR). `Style` has been removed. This accomplishes a number of goals: ## Ergonomics wins Specifying both `Node` and `Style` is now no longer required for non-default styles Before: ```rust commands.spawn(( Node::default(), Style { width: Val::Px(100.), ..default() }, )); ``` After: ```rust commands.spawn(Node { width: Val::Px(100.), ..default() }); ``` ## Conceptual clarity `Style` was never a comprehensive "style sheet". It only defined "core" style properties that all `Nodes` shared. Any "styled property" that couldn't fit that mold had to be in a separate component. A "real" style system would style properties _across_ components (`Node`, `Button`, etc). We have plans to build a true style system (see the doc linked above). By moving the `Style` fields to `Node`, we fully embrace `Node` as the driving concept and remove the "style system" confusion. ## Next Steps * Consider identifying and splitting out "style properties that aren't core to Node". This should not happen for Bevy 0.15. --- ## Migration Guide Move any fields set on `Style` into `Node` and replace all `Style` component usage with `Node`. Before: ```rust commands.spawn(( Node::default(), Style { width: Val::Px(100.), ..default() }, )); ``` After: ```rust commands.spawn(Node { width: Val::Px(100.), ..default() }); ``` For any usage of the "computed node properties" that used to live on `Node`, use `ComputedNode` instead: Before: ```rust fn system(nodes: Query<&Node>) { for node in &nodes { let computed_size = node.size(); } } ``` After: ```rust fn system(computed_nodes: Query<&ComputedNode>) { for computed_node in &computed_nodes { let computed_size = computed_node.size(); } } ```
120 lines
4.8 KiB
Rust
120 lines
4.8 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::{
|
|
color::palettes::basic::{BLUE, GRAY, LIME, PURPLE, RED, YELLOW},
|
|
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(Camera2d);
|
|
|
|
// 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(Node {
|
|
width: Val::Percent(100.),
|
|
height: Val::Percent(100.),
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
parent
|
|
.spawn((
|
|
Node {
|
|
width: Val::Px(180.0),
|
|
height: Val::Px(100.0),
|
|
..default()
|
|
},
|
|
BackgroundColor(GRAY.into()),
|
|
))
|
|
.with_children(|parent| {
|
|
// spawn a node with default z-index.
|
|
parent.spawn((
|
|
Node {
|
|
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()
|
|
},
|
|
BackgroundColor(RED.into()),
|
|
));
|
|
|
|
// spawn a node with a positive local z-index of 2.
|
|
// it will show above other nodes in the gray container.
|
|
parent.spawn((
|
|
Node {
|
|
position_type: PositionType::Absolute,
|
|
left: Val::Px(45.0),
|
|
bottom: Val::Px(30.0),
|
|
width: Val::Px(100.),
|
|
height: Val::Px(50.),
|
|
..default()
|
|
},
|
|
ZIndex(2),
|
|
BackgroundColor(BLUE.into()),
|
|
));
|
|
|
|
// spawn a node with a negative local z-index.
|
|
// it will show under other nodes in the gray container.
|
|
parent.spawn((
|
|
Node {
|
|
position_type: PositionType::Absolute,
|
|
left: Val::Px(70.0),
|
|
bottom: Val::Px(20.0),
|
|
width: Val::Px(100.),
|
|
height: Val::Px(75.),
|
|
..default()
|
|
},
|
|
ZIndex(-1),
|
|
BackgroundColor(LIME.into()),
|
|
));
|
|
|
|
// 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((
|
|
Node {
|
|
position_type: PositionType::Absolute,
|
|
left: Val::Px(15.0),
|
|
bottom: Val::Px(10.0),
|
|
width: Val::Px(100.),
|
|
height: Val::Px(60.),
|
|
..default()
|
|
},
|
|
BackgroundColor(PURPLE.into()),
|
|
GlobalZIndex(1),
|
|
));
|
|
|
|
// 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((
|
|
Node {
|
|
position_type: PositionType::Absolute,
|
|
left: Val::Px(-15.0),
|
|
bottom: Val::Px(-15.0),
|
|
width: Val::Px(100.),
|
|
height: Val::Px(125.),
|
|
..default()
|
|
},
|
|
BackgroundColor(YELLOW.into()),
|
|
GlobalZIndex(-1),
|
|
));
|
|
});
|
|
});
|
|
}
|