Skip to content

Commit

Permalink
many_buttons --respawn arg (#16390)
Browse files Browse the repository at this point in the history
# Objective

To capture the performance impact of removing and adding UI nodes add a
`respawn` commandline argument to the `many_buttons` stress test example
that despawns the existing UI layout and then spawns a new layout to
replace it every frame.

## Testing

To run the example with the new changes use:
```cargo run --example many_buttons --release -- --respawn```
  • Loading branch information
ickshonpe authored Nov 14, 2024
1 parent 4eaebd4 commit 2d91a6f
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions examples/stress_tests/many_buttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ struct Args {
/// use the grid layout model
#[argh(switch)]
grid: bool,

/// at the start of each frame despawn any existing UI nodes and spawn a new UI tree
#[argh(switch)]
respawn: bool,
}

/// This example shows what happens when there is a lot of buttons on screen.
Expand All @@ -52,6 +56,8 @@ fn main() {
#[cfg(target_arch = "wasm32")]
let args = Args::from_args(&[], &[]).unwrap();

warn!(include_str!("warning_string.txt"));

let mut app = App::new();

app.add_plugins((
Expand All @@ -72,6 +78,10 @@ fn main() {
})
.add_systems(Update, (button_system, set_text_colors_changed));

app.add_systems(Startup, |mut commands: Commands| {
commands.spawn(Camera2d);
});

if args.grid {
app.add_systems(Startup, setup_grid);
} else {
Expand All @@ -92,6 +102,14 @@ fn main() {
});
}

if args.respawn {
if args.grid {
app.add_systems(Update, (despawn_ui, setup_grid).chain());
} else {
app.add_systems(Update, (despawn_ui, setup_flex).chain());
}
}

app.insert_resource(args).run();
}

Expand Down Expand Up @@ -119,7 +137,6 @@ fn button_system(
}

fn setup_flex(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<Args>) {
warn!(include_str!("warning_string.txt"));
let image = if 0 < args.image_freq {
Some(asset_server.load("branding/icon.png"))
} else {
Expand All @@ -134,7 +151,6 @@ fn setup_flex(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<
};

let as_rainbow = |i: usize| Color::hsl((i as f32 / buttons_f) * 360.0, 0.9, 0.8);
commands.spawn(Camera2d);
commands
.spawn(Node {
flex_direction: FlexDirection::Column,
Expand Down Expand Up @@ -171,7 +187,6 @@ fn setup_flex(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<
}

fn setup_grid(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<Args>) {
warn!(include_str!("warning_string.txt"));
let image = if 0 < args.image_freq {
Some(asset_server.load("branding/icon.png"))
} else {
Expand All @@ -186,7 +201,6 @@ fn setup_grid(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<
};

let as_rainbow = |i: usize| Color::hsl((i as f32 / buttons_f) * 360.0, 0.9, 0.8);
commands.spawn(Camera2d);
commands
.spawn(Node {
display: Display::Grid,
Expand Down Expand Up @@ -268,3 +282,7 @@ fn spawn_button(
});
}
}

fn despawn_ui(mut commands: Commands, root_node: Single<Entity, (With<Node>, Without<Parent>)>) {
commands.entity(*root_node).despawn_recursive();
}

0 comments on commit 2d91a6f

Please sign in to comment.