Positioning of Text2D relative to parent when using TextBounds #18679
-
I'm trying to draw a shape and then write some text on top of the shape. The text should be centred vertically and horizontally on the shape, and should wrap on word breaks. Looking at the documentation and the examples it seems I need some combination of My question: is this the best approach? I can't help feeling my solution is a kludge and I'm missing something obvious! Example code, using Bevy 0.15.3: use bevy::color::palettes::css::{DIM_GRAY, WHITE};
use bevy::prelude::*;
use bevy::text::TextBounds;
const BOUNDS_WIDTH: f32 = 80.0;
fn main() {
App::new()
.insert_resource(ClearColor(Color::Srgba(DIM_GRAY)))
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
commands.spawn(Camera2d);
let white = materials.add(Color::Srgba(WHITE));
let square = meshes.add(Rectangle::new(100.0, 100.0));
// Short; doesn't need to wrap; no TextBounds
// Works as expected
commands
.spawn((
Mesh2d(square.clone()),
MeshMaterial2d(white.clone()),
Transform::from_xyz(-60.0, 60.0, 0.0),
))
.with_children(|parent| {
parent.spawn((
Text2d("No wrap".to_string()),
TextColor(Color::BLACK),
TextFont {
font_size: 15.0,
..default()
},
TextLayout::new(JustifyText::Center, LineBreak::WordBoundary),
));
});
// Short; doesn't need to wrap; has TextBounds
// Offset to the right
commands
.spawn((
Mesh2d(square.clone()),
MeshMaterial2d(white.clone()),
Transform::from_xyz(60.0, 60.0, 0.0),
))
.with_children(|parent| {
parent.spawn((
Text2d("No wrap".to_string()),
TextColor(Color::BLACK),
TextFont {
font_size: 15.0,
..default()
},
TextLayout::new(JustifyText::Center, LineBreak::WordBoundary),
TextBounds::new_horizontal(BOUNDS_WIDTH),
));
});
// Longer; needs to wrap; has TextBounds
// Offset to the right
commands
.spawn((
Mesh2d(square.clone()),
MeshMaterial2d(white.clone()),
Transform::from_xyz(-60.0, -60.0, 0.0),
))
.with_children(|parent| {
parent.spawn((
Text2d("Long with wrap".to_string()),
TextColor(Color::BLACK),
TextFont {
font_size: 15.0,
..default()
},
TextLayout::new(JustifyText::Center, LineBreak::WordBoundary),
TextBounds::new_horizontal(BOUNDS_WIDTH),
));
});
// Longer; needs to wrap; has TextBounds; offset X by BOUNDS_WIDTH / 4.0
// Expcted visual output, requires manual offset
commands
.spawn((
Mesh2d(square.clone()),
MeshMaterial2d(white.clone()),
Transform::from_xyz(60.0, -60.0, 0.0),
))
.with_children(|parent| {
parent.spawn((
Text2d("Long with wrap".to_string()),
TextColor(Color::BLACK),
TextFont {
font_size: 15.0,
..default()
},
TextLayout::new(JustifyText::Center, LineBreak::WordBoundary),
TextBounds::new_horizontal(BOUNDS_WIDTH),
Transform::from_xyz(-(BOUNDS_WIDTH / 4.0), 0.0, 0.0),
));
});
} |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Actually, my offset solution doesn't work. If I make one of the words longer than the width of the Add these two additional squares and text to // Longer + long word; needs to wrap; has TextBounds; offset X by BOUNDS_WIDTH / 4.0
// Offset doesn't work
commands
.spawn((
Mesh2d(square.clone()),
MeshMaterial2d(white.clone()),
Transform::from_xyz(-60.0, -180.0, 0.0),
))
.with_children(|parent| {
parent.spawn((
Text2d("Longword with wrap".to_string()),
TextColor(Color::BLACK),
TextFont {
font_size: 15.0,
..default()
},
TextLayout::new(JustifyText::Center, LineBreak::WordBoundary),
TextBounds::new_horizontal(BOUNDS_WIDTH),
Transform::from_xyz(-(BOUNDS_WIDTH / 4.0), 0.0, 0.0),
));
});
// Longer + long word; needs to wrap; has TextBounds; no offset
// Visually slightly to the right of centre
commands
.spawn((
Mesh2d(square.clone()),
MeshMaterial2d(white.clone()),
Transform::from_xyz(60.0, -180.0, 0.0),
))
.with_children(|parent| {
parent.spawn((
Text2d("Longword with wrap".to_string()),
TextColor(Color::BLACK),
TextFont {
font_size: 15.0,
..default()
},
TextLayout::new(JustifyText::Center, LineBreak::WordBoundary),
TextBounds::new_horizontal(BOUNDS_WIDTH),
));
}); |
Beta Was this translation helpful? Give feedback.
-
This feels a bit like #12319, but with added weirdness.
The There may be a specific issue to raise about this ? |
Beta Was this translation helpful? Give feedback.
-
Thanks for the feedback, I've opened #18789 with an example image showing the problem and code to reproduce. |
Beta Was this translation helpful? Give feedback.
-
It turns out this was a bug and is fixed in 0.16.0. |
Beta Was this translation helpful? Give feedback.
It turns out this was a bug and is fixed in 0.16.0.