mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +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(); } } ```
170 lines
5.8 KiB
Rust
170 lines
5.8 KiB
Rust
//! Demonstrates how the `AlignItems` and `JustifyContent` properties can be composed to layout text.
|
|
use bevy::prelude::*;
|
|
|
|
const ALIGN_ITEMS_COLOR: Color = Color::srgb(1., 0.066, 0.349);
|
|
const JUSTIFY_CONTENT_COLOR: Color = Color::srgb(0.102, 0.522, 1.);
|
|
const MARGIN: Val = Val::Px(12.);
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
|
primary_window: Some(Window {
|
|
title: "Bevy Flex Layout Example".to_string(),
|
|
..Default::default()
|
|
}),
|
|
..Default::default()
|
|
}))
|
|
.add_systems(Startup, spawn_layout)
|
|
.run();
|
|
}
|
|
|
|
fn spawn_layout(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
let font = asset_server.load("fonts/FiraSans-Bold.ttf");
|
|
commands.spawn(Camera2d);
|
|
commands
|
|
.spawn((
|
|
Node {
|
|
// fill the entire window
|
|
width: Val::Percent(100.),
|
|
height: Val::Percent(100.),
|
|
flex_direction: FlexDirection::Column,
|
|
align_items: AlignItems::Center,
|
|
padding: UiRect::all(MARGIN),
|
|
row_gap: MARGIN,
|
|
..Default::default()
|
|
},
|
|
BackgroundColor(Color::BLACK),
|
|
))
|
|
.with_children(|builder| {
|
|
// spawn the key
|
|
builder
|
|
.spawn(Node {
|
|
flex_direction: FlexDirection::Row,
|
|
..default()
|
|
})
|
|
.with_children(|builder| {
|
|
spawn_nested_text_bundle(
|
|
builder,
|
|
font.clone(),
|
|
ALIGN_ITEMS_COLOR,
|
|
UiRect::right(MARGIN),
|
|
"AlignItems",
|
|
);
|
|
spawn_nested_text_bundle(
|
|
builder,
|
|
font.clone(),
|
|
JUSTIFY_CONTENT_COLOR,
|
|
UiRect::default(),
|
|
"JustifyContent",
|
|
);
|
|
});
|
|
|
|
builder
|
|
.spawn(Node {
|
|
width: Val::Percent(100.),
|
|
height: Val::Percent(100.),
|
|
flex_direction: FlexDirection::Column,
|
|
row_gap: MARGIN,
|
|
..default()
|
|
})
|
|
.with_children(|builder| {
|
|
// spawn one child node for each combination of `AlignItems` and `JustifyContent`
|
|
let justifications = [
|
|
JustifyContent::FlexStart,
|
|
JustifyContent::Center,
|
|
JustifyContent::FlexEnd,
|
|
JustifyContent::SpaceEvenly,
|
|
JustifyContent::SpaceAround,
|
|
JustifyContent::SpaceBetween,
|
|
];
|
|
let alignments = [
|
|
AlignItems::Baseline,
|
|
AlignItems::FlexStart,
|
|
AlignItems::Center,
|
|
AlignItems::FlexEnd,
|
|
AlignItems::Stretch,
|
|
];
|
|
for align_items in alignments {
|
|
builder
|
|
.spawn(Node {
|
|
width: Val::Percent(100.),
|
|
height: Val::Percent(100.),
|
|
flex_direction: FlexDirection::Row,
|
|
column_gap: MARGIN,
|
|
..Default::default()
|
|
})
|
|
.with_children(|builder| {
|
|
for justify_content in justifications {
|
|
spawn_child_node(
|
|
builder,
|
|
font.clone(),
|
|
align_items,
|
|
justify_content,
|
|
);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
fn spawn_child_node(
|
|
builder: &mut ChildBuilder,
|
|
font: Handle<Font>,
|
|
align_items: AlignItems,
|
|
justify_content: JustifyContent,
|
|
) {
|
|
builder
|
|
.spawn((
|
|
Node {
|
|
flex_direction: FlexDirection::Column,
|
|
align_items,
|
|
justify_content,
|
|
width: Val::Percent(100.),
|
|
height: Val::Percent(100.),
|
|
..default()
|
|
},
|
|
BackgroundColor(Color::srgb(0.25, 0.25, 0.25)),
|
|
))
|
|
.with_children(|builder| {
|
|
let labels = [
|
|
(format!("{align_items:?}"), ALIGN_ITEMS_COLOR, 0.),
|
|
(format!("{justify_content:?}"), JUSTIFY_CONTENT_COLOR, 3.),
|
|
];
|
|
for (text, color, top_margin) in labels {
|
|
// We nest the text within a parent node because margins and padding can't be directly applied to text nodes currently.
|
|
spawn_nested_text_bundle(
|
|
builder,
|
|
font.clone(),
|
|
color,
|
|
UiRect::top(Val::Px(top_margin)),
|
|
&text,
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
fn spawn_nested_text_bundle(
|
|
builder: &mut ChildBuilder,
|
|
font: Handle<Font>,
|
|
background_color: Color,
|
|
margin: UiRect,
|
|
text: &str,
|
|
) {
|
|
builder
|
|
.spawn((
|
|
Node {
|
|
margin,
|
|
padding: UiRect::axes(Val::Px(5.), Val::Px(1.)),
|
|
..default()
|
|
},
|
|
BackgroundColor(background_color),
|
|
))
|
|
.with_children(|builder| {
|
|
builder.spawn((
|
|
Text::new(text),
|
|
TextFont { font, ..default() },
|
|
TextColor::BLACK,
|
|
));
|
|
});
|
|
}
|