1
1
use crate :: pipeline:: CosmicFontSystem ;
2
2
use crate :: {
3
- CosmicBuffer , Font , FontAtlasSets , LineBreak , PositionedGlyph , SwashCache , Text , TextBounds ,
4
- TextError , TextLayoutInfo , TextPipeline , YAxisOrientation ,
3
+ ComputedTextBlock , Font , FontAtlasSets , LineBreak , PositionedGlyph , SwashCache , TextBlock ,
4
+ TextBlocks , TextBounds , TextError , TextLayoutInfo , TextPipeline , TextSpanReader , TextStyle ,
5
+ YAxisOrientation ,
5
6
} ;
6
7
use bevy_asset:: Assets ;
7
8
use bevy_color:: LinearRgba ;
9
+ use bevy_derive:: { Deref , DerefMut } ;
10
+ use bevy_ecs:: component:: Component ;
8
11
use bevy_ecs:: {
9
- bundle:: Bundle ,
10
12
change_detection:: { DetectChanges , Ref } ,
11
13
entity:: Entity ,
12
14
event:: EventReader ,
13
- prelude:: With ,
15
+ prelude:: { ReflectComponent , With } ,
14
16
query:: { Changed , Without } ,
15
17
system:: { Commands , Local , Query , Res , ResMut } ,
16
18
} ;
17
19
use bevy_math:: Vec2 ;
20
+ use bevy_reflect:: { prelude:: ReflectDefault , Reflect } ;
21
+ use bevy_render:: view:: Visibility ;
18
22
use bevy_render:: {
19
23
primitives:: Aabb ,
20
24
texture:: Image ,
21
- view:: { InheritedVisibility , NoFrustumCulling , ViewVisibility , Visibility } ,
25
+ view:: { NoFrustumCulling , ViewVisibility } ,
22
26
Extract ,
23
27
} ;
24
28
use bevy_sprite:: { Anchor , ExtractedSprite , ExtractedSprites , SpriteSource , TextureAtlasLayout } ;
25
- use bevy_transform:: prelude:: { GlobalTransform , Transform } ;
29
+ use bevy_transform:: components:: Transform ;
30
+ use bevy_transform:: prelude:: GlobalTransform ;
26
31
use bevy_utils:: HashSet ;
27
32
use bevy_window:: { PrimaryWindow , Window , WindowScaleFactorChanged } ;
28
33
@@ -89,6 +94,12 @@ impl Text2d {
89
94
}
90
95
}
91
96
97
+ impl TextSpanReader for Text2d {
98
+ fn read_span ( & self ) -> & str {
99
+ self . as_str ( )
100
+ }
101
+ }
102
+
92
103
impl From < & str > for Text2d {
93
104
fn from ( value : & str ) -> Self {
94
105
Self ( String :: from ( value) )
@@ -134,9 +145,9 @@ world.spawn((
134
145
));
135
146
```
136
147
*/
137
- #[ derive( Component , Clone , Debug , Default , Reflect ) ]
148
+ #[ derive( Component , Clone , Debug , Default , Deref , DerefMut , Reflect ) ]
138
149
#[ reflect( Component , Default , Debug ) ]
139
- #[ require( TextStyle , Visibility = Visibility :: Hidden , Transform ) ]
150
+ #[ require( TextStyle , Visibility ( visibility_hidden ) , Transform ) ]
140
151
pub struct TextSpan2d ( pub String ) ;
141
152
142
153
impl TextSpanReader for TextSpan2d {
@@ -145,6 +156,10 @@ impl TextSpanReader for TextSpan2d {
145
156
}
146
157
}
147
158
159
+ fn visibility_hidden ( ) -> Visibility {
160
+ Visibility :: Hidden
161
+ }
162
+
148
163
/// This system extracts the sprites from the 2D text components and adds them to the
149
164
/// "render world".
150
165
pub fn extract_text2d_sprite (
@@ -204,6 +219,7 @@ pub fn extract_text2d_sprite(
204
219
computed_block
205
220
. entities ( )
206
221
. get ( * span_index)
222
+ . map ( |t| t. entity )
207
223
. unwrap_or ( Entity :: PLACEHOLDER ) ,
208
224
)
209
225
. map ( |style| LinearRgba :: from ( style. color ) )
@@ -251,13 +267,12 @@ pub fn update_text2d_layout(
251
267
mut text_pipeline : ResMut < TextPipeline > ,
252
268
mut text_query : Query < (
253
269
Entity ,
254
- Ref < Text2d > ,
255
270
Ref < TextBlock > ,
256
271
Ref < TextBounds > ,
257
272
& mut TextLayoutInfo ,
258
273
& mut ComputedTextBlock ,
259
274
) > ,
260
- mut spans : TextSpans < TextSpan2d > ,
275
+ mut blocks : TextBlocks < Text2d , TextSpan2d > ,
261
276
mut font_system : ResMut < CosmicFontSystem > ,
262
277
mut swash_cache : ResMut < SwashCache > ,
263
278
) {
@@ -272,14 +287,14 @@ pub fn update_text2d_layout(
272
287
273
288
let inverse_scale_factor = scale_factor. recip ( ) ;
274
289
275
- for ( entity, text , block, bounds, text_layout_info, mut computed) in & mut text_query {
290
+ for ( entity, block, bounds, text_layout_info, mut computed) in & mut text_query {
276
291
if factor_changed
277
292
|| computed. needs_rerender ( )
278
293
|| bounds. is_changed ( )
279
294
|| queue. remove ( & entity)
280
295
{
281
296
let text_bounds = TextBounds {
282
- width : if text . linebreak == LineBreak :: NoWrap {
297
+ width : if block . linebreak == LineBreak :: NoWrap {
283
298
None
284
299
} else {
285
300
bounds. width . map ( |width| scale_value ( width, scale_factor) )
@@ -293,9 +308,9 @@ pub fn update_text2d_layout(
293
308
match text_pipeline. queue_text (
294
309
text_layout_info,
295
310
& fonts,
296
- spans . iter_from_base ( entity, text . as_str ( ) , text_style , maybe_children ) ,
311
+ blocks . iter ( entity) ,
297
312
scale_factor. into ( ) ,
298
- block,
313
+ & block,
299
314
text_bounds,
300
315
& mut font_atlas_sets,
301
316
& mut texture_atlases,
0 commit comments