bevy/examples/ui/font_atlas_debug.rs
VitalyR eb19a9ea0b
Migrate UI bundles to required components (#15898)
# Objective

- Migrate UI bundles to required components, fixes #15889

## Solution

- deprecate `NodeBundle` in favor of `Node`
- deprecate `ImageBundle` in favor of `UiImage`
- deprecate `ButtonBundle` in favor of `Button`

## Testing

CI.

## Migration Guide

- Replace all uses of `NodeBundle` with `Node`. e.g.
```diff
     commands
-        .spawn(NodeBundle {
-            style: Style {
+        .spawn((
+            Node::default(),
+            Style {
                 width: Val::Percent(100.),
                 align_items: AlignItems::Center,
                 justify_content: JustifyContent::Center,
                 ..default()
             },
-            ..default()
-        })
+        ))
``` 
- Replace all uses of `ButtonBundle` with `Button`. e.g.
```diff
                     .spawn((
-                        ButtonBundle {
-                            style: Style {
-                                width: Val::Px(w),
-                                height: Val::Px(h),
-                                // horizontally center child text
-                                justify_content: JustifyContent::Center,
-                                // vertically center child text
-                                align_items: AlignItems::Center,
-                                margin: UiRect::all(Val::Px(20.0)),
-                                ..default()
-                            },
-                            image: image.clone().into(),
+                        Button,
+                        Style {
+                            width: Val::Px(w),
+                            height: Val::Px(h),
+                            // horizontally center child text
+                            justify_content: JustifyContent::Center,
+                            // vertically center child text
+                            align_items: AlignItems::Center,
+                            margin: UiRect::all(Val::Px(20.0)),
                             ..default()
                         },
+                        UiImage::from(image.clone()),
                         ImageScaleMode::Sliced(slicer.clone()),
                     ))
```
- Replace all uses of `ImageBundle` with `UiImage`. e.g.
```diff
-    commands.spawn(ImageBundle {
-        image: UiImage {
+    commands.spawn((
+        UiImage {
             texture: metering_mask,
             ..default()
         },
-        style: Style {
+        Style {
             width: Val::Percent(100.0),
             height: Val::Percent(100.0),
             ..default()
         },
-        ..default()
-    });
+    ));
 ```

---------

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2024-10-17 21:11:02 +00:00

111 lines
3.3 KiB
Rust

//! This example illustrates how `FontAtlas`'s are populated.
//! Bevy uses `FontAtlas`'s under the hood to optimize text rendering.
use bevy::{color::palettes::basic::YELLOW, prelude::*, text::FontAtlasSets};
use rand::{Rng, SeedableRng};
use rand_chacha::ChaCha8Rng;
fn main() {
App::new()
.init_resource::<State>()
.insert_resource(ClearColor(Color::BLACK))
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, (text_update_system, atlas_render_system))
.run();
}
#[derive(Resource)]
struct State {
atlas_count: u32,
handle: Handle<Font>,
timer: Timer,
}
impl Default for State {
fn default() -> Self {
Self {
atlas_count: 0,
handle: Handle::default(),
timer: Timer::from_seconds(0.05, TimerMode::Repeating),
}
}
}
#[derive(Resource, Deref, DerefMut)]
struct SeededRng(ChaCha8Rng);
fn atlas_render_system(
mut commands: Commands,
mut state: ResMut<State>,
font_atlas_sets: Res<FontAtlasSets>,
) {
if let Some(set) = font_atlas_sets.get(&state.handle) {
if let Some((_size, font_atlas)) = set.iter().next() {
let x_offset = state.atlas_count as f32;
if state.atlas_count == font_atlas.len() as u32 {
return;
}
let font_atlas = &font_atlas[state.atlas_count as usize];
state.atlas_count += 1;
commands.spawn((
UiImage::new(font_atlas.texture.clone()),
Style {
position_type: PositionType::Absolute,
top: Val::ZERO,
left: Val::Px(512.0 * x_offset),
..default()
},
));
}
}
}
fn text_update_system(
mut state: ResMut<State>,
time: Res<Time>,
mut query: Query<&mut Text>,
mut seeded_rng: ResMut<SeededRng>,
) {
if state.timer.tick(time.delta()).finished() {
for mut text in &mut query {
let c = seeded_rng.gen::<u8>() as char;
let string = &mut **text;
if !string.contains(c) {
string.push(c);
}
}
state.timer.reset();
}
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut state: ResMut<State>) {
let font_handle = asset_server.load("fonts/FiraSans-Bold.ttf");
state.handle = font_handle.clone();
commands.spawn(Camera2d);
commands
.spawn((
Node::default(),
BackgroundColor(Color::NONE),
Style {
position_type: PositionType::Absolute,
bottom: Val::ZERO,
..default()
},
))
.with_children(|parent| {
parent.spawn((
Text::new("a"),
TextFont {
font: font_handle,
font_size: 50.0,
..default()
},
TextColor(YELLOW.into()),
));
});
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
commands.insert_resource(SeededRng(ChaCha8Rng::seed_from_u64(19878367467713)));
}