Skip to content

Commit 68aebf3

Browse files
authored
Text2d TextBackgroundColor support (#20464)
# Objective Add support for `TextBackgroundColor` to `Text2d`. ## Solution Add a `TextBackgroundColor` query to `extract_text2d_sprite` and queue a background sprite for each text section with a `TextBackgroundColor`. ## Testing Added `TextBackgroundColor`s to some of the text in the `text2d` example: ```cargo run --example text2d``` --- ## Showcase <img width="1924" height="1127" alt="text2d-background" src="https://github.com/user-attachments/assets/faa64dbe-cf80-4aca-b749-d24bb4399db3" />
1 parent 7ce5abe commit 68aebf3

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

crates/bevy_text/src/text2d.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
TextError, TextFont, TextLayout, TextLayoutInfo, TextPipeline, TextReader, TextRoot,
55
TextSpanAccess, TextWriter,
66
};
7+
78
use bevy_asset::Assets;
89
use bevy_camera::primitives::Aabb;
910
use bevy_camera::visibility::{self, NoFrustumCulling, Visibility, VisibilityClass};

crates/bevy_ui_render/src/text2d.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bevy_asset::Assets;
1+
use bevy_asset::{AssetId, Assets};
22
use bevy_camera::visibility::ViewVisibility;
33
use bevy_color::LinearRgba;
44
use bevy_ecs::{
@@ -11,9 +11,12 @@ use bevy_math::Vec2;
1111
use bevy_render::sync_world::TemporaryRenderEntity;
1212
use bevy_render::Extract;
1313
use bevy_sprite::Anchor;
14-
use bevy_sprite_render::{ExtractedSlice, ExtractedSlices, ExtractedSprite, ExtractedSprites};
14+
use bevy_sprite_render::{
15+
ExtractedSlice, ExtractedSlices, ExtractedSprite, ExtractedSpriteKind, ExtractedSprites,
16+
};
1517
use bevy_text::{
16-
ComputedTextBlock, PositionedGlyph, Text2dShadow, TextBounds, TextColor, TextLayoutInfo,
18+
ComputedTextBlock, PositionedGlyph, Text2dShadow, TextBackgroundColor, TextBounds, TextColor,
19+
TextLayoutInfo,
1720
};
1821
use bevy_transform::prelude::GlobalTransform;
1922
use bevy_window::{PrimaryWindow, Window};
@@ -39,6 +42,7 @@ pub fn extract_text2d_sprite(
3942
)>,
4043
>,
4144
text_colors: Extract<Query<&TextColor>>,
45+
text_background_colors_query: Extract<Query<&TextBackgroundColor>>,
4246
) {
4347
let mut start = extracted_slices.slices.len();
4448
let mut end = start + 1;
@@ -72,6 +76,33 @@ pub fn extract_text2d_sprite(
7276

7377
let top_left = (Anchor::TOP_LEFT.0 - anchor.as_vec()) * size;
7478

79+
for &(section_entity, rect) in text_layout_info.section_rects.iter() {
80+
let Ok(text_background_color) = text_background_colors_query.get(section_entity) else {
81+
continue;
82+
};
83+
let render_entity = commands.spawn(TemporaryRenderEntity).id();
84+
let offset = Vec2::new(rect.center().x, -rect.center().y);
85+
let transform = *global_transform
86+
* GlobalTransform::from_translation(top_left.extend(0.))
87+
* scaling
88+
* GlobalTransform::from_translation(offset.extend(0.));
89+
extracted_sprites.sprites.push(ExtractedSprite {
90+
main_entity,
91+
render_entity,
92+
transform,
93+
color: text_background_color.0.into(),
94+
image_handle_id: AssetId::default(),
95+
flip_x: false,
96+
flip_y: false,
97+
kind: ExtractedSpriteKind::Single {
98+
anchor: Vec2::ZERO,
99+
rect: None,
100+
scaling_mode: None,
101+
custom_size: Some(rect.size()),
102+
},
103+
});
104+
}
105+
75106
if let Some(shadow) = maybe_shadow {
76107
let shadow_transform = *global_transform
77108
* GlobalTransform::from_translation((top_left + shadow.offset).extend(0.))
@@ -112,7 +143,7 @@ pub fn extract_text2d_sprite(
112143
image_handle_id: atlas_info.texture,
113144
flip_x: false,
114145
flip_y: false,
115-
kind: bevy_sprite_render::ExtractedSpriteKind::Slices {
146+
kind: ExtractedSpriteKind::Slices {
116147
indices: start..end,
117148
},
118149
});
@@ -174,7 +205,7 @@ pub fn extract_text2d_sprite(
174205
image_handle_id: atlas_info.texture,
175206
flip_x: false,
176207
flip_y: false,
177-
kind: bevy_sprite_render::ExtractedSpriteKind::Slices {
208+
kind: ExtractedSpriteKind::Slices {
178209
indices: start..end,
179210
},
180211
});

examples/2d/text2d.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,29 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
4444
commands.spawn(Camera2d);
4545
// Demonstrate changing translation
4646
commands.spawn((
47-
Text2d::new("translation"),
47+
Text2d::new(" translation "),
4848
text_font.clone(),
4949
TextLayout::new_with_justify(text_justification),
50+
TextBackgroundColor(Color::BLACK.with_alpha(0.5)),
5051
Text2dShadow::default(),
5152
AnimateTranslation,
5253
));
5354
// Demonstrate changing rotation
5455
commands.spawn((
55-
Text2d::new("rotation"),
56+
Text2d::new(" rotation "),
5657
text_font.clone(),
5758
TextLayout::new_with_justify(text_justification),
59+
TextBackgroundColor(Color::BLACK.with_alpha(0.5)),
5860
Text2dShadow::default(),
5961
AnimateRotation,
6062
));
6163
// Demonstrate changing scale
6264
commands.spawn((
63-
Text2d::new("scale"),
65+
Text2d::new(" scale "),
6466
text_font,
6567
TextLayout::new_with_justify(text_justification),
6668
Transform::from_translation(Vec3::new(400.0, 0.0, 0.0)),
69+
TextBackgroundColor(Color::BLACK.with_alpha(0.5)),
6770
Text2dShadow::default(),
6871
AnimateScale,
6972
));
@@ -150,16 +153,20 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
150153
Text2d::new(" Anchor".to_string()),
151154
slightly_smaller_text_font.clone(),
152155
text_anchor,
156+
TextBackgroundColor(Color::WHITE.darker(0.8)),
157+
Transform::from_translation(-1. * Vec3::Z),
153158
))
154159
.with_child((
155160
TextSpan("::".to_string()),
156161
slightly_smaller_text_font.clone(),
157162
TextColor(LIGHT_GREY.into()),
163+
TextBackgroundColor(DARK_BLUE.into()),
158164
))
159165
.with_child((
160166
TextSpan(format!("{text_anchor:?} ")),
161167
slightly_smaller_text_font.clone(),
162168
TextColor(color),
169+
TextBackgroundColor(color.darker(0.3)),
163170
));
164171
}
165172
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: `TextBackgroundColor` support for `Text2d`
3+
authors: ["@ickshonpe"]
4+
pull_requests: [20464]
5+
---
6+
7+
`Text2d` now supports the `TextBackgroundColor` component.
8+
9+
Add a `TextBackgroundColor` to `Text2d` entity or its child `TextSection` entities to draw a background color for that section of text.

0 commit comments

Comments
 (0)