Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Update layout/style when scale factor changes too #4689

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 20 additions & 26 deletions crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ use bevy_ecs::{
bundle::Bundle,
component::Component,
entity::Entity,
query::{Changed, With},
event::EventReader,
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_window::{WindowId, Windows};
use bevy_utils::HashSet;
use bevy_window::{WindowId, WindowScaleFactorChanged, Windows};

use crate::{
DefaultTextPipeline, Font, FontAtlasSet, HorizontalAlign, Text, TextError, VerticalAlign,
Expand Down Expand Up @@ -125,42 +127,36 @@ pub fn extract_text2d_sprite(

#[derive(Debug, Default)]
pub struct QueuedText2d {
entities: Vec<Entity>,
entities: HashSet<Entity>,
}
DJMcNab marked this conversation as resolved.
Show resolved Hide resolved

/// 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<QueuedText2d>,
mut queue: Local<QueuedText2d>,
DJMcNab marked this conversation as resolved.
Show resolved Hide resolved
mut textures: ResMut<Assets<Image>>,
fonts: Res<Assets<Font>>,
windows: Res<Windows>,
mut scale_factor_changed: EventReader<WindowScaleFactorChanged>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
mut font_atlas_set_storage: ResMut<Assets<FontAtlasSet>>,
mut text_pipeline: ResMut<DefaultTextPipeline>,
mut text_queries: ParamSet<(
Query<Entity, (With<Text2dSize>, Changed<Text>)>,
Query<(&Text, Option<&Text2dBounds>, &mut Text2dSize), With<Text2dSize>>,
mut text_query: Query<(
Entity,
Changed<Text>,
&Text,
Option<&Text2dBounds>,
&mut Text2dSize,
)>,
) {
// 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);
}

if queued_text.entities.is_empty() {
return;
}

// We need to consume the entire iterator, hence `last`
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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

queue.entities.remove might look expensive, but the other changes more than drown out this overhead.

let text_bounds = match maybe_bounds {
Some(bounds) => Vec2::new(
scale_value(bounds.size.x, scale_factor),
scale_value(bounds.size.y, scale_factor),
Expand All @@ -181,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);
Expand All @@ -198,8 +194,6 @@ pub fn text2d_system(
}
}
}

queued_text.entities = new_queue;
}

pub fn scale_value(value: f32, factor: f64) -> f32 {
Expand Down