mirror of
https://github.com/bevyengine/bevy
synced 2024-11-23 05:03:47 +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.
118 lines
3.5 KiB
Rust
118 lines
3.5 KiB
Rust
//! Example demonstrating bordered UI nodes
|
|
|
|
use bevy::prelude::*;
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
.add_systems(Startup, setup)
|
|
.run();
|
|
}
|
|
|
|
fn setup(mut commands: Commands) {
|
|
commands.spawn(Camera2dBundle::default());
|
|
let root = commands
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
margin: UiRect::all(Val::Px(25.0)),
|
|
align_self: AlignSelf::Stretch,
|
|
justify_self: JustifySelf::Stretch,
|
|
flex_wrap: FlexWrap::Wrap,
|
|
justify_content: JustifyContent::FlexStart,
|
|
align_items: AlignItems::FlexStart,
|
|
align_content: AlignContent::FlexStart,
|
|
..Default::default()
|
|
},
|
|
background_color: BackgroundColor(Color::BLACK),
|
|
..Default::default()
|
|
})
|
|
.id();
|
|
|
|
// all the different combinations of border edges
|
|
let borders = [
|
|
UiRect::default(),
|
|
UiRect::all(Val::Px(10.)),
|
|
UiRect::left(Val::Px(10.)),
|
|
UiRect::right(Val::Px(10.)),
|
|
UiRect::top(Val::Px(10.)),
|
|
UiRect::bottom(Val::Px(10.)),
|
|
UiRect::horizontal(Val::Px(10.)),
|
|
UiRect::vertical(Val::Px(10.)),
|
|
UiRect {
|
|
left: Val::Px(10.),
|
|
top: Val::Px(10.),
|
|
..Default::default()
|
|
},
|
|
UiRect {
|
|
left: Val::Px(10.),
|
|
bottom: Val::Px(10.),
|
|
..Default::default()
|
|
},
|
|
UiRect {
|
|
right: Val::Px(10.),
|
|
top: Val::Px(10.),
|
|
..Default::default()
|
|
},
|
|
UiRect {
|
|
right: Val::Px(10.),
|
|
bottom: Val::Px(10.),
|
|
..Default::default()
|
|
},
|
|
UiRect {
|
|
right: Val::Px(10.),
|
|
top: Val::Px(10.),
|
|
bottom: Val::Px(10.),
|
|
..Default::default()
|
|
},
|
|
UiRect {
|
|
left: Val::Px(10.),
|
|
top: Val::Px(10.),
|
|
bottom: Val::Px(10.),
|
|
..Default::default()
|
|
},
|
|
UiRect {
|
|
left: Val::Px(10.),
|
|
right: Val::Px(10.),
|
|
top: Val::Px(10.),
|
|
..Default::default()
|
|
},
|
|
UiRect {
|
|
left: Val::Px(10.),
|
|
right: Val::Px(10.),
|
|
bottom: Val::Px(10.),
|
|
..Default::default()
|
|
},
|
|
];
|
|
|
|
for i in 0..64 {
|
|
let inner_spot = commands
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
width: Val::Px(10.),
|
|
height: Val::Px(10.),
|
|
..Default::default()
|
|
},
|
|
background_color: Color::YELLOW.into(),
|
|
..Default::default()
|
|
})
|
|
.id();
|
|
let bordered_node = commands
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
width: Val::Px(50.),
|
|
height: Val::Px(50.),
|
|
border: borders[i % borders.len()],
|
|
margin: UiRect::all(Val::Px(2.)),
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..Default::default()
|
|
},
|
|
background_color: Color::BLUE.into(),
|
|
border_color: Color::WHITE.with_a(0.5).into(),
|
|
..Default::default()
|
|
})
|
|
.add_child(inner_spot)
|
|
.id();
|
|
commands.entity(root).add_child(bordered_node);
|
|
}
|
|
}
|