Skip to content

Desktop scale change does not update text #4688

@lukors

Description

@lukors

Bevy version

d867b61

Operating system & version

Windows 10

What you did

  1. Run the below code, observe how the text fits in the box for future comparison.
  2. While it's running, change the desktop scaling in Windows, or move the window to another screen with a different scaling. Observe that the text no longer fits the same way in the box.
  3. Press space to "touch" the text. Observe that the text now fits in the box like it should.
use bevy::{prelude::*, text::Text2dBounds};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_startup_system(setup)
        .add_system(touch_text)
        .run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    let font = asset_server.load("fonts/FiraSans-Bold.ttf");
    let text_style = TextStyle {
        font,
        font_size: 60.0,
        color: Color::WHITE,
    };

    // 2d camera
    commands.spawn_bundle(OrthographicCameraBundle::new_2d());

    // Demonstrate text wrapping
    let box_size = Vec2::new(300.0, 200.0);
    let box_position = Vec2::new(0.0, -250.0);
    commands.spawn_bundle(SpriteBundle {
        sprite: Sprite {
            color: Color::rgb(0.25, 0.25, 0.75),
            custom_size: Some(Vec2::new(box_size.x, box_size.y)),
            ..default()
        },
        transform: Transform::from_translation(box_position.extend(0.0)),
        ..default()
    });
    let text_alignment_topleft = TextAlignment {
        vertical: VerticalAlign::Top,
        horizontal: HorizontalAlign::Left,
    };
    commands.spawn_bundle(Text2dBundle {
        text: Text::with_section(
            "this text wraps in the box",
            text_style,
            text_alignment_topleft,
        ),
        text_2d_bounds: Text2dBounds {
            // Wrap text in the rectangle
            size: box_size,
        },
        // We align text to the top-left, so this transform is the top-left corner of our text. The
        // box is centered at box_position, so it is necessary to move by half of the box size to
        // keep the text in the box.
        transform: Transform::from_xyz(
            box_position.x - box_size.x / 2.0,
            box_position.y + box_size.y / 2.0,
            1.0,
        ),
        ..default()
    });
}

fn touch_text(
    keyboard_input: Res<Input<KeyCode>>,
    mut q_text: Query<&mut Text>,
) {
    if keyboard_input.just_pressed(KeyCode::Space) {
        println!("Pressed space");
        for mut text in q_text.iter_mut() {
            text.sections.iter_mut();
        }
    }
}

What you expected to happen

The text should always fit the same way in the box.

What actually happened

The text does not update along with everything else when the scale changes.

Additional information

This system can be used to work around the issue, but it would be better if something equivalent happened automatically inside Bevy by default:

fn touch_text(
    mut window_resized_events: EventReader<WindowResized>,
    mut q_text: Query<&mut Text>,
) {
    if window_resized_events.iter().next().is_some() {
        for mut text in q_text.iter_mut() {
            text.sections.iter_mut();
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-RenderingDrawing game state to the screenC-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