Skip to content

Commit

Permalink
Fix text positioning in bevy_ui
Browse files Browse the repository at this point in the history
  • Loading branch information
AlisCode committed Nov 6, 2020
1 parent 5a6768b commit 7d6b66c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
6 changes: 3 additions & 3 deletions crates/bevy_text/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ impl TextPipeline {
let first_glyph = section_glyphs.first().unwrap();
let mut min_x: f32 = first_glyph.glyph.position.x - scaled_font.h_side_bearing(first_glyph.glyph.id);
let mut min_y: f32 = first_glyph.glyph.position.y - scaled_font.ascent();
let mut max_x: f32 = 0.0;
let mut max_y: f32 = 0.0;
let mut max_x: f32 = first_glyph.glyph.position.x + scaled_font.h_advance(first_glyph.glyph.id);
let mut max_y: f32 = first_glyph.glyph.position.y - scaled_font.descent();
for section_glyph in section_glyphs.iter() {
let glyph = &section_glyph.glyph;
min_x = min_x.min(glyph.position.x - scaled_font.h_side_bearing(glyph.id));
min_y = min_y.min(glyph.position.y - scaled_font.ascent());
max_x = max_x.max(glyph.position.x + scaled_font.h_advance(glyph.id));
max_y = max_y.max(glyph.position.y - scaled_font.descent());
}
let size = Size::new(max_x - min_x, max_y - min_y - scaled_font.descent());
let size = Size::new(max_x - min_x, max_y - min_y);
Ok(size)
}

Expand Down
29 changes: 17 additions & 12 deletions crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ pub struct Text {
pub style: TextStyle,
}

/// Defines how min_size, size, and max_size affects the bounds of a text
/// block.
pub fn text_constraint(min_size: Val, size: Val, max_size: Val) -> f32 {
// Needs support for percentages
match (min_size, size, max_size) {
(_, _, Val::Px(max)) => max,
(Val::Px(min), _, _) => min,
(Val::Undefined, Val::Px(size), Val::Undefined) => size,
(Val::Auto, Val::Px(size), Val::Auto) => size,
_ => f32::MAX,
}
}

/// Computes the size of a text block and updates the TextVertices with the
/// new computed vertices
pub fn text_system(
mut textures: ResMut<Assets<Texture>>,
fonts: Res<Assets<Font>>,
Expand All @@ -39,18 +54,8 @@ pub fn text_system(
) {
for ((text, style), mut vertices, mut calculated_size) in &mut text_query.iter() {
let node_size = Size::new(
match style.size.width {
Val::Auto => f32::MAX,
Val::Undefined => f32::MAX,
Val::Px(num) => num,
Val::Percent(_) => f32::MAX, // TODO: support percentages
},
match style.size.height {
Val::Auto => f32::MAX,
Val::Undefined => f32::MAX,
Val::Px(num) => num,
Val::Percent(_) => f32::MAX, // TODO: support percentages
},
text_constraint(style.min_size.width, style.size.width, style.max_size.width),
text_constraint(style.min_size.height, style.size.height, style.max_size.height),
);

if let Err(e) = text_pipeline.queue_text(
Expand Down
8 changes: 4 additions & 4 deletions examples/ui/text_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
right: Val::Px(15.0),
..Default::default()
},
size: Size {
width: Val::Px(400.0),
..Default::default()
max_size: Size {
width: Val::Px(400.),
height: Val::Undefined,
},
..Default::default()
},
text: Text {
value:
"This is very long text with limited width in the top right and is also pink"
"This is very long text with limited width in the top right and is also pink"
.to_string(),
font: font.clone(),
style: TextStyle {
Expand Down

0 comments on commit 7d6b66c

Please sign in to comment.