@@ -42,6 +42,10 @@ struct Args {
42
42
/// use the grid layout model
43
43
#[ argh( switch) ]
44
44
grid : bool ,
45
+
46
+ /// at the start of each frame despawn any existing UI nodes and spawn a new UI tree
47
+ #[ argh( switch) ]
48
+ respawn : bool ,
45
49
}
46
50
47
51
/// This example shows what happens when there is a lot of buttons on screen.
@@ -52,6 +56,8 @@ fn main() {
52
56
#[ cfg( target_arch = "wasm32" ) ]
53
57
let args = Args :: from_args ( & [ ] , & [ ] ) . unwrap ( ) ;
54
58
59
+ warn ! ( include_str!( "warning_string.txt" ) ) ;
60
+
55
61
let mut app = App :: new ( ) ;
56
62
57
63
app. add_plugins ( (
@@ -72,6 +78,10 @@ fn main() {
72
78
} )
73
79
. add_systems ( Update , ( button_system, set_text_colors_changed) ) ;
74
80
81
+ app. add_systems ( Startup , |mut commands : Commands | {
82
+ commands. spawn ( Camera2d ) ;
83
+ } ) ;
84
+
75
85
if args. grid {
76
86
app. add_systems ( Startup , setup_grid) ;
77
87
} else {
@@ -92,6 +102,14 @@ fn main() {
92
102
} ) ;
93
103
}
94
104
105
+ if args. respawn {
106
+ if args. grid {
107
+ app. add_systems ( Update , ( despawn_ui, setup_grid) . chain ( ) ) ;
108
+ } else {
109
+ app. add_systems ( Update , ( despawn_ui, setup_flex) . chain ( ) ) ;
110
+ }
111
+ }
112
+
95
113
app. insert_resource ( args) . run ( ) ;
96
114
}
97
115
@@ -119,7 +137,6 @@ fn button_system(
119
137
}
120
138
121
139
fn setup_flex ( mut commands : Commands , asset_server : Res < AssetServer > , args : Res < Args > ) {
122
- warn ! ( include_str!( "warning_string.txt" ) ) ;
123
140
let image = if 0 < args. image_freq {
124
141
Some ( asset_server. load ( "branding/icon.png" ) )
125
142
} else {
@@ -134,7 +151,6 @@ fn setup_flex(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<
134
151
} ;
135
152
136
153
let as_rainbow = |i : usize | Color :: hsl ( ( i as f32 / buttons_f) * 360.0 , 0.9 , 0.8 ) ;
137
- commands. spawn ( Camera2d ) ;
138
154
commands
139
155
. spawn ( Node {
140
156
flex_direction : FlexDirection :: Column ,
@@ -171,7 +187,6 @@ fn setup_flex(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<
171
187
}
172
188
173
189
fn setup_grid ( mut commands : Commands , asset_server : Res < AssetServer > , args : Res < Args > ) {
174
- warn ! ( include_str!( "warning_string.txt" ) ) ;
175
190
let image = if 0 < args. image_freq {
176
191
Some ( asset_server. load ( "branding/icon.png" ) )
177
192
} else {
@@ -186,7 +201,6 @@ fn setup_grid(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<
186
201
} ;
187
202
188
203
let as_rainbow = |i : usize | Color :: hsl ( ( i as f32 / buttons_f) * 360.0 , 0.9 , 0.8 ) ;
189
- commands. spawn ( Camera2d ) ;
190
204
commands
191
205
. spawn ( Node {
192
206
display : Display :: Grid ,
@@ -268,3 +282,7 @@ fn spawn_button(
268
282
} ) ;
269
283
}
270
284
}
285
+
286
+ fn despawn_ui ( mut commands : Commands , root_node : Single < Entity , ( With < Node > , Without < Parent > ) > ) {
287
+ commands. entity ( * root_node) . despawn_recursive ( ) ;
288
+ }
0 commit comments