mirror of
https://github.com/bevyengine/bevy
synced 2024-11-25 22:20:20 +00:00
c2c19e5ae4
**Ready for review. Examples migration progress: 100%.** # Objective - Implement https://github.com/bevyengine/bevy/discussions/15014 ## Solution This implements [cart's proposal](https://github.com/bevyengine/bevy/discussions/15014#discussioncomment-10574459) faithfully except for one change. I separated `TextSpan` from `TextSpan2d` because `TextSpan` needs to require the `GhostNode` component, which is a `bevy_ui` component only usable by UI. Extra changes: - Added `EntityCommands::commands_mut` that returns a mutable reference. This is a blocker for extension methods that return something other than `self`. Note that `sickle_ui`'s `UiBuilder::commands` returns a mutable reference for this reason. ## Testing - [x] Text examples all work. --- ## Showcase TODO: showcase-worthy ## Migration Guide TODO: very breaking ### Accessing text spans by index Text sections are now text sections on different entities in a hierarchy, Use the new `TextReader` and `TextWriter` system parameters to access spans by index. Before: ```rust fn refresh_text(mut query: Query<&mut Text, With<TimeText>>, time: Res<Time>) { let text = query.single_mut(); text.sections[1].value = format_time(time.elapsed()); } ``` After: ```rust fn refresh_text( query: Query<Entity, With<TimeText>>, mut writer: UiTextWriter, time: Res<Time> ) { let entity = query.single(); *writer.text(entity, 1) = format_time(time.elapsed()); } ``` ### Iterating text spans Text spans are now entities in a hierarchy, so the new `UiTextReader` and `UiTextWriter` system parameters provide ways to iterate that hierarchy. The `UiTextReader::iter` method will give you a normal iterator over spans, and `UiTextWriter::for_each` lets you visit each of the spans. --------- Co-authored-by: ickshonpe <david.curthoys@googlemail.com> Co-authored-by: Carter Anderson <mcanders1@gmail.com>
185 lines
6.5 KiB
Rust
185 lines
6.5 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(NodeBundle {
|
|
style: Style {
|
|
// 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()
|
|
},
|
|
background_color: BackgroundColor(Color::BLACK),
|
|
..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 {
|
|
width: Val::Percent(100.),
|
|
height: Val::Percent(100.),
|
|
flex_direction: FlexDirection::Column,
|
|
row_gap: MARGIN,
|
|
..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,
|
|
];
|
|
for align_items in alignments {
|
|
builder
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
width: Val::Percent(100.),
|
|
height: Val::Percent(100.),
|
|
flex_direction: FlexDirection::Row,
|
|
column_gap: MARGIN,
|
|
..Default::default()
|
|
},
|
|
..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(NodeBundle {
|
|
style: Style {
|
|
flex_direction: FlexDirection::Column,
|
|
align_items,
|
|
justify_content,
|
|
width: Val::Percent(100.),
|
|
height: Val::Percent(100.),
|
|
..Default::default()
|
|
},
|
|
background_color: BackgroundColor(Color::srgb(0.25, 0.25, 0.25)),
|
|
..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>,
|
|
background_color: Color,
|
|
margin: UiRect,
|
|
text: &str,
|
|
) {
|
|
builder
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
margin,
|
|
padding: UiRect::axes(Val::Px(5.), Val::Px(1.)),
|
|
..Default::default()
|
|
},
|
|
background_color: BackgroundColor(background_color),
|
|
..Default::default()
|
|
})
|
|
.with_children(|builder| {
|
|
builder.spawn((
|
|
Text::new(text),
|
|
TextStyle {
|
|
font,
|
|
color: Color::BLACK,
|
|
..default()
|
|
},
|
|
));
|
|
});
|
|
}
|