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

Calculate the text alignment offset #6724

Closed
wants to merge 22 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use bevy_render::{
Extract, RenderApp, RenderStage,
};
use bevy_sprite::{SpriteAssetEvents, TextureAtlas};
use bevy_text::{Text, TextLayoutInfo};
use bevy_text::{HorizontalAlign, Text, TextLayoutInfo, VerticalAlign};
use bevy_transform::components::GlobalTransform;
use bevy_utils::FloatOrd;
use bevy_utils::HashMap;
Expand Down Expand Up @@ -326,9 +326,19 @@ pub fn extract_text_uinodes(
if uinode.size() == Vec2::ZERO {
continue;
}
let text_glyphs = &text_layout_info.glyphs;
let alignment_offset = (uinode.size() / -2.0).extend(0.0);

let alignment_offset = (match text.alignment.vertical {
VerticalAlign::Center => -0.5 * text_layout_info.size.y,
VerticalAlign::Top => -0.5 * uinode.size().y,
VerticalAlign::Bottom => 0.5 * uinode.size().y - text_layout_info.size.y,
} * Vec3::Y)
+ (match text.alignment.horizontal {
HorizontalAlign::Left => -0.5 * uinode.size().x,
HorizontalAlign::Center => -0.5 * text_layout_info.size.x,
HorizontalAlign::Right => 0.5 * uinode.size().x - text_layout_info.size.x,
} * Vec3::X);
ickshonpe marked this conversation as resolved.
Show resolved Hide resolved

let text_glyphs = &text_layout_info.glyphs;
let mut color = Color::WHITE;
let mut current_section = usize::MAX;
for text_glyph in text_glyphs {
Expand All @@ -348,6 +358,7 @@ pub fn extract_text_uinodes(
let atlas_size = Some(atlas.size);

// NOTE: Should match `bevy_text::text2d::extract_text2d_sprite`
ickshonpe marked this conversation as resolved.
Show resolved Hide resolved

let extracted_transform = global_transform.compute_matrix()
* Mat4::from_scale(Vec3::splat(scale_factor.recip()))
* Mat4::from_translation(
Expand Down