2023-01-27 19:07:48 +00:00
|
|
|
//! Demonstrates how the `AlignItems` and `JustifyContent` properties can be composed to layout text.
|
|
|
|
use bevy::prelude::*;
|
|
|
|
|
2024-02-24 21:35:32 +00:00
|
|
|
const ALIGN_ITEMS_COLOR: LegacyColor = LegacyColor::rgb(1., 0.066, 0.349);
|
|
|
|
const JUSTIFY_CONTENT_COLOR: LegacyColor = LegacyColor::rgb(0.102, 0.522, 1.);
|
2024-02-26 16:04:02 +00:00
|
|
|
const MARGIN: Val = Val::Px(12.);
|
2023-01-27 19:07:48 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::new()
|
|
|
|
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
|
|
|
primary_window: Some(Window {
|
2023-03-22 08:22:56 +00:00
|
|
|
title: "Bevy Flex Layout Example".to_string(),
|
2023-01-27 19:07:48 +00:00
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
}))
|
2023-03-18 01:45:34 +00:00
|
|
|
.add_systems(Startup, spawn_layout)
|
2023-01-27 19:07:48 +00:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spawn_layout(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
|
|
let font = asset_server.load("fonts/FiraSans-Bold.ttf");
|
|
|
|
commands.spawn(Camera2dBundle::default());
|
|
|
|
commands
|
|
|
|
.spawn(NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
// fill the entire window
|
Flatten UI `Style` properties that use `Size` + remove `Size` (#8548)
# Objective
- Simplify API and make authoring styles easier
See:
https://github.com/bevyengine/bevy/issues/8540#issuecomment-1536177102
## Solution
- The `size`, `min_size`, `max_size`, and `gap` properties have been
replaced by `width`, `height`, `min_width`, `min_height`, `max_width`,
`max_height`, `row_gap`, and `column_gap` properties
---
## Changelog
- Flattened `Style` properties that have a `Size` value directly into
`Style`
## Migration Guide
- The `size`, `min_size`, `max_size`, and `gap` properties have been
replaced by the `width`, `height`, `min_width`, `min_height`,
`max_width`, `max_height`, `row_gap`, and `column_gap` properties. Use
the new properties instead.
---------
Co-authored-by: ickshonpe <david.curthoys@googlemail.com>
2023-05-16 01:36:32 +00:00
|
|
|
width: Val::Percent(100.),
|
2024-02-26 16:04:02 +00:00
|
|
|
height: Val::Percent(100.),
|
2023-01-27 19:07:48 +00:00
|
|
|
flex_direction: FlexDirection::Column,
|
|
|
|
align_items: AlignItems::Center,
|
2024-02-26 16:04:02 +00:00
|
|
|
padding: UiRect::all(MARGIN),
|
|
|
|
row_gap: MARGIN,
|
2023-01-27 19:07:48 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2024-02-24 21:35:32 +00:00
|
|
|
background_color: BackgroundColor(LegacyColor::BLACK),
|
2023-01-27 19:07:48 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.with_children(|builder| {
|
|
|
|
// spawn the key
|
|
|
|
builder
|
|
|
|
.spawn(NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
flex_direction: FlexDirection::Row,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::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(NodeBundle {
|
|
|
|
style: Style {
|
2024-02-26 16:04:02 +00:00
|
|
|
width: Val::Percent(100.),
|
|
|
|
height: Val::Percent(100.),
|
2023-01-27 19:07:48 +00:00
|
|
|
flex_direction: FlexDirection::Column,
|
2024-02-26 16:04:02 +00:00
|
|
|
row_gap: MARGIN,
|
2023-01-27 19:07:48 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::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,
|
|
|
|
];
|
2024-02-26 16:04:02 +00:00
|
|
|
for align_items in alignments {
|
2023-01-27 19:07:48 +00:00
|
|
|
builder
|
|
|
|
.spawn(NodeBundle {
|
|
|
|
style: Style {
|
2024-02-26 16:04:02 +00:00
|
|
|
width: Val::Percent(100.),
|
|
|
|
height: Val::Percent(100.),
|
2023-01-27 19:07:48 +00:00
|
|
|
flex_direction: FlexDirection::Row,
|
2024-02-26 16:04:02 +00:00
|
|
|
column_gap: MARGIN,
|
2023-01-27 19:07:48 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.with_children(|builder| {
|
2024-02-26 16:04:02 +00:00
|
|
|
for justify_content in justifications {
|
2023-01-27 19:07:48 +00:00
|
|
|
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(NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
flex_direction: FlexDirection::Column,
|
|
|
|
align_items,
|
|
|
|
justify_content,
|
2024-02-26 16:04:02 +00:00
|
|
|
width: Val::Percent(100.),
|
|
|
|
height: Val::Percent(100.),
|
2023-01-27 19:07:48 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2024-02-24 21:35:32 +00:00
|
|
|
background_color: BackgroundColor(LegacyColor::DARK_GRAY),
|
2023-01-27 19:07:48 +00:00
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.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>,
|
2024-02-24 21:35:32 +00:00
|
|
|
background_color: LegacyColor,
|
2023-01-27 19:07:48 +00:00
|
|
|
margin: UiRect,
|
|
|
|
text: &str,
|
|
|
|
) {
|
|
|
|
builder
|
|
|
|
.spawn(NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
margin,
|
2023-08-23 12:49:10 +00:00
|
|
|
padding: UiRect::axes(Val::Px(5.), Val::Px(1.)),
|
2023-01-27 19:07:48 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
background_color: BackgroundColor(background_color),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.with_children(|builder| {
|
|
|
|
builder.spawn(TextBundle::from_section(
|
|
|
|
text,
|
|
|
|
TextStyle {
|
|
|
|
font,
|
|
|
|
font_size: 24.0,
|
2024-02-24 21:35:32 +00:00
|
|
|
color: LegacyColor::BLACK,
|
2023-01-27 19:07:48 +00:00
|
|
|
},
|
|
|
|
));
|
|
|
|
});
|
|
|
|
}
|