refactor ui/borders example to use new children! macro (#18962)
# Objective
Refactor
[`examples/ui/borders.rs`](7f0490655c/examples/ui/borders.rs
)
to use the new spawning/hierarchy APIs in 0.16.
## Solution
This refactor reduces the number of `.spawn` calls from about 16 to 2,
using one spawn for each major feature:
* camera2d
* ui layout
The `Children::spawn` relationship API is used to take advantage of
`SpawnIter` for the borders examples in each block.
Each block of examples now returns a Bundle into its respective
variable, which is then used in combination with the new `label` widget
which makes use of the new `impl Bundle` return capability. This allows
the ui layout to use a single `.spawn` with the `children!` macro.
The blocks of examples are still in separate variables because it felt
like a useful way to organize it still, even without needing to spawn at
those locations.
Functionality of the demo hasn't changed, this is just an API/code
update.
## Showcase

<details>
<summary>Before screenshot</summary>

</details>
---------
Co-authored-by: François Mockers <mockersf@gmail.com>
This commit is contained in:
parent
c55c69e3fc
commit
56405890f2
@ -1,6 +1,6 @@
|
|||||||
//! Example demonstrating bordered UI nodes
|
//! Example demonstrating bordered UI nodes
|
||||||
|
|
||||||
use bevy::{color::palettes::css::*, prelude::*};
|
use bevy::{color::palettes::css::*, ecs::spawn::SpawnIter, prelude::*};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
@ -11,37 +11,6 @@ fn main() {
|
|||||||
|
|
||||||
fn setup(mut commands: Commands) {
|
fn setup(mut commands: Commands) {
|
||||||
commands.spawn(Camera2d);
|
commands.spawn(Camera2d);
|
||||||
let root = commands
|
|
||||||
.spawn((
|
|
||||||
Node {
|
|
||||||
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()
|
|
||||||
},
|
|
||||||
BackgroundColor(Color::srgb(0.25, 0.25, 0.25)),
|
|
||||||
))
|
|
||||||
.id();
|
|
||||||
|
|
||||||
let root_rounded = commands
|
|
||||||
.spawn((
|
|
||||||
Node {
|
|
||||||
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()
|
|
||||||
},
|
|
||||||
BackgroundColor(Color::srgb(0.25, 0.25, 0.25)),
|
|
||||||
))
|
|
||||||
.id();
|
|
||||||
|
|
||||||
// labels for the different border edges
|
// labels for the different border edges
|
||||||
let border_labels = [
|
let border_labels = [
|
||||||
@ -77,228 +46,201 @@ fn setup(mut commands: Commands) {
|
|||||||
UiRect {
|
UiRect {
|
||||||
left: Val::Px(20.),
|
left: Val::Px(20.),
|
||||||
top: Val::Px(10.),
|
top: Val::Px(10.),
|
||||||
..Default::default()
|
..default()
|
||||||
},
|
},
|
||||||
UiRect {
|
UiRect {
|
||||||
left: Val::Px(10.),
|
left: Val::Px(10.),
|
||||||
bottom: Val::Px(20.),
|
bottom: Val::Px(20.),
|
||||||
..Default::default()
|
..default()
|
||||||
},
|
},
|
||||||
UiRect {
|
UiRect {
|
||||||
right: Val::Px(20.),
|
right: Val::Px(20.),
|
||||||
top: Val::Px(10.),
|
top: Val::Px(10.),
|
||||||
..Default::default()
|
..default()
|
||||||
},
|
},
|
||||||
UiRect {
|
UiRect {
|
||||||
right: Val::Px(10.),
|
right: Val::Px(10.),
|
||||||
bottom: Val::Px(10.),
|
bottom: Val::Px(10.),
|
||||||
..Default::default()
|
..default()
|
||||||
},
|
},
|
||||||
UiRect {
|
UiRect {
|
||||||
right: Val::Px(10.),
|
right: Val::Px(10.),
|
||||||
top: Val::Px(20.),
|
top: Val::Px(20.),
|
||||||
bottom: Val::Px(10.),
|
bottom: Val::Px(10.),
|
||||||
..Default::default()
|
..default()
|
||||||
},
|
},
|
||||||
UiRect {
|
UiRect {
|
||||||
left: Val::Px(10.),
|
left: Val::Px(10.),
|
||||||
top: Val::Px(10.),
|
top: Val::Px(10.),
|
||||||
bottom: Val::Px(10.),
|
bottom: Val::Px(10.),
|
||||||
..Default::default()
|
..default()
|
||||||
},
|
},
|
||||||
UiRect {
|
UiRect {
|
||||||
left: Val::Px(20.),
|
left: Val::Px(20.),
|
||||||
right: Val::Px(10.),
|
right: Val::Px(10.),
|
||||||
top: Val::Px(10.),
|
top: Val::Px(10.),
|
||||||
..Default::default()
|
..default()
|
||||||
},
|
},
|
||||||
UiRect {
|
UiRect {
|
||||||
left: Val::Px(10.),
|
left: Val::Px(10.),
|
||||||
right: Val::Px(10.),
|
right: Val::Px(10.),
|
||||||
bottom: Val::Px(20.),
|
bottom: Val::Px(20.),
|
||||||
..Default::default()
|
..default()
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
for (label, border) in border_labels.into_iter().zip(borders) {
|
let borders_examples = (
|
||||||
let inner_spot = commands
|
Node {
|
||||||
.spawn((
|
margin: UiRect::all(Val::Px(25.0)),
|
||||||
Node {
|
align_self: AlignSelf::Stretch,
|
||||||
width: Val::Px(10.),
|
justify_self: JustifySelf::Stretch,
|
||||||
height: Val::Px(10.),
|
flex_wrap: FlexWrap::Wrap,
|
||||||
..default()
|
justify_content: JustifyContent::FlexStart,
|
||||||
},
|
align_items: AlignItems::FlexStart,
|
||||||
BackgroundColor(YELLOW.into()),
|
align_content: AlignContent::FlexStart,
|
||||||
))
|
..default()
|
||||||
.id();
|
},
|
||||||
let border_node = commands
|
Children::spawn(SpawnIter(border_labels.into_iter().zip(borders).map(
|
||||||
.spawn((
|
|(label, border)| {
|
||||||
Node {
|
(
|
||||||
width: Val::Px(50.),
|
Node {
|
||||||
height: Val::Px(50.),
|
flex_direction: FlexDirection::Column,
|
||||||
border,
|
align_items: AlignItems::Center,
|
||||||
margin: UiRect::all(Val::Px(20.)),
|
..default()
|
||||||
align_items: AlignItems::Center,
|
},
|
||||||
justify_content: JustifyContent::Center,
|
children![
|
||||||
..default()
|
(
|
||||||
},
|
Node {
|
||||||
BackgroundColor(MAROON.into()),
|
width: Val::Px(50.),
|
||||||
BorderColor(RED.into()),
|
height: Val::Px(50.),
|
||||||
Outline {
|
border,
|
||||||
width: Val::Px(6.),
|
margin: UiRect::all(Val::Px(20.)),
|
||||||
offset: Val::Px(6.),
|
align_items: AlignItems::Center,
|
||||||
color: Color::WHITE,
|
justify_content: JustifyContent::Center,
|
||||||
},
|
..default()
|
||||||
))
|
},
|
||||||
.add_child(inner_spot)
|
BackgroundColor(MAROON.into()),
|
||||||
.id();
|
BorderColor(RED.into()),
|
||||||
let label_node = commands
|
Outline {
|
||||||
.spawn((
|
width: Val::Px(6.),
|
||||||
Text::new(label),
|
offset: Val::Px(6.),
|
||||||
TextFont {
|
color: Color::WHITE,
|
||||||
font_size: 9.0,
|
},
|
||||||
..Default::default()
|
children![(
|
||||||
},
|
Node {
|
||||||
))
|
width: Val::Px(10.),
|
||||||
.id();
|
height: Val::Px(10.),
|
||||||
let container = commands
|
..default()
|
||||||
.spawn(Node {
|
},
|
||||||
flex_direction: FlexDirection::Column,
|
BackgroundColor(YELLOW.into()),
|
||||||
align_items: AlignItems::Center,
|
)]
|
||||||
..default()
|
),
|
||||||
})
|
(Text::new(label), TextFont::from_font_size(9.0))
|
||||||
.add_children(&[border_node, label_node])
|
],
|
||||||
.id();
|
)
|
||||||
commands.entity(root).add_child(container);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (label, border) in border_labels.into_iter().zip(borders) {
|
|
||||||
let inner_spot = commands
|
|
||||||
.spawn((
|
|
||||||
Node {
|
|
||||||
width: Val::Px(10.),
|
|
||||||
height: Val::Px(10.),
|
|
||||||
..default()
|
|
||||||
},
|
|
||||||
BorderRadius::MAX,
|
|
||||||
BackgroundColor(YELLOW.into()),
|
|
||||||
))
|
|
||||||
.id();
|
|
||||||
let non_zero = |x, y| x != Val::Px(0.) && y != Val::Px(0.);
|
|
||||||
let border_size = |x, y| if non_zero(x, y) { f32::MAX } else { 0. };
|
|
||||||
let border_radius = BorderRadius::px(
|
|
||||||
border_size(border.left, border.top),
|
|
||||||
border_size(border.right, border.top),
|
|
||||||
border_size(border.right, border.bottom),
|
|
||||||
border_size(border.left, border.bottom),
|
|
||||||
);
|
|
||||||
let border_node = commands
|
|
||||||
.spawn((
|
|
||||||
Node {
|
|
||||||
width: Val::Px(50.),
|
|
||||||
height: Val::Px(50.),
|
|
||||||
border,
|
|
||||||
margin: UiRect::all(Val::Px(20.)),
|
|
||||||
align_items: AlignItems::Center,
|
|
||||||
justify_content: JustifyContent::Center,
|
|
||||||
..default()
|
|
||||||
},
|
|
||||||
BackgroundColor(MAROON.into()),
|
|
||||||
BorderColor(RED.into()),
|
|
||||||
border_radius,
|
|
||||||
Outline {
|
|
||||||
width: Val::Px(6.),
|
|
||||||
offset: Val::Px(6.),
|
|
||||||
color: Color::WHITE,
|
|
||||||
},
|
|
||||||
))
|
|
||||||
.add_child(inner_spot)
|
|
||||||
.id();
|
|
||||||
let label_node = commands
|
|
||||||
.spawn((
|
|
||||||
Text::new(label),
|
|
||||||
TextFont {
|
|
||||||
font_size: 9.0,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
))
|
|
||||||
.id();
|
|
||||||
let container = commands
|
|
||||||
.spawn(Node {
|
|
||||||
flex_direction: FlexDirection::Column,
|
|
||||||
align_items: AlignItems::Center,
|
|
||||||
..default()
|
|
||||||
})
|
|
||||||
.add_children(&[border_node, label_node])
|
|
||||||
.id();
|
|
||||||
commands.entity(root_rounded).add_child(container);
|
|
||||||
}
|
|
||||||
|
|
||||||
let border_label = commands
|
|
||||||
.spawn((
|
|
||||||
Node {
|
|
||||||
margin: UiRect {
|
|
||||||
left: Val::Px(25.0),
|
|
||||||
right: Val::Px(25.0),
|
|
||||||
top: Val::Px(25.0),
|
|
||||||
bottom: Val::Px(0.0),
|
|
||||||
},
|
|
||||||
..default()
|
|
||||||
},
|
},
|
||||||
BackgroundColor(Color::srgb(0.25, 0.25, 0.25)),
|
))),
|
||||||
))
|
);
|
||||||
.with_children(|builder| {
|
|
||||||
builder.spawn((
|
|
||||||
Text::new("Borders"),
|
|
||||||
TextFont {
|
|
||||||
font_size: 20.0,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
));
|
|
||||||
})
|
|
||||||
.id();
|
|
||||||
|
|
||||||
let border_rounded_label = commands
|
let non_zero = |x, y| x != Val::Px(0.) && y != Val::Px(0.);
|
||||||
.spawn((
|
let border_size = move |x, y| {
|
||||||
Node {
|
if non_zero(x, y) {
|
||||||
margin: UiRect {
|
f32::MAX
|
||||||
left: Val::Px(25.0),
|
} else {
|
||||||
right: Val::Px(25.0),
|
0.
|
||||||
top: Val::Px(25.0),
|
}
|
||||||
bottom: Val::Px(0.0),
|
};
|
||||||
},
|
|
||||||
..default()
|
|
||||||
},
|
|
||||||
BackgroundColor(Color::srgb(0.25, 0.25, 0.25)),
|
|
||||||
))
|
|
||||||
.with_children(|builder| {
|
|
||||||
builder.spawn((
|
|
||||||
Text::new("Borders Rounded"),
|
|
||||||
TextFont {
|
|
||||||
font_size: 20.0,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
));
|
|
||||||
})
|
|
||||||
.id();
|
|
||||||
|
|
||||||
commands
|
let borders_examples_rounded = (
|
||||||
.spawn((
|
Node {
|
||||||
Node {
|
margin: UiRect::all(Val::Px(25.0)),
|
||||||
margin: UiRect::all(Val::Px(25.0)),
|
align_self: AlignSelf::Stretch,
|
||||||
flex_direction: FlexDirection::Column,
|
justify_self: JustifySelf::Stretch,
|
||||||
align_self: AlignSelf::Stretch,
|
flex_wrap: FlexWrap::Wrap,
|
||||||
justify_self: JustifySelf::Stretch,
|
justify_content: JustifyContent::FlexStart,
|
||||||
flex_wrap: FlexWrap::Wrap,
|
align_items: AlignItems::FlexStart,
|
||||||
justify_content: JustifyContent::FlexStart,
|
align_content: AlignContent::FlexStart,
|
||||||
align_items: AlignItems::FlexStart,
|
..default()
|
||||||
align_content: AlignContent::FlexStart,
|
},
|
||||||
..default()
|
Children::spawn(SpawnIter(border_labels.into_iter().zip(borders).map(
|
||||||
|
move |(label, border)| {
|
||||||
|
(
|
||||||
|
Node {
|
||||||
|
flex_direction: FlexDirection::Column,
|
||||||
|
align_items: AlignItems::Center,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
children![
|
||||||
|
(
|
||||||
|
Node {
|
||||||
|
width: Val::Px(50.),
|
||||||
|
height: Val::Px(50.),
|
||||||
|
border,
|
||||||
|
margin: UiRect::all(Val::Px(20.)),
|
||||||
|
align_items: AlignItems::Center,
|
||||||
|
justify_content: JustifyContent::Center,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
BackgroundColor(MAROON.into()),
|
||||||
|
BorderColor(RED.into()),
|
||||||
|
BorderRadius::px(
|
||||||
|
border_size(border.left, border.top),
|
||||||
|
border_size(border.right, border.top),
|
||||||
|
border_size(border.right, border.bottom,),
|
||||||
|
border_size(border.left, border.bottom),
|
||||||
|
),
|
||||||
|
Outline {
|
||||||
|
width: Val::Px(6.),
|
||||||
|
offset: Val::Px(6.),
|
||||||
|
color: Color::WHITE,
|
||||||
|
},
|
||||||
|
children![(
|
||||||
|
Node {
|
||||||
|
width: Val::Px(10.),
|
||||||
|
height: Val::Px(10.),
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
BorderRadius::MAX,
|
||||||
|
BackgroundColor(YELLOW.into()),
|
||||||
|
)],
|
||||||
|
),
|
||||||
|
(Text::new(label), TextFont::from_font_size(9.0))
|
||||||
|
],
|
||||||
|
)
|
||||||
},
|
},
|
||||||
BackgroundColor(Color::srgb(0.25, 0.25, 0.25)),
|
))),
|
||||||
))
|
);
|
||||||
.add_child(border_label)
|
|
||||||
.add_child(root)
|
commands.spawn((
|
||||||
.add_child(border_rounded_label)
|
Node {
|
||||||
.add_child(root_rounded);
|
margin: UiRect::all(Val::Px(25.0)),
|
||||||
|
flex_direction: FlexDirection::Column,
|
||||||
|
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()
|
||||||
|
},
|
||||||
|
BackgroundColor(Color::srgb(0.25, 0.25, 0.25)),
|
||||||
|
children![
|
||||||
|
label("Borders"),
|
||||||
|
borders_examples,
|
||||||
|
label("Borders Rounded"),
|
||||||
|
borders_examples_rounded
|
||||||
|
],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// A label widget that accepts a &str and returns
|
||||||
|
// a Bundle that can be spawned
|
||||||
|
fn label(text: &str) -> impl Bundle {
|
||||||
|
(
|
||||||
|
Node {
|
||||||
|
margin: UiRect::all(Val::Px(25.0)),
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
children![(Text::new(text), TextFont::from_font_size(20.0))],
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user