Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Positioning of text with line breaks is offset #717

Closed
tigregalis opened this issue Oct 22, 2020 · 2 comments
Closed

Positioning of text with line breaks is offset #717

tigregalis opened this issue Oct 22, 2020 · 2 comments
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior

Comments

@tigregalis
Copy link
Contributor

tigregalis commented Oct 22, 2020

Using bevy master, latest commit c743a96, on Windows 10.

Possibly related to #81, #401 and #182 (#183).

I'm trying to position text ("this\ntext\nwraps") that has line breaks in it in the top right corner of the window like so:

____________________________
                     this  |
                     text  |
                     wraps |

I use

            style: Style {
                position_type: PositionType::Absolute,
                position: Rect {
                    top: Val::Px(5.0),
                    right: Val::Px(5.0),
                    ..Default::default()
                },
                ..Default::default()
            },

It appears to calculate the width of the rectangle from the text without the line breaks ("thistextwraps").
Then when it is trying to position the text in the top right, it takes this rectangle (without the line breaks), then afterwards it wraps the text.

Minimal example:

use bevy::prelude::*;

fn main() {
    App::build()
        .add_default_plugins()
        .add_resource(ClearColor(Color::BLACK)) // the window's background colour
        .add_startup_system(setup.system())
        .run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands
        .spawn(UiCameraComponents::default())
        .spawn(TextComponents {
            text: Text {
                font: asset_server.load("fonts/FiraSans-Bold.ttf"),
                value: "This\ntext\nwraps".to_string(), // this is the text I want to position in the top right
                style: TextStyle {
                    color: Color::rgba(1.0, 1.0, 1.0, 0.5), // White text
                    font_size: 40.0,
                },
            },
            style: Style {
                position_type: PositionType::Absolute,
                position: Rect {
                    top: Val::Px(5.0),
                    right: Val::Px(5.0),
                    ..Default::default()
                },
                ..Default::default()
            },
            ..Default::default()
        })
        .spawn(TextComponents {
            text: Text {
                font: asset_server.load("fonts/FiraSans-Bold.ttf"),
                value: "Thistextwraps".to_string(), // this is a "shadow" that demonstrates what's happening
                style: TextStyle {
                    color: Color::rgba(0.7, 0.7, 1.0, 0.5), // Blue text
                    font_size: 40.0,
                },
            },
            style: Style {
                position_type: PositionType::Absolute,
                position: Rect {
                    top: Val::Px(5.0),
                    right: Val::Px(5.0),
                    ..Default::default()
                },
                ..Default::default()
            },
            ..Default::default()
        });
}

image

@tigregalis tigregalis added the C-Bug An unexpected or incorrect behavior label Oct 22, 2020
@Moxinilian Moxinilian added the A-UI Graphical user interfaces, styles, layouts, and widgets label Oct 22, 2020
@AlisCode
Copy link
Contributor

Fixed by #765.

With a text added as follows :

commands
        .spawn(TextComponents {
            style: Style {
                align_self: AlignSelf::FlexEnd,
                position_type: PositionType::Absolute,
                position: Rect {
                    top: Val::Px(5.0),
                    right: Val::Px(15.0),
                    ..Default::default()
                },
                ..Default::default()
            },
            text: Text {
                value: "This\ntext\nwraps".to_string(),
                font: font.clone(),
                style: TextStyle {
                    font_size: 50.0,
                    color: Color::rgb(0.8, 0.2, 0.7),
                    ..Default::default()
                },
            },
            ..Default::default()
        });

Text on top-right of the screen, left-aligned (not that it matters in this case anyway), text wraps as seen here :

image

@tigregalis
Copy link
Contributor Author

Perfect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

No branches or pull requests

3 participants