Skip to content

Commit 5703c75

Browse files
authored
Allow many_buttons to be run without text (#8116)
1 parent f255872 commit 5703c75

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

examples/stress_tests/many_buttons.rs

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
//! This example shows what happens when there is a lot of buttons on screen.
2+
//!
3+
//! To start the demo without text run
4+
//! `cargo run --example many_buttons --release no-text`
5+
16
use bevy::{
27
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
38
prelude::*,
@@ -68,40 +73,52 @@ fn setup(mut commands: Commands, font: Res<UiFont>) {
6873
..default()
6974
})
7075
.with_children(|commands| {
76+
let spawn_text = std::env::args().nth(1).as_deref() != Some("no-text");
7177
for i in 0..count {
7278
for j in 0..count {
7379
let color = as_rainbow(j % i.max(1)).into();
74-
spawn_button(commands, font.0.clone_weak(), color, count_f, i, j);
80+
spawn_button(
81+
commands,
82+
font.0.clone_weak(),
83+
color,
84+
count_f,
85+
i,
86+
j,
87+
spawn_text,
88+
);
7589
}
7690
}
7791
});
7892
}
93+
7994
fn spawn_button(
8095
commands: &mut ChildBuilder,
8196
font: Handle<Font>,
8297
color: BackgroundColor,
8398
total: f32,
8499
i: usize,
85100
j: usize,
101+
spawn_text: bool,
86102
) {
87103
let width = 90.0 / total;
88-
commands
89-
.spawn((
90-
ButtonBundle {
91-
style: Style {
92-
size: Size::new(Val::Percent(width), Val::Percent(width)),
93-
bottom: Val::Percent(100.0 / total * i as f32),
94-
left: Val::Percent(100.0 / total * j as f32),
95-
align_items: AlignItems::Center,
96-
position_type: PositionType::Absolute,
97-
..default()
98-
},
99-
background_color: color,
104+
let mut builder = commands.spawn((
105+
ButtonBundle {
106+
style: Style {
107+
size: Size::new(Val::Percent(width), Val::Percent(width)),
108+
bottom: Val::Percent(100.0 / total * i as f32),
109+
left: Val::Percent(100.0 / total * j as f32),
110+
align_items: AlignItems::Center,
111+
position_type: PositionType::Absolute,
100112
..default()
101113
},
102-
IdleColor(color),
103-
))
104-
.with_children(|commands| {
114+
background_color: color,
115+
..default()
116+
},
117+
IdleColor(color),
118+
));
119+
120+
if spawn_text {
121+
builder.with_children(|commands| {
105122
commands.spawn(TextBundle::from_section(
106123
format!("{i}, {j}"),
107124
TextStyle {
@@ -111,4 +128,5 @@ fn spawn_button(
111128
},
112129
));
113130
});
131+
}
114132
}

0 commit comments

Comments
 (0)