Allow many_buttons to be run without text (#8116)

This commit is contained in:
ickshonpe 2023-03-18 22:58:17 +00:00 committed by GitHub
parent f255872c1e
commit 5703c75d76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,3 +1,8 @@
//! This example shows what happens when there is a lot of buttons on screen.
//!
//! To start the demo without text run
//! `cargo run --example many_buttons --release no-text`
use bevy::{ use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*, prelude::*,
@ -68,14 +73,24 @@ fn setup(mut commands: Commands, font: Res<UiFont>) {
..default() ..default()
}) })
.with_children(|commands| { .with_children(|commands| {
let spawn_text = std::env::args().nth(1).as_deref() != Some("no-text");
for i in 0..count { for i in 0..count {
for j in 0..count { for j in 0..count {
let color = as_rainbow(j % i.max(1)).into(); let color = as_rainbow(j % i.max(1)).into();
spawn_button(commands, font.0.clone_weak(), color, count_f, i, j); spawn_button(
commands,
font.0.clone_weak(),
color,
count_f,
i,
j,
spawn_text,
);
} }
} }
}); });
} }
fn spawn_button( fn spawn_button(
commands: &mut ChildBuilder, commands: &mut ChildBuilder,
font: Handle<Font>, font: Handle<Font>,
@ -83,25 +98,27 @@ fn spawn_button(
total: f32, total: f32,
i: usize, i: usize,
j: usize, j: usize,
spawn_text: bool,
) { ) {
let width = 90.0 / total; let width = 90.0 / total;
commands let mut builder = commands.spawn((
.spawn(( ButtonBundle {
ButtonBundle { style: Style {
style: Style { size: Size::new(Val::Percent(width), Val::Percent(width)),
size: Size::new(Val::Percent(width), Val::Percent(width)), bottom: Val::Percent(100.0 / total * i as f32),
bottom: Val::Percent(100.0 / total * i as f32), left: Val::Percent(100.0 / total * j as f32),
left: Val::Percent(100.0 / total * j as f32), align_items: AlignItems::Center,
align_items: AlignItems::Center, position_type: PositionType::Absolute,
position_type: PositionType::Absolute,
..default()
},
background_color: color,
..default() ..default()
}, },
IdleColor(color), background_color: color,
)) ..default()
.with_children(|commands| { },
IdleColor(color),
));
if spawn_text {
builder.with_children(|commands| {
commands.spawn(TextBundle::from_section( commands.spawn(TextBundle::from_section(
format!("{i}, {j}"), format!("{i}, {j}"),
TextStyle { TextStyle {
@ -111,4 +128,5 @@ fn spawn_button(
}, },
)); ));
}); });
}
} }