@@ -3,16 +3,18 @@ use bevy_ecs::{
3
3
bundle:: Bundle ,
4
4
component:: Component ,
5
5
entity:: Entity ,
6
- query:: { Changed , With } ,
6
+ event:: EventReader ,
7
+ query:: Changed ,
7
8
reflect:: ReflectComponent ,
8
- system:: { Local , ParamSet , Query , Res , ResMut } ,
9
+ system:: { Local , Query , Res , ResMut } ,
9
10
} ;
10
11
use bevy_math:: { Vec2 , Vec3 } ;
11
12
use bevy_reflect:: Reflect ;
12
13
use bevy_render:: { texture:: Image , view:: Visibility , RenderWorld } ;
13
14
use bevy_sprite:: { Anchor , ExtractedSprite , ExtractedSprites , TextureAtlas } ;
14
15
use bevy_transform:: prelude:: { GlobalTransform , Transform } ;
15
- use bevy_window:: { WindowId , Windows } ;
16
+ use bevy_utils:: HashSet ;
17
+ use bevy_window:: { WindowId , WindowScaleFactorChanged , Windows } ;
16
18
17
19
use crate :: {
18
20
DefaultTextPipeline , Font , FontAtlasSet , HorizontalAlign , Text , TextError , VerticalAlign ,
@@ -123,44 +125,34 @@ pub fn extract_text2d_sprite(
123
125
}
124
126
}
125
127
126
- #[ derive( Debug , Default ) ]
127
- pub struct QueuedText2d {
128
- entities : Vec < Entity > ,
129
- }
130
-
131
128
/// Updates the layout and size information whenever the text or style is changed.
132
129
/// This information is computed by the `TextPipeline` on insertion, then stored.
133
130
#[ allow( clippy:: too_many_arguments, clippy:: type_complexity) ]
134
- pub fn text2d_system (
135
- mut queued_text : Local < QueuedText2d > ,
131
+ pub fn update_text2d_layout (
132
+ // Text items which should be reprocessed again, generally when the font hasn't loaded yet.
133
+ mut queue : Local < HashSet < Entity > > ,
136
134
mut textures : ResMut < Assets < Image > > ,
137
135
fonts : Res < Assets < Font > > ,
138
136
windows : Res < Windows > ,
137
+ mut scale_factor_changed : EventReader < WindowScaleFactorChanged > ,
139
138
mut texture_atlases : ResMut < Assets < TextureAtlas > > ,
140
139
mut font_atlas_set_storage : ResMut < Assets < FontAtlasSet > > ,
141
140
mut text_pipeline : ResMut < DefaultTextPipeline > ,
142
- mut text_queries : ParamSet < (
143
- Query < Entity , ( With < Text2dSize > , Changed < Text > ) > ,
144
- Query < ( & Text , Option < & Text2dBounds > , & mut Text2dSize ) , With < Text2dSize > > ,
141
+ mut text_query : Query < (
142
+ Entity ,
143
+ Changed < Text > ,
144
+ & Text ,
145
+ Option < & Text2dBounds > ,
146
+ & mut Text2dSize ,
145
147
) > ,
146
148
) {
147
- // Adds all entities where the text or the style has changed to the local queue
148
- for entity in text_queries. p0 ( ) . iter_mut ( ) {
149
- queued_text. entities . push ( entity) ;
150
- }
151
-
152
- if queued_text. entities . is_empty ( ) {
153
- return ;
154
- }
155
-
149
+ // We need to consume the entire iterator, hence `last`
150
+ let factor_changed = scale_factor_changed. iter ( ) . last ( ) . is_some ( ) ;
156
151
let scale_factor = windows. scale_factor ( WindowId :: primary ( ) ) ;
157
152
158
- // Computes all text in the local queue
159
- let mut new_queue = Vec :: new ( ) ;
160
- let mut query = text_queries. p1 ( ) ;
161
- for entity in queued_text. entities . drain ( ..) {
162
- if let Ok ( ( text, bounds, mut calculated_size) ) = query. get_mut ( entity) {
163
- let text_bounds = match bounds {
153
+ for ( entity, text_changed, text, maybe_bounds, mut calculated_size) in text_query. iter_mut ( ) {
154
+ if factor_changed || text_changed || queue. remove ( & entity) {
155
+ let text_bounds = match maybe_bounds {
164
156
Some ( bounds) => Vec2 :: new (
165
157
scale_value ( bounds. size . x , scale_factor) ,
166
158
scale_value ( bounds. size . y , scale_factor) ,
@@ -181,7 +173,7 @@ pub fn text2d_system(
181
173
Err ( TextError :: NoSuchFont ) => {
182
174
// There was an error processing the text layout, let's add this entity to the
183
175
// queue for further processing
184
- new_queue . push ( entity) ;
176
+ queue . insert ( entity) ;
185
177
}
186
178
Err ( e @ TextError :: FailedToAddGlyph ( _) ) => {
187
179
panic ! ( "Fatal error when processing text: {}." , e) ;
@@ -198,8 +190,6 @@ pub fn text2d_system(
198
190
}
199
191
}
200
192
}
201
-
202
- queued_text. entities = new_queue;
203
193
}
204
194
205
195
pub fn scale_value ( value : f32 , factor : f64 ) -> f32 {
0 commit comments