2024-10-02 00:24:28 +00:00
|
|
|
//! This example demonstrates the use of Ghost Nodes.
|
|
|
|
//!
|
|
|
|
//! UI layout will ignore ghost nodes, and treat their children as if they were direct descendants of the first non-ghost ancestor.
|
|
|
|
|
|
|
|
use bevy::{prelude::*, ui::GhostNode, winit::WinitSettings};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::new()
|
|
|
|
.add_plugins(DefaultPlugins)
|
|
|
|
.insert_resource(WinitSettings::desktop_app())
|
|
|
|
.add_systems(Startup, setup)
|
|
|
|
.add_systems(Update, button_system)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
struct Counter(i32);
|
|
|
|
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
|
|
let font_handle = asset_server.load("fonts/FiraSans-Bold.ttf");
|
|
|
|
|
2024-10-05 01:59:52 +00:00
|
|
|
commands.spawn(Camera2d);
|
2024-10-02 00:24:28 +00:00
|
|
|
|
|
|
|
// Ghost UI root
|
|
|
|
commands.spawn(GhostNode).with_children(|ghost_root| {
|
|
|
|
ghost_root
|
|
|
|
.spawn(NodeBundle::default())
|
|
|
|
.with_child(create_label(
|
|
|
|
"This text node is rendered under a ghost root",
|
|
|
|
font_handle.clone(),
|
|
|
|
));
|
|
|
|
});
|
|
|
|
|
|
|
|
// Normal UI root
|
|
|
|
commands
|
|
|
|
.spawn(NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
width: Val::Percent(100.0),
|
|
|
|
height: Val::Percent(100.0),
|
|
|
|
align_items: AlignItems::Center,
|
|
|
|
justify_content: JustifyContent::Center,
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
..default()
|
|
|
|
})
|
|
|
|
.with_children(|parent| {
|
|
|
|
parent
|
|
|
|
.spawn((NodeBundle::default(), Counter(0)))
|
|
|
|
.with_children(|layout_parent| {
|
|
|
|
layout_parent
|
|
|
|
.spawn((GhostNode, Counter(0)))
|
|
|
|
.with_children(|ghost_parent| {
|
|
|
|
// Ghost children using a separate counter state
|
|
|
|
// These buttons are being treated as children of layout_parent in the context of UI
|
|
|
|
ghost_parent
|
|
|
|
.spawn(create_button())
|
|
|
|
.with_child(create_label("0", font_handle.clone()));
|
|
|
|
ghost_parent
|
|
|
|
.spawn(create_button())
|
|
|
|
.with_child(create_label("0", font_handle.clone()));
|
|
|
|
});
|
|
|
|
|
|
|
|
// A normal child using the layout parent counter
|
|
|
|
layout_parent
|
|
|
|
.spawn(create_button())
|
|
|
|
.with_child(create_label("0", font_handle.clone()));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_button() -> ButtonBundle {
|
|
|
|
ButtonBundle {
|
|
|
|
style: Style {
|
|
|
|
width: Val::Px(150.0),
|
|
|
|
height: Val::Px(65.0),
|
|
|
|
border: UiRect::all(Val::Px(5.0)),
|
|
|
|
// horizontally center child text
|
|
|
|
justify_content: JustifyContent::Center,
|
|
|
|
// vertically center child text
|
|
|
|
align_items: AlignItems::Center,
|
|
|
|
..default()
|
|
|
|
},
|
|
|
|
border_color: BorderColor(Color::BLACK),
|
|
|
|
border_radius: BorderRadius::MAX,
|
|
|
|
background_color: Color::srgb(0.15, 0.15, 0.15).into(),
|
|
|
|
..default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-13 17:06:22 +00:00
|
|
|
fn create_label(text: &str, font: Handle<Font>) -> (Text, TextFont, TextColor) {
|
Text rework (#15591)
**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>
2024-10-09 18:35:36 +00:00
|
|
|
(
|
|
|
|
Text::new(text),
|
2024-10-13 17:06:22 +00:00
|
|
|
TextFont {
|
2024-10-02 00:24:28 +00:00
|
|
|
font,
|
|
|
|
font_size: 33.0,
|
Text rework (#15591)
**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>
2024-10-09 18:35:36 +00:00
|
|
|
..default()
|
2024-10-02 00:24:28 +00:00
|
|
|
},
|
2024-10-13 17:06:22 +00:00
|
|
|
TextColor(Color::srgb(0.9, 0.9, 0.9)),
|
2024-10-02 00:24:28 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn button_system(
|
|
|
|
mut interaction_query: Query<(&Interaction, &Parent), (Changed<Interaction>, With<Button>)>,
|
|
|
|
labels_query: Query<(&Children, &Parent), With<Button>>,
|
|
|
|
mut text_query: Query<&mut Text>,
|
|
|
|
mut counter_query: Query<&mut Counter>,
|
|
|
|
) {
|
|
|
|
// Update parent counter on click
|
|
|
|
for (interaction, parent) in &mut interaction_query {
|
|
|
|
if matches!(interaction, Interaction::Pressed) {
|
|
|
|
let mut counter = counter_query.get_mut(parent.get()).unwrap();
|
|
|
|
counter.0 += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update button labels to match their parent counter
|
|
|
|
for (children, parent) in &labels_query {
|
|
|
|
let counter = counter_query.get(parent.get()).unwrap();
|
|
|
|
let mut text = text_query.get_mut(children[0]).unwrap();
|
|
|
|
|
Text rework (#15591)
**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>
2024-10-09 18:35:36 +00:00
|
|
|
**text = counter.0.to_string();
|
2024-10-02 00:24:28 +00:00
|
|
|
}
|
|
|
|
}
|