From 98dcee28532647a3530b73174b227abd0cccd3a4 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Tue, 11 Feb 2025 22:18:47 +0000 Subject: [PATCH] UI text extraction refactor (#17805) ## Objective There's no need for the `span_index` and `color` variables in `extract_text_shadows` and `extract_text_sections` and we can remove one of the span index comparisons since text colors are only set per section. ## Testing trace --- crates/bevy_ui/src/render/mod.rs | 36 +++++++++++--------------------- 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index d7eebc35b032e..f41c8f2b81620 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -739,9 +739,6 @@ pub fn extract_text_sections( let transform = global_transform.affine() * bevy_math::Affine3A::from_translation((-0.5 * uinode.size()).extend(0.)); - let mut color = LinearRgba::WHITE; - let mut current_span = usize::MAX; - for ( i, PositionedGlyph { @@ -752,20 +749,6 @@ pub fn extract_text_sections( }, ) in text_layout_info.glyphs.iter().enumerate() { - if *span_index != current_span { - color = text_styles - .get( - computed_block - .entities() - .get(*span_index) - .map(|t| t.entity) - .unwrap_or(Entity::PLACEHOLDER), - ) - .map(|text_color| LinearRgba::from(text_color.0)) - .unwrap_or_default(); - current_span = *span_index; - } - let rect = texture_atlases .get(&atlas_info.texture_atlas) .unwrap() @@ -777,8 +760,18 @@ pub fn extract_text_sections( }); if text_layout_info.glyphs.get(i + 1).is_none_or(|info| { - info.span_index != current_span || info.atlas_info.texture != atlas_info.texture + info.span_index != *span_index || info.atlas_info.texture != atlas_info.texture }) { + let color = text_styles + .get( + computed_block + .entities() + .get(*span_index) + .map(|t| t.entity) + .unwrap_or(Entity::PLACEHOLDER), + ) + .map(|text_color| LinearRgba::from(text_color.0)) + .unwrap_or_default(); extracted_uinodes.uinodes.push(ExtractedUiNode { render_entity: commands.spawn(TemporaryRenderEntity).id(), stack_index: uinode.stack_index, @@ -850,7 +843,6 @@ pub fn extract_text_shadows( (-0.5 * uinode.size() + shadow.offset / uinode.inverse_scale_factor()).extend(0.), ); - let mut current_span = usize::MAX; for ( i, PositionedGlyph { @@ -861,10 +853,6 @@ pub fn extract_text_shadows( }, ) in text_layout_info.glyphs.iter().enumerate() { - if *span_index != current_span { - current_span = *span_index; - } - let rect = texture_atlases .get(&atlas_info.texture_atlas) .unwrap() @@ -876,7 +864,7 @@ pub fn extract_text_shadows( }); if text_layout_info.glyphs.get(i + 1).is_none_or(|info| { - info.span_index != current_span || info.atlas_info.texture != atlas_info.texture + info.span_index != *span_index || info.atlas_info.texture != atlas_info.texture }) { extracted_uinodes.uinodes.push(ExtractedUiNode { render_entity: commands.spawn(TemporaryRenderEntity).id(),