From 2f9613f22ca696569c4266454213b3994b3a1b75 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Wed, 12 Feb 2025 11:14:34 -0700 Subject: [PATCH] Minor tidying in `font_atlas_debug` (#17825) # Objective Tidy up a few little things I noticed while working with this example ## Solution - Fix manual resetting of a repeating timer - Use atlas image size instead of hardcoded value. Atlases are always 512x512 right now, but hopefully not in the future. - Pluralize a variable name for a variable holding a `Vec` --- examples/ui/font_atlas_debug.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/examples/ui/font_atlas_debug.rs b/examples/ui/font_atlas_debug.rs index f117a85aa7..32059e7e2e 100644 --- a/examples/ui/font_atlas_debug.rs +++ b/examples/ui/font_atlas_debug.rs @@ -39,21 +39,23 @@ fn atlas_render_system( mut commands: Commands, mut state: ResMut, font_atlas_sets: Res, + images: Res>, ) { if let Some(set) = font_atlas_sets.get(&state.handle) { - if let Some((_size, font_atlas)) = set.iter().next() { + if let Some((_size, font_atlases)) = set.iter().next() { let x_offset = state.atlas_count as f32; - if state.atlas_count == font_atlas.len() as u32 { + if state.atlas_count == font_atlases.len() as u32 { return; } - let font_atlas = &font_atlas[state.atlas_count as usize]; + let font_atlas = &font_atlases[state.atlas_count as usize]; + let image = images.get(&font_atlas.texture).unwrap(); state.atlas_count += 1; commands.spawn(( ImageNode::new(font_atlas.texture.clone()), Node { position_type: PositionType::Absolute, top: Val::ZERO, - left: Val::Px(512.0 * x_offset), + left: Val::Px(image.width() as f32 * x_offset), ..default() }, )); @@ -67,16 +69,16 @@ fn text_update_system( mut query: Query<&mut Text>, mut seeded_rng: ResMut, ) { - if state.timer.tick(time.delta()).finished() { - for mut text in &mut query { - let c = seeded_rng.gen::() as char; - let string = &mut **text; - if !string.contains(c) { - string.push(c); - } - } + if !state.timer.tick(time.delta()).just_finished() { + return; + } - state.timer.reset(); + for mut text in &mut query { + let c = seeded_rng.gen::() as char; + let string = &mut **text; + if !string.contains(c) { + string.push(c); + } } }