Skip to content

Commit

Permalink
Text responds to scale factor changes (#1769)
Browse files Browse the repository at this point in the history
Fixes #1768

If the scale factor changes, queue up all text to be drawn instead of just changed text.
  • Loading branch information
cart committed Mar 27, 2021
1 parent 9a78add commit 94c4184
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{CalculatedSize, Node, Style, Val};
use bevy_asset::Assets;
use bevy_ecs::{
entity::Entity,
query::{Changed, Or, Without},
query::{Changed, Or, With, Without},
system::{Local, Query, QuerySet, Res, ResMut},
};
use bevy_math::Size;
Expand Down Expand Up @@ -45,6 +45,7 @@ pub fn text_constraint(min_size: Val, size: Val, max_size: Val, scale_factor: f6
#[allow(clippy::too_many_arguments)]
pub fn text_system(
mut queued_text: Local<QueuedText>,
mut last_scale_factor: Local<f64>,
mut textures: ResMut<Assets<Texture>>,
fonts: Res<Assets<Font>>,
windows: Res<Windows>,
Expand All @@ -53,6 +54,7 @@ pub fn text_system(
mut text_pipeline: ResMut<DefaultTextPipeline>,
mut text_queries: QuerySet<(
Query<Entity, Or<(Changed<Text>, Changed<Style>)>>,
Query<Entity, (With<Text>, With<Style>)>,
Query<(&Text, &Style, &mut CalculatedSize)>,
)>,
) {
Expand All @@ -64,9 +66,18 @@ pub fn text_system(

let inv_scale_factor = 1. / scale_factor;

// Adds all entities where the text or the style has changed to the local queue
for entity in text_queries.q0_mut().iter_mut() {
queued_text.entities.push(entity);
#[allow(clippy::float_cmp)]
if *last_scale_factor == scale_factor {
// Adds all entities where the text or the style has changed to the local queue
for entity in text_queries.q0().iter() {
queued_text.entities.push(entity);
}
} else {
// If the scale factor has changed, queue all text
for entity in text_queries.q1().iter() {
queued_text.entities.push(entity);
}
*last_scale_factor = scale_factor;
}

if queued_text.entities.is_empty() {
Expand All @@ -75,7 +86,7 @@ pub fn text_system(

// Computes all text in the local queue
let mut new_queue = Vec::new();
let query = text_queries.q1_mut();
let query = text_queries.q2_mut();
for entity in queued_text.entities.drain(..) {
if let Ok((text, style, mut calculated_size)) = query.get_mut(entity) {
let node_size = Size::new(
Expand Down

0 comments on commit 94c4184

Please sign in to comment.