From 8a51406f2af30c2ecd5dc31626c38cc180d4f10a Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 7 May 2022 12:41:20 +0100 Subject: [PATCH 1/4] Update layout/style when scale factor changes too --- crates/bevy_text/src/text2d.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index 9127c5932bfcf..ae25b56c1aa03 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -3,6 +3,7 @@ use bevy_ecs::{ bundle::Bundle, component::Component, entity::Entity, + event::EventReader, query::{Changed, With}, reflect::ReflectComponent, system::{Local, ParamSet, Query, Res, ResMut}, @@ -12,7 +13,7 @@ use bevy_reflect::Reflect; use bevy_render::{texture::Image, view::Visibility, RenderWorld}; use bevy_sprite::{Anchor, ExtractedSprite, ExtractedSprites, TextureAtlas}; use bevy_transform::prelude::{GlobalTransform, Transform}; -use bevy_window::{WindowId, Windows}; +use bevy_window::{WindowId, WindowScaleFactorChanged, Windows}; use crate::{ DefaultTextPipeline, Font, FontAtlasSet, HorizontalAlign, Text, TextError, VerticalAlign, @@ -136,17 +137,22 @@ pub fn text2d_system( mut textures: ResMut>, fonts: Res>, windows: Res, + mut scale_factor_changed: EventReader, mut texture_atlases: ResMut>, mut font_atlas_set_storage: ResMut>, mut text_pipeline: ResMut, mut text_queries: ParamSet<( - Query, Changed)>, + Query<(Entity, Changed), With>, Query<(&Text, Option<&Text2dBounds>, &mut Text2dSize), With>, )>, ) { + // We need to consume the entire iterator, hence `last` + let changed = scale_factor_changed.iter().last().is_some(); // Adds all entities where the text or the style has changed to the local queue - for entity in text_queries.p0().iter_mut() { - queued_text.entities.push(entity); + for (entity, text_changed) in text_queries.p0().iter_mut() { + if changed | text_changed { + queued_text.entities.push(entity); + } } if queued_text.entities.is_empty() { From ad73ef2c1f69a763e4506d934d84be8f4e419ed4 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 7 May 2022 12:54:35 +0100 Subject: [PATCH 2/4] Migrate to single query --- crates/bevy_text/src/text2d.rs | 44 +++++++++++++--------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index ae25b56c1aa03..12dc33024f3e0 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -4,15 +4,16 @@ use bevy_ecs::{ component::Component, entity::Entity, event::EventReader, - query::{Changed, With}, + query::Changed, reflect::ReflectComponent, - system::{Local, ParamSet, Query, Res, ResMut}, + system::{Local, Query, Res, ResMut}, }; use bevy_math::{Vec2, Vec3}; use bevy_reflect::Reflect; use bevy_render::{texture::Image, view::Visibility, RenderWorld}; use bevy_sprite::{Anchor, ExtractedSprite, ExtractedSprites, TextureAtlas}; use bevy_transform::prelude::{GlobalTransform, Transform}; +use bevy_utils::HashSet; use bevy_window::{WindowId, WindowScaleFactorChanged, Windows}; use crate::{ @@ -126,14 +127,14 @@ pub fn extract_text2d_sprite( #[derive(Debug, Default)] pub struct QueuedText2d { - entities: Vec, + entities: HashSet, } /// Updates the layout and size information whenever the text or style is changed. /// This information is computed by the `TextPipeline` on insertion, then stored. #[allow(clippy::too_many_arguments, clippy::type_complexity)] pub fn text2d_system( - mut queued_text: Local, + mut queue: Local, mut textures: ResMut>, fonts: Res>, windows: Res, @@ -141,32 +142,21 @@ pub fn text2d_system( mut texture_atlases: ResMut>, mut font_atlas_set_storage: ResMut>, mut text_pipeline: ResMut, - mut text_queries: ParamSet<( - Query<(Entity, Changed), With>, - Query<(&Text, Option<&Text2dBounds>, &mut Text2dSize), With>, + mut text_query: Query<( + Entity, + Changed, + &Text, + Option<&Text2dBounds>, + &mut Text2dSize, )>, ) { // We need to consume the entire iterator, hence `last` - let changed = scale_factor_changed.iter().last().is_some(); - // Adds all entities where the text or the style has changed to the local queue - for (entity, text_changed) in text_queries.p0().iter_mut() { - if changed | text_changed { - queued_text.entities.push(entity); - } - } - - if queued_text.entities.is_empty() { - return; - } - + let factor_changed = scale_factor_changed.iter().last().is_some(); let scale_factor = windows.scale_factor(WindowId::primary()); - // Computes all text in the local queue - let mut new_queue = Vec::new(); - let mut query = text_queries.p1(); - for entity in queued_text.entities.drain(..) { - if let Ok((text, bounds, mut calculated_size)) = query.get_mut(entity) { - let text_bounds = match bounds { + for (entity, text_changed, text, maybe_bounds, mut calculated_size) in text_query.iter_mut() { + if factor_changed || text_changed || queue.entities.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), @@ -187,7 +177,7 @@ pub fn text2d_system( Err(TextError::NoSuchFont) => { // There was an error processing the text layout, let's add this entity to the // queue for further processing - new_queue.push(entity); + queue.entities.insert(entity); } Err(e @ TextError::FailedToAddGlyph(_)) => { panic!("Fatal error when processing text: {}.", e); @@ -204,8 +194,6 @@ pub fn text2d_system( } } } - - queued_text.entities = new_queue; } pub fn scale_value(value: f32, factor: f64) -> f32 { From 959d3a8c1b1398c6ac6df6bf7d5986af6a807cb7 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 7 May 2022 14:49:16 +0100 Subject: [PATCH 3/4] Remove superfluous struct --- crates/bevy_text/src/lib.rs | 5 ++++- crates/bevy_text/src/text2d.rs | 15 ++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/bevy_text/src/lib.rs b/crates/bevy_text/src/lib.rs index 12075643ea2a5..4dc1fc3b34070 100644 --- a/crates/bevy_text/src/lib.rs +++ b/crates/bevy_text/src/lib.rs @@ -47,7 +47,10 @@ impl Plugin for TextPlugin { .register_type::() .init_asset_loader::() .insert_resource(DefaultTextPipeline::default()) - .add_system_to_stage(CoreStage::PostUpdate, text2d_system.after(ModifiesWindows)); + .add_system_to_stage( + CoreStage::PostUpdate, + update_text2d_layout.after(ModifiesWindows), + ); if let Ok(render_app) = app.get_sub_app_mut(RenderApp) { render_app.add_system_to_stage( diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index 12dc33024f3e0..7aad84e7c8584 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -125,16 +125,13 @@ pub fn extract_text2d_sprite( } } -#[derive(Debug, Default)] -pub struct QueuedText2d { - entities: HashSet, -} - /// Updates the layout and size information whenever the text or style is changed. /// This information is computed by the `TextPipeline` on insertion, then stored. #[allow(clippy::too_many_arguments, clippy::type_complexity)] -pub fn text2d_system( - mut queue: Local, +pub fn update_text2d_layout( + // Text items which should be reprocessed again, generally when the + // font hasn't loaded yet + mut queue: Local>, mut textures: ResMut>, fonts: Res>, windows: Res, @@ -155,7 +152,7 @@ pub fn text2d_system( let scale_factor = windows.scale_factor(WindowId::primary()); for (entity, text_changed, text, maybe_bounds, mut calculated_size) in text_query.iter_mut() { - if factor_changed || text_changed || queue.entities.remove(&entity) { + 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), @@ -177,7 +174,7 @@ pub fn text2d_system( Err(TextError::NoSuchFont) => { // There was an error processing the text layout, let's add this entity to the // queue for further processing - queue.entities.insert(entity); + queue.insert(entity); } Err(e @ TextError::FailedToAddGlyph(_)) => { panic!("Fatal error when processing text: {}.", e); From 01d5a25455a92f0f8c69087b5b5afd82c69492e9 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 7 May 2022 15:02:59 +0100 Subject: [PATCH 4/4] Update crates/bevy_text/src/text2d.rs Co-authored-by: devil ira --- crates/bevy_text/src/text2d.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index 7aad84e7c8584..a9eefb7528d36 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -129,8 +129,7 @@ pub fn extract_text2d_sprite( /// This information is computed by the `TextPipeline` on insertion, then stored. #[allow(clippy::too_many_arguments, clippy::type_complexity)] pub fn update_text2d_layout( - // Text items which should be reprocessed again, generally when the - // font hasn't loaded yet + // Text items which should be reprocessed again, generally when the font hasn't loaded yet. mut queue: Local>, mut textures: ResMut>, fonts: Res>,