From 6f269ccadba8052789da2317a88473b24deeb6f7 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 24 Nov 2022 09:58:09 +0000 Subject: [PATCH 1/6] changes: * removed the Text2dSize component * clarified the text2d alignment offset calculations * removes some unnecessary scale factor multiplications --- crates/bevy_text/src/text2d.rs | 63 +++++++++++++--------------------- 1 file changed, 23 insertions(+), 40 deletions(-) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index 43cafbffe8923..3368f19a7aaff 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -26,13 +26,6 @@ use crate::{ TextSettings, VerticalAlign, YAxisOrientation, }; -/// The calculated size of text drawn in 2D scene. -#[derive(Component, Default, Copy, Clone, Debug, Reflect)] -#[reflect(Component)] -pub struct Text2dSize { - pub size: Vec2, -} - /// The maximum width and height of text. The text will wrap according to the specified size. /// Characters out of the bounds after wrapping will be truncated. Text is aligned according to the /// specified `TextAlignment`. @@ -61,7 +54,6 @@ pub struct Text2dBundle { pub text: Text, pub transform: Transform, pub global_transform: GlobalTransform, - pub text_2d_size: Text2dSize, pub text_2d_bounds: Text2dBounds, pub visibility: Visibility, pub computed_visibility: ComputedVisibility, @@ -78,30 +70,31 @@ pub fn extract_text2d_sprite( &Text, &TextLayoutInfo, &GlobalTransform, - &Text2dSize, )>, >, ) { let scale_factor = windows.scale_factor(WindowId::primary()) as f32; - for (entity, computed_visibility, text, text_layout_info, text_transform, calculated_size) in - text2d_query.iter() + for (entity, computed_visibility, text, text_layout_info, text_transform) in text2d_query.iter() { if !computed_visibility.is_visible() { continue; } - let (width, height) = (calculated_size.size.x, calculated_size.size.y); let text_glyphs = &text_layout_info.glyphs; - let alignment_offset = match text.alignment.vertical { - VerticalAlign::Top => Vec3::new(0.0, -height, 0.0), - VerticalAlign::Center => Vec3::new(0.0, -height * 0.5, 0.0), - VerticalAlign::Bottom => Vec3::ZERO, - } + match text.alignment.horizontal { - HorizontalAlign::Left => Vec3::ZERO, - HorizontalAlign::Center => Vec3::new(-width * 0.5, 0.0, 0.0), - HorizontalAlign::Right => Vec3::new(-width, 0.0, 0.0), - }; + let alignment_offset = -text_layout_info.size + * Vec2::new( + match text.alignment.horizontal { + HorizontalAlign::Left => 0.0, + HorizontalAlign::Center => 0.5, + HorizontalAlign::Right => 1., + }, + match text.alignment.vertical { + VerticalAlign::Top => 0.0, + VerticalAlign::Center => 0.5, + VerticalAlign::Bottom => 1., + }, + ); let mut color = Color::WHITE; let mut current_section = usize::MAX; @@ -120,10 +113,9 @@ pub fn extract_text2d_sprite( let index = text_glyph.atlas_info.glyph_index; let rect = Some(atlas.textures[index]); - let glyph_transform = Transform::from_translation( - alignment_offset * scale_factor + text_glyph.position.extend(0.), - ); - // NOTE: Should match `bevy_ui::render::extract_text_uinodes` + let glyph_transform = + Transform::from_translation((alignment_offset + text_glyph.position).extend(0.)); + let transform = *text_transform * GlobalTransform::from_scale(Vec3::splat(scale_factor.recip())) * glyph_transform; @@ -168,7 +160,6 @@ pub fn update_text2d_layout( Changed, &Text, Option<&Text2dBounds>, - &mut Text2dSize, Option<&mut TextLayoutInfo>, )>, ) { @@ -176,9 +167,7 @@ pub fn update_text2d_layout( let factor_changed = scale_factor_changed.iter().last().is_some(); let scale_factor = windows.scale_factor(WindowId::primary()); - for (entity, text_changed, text, maybe_bounds, mut calculated_size, text_layout_info) in - &mut text_query - { + for (entity, text_changed, text, maybe_bounds, text_layout_info) in &mut text_query { if factor_changed || text_changed || queue.remove(&entity) { let text_bounds = match maybe_bounds { Some(bounds) => Vec2::new( @@ -209,18 +198,12 @@ pub fn update_text2d_layout( | Err(e @ TextError::ExceedMaxTextAtlases(_)) => { panic!("Fatal error when processing text: {e}."); } - Ok(info) => { - calculated_size.size = Vec2::new( - scale_value(info.size.x, 1. / scale_factor), - scale_value(info.size.y, 1. / scale_factor), - ); - match text_layout_info { - Some(mut t) => *t = info, - None => { - commands.entity(entity).insert(info); - } + Ok(info) => match text_layout_info { + Some(mut t) => *t = info, + None => { + commands.entity(entity).insert(info); } - } + }, } } } From 5a7455336c03c5d2b751e39ad99fbfa97b9f7ea3 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 24 Nov 2022 10:17:02 +0000 Subject: [PATCH 2/6] fixed vertical alignment direction --- crates/bevy_text/src/text2d.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index 3368f19a7aaff..95f799e1dfea4 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -90,9 +90,9 @@ pub fn extract_text2d_sprite( HorizontalAlign::Right => 1., }, match text.alignment.vertical { - VerticalAlign::Top => 0.0, + VerticalAlign::Bottom => 0., VerticalAlign::Center => 0.5, - VerticalAlign::Bottom => 1., + VerticalAlign::Top => 1., }, ); From 0e736a5049b14a361febf9e932a70d109f7e368f Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 24 Nov 2022 11:21:28 +0000 Subject: [PATCH 3/6] query on Text2dBounds --- crates/bevy_text/src/text2d.rs | 44 ++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index 95f799e1dfea4..4cb0e7a515f84 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -4,7 +4,7 @@ use bevy_ecs::{ component::Component, entity::Entity, event::EventReader, - query::Changed, + query::{Changed, With}, reflect::ReflectComponent, system::{Commands, Local, Query, Res, ResMut}, }; @@ -64,13 +64,16 @@ pub fn extract_text2d_sprite( texture_atlases: Extract>>, windows: Extract>, text2d_query: Extract< - Query<( - Entity, - &ComputedVisibility, - &Text, - &TextLayoutInfo, - &GlobalTransform, - )>, + Query< + ( + Entity, + &ComputedVisibility, + &Text, + &TextLayoutInfo, + &GlobalTransform, + ), + With, + >, >, ) { let scale_factor = windows.scale_factor(WindowId::primary()) as f32; @@ -90,7 +93,7 @@ pub fn extract_text2d_sprite( HorizontalAlign::Right => 1., }, match text.alignment.vertical { - VerticalAlign::Bottom => 0., + VerticalAlign::Bottom => 0., VerticalAlign::Center => 0.5, VerticalAlign::Top => 1., }, @@ -159,7 +162,7 @@ pub fn update_text2d_layout( Entity, Changed, &Text, - Option<&Text2dBounds>, + &Text2dBounds, Option<&mut TextLayoutInfo>, )>, ) { @@ -167,15 +170,20 @@ pub fn update_text2d_layout( let factor_changed = scale_factor_changed.iter().last().is_some(); let scale_factor = windows.scale_factor(WindowId::primary()); - for (entity, text_changed, text, maybe_bounds, text_layout_info) in &mut text_query { + for (entity, text_changed, text, bounds, text_layout_info) in &mut text_query { if factor_changed || text_changed || queue.remove(&entity) { - let text_bounds = match maybe_bounds { - Some(bounds) => Vec2::new( - scale_value(bounds.size.x, scale_factor), - scale_value(bounds.size.y, scale_factor), - ), - None => Vec2::new(f32::MAX, f32::MAX), - }; + let text_bounds = Vec2::new( + if bounds.size.x != f32::MAX { + scale_value(bounds.size.x, scale_factor) + } else { + f32::MAX + }, + if bounds.size.x != f32::MAX { + scale_value(bounds.size.y, scale_factor) + } else { + f32::MAX + }, + ); match text_pipeline.queue_text( &fonts, From f02edeed65368a517369dfb7d7c63708504d33d8 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 24 Nov 2022 14:32:40 +0000 Subject: [PATCH 4/6] remove trailing 0 --- crates/bevy_text/src/text2d.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index 4cb0e7a515f84..ae40e68772c7a 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -88,7 +88,7 @@ pub fn extract_text2d_sprite( let alignment_offset = -text_layout_info.size * Vec2::new( match text.alignment.horizontal { - HorizontalAlign::Left => 0.0, + HorizontalAlign::Left => 0., HorizontalAlign::Center => 0.5, HorizontalAlign::Right => 1., }, From 41d90d0f6ad9ee29b2612cb50c61472bd6670259 Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 24 Nov 2022 16:12:14 +0000 Subject: [PATCH 5/6] replaced Vec2::new with splat --- crates/bevy_text/src/text2d.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index ae40e68772c7a..debdce198d76e 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -42,7 +42,7 @@ pub struct Text2dBounds { impl Default for Text2dBounds { fn default() -> Self { Self { - size: Vec2::new(f32::MAX, f32::MAX), + size: Vec2::splat(f32::MAX), } } } From 0950767968d40203a80b3dcfe3471b9b2768c21d Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Wed, 30 Nov 2022 16:55:34 +0000 Subject: [PATCH 6/6] added UNBOUNDED const value for unbounded text --- crates/bevy_text/src/text2d.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index debdce198d76e..215848d735d3c 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -40,13 +40,16 @@ pub struct Text2dBounds { } impl Default for Text2dBounds { + #[inline] fn default() -> Self { - Self { - size: Vec2::splat(f32::MAX), - } + Self::UNBOUNDED } } +impl Text2dBounds { + pub const UNBOUNDED: Self = Self { size: Vec2::splat(f32::MAX) }; +} + /// The bundle of components needed to draw text in a 2D scene via a 2D `Camera2dBundle`. /// [Example usage.](https://github.com/bevyengine/bevy/blob/latest/examples/2d/text2d.rs) #[derive(Bundle, Clone, Debug, Default)]