# Objective Continue improving the user experience of our UI Node API in the direction specified by [Bevy's Next Generation Scene / UI System](https://github.com/bevyengine/bevy/discussions/14437) ## Solution As specified in the document above, merge `Style` fields into `Node`, and move "computed Node fields" into `ComputedNode` (I chose this name over something like `ComputedNodeLayout` because it currently contains more than just layout info. If we want to break this up / rename these concepts, lets do that in a separate PR). `Style` has been removed. This accomplishes a number of goals: ## Ergonomics wins Specifying both `Node` and `Style` is now no longer required for non-default styles Before: ```rust commands.spawn(( Node::default(), Style { width: Val::Px(100.), ..default() }, )); ``` After: ```rust commands.spawn(Node { width: Val::Px(100.), ..default() }); ``` ## Conceptual clarity `Style` was never a comprehensive "style sheet". It only defined "core" style properties that all `Nodes` shared. Any "styled property" that couldn't fit that mold had to be in a separate component. A "real" style system would style properties _across_ components (`Node`, `Button`, etc). We have plans to build a true style system (see the doc linked above). By moving the `Style` fields to `Node`, we fully embrace `Node` as the driving concept and remove the "style system" confusion. ## Next Steps * Consider identifying and splitting out "style properties that aren't core to Node". This should not happen for Bevy 0.15. --- ## Migration Guide Move any fields set on `Style` into `Node` and replace all `Style` component usage with `Node`. Before: ```rust commands.spawn(( Node::default(), Style { width: Val::Px(100.), ..default() }, )); ``` After: ```rust commands.spawn(Node { width: Val::Px(100.), ..default() }); ``` For any usage of the "computed node properties" that used to live on `Node`, use `ComputedNode` instead: Before: ```rust fn system(nodes: Query<&Node>) { for node in &nodes { let computed_size = node.size(); } } ``` After: ```rust fn system(computed_nodes: Query<&ComputedNode>) { for computed_node in &computed_nodes { let computed_size = computed_node.size(); } } ```
		
			
				
	
	
		
			111 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			111 lines
		
	
	
		
			3.2 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()),
 | 
						|
                Node {
 | 
						|
                    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 {
 | 
						|
                position_type: PositionType::Absolute,
 | 
						|
                bottom: Val::ZERO,
 | 
						|
                ..default()
 | 
						|
            },
 | 
						|
            BackgroundColor(Color::NONE),
 | 
						|
        ))
 | 
						|
        .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)));
 | 
						|
}
 |