2024-03-11 19:26:14 +00:00
|
|
|
//! Showcase how to use and configure FPS overlay.
|
|
|
|
|
|
|
|
use bevy::{
|
|
|
|
dev_tools::fps_overlay::{FpsOverlayConfig, FpsOverlayPlugin},
|
|
|
|
prelude::*,
|
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::FontSmoothing,
|
2024-03-11 19:26:14 +00:00
|
|
|
};
|
|
|
|
|
2024-10-08 22:17:55 +00:00
|
|
|
struct OverlayColor;
|
|
|
|
|
|
|
|
impl OverlayColor {
|
|
|
|
const RED: Color = Color::srgb(1.0, 0.0, 0.0);
|
|
|
|
const GREEN: Color = Color::srgb(0.0, 1.0, 0.0);
|
|
|
|
}
|
|
|
|
|
2024-03-11 19:26:14 +00:00
|
|
|
fn main() {
|
|
|
|
App::new()
|
|
|
|
.add_plugins((
|
|
|
|
DefaultPlugins,
|
|
|
|
FpsOverlayPlugin {
|
|
|
|
config: FpsOverlayConfig {
|
2024-10-13 17:06:22 +00:00
|
|
|
text_config: TextFont {
|
2024-03-11 19:26:14 +00:00
|
|
|
// Here we define size of our overlay
|
2024-09-16 23:14:37 +00:00
|
|
|
font_size: 42.0,
|
2024-03-11 19:26:14 +00:00
|
|
|
// If we want, we can use a custom font
|
|
|
|
font: default(),
|
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
|
|
|
// We could also disable font smoothing,
|
|
|
|
font_smoothing: FontSmoothing::default(),
|
2024-03-11 19:26:14 +00:00
|
|
|
},
|
2024-10-13 17:06:22 +00:00
|
|
|
// We can also change color of the overlay
|
|
|
|
text_color: OverlayColor::GREEN,
|
2024-09-17 15:16:28 +00:00
|
|
|
enabled: true,
|
2024-03-11 19:26:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
))
|
|
|
|
.add_systems(Startup, setup)
|
|
|
|
.add_systems(Update, customize_config)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(mut commands: Commands) {
|
2024-03-20 13:11:48 +00:00
|
|
|
// We need to spawn a camera (2d or 3d) to see the overlay
|
2024-10-05 01:59:52 +00:00
|
|
|
commands.spawn(Camera2d);
|
2024-03-20 13:11:48 +00:00
|
|
|
|
|
|
|
// Instruction text
|
|
|
|
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(|c| {
|
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
|
|
|
c.spawn(Text::new(concat!(
|
|
|
|
"Press 1 to toggle the overlay color.\n",
|
|
|
|
"Press 2 to decrease the overlay size.\n",
|
|
|
|
"Press 3 to increase the overlay size.\n",
|
|
|
|
"Press 4 to toggle the overlay visibility."
|
|
|
|
)));
|
2024-03-20 13:11:48 +00:00
|
|
|
});
|
2024-03-11 19:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn customize_config(input: Res<ButtonInput<KeyCode>>, mut overlay: ResMut<FpsOverlayConfig>) {
|
|
|
|
if input.just_pressed(KeyCode::Digit1) {
|
|
|
|
// Changing resource will affect overlay
|
2024-10-13 17:06:22 +00:00
|
|
|
if overlay.text_color == OverlayColor::GREEN {
|
|
|
|
overlay.text_color = OverlayColor::RED;
|
2024-10-08 22:17:55 +00:00
|
|
|
} else {
|
2024-10-13 17:06:22 +00:00
|
|
|
overlay.text_color = OverlayColor::GREEN;
|
2024-10-08 22:17:55 +00:00
|
|
|
}
|
2024-03-11 19:26:14 +00:00
|
|
|
}
|
|
|
|
if input.just_pressed(KeyCode::Digit2) {
|
|
|
|
overlay.text_config.font_size -= 2.0;
|
|
|
|
}
|
2024-09-17 15:16:28 +00:00
|
|
|
if input.just_pressed(KeyCode::Digit3) {
|
2024-10-08 22:17:55 +00:00
|
|
|
overlay.text_config.font_size += 2.0;
|
|
|
|
}
|
|
|
|
if input.just_pressed(KeyCode::Digit4) {
|
2024-09-17 15:16:28 +00:00
|
|
|
overlay.enabled = !overlay.enabled;
|
|
|
|
}
|
2024-03-11 19:26:14 +00:00
|
|
|
}
|