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
+
1
6
use bevy:: {
2
7
diagnostic:: { FrameTimeDiagnosticsPlugin , LogDiagnosticsPlugin } ,
3
8
prelude:: * ,
@@ -68,40 +73,52 @@ fn setup(mut commands: Commands, font: Res<UiFont>) {
68
73
..default ( )
69
74
} )
70
75
. with_children ( |commands| {
76
+ let spawn_text = std:: env:: args ( ) . nth ( 1 ) . as_deref ( ) != Some ( "no-text" ) ;
71
77
for i in 0 ..count {
72
78
for j in 0 ..count {
73
79
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
+ ) ;
75
89
}
76
90
}
77
91
} ) ;
78
92
}
93
+
79
94
fn spawn_button (
80
95
commands : & mut ChildBuilder ,
81
96
font : Handle < Font > ,
82
97
color : BackgroundColor ,
83
98
total : f32 ,
84
99
i : usize ,
85
100
j : usize ,
101
+ spawn_text : bool ,
86
102
) {
87
103
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 ,
100
112
..default ( )
101
113
} ,
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| {
105
122
commands. spawn ( TextBundle :: from_section (
106
123
format ! ( "{i}, {j}" ) ,
107
124
TextStyle {
@@ -111,4 +128,5 @@ fn spawn_button(
111
128
} ,
112
129
) ) ;
113
130
} ) ;
131
+ }
114
132
}
0 commit comments