Fix the Text2d text anchor's incorrect horizontal alignment (#8019)

This commit is contained in:
ickshonpe 2023-03-10 14:57:41 +00:00 committed by GitHub
parent 8aa217cc8b
commit 729458815c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -103,7 +103,7 @@ pub fn extract_text2d_sprite(
}
let text_glyphs = &text_layout_info.glyphs;
let text_anchor = anchor.as_vec() * Vec2::new(1., -1.) - 0.5;
let text_anchor = -(anchor.as_vec() + 0.5);
let alignment_offset = text_layout_info.size * text_anchor;
let mut color = Color::WHITE;
let mut current_section = usize::MAX;

View File

@ -7,6 +7,7 @@
use bevy::{
prelude::*,
sprite::Anchor,
text::{BreakLineOn, Text2dBounds},
};
@ -133,6 +134,29 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
..default()
});
});
for (text_anchor, color) in [
(Anchor::TopLeft, Color::RED),
(Anchor::TopRight, Color::GREEN),
(Anchor::BottomRight, Color::BLUE),
(Anchor::BottomLeft, Color::YELLOW),
] {
commands.spawn(Text2dBundle {
text: Text {
sections: vec![TextSection::new(
format!(" Anchor::{text_anchor:?} "),
TextStyle {
color,
..slightly_smaller_text_style.clone()
},
)],
..Default::default()
},
transform: Transform::from_translation(250. * Vec3::Y),
text_anchor,
..default()
});
}
}
fn animate_translation(