Skip to content

MeasureFunc Text aspect ratio bug #6748

@ickshonpe

Description

@ickshonpe

Bevy version

0.9

& identical behaviour observed with the taffy-2 PR

What you did

use bevy::prelude::*;

const MESSAGES: [&str; 8] = [
    "123",
    "12345",
    "1234567",
    "1234567890",
    "123\nxyz",
    "12345\nxyz",
    "1234567\nxyz",
    "1234567890\nxyz",
];

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_startup_system(setup)
        .add_system(update)
        .run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.insert_resource(UiScale { scale: 1.0 });
    commands.spawn(Camera2dBundle::default());
    commands
        .spawn(NodeBundle {
            style: Style {
                align_items: AlignItems::Center,
                justify_content: JustifyContent::Center,
                size: Size::new(Val::Percent(100.), Val::Percent(100.)),                
                ..Default::default()
            },
            ..Default::default()
        })
        .with_children(|builder| {
            builder
                .spawn(NodeBundle {
                    style: Style {
                        size: Size::new(Val::Percent(80.), Val::Percent(80.)),
                        flex_direction: FlexDirection::Column,
                        ..Default::default()
                    },
                    background_color: BackgroundColor(Color::NAVY),
                    ..Default::default()
                })
                .with_children(|builder| {
                    builder.spawn((TextBundle {
                        text: Text::from_section(
                            "".to_string(),
                            TextStyle {
                                font: asset_server.load("fonts/FiraSans-Bold.ttf"),
                                font_size: 24.0,
                                color: Color::WHITE,
                            },
                        ),
                        ..Default::default()
                    },
                    BackgroundColor(Color::RED))
                );
                });
        });
}

pub fn update(
    mut t: Local<f32>,
    mut i: Local<usize>,
    time: Res<Time>,
    mut text_query: Query<&mut Text>,
) {
    *t -= time.delta_seconds();
    if *t <= 0. {
        *t = 1.;
        *i = (*i + 1) % MESSAGES.len();
        let mut text = text_query.single_mut();
        text.sections[0].value = MESSAGES[*i].into();
    }
}

What went wrong

For the single-line text, the length of the cross-axis (which is vertical here) changes depending on the width of the text.
But for multiline text, the length of the cross-axis is constant even though the width of the text changes.

If you change the FlexDirection, the behaviour reverses and the cross-axis varies with the widths of the multiline text and is constant for single-line text.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-UIGraphical user interfaces, styles, layouts, and widgetsC-BugAn unexpected or incorrect behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions