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::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
@ -68,14 +73,24 @@ fn setup(mut commands: Commands, font: Res<UiFont>) {
..default()
})
.with_children(|commands| {
let spawn_text = std::env::args().nth(1).as_deref() != Some("no-text");
for i in 0..count {
for j in 0..count {
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(
commands: &mut ChildBuilder,
font: Handle<Font>,
@ -83,25 +98,27 @@ fn spawn_button(
total: f32,
i: usize,
j: usize,
spawn_text: bool,
) {
let width = 90.0 / total;
commands
.spawn((
ButtonBundle {
style: Style {
size: Size::new(Val::Percent(width), Val::Percent(width)),
bottom: Val::Percent(100.0 / total * i as f32),
left: Val::Percent(100.0 / total * j as f32),
align_items: AlignItems::Center,
position_type: PositionType::Absolute,
..default()
},
background_color: color,
let mut builder = commands.spawn((
ButtonBundle {
style: Style {
size: Size::new(Val::Percent(width), Val::Percent(width)),
bottom: Val::Percent(100.0 / total * i as f32),
left: Val::Percent(100.0 / total * j as f32),
align_items: AlignItems::Center,
position_type: PositionType::Absolute,
..default()
},
IdleColor(color),
))
.with_children(|commands| {
background_color: color,
..default()
},
IdleColor(color),
));
if spawn_text {
builder.with_children(|commands| {
commands.spawn(TextBundle::from_section(
format!("{i}, {j}"),
TextStyle {
@ -111,4 +128,5 @@ fn spawn_button(
},
));
});
}
}