2023-09-08 15:02:05 +00:00
|
|
|
/// General UI benchmark that stress tests layouting, text, interaction and rendering
|
|
|
|
use argh::FromArgs;
|
2022-07-21 14:39:03 +00:00
|
|
|
use bevy::{
|
|
|
|
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
|
|
|
prelude::*,
|
2023-01-19 00:38:28 +00:00
|
|
|
window::{PresentMode, WindowPlugin},
|
2022-07-21 14:39:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const FONT_SIZE: f32 = 7.0;
|
|
|
|
|
2023-09-08 15:02:05 +00:00
|
|
|
#[derive(FromArgs, Resource)]
|
|
|
|
/// `many_buttons` general UI benchmark that stress tests layouting, text, interaction and rendering
|
|
|
|
struct Args {
|
|
|
|
/// whether to add text to each button
|
|
|
|
#[argh(switch)]
|
|
|
|
no_text: bool,
|
|
|
|
|
|
|
|
/// whether to add borders to each button
|
|
|
|
#[argh(switch)]
|
|
|
|
no_borders: bool,
|
|
|
|
|
|
|
|
/// whether to perform a full relayout each frame
|
|
|
|
#[argh(switch)]
|
|
|
|
relayout: bool,
|
|
|
|
|
|
|
|
/// whether to recompute all text each frame
|
|
|
|
#[argh(switch)]
|
|
|
|
recompute_text: bool,
|
|
|
|
|
|
|
|
/// how many buttons per row and column of the grid.
|
|
|
|
#[argh(option, default = "110")]
|
|
|
|
buttons: usize,
|
|
|
|
|
|
|
|
/// give every nth button an image
|
|
|
|
#[argh(option, default = "4")]
|
|
|
|
image_freq: usize,
|
|
|
|
|
|
|
|
/// use the grid layout model
|
|
|
|
#[argh(switch)]
|
|
|
|
grid: bool,
|
|
|
|
}
|
|
|
|
|
2022-07-21 14:39:03 +00:00
|
|
|
/// This example shows what happens when there is a lot of buttons on screen.
|
|
|
|
fn main() {
|
2023-09-08 15:02:05 +00:00
|
|
|
let args: Args = argh::from_env();
|
2023-04-17 17:49:11 +00:00
|
|
|
let mut app = App::new();
|
|
|
|
|
2023-06-21 20:51:03 +00:00
|
|
|
app.add_plugins((
|
|
|
|
DefaultPlugins.set(WindowPlugin {
|
|
|
|
primary_window: Some(Window {
|
|
|
|
present_mode: PresentMode::AutoNoVsync,
|
|
|
|
..default()
|
|
|
|
}),
|
2022-07-21 14:39:03 +00:00
|
|
|
..default()
|
2023-04-17 17:49:11 +00:00
|
|
|
}),
|
2023-06-21 20:51:03 +00:00
|
|
|
FrameTimeDiagnosticsPlugin,
|
|
|
|
LogDiagnosticsPlugin::default(),
|
|
|
|
))
|
2023-04-17 17:49:11 +00:00
|
|
|
.add_systems(Update, button_system);
|
|
|
|
|
2023-09-08 15:02:05 +00:00
|
|
|
if args.grid {
|
|
|
|
app.add_systems(Startup, setup_grid);
|
|
|
|
} else {
|
|
|
|
app.add_systems(Startup, setup_flex);
|
|
|
|
}
|
|
|
|
|
|
|
|
if args.relayout {
|
|
|
|
app.add_systems(Update, |mut style_query: Query<&mut Style>| {
|
|
|
|
style_query.for_each_mut(|mut style| style.set_changed());
|
2023-04-17 17:49:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-08 15:02:05 +00:00
|
|
|
if args.recompute_text {
|
2023-04-17 17:49:11 +00:00
|
|
|
app.add_systems(Update, |mut text_query: Query<&mut Text>| {
|
|
|
|
text_query.for_each_mut(|mut text| text.set_changed());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-08 15:02:05 +00:00
|
|
|
app.insert_resource(args).run();
|
2022-07-21 14:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Component)]
|
2022-09-25 00:39:17 +00:00
|
|
|
struct IdleColor(BackgroundColor);
|
2022-07-21 14:39:03 +00:00
|
|
|
|
|
|
|
fn button_system(
|
2022-09-25 00:39:17 +00:00
|
|
|
mut interaction_query: Query<
|
|
|
|
(&Interaction, &mut BackgroundColor, &IdleColor),
|
|
|
|
Changed<Interaction>,
|
|
|
|
>,
|
2022-07-21 14:39:03 +00:00
|
|
|
) {
|
2023-09-08 15:02:05 +00:00
|
|
|
for (interaction, mut button_color, IdleColor(idle_color)) in interaction_query.iter_mut() {
|
|
|
|
*button_color = match interaction {
|
|
|
|
Interaction::Hovered => Color::ORANGE_RED.into(),
|
|
|
|
_ => *idle_color,
|
|
|
|
};
|
2022-07-21 14:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 15:02:05 +00:00
|
|
|
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 {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let buttons_f = args.buttons as f32;
|
|
|
|
let border = if args.no_borders {
|
|
|
|
UiRect::ZERO
|
|
|
|
} else {
|
|
|
|
UiRect::all(Val::VMin(0.05 * 90. / buttons_f))
|
|
|
|
};
|
|
|
|
|
|
|
|
let as_rainbow = |i: usize| Color::hsl((i as f32 / buttons_f) * 360.0, 0.9, 0.8);
|
|
|
|
commands.spawn(Camera2dBundle::default());
|
|
|
|
commands
|
|
|
|
.spawn(NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
flex_direction: FlexDirection::Column,
|
|
|
|
justify_content: JustifyContent::Center,
|
|
|
|
align_items: AlignItems::Center,
|
|
|
|
width: Val::Percent(100.),
|
2023-09-19 15:14:46 +00:00
|
|
|
height: Val::Percent(100.),
|
2023-09-08 15:02:05 +00:00
|
|
|
..default()
|
|
|
|
},
|
|
|
|
..default()
|
|
|
|
})
|
|
|
|
.with_children(|commands| {
|
|
|
|
for column in 0..args.buttons {
|
|
|
|
commands
|
|
|
|
.spawn(NodeBundle::default())
|
|
|
|
.with_children(|commands| {
|
|
|
|
for row in 0..args.buttons {
|
|
|
|
let color = as_rainbow(row % column.max(1)).into();
|
|
|
|
let border_color = Color::WHITE.with_a(0.5).into();
|
|
|
|
spawn_button(
|
|
|
|
commands,
|
|
|
|
color,
|
|
|
|
buttons_f,
|
|
|
|
column,
|
|
|
|
row,
|
|
|
|
!args.no_text,
|
|
|
|
border,
|
|
|
|
border_color,
|
|
|
|
image
|
|
|
|
.as_ref()
|
|
|
|
.filter(|_| (column + row) % args.image_freq == 0)
|
|
|
|
.cloned(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup_grid(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<Args>) {
|
2023-04-24 14:35:03 +00:00
|
|
|
warn!(include_str!("warning_string.txt"));
|
2023-09-08 15:02:05 +00:00
|
|
|
let image = if 0 < args.image_freq {
|
|
|
|
Some(asset_server.load("branding/icon.png"))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let buttons_f = args.buttons as f32;
|
|
|
|
let border = if args.no_borders {
|
|
|
|
UiRect::ZERO
|
|
|
|
} else {
|
|
|
|
UiRect::all(Val::VMin(0.05 * 90. / buttons_f))
|
|
|
|
};
|
2023-04-24 14:35:03 +00:00
|
|
|
|
2023-09-08 15:02:05 +00:00
|
|
|
let as_rainbow = |i: usize| Color::hsl((i as f32 / buttons_f) * 360.0, 0.9, 0.8);
|
Spawn now takes a Bundle (#6054)
# Objective
Now that we can consolidate Bundles and Components under a single insert (thanks to #2975 and #6039), almost 100% of world spawns now look like `world.spawn().insert((Some, Tuple, Here))`. Spawning an entity without any components is an extremely uncommon pattern, so it makes sense to give spawn the "first class" ergonomic api. This consolidated api should be made consistent across all spawn apis (such as World and Commands).
## Solution
All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input:
```rust
// before:
commands
.spawn()
.insert((A, B, C));
world
.spawn()
.insert((A, B, C);
// after
commands.spawn((A, B, C));
world.spawn((A, B, C));
```
All existing instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api. A new `spawn_empty` has been added, replacing the old `spawn` api.
By allowing `world.spawn(some_bundle)` to replace `world.spawn().insert(some_bundle)`, this opened the door to removing the initial entity allocation in the "empty" archetype / table done in `spawn()` (and subsequent move to the actual archetype in `.insert(some_bundle)`).
This improves spawn performance by over 10%:
![image](https://user-images.githubusercontent.com/2694663/191627587-4ab2f949-4ccd-4231-80eb-80dd4d9ad6b9.png)
To take this measurement, I added a new `world_spawn` benchmark.
Unfortunately, optimizing `Commands::spawn` is slightly less trivial, as Commands expose the Entity id of spawned entities prior to actually spawning. Doing the optimization would (naively) require assurances that the `spawn(some_bundle)` command is applied before all other commands involving the entity (which would not necessarily be true, if memory serves). Optimizing `Commands::spawn` this way does feel possible, but it will require careful thought (and maybe some additional checks), which deserves its own PR. For now, it has the same performance characteristics of the current `Commands::spawn_bundle` on main.
**Note that 99% of this PR is simple renames and refactors. The only code that needs careful scrutiny is the new `World::spawn()` impl, which is relatively straightforward, but it has some new unsafe code (which re-uses battle tested BundlerSpawner code path).**
---
## Changelog
- All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input
- All instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api
- World and Commands now have `spawn_empty()`, which is equivalent to the old `spawn()` behavior.
## Migration Guide
```rust
// Old (0.8):
commands
.spawn()
.insert_bundle((A, B, C));
// New (0.9)
commands.spawn((A, B, C));
// Old (0.8):
commands.spawn_bundle((A, B, C));
// New (0.9)
commands.spawn((A, B, C));
// Old (0.8):
let entity = commands.spawn().id();
// New (0.9)
let entity = commands.spawn_empty().id();
// Old (0.8)
let entity = world.spawn().id();
// New (0.9)
let entity = world.spawn_empty();
```
2022-09-23 19:55:54 +00:00
|
|
|
commands.spawn(Camera2dBundle::default());
|
2022-07-21 14:39:03 +00:00
|
|
|
commands
|
Spawn now takes a Bundle (#6054)
# Objective
Now that we can consolidate Bundles and Components under a single insert (thanks to #2975 and #6039), almost 100% of world spawns now look like `world.spawn().insert((Some, Tuple, Here))`. Spawning an entity without any components is an extremely uncommon pattern, so it makes sense to give spawn the "first class" ergonomic api. This consolidated api should be made consistent across all spawn apis (such as World and Commands).
## Solution
All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input:
```rust
// before:
commands
.spawn()
.insert((A, B, C));
world
.spawn()
.insert((A, B, C);
// after
commands.spawn((A, B, C));
world.spawn((A, B, C));
```
All existing instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api. A new `spawn_empty` has been added, replacing the old `spawn` api.
By allowing `world.spawn(some_bundle)` to replace `world.spawn().insert(some_bundle)`, this opened the door to removing the initial entity allocation in the "empty" archetype / table done in `spawn()` (and subsequent move to the actual archetype in `.insert(some_bundle)`).
This improves spawn performance by over 10%:
![image](https://user-images.githubusercontent.com/2694663/191627587-4ab2f949-4ccd-4231-80eb-80dd4d9ad6b9.png)
To take this measurement, I added a new `world_spawn` benchmark.
Unfortunately, optimizing `Commands::spawn` is slightly less trivial, as Commands expose the Entity id of spawned entities prior to actually spawning. Doing the optimization would (naively) require assurances that the `spawn(some_bundle)` command is applied before all other commands involving the entity (which would not necessarily be true, if memory serves). Optimizing `Commands::spawn` this way does feel possible, but it will require careful thought (and maybe some additional checks), which deserves its own PR. For now, it has the same performance characteristics of the current `Commands::spawn_bundle` on main.
**Note that 99% of this PR is simple renames and refactors. The only code that needs careful scrutiny is the new `World::spawn()` impl, which is relatively straightforward, but it has some new unsafe code (which re-uses battle tested BundlerSpawner code path).**
---
## Changelog
- All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input
- All instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api
- World and Commands now have `spawn_empty()`, which is equivalent to the old `spawn()` behavior.
## Migration Guide
```rust
// Old (0.8):
commands
.spawn()
.insert_bundle((A, B, C));
// New (0.9)
commands.spawn((A, B, C));
// Old (0.8):
commands.spawn_bundle((A, B, C));
// New (0.9)
commands.spawn((A, B, C));
// Old (0.8):
let entity = commands.spawn().id();
// New (0.9)
let entity = commands.spawn_empty().id();
// Old (0.8)
let entity = world.spawn().id();
// New (0.9)
let entity = world.spawn_empty();
```
2022-09-23 19:55:54 +00:00
|
|
|
.spawn(NodeBundle {
|
2022-07-21 14:39:03 +00:00
|
|
|
style: Style {
|
2023-09-08 15:02:05 +00:00
|
|
|
display: Display::Grid,
|
Flatten UI `Style` properties that use `Size` + remove `Size` (#8548)
# Objective
- Simplify API and make authoring styles easier
See:
https://github.com/bevyengine/bevy/issues/8540#issuecomment-1536177102
## Solution
- The `size`, `min_size`, `max_size`, and `gap` properties have been
replaced by `width`, `height`, `min_width`, `min_height`, `max_width`,
`max_height`, `row_gap`, and `column_gap` properties
---
## Changelog
- Flattened `Style` properties that have a `Size` value directly into
`Style`
## Migration Guide
- The `size`, `min_size`, `max_size`, and `gap` properties have been
replaced by the `width`, `height`, `min_width`, `min_height`,
`max_width`, `max_height`, `row_gap`, and `column_gap` properties. Use
the new properties instead.
---------
Co-authored-by: ickshonpe <david.curthoys@googlemail.com>
2023-05-16 01:36:32 +00:00
|
|
|
width: Val::Percent(100.),
|
2023-09-08 15:02:05 +00:00
|
|
|
height: Val::Percent(100.0),
|
|
|
|
grid_template_columns: RepeatedGridTrack::flex(args.buttons as u16, 1.0),
|
|
|
|
grid_template_rows: RepeatedGridTrack::flex(args.buttons as u16, 1.0),
|
2022-07-21 14:39:03 +00:00
|
|
|
..default()
|
|
|
|
},
|
|
|
|
..default()
|
|
|
|
})
|
|
|
|
.with_children(|commands| {
|
2023-09-08 15:02:05 +00:00
|
|
|
for column in 0..args.buttons {
|
|
|
|
for row in 0..args.buttons {
|
|
|
|
let color = as_rainbow(row % column.max(1)).into();
|
|
|
|
let border_color = Color::WHITE.with_a(0.5).into();
|
2023-07-05 01:55:13 +00:00
|
|
|
spawn_button(
|
|
|
|
commands,
|
|
|
|
color,
|
2023-09-08 15:02:05 +00:00
|
|
|
buttons_f,
|
|
|
|
column,
|
|
|
|
row,
|
|
|
|
!args.no_text,
|
2023-07-05 01:55:13 +00:00
|
|
|
border,
|
|
|
|
border_color,
|
2023-09-08 15:02:05 +00:00
|
|
|
image
|
|
|
|
.as_ref()
|
|
|
|
.filter(|_| (column + row) % args.image_freq == 0)
|
|
|
|
.cloned(),
|
2023-07-05 01:55:13 +00:00
|
|
|
);
|
2022-07-21 14:39:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-03-18 22:58:17 +00:00
|
|
|
|
2023-07-05 01:55:13 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2022-07-21 14:39:03 +00:00
|
|
|
fn spawn_button(
|
|
|
|
commands: &mut ChildBuilder,
|
2023-07-05 01:55:13 +00:00
|
|
|
background_color: BackgroundColor,
|
2023-09-08 15:02:05 +00:00
|
|
|
buttons: f32,
|
|
|
|
column: usize,
|
|
|
|
row: usize,
|
2023-03-18 22:58:17 +00:00
|
|
|
spawn_text: bool,
|
2023-07-05 01:55:13 +00:00
|
|
|
border: UiRect,
|
|
|
|
border_color: BorderColor,
|
2023-09-08 15:02:05 +00:00
|
|
|
image: Option<Handle<Image>>,
|
2022-07-21 14:39:03 +00:00
|
|
|
) {
|
2023-09-08 15:02:05 +00:00
|
|
|
let width = Val::Vw(90.0 / buttons);
|
|
|
|
let height = Val::Vh(90.0 / buttons);
|
|
|
|
let margin = UiRect::axes(width * 0.05, height * 0.05);
|
2023-03-18 22:58:17 +00:00
|
|
|
let mut builder = commands.spawn((
|
|
|
|
ButtonBundle {
|
|
|
|
style: Style {
|
2023-09-08 15:02:05 +00:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
margin,
|
2023-03-18 22:58:17 +00:00
|
|
|
align_items: AlignItems::Center,
|
2023-09-08 15:02:05 +00:00
|
|
|
justify_content: JustifyContent::Center,
|
2023-07-05 01:55:13 +00:00
|
|
|
border,
|
2022-07-21 14:39:03 +00:00
|
|
|
..default()
|
|
|
|
},
|
2023-07-05 01:55:13 +00:00
|
|
|
background_color,
|
|
|
|
border_color,
|
2023-03-18 22:58:17 +00:00
|
|
|
..default()
|
|
|
|
},
|
2023-07-05 01:55:13 +00:00
|
|
|
IdleColor(background_color),
|
2023-03-18 22:58:17 +00:00
|
|
|
));
|
|
|
|
|
2023-09-08 15:02:05 +00:00
|
|
|
if let Some(image) = image {
|
|
|
|
builder.insert(UiImage::new(image));
|
|
|
|
}
|
|
|
|
|
2023-03-18 22:58:17 +00:00
|
|
|
if spawn_text {
|
2023-09-08 15:02:05 +00:00
|
|
|
builder.with_children(|parent| {
|
|
|
|
parent.spawn(TextBundle::from_section(
|
|
|
|
format!("{column}, {row}"),
|
2022-07-21 14:39:03 +00:00
|
|
|
TextStyle {
|
|
|
|
font_size: FONT_SIZE,
|
|
|
|
color: Color::rgb(0.2, 0.2, 0.2),
|
2023-04-21 22:30:18 +00:00
|
|
|
..default()
|
2022-07-21 14:39:03 +00:00
|
|
|
},
|
|
|
|
));
|
Spawn now takes a Bundle (#6054)
# Objective
Now that we can consolidate Bundles and Components under a single insert (thanks to #2975 and #6039), almost 100% of world spawns now look like `world.spawn().insert((Some, Tuple, Here))`. Spawning an entity without any components is an extremely uncommon pattern, so it makes sense to give spawn the "first class" ergonomic api. This consolidated api should be made consistent across all spawn apis (such as World and Commands).
## Solution
All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input:
```rust
// before:
commands
.spawn()
.insert((A, B, C));
world
.spawn()
.insert((A, B, C);
// after
commands.spawn((A, B, C));
world.spawn((A, B, C));
```
All existing instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api. A new `spawn_empty` has been added, replacing the old `spawn` api.
By allowing `world.spawn(some_bundle)` to replace `world.spawn().insert(some_bundle)`, this opened the door to removing the initial entity allocation in the "empty" archetype / table done in `spawn()` (and subsequent move to the actual archetype in `.insert(some_bundle)`).
This improves spawn performance by over 10%:
![image](https://user-images.githubusercontent.com/2694663/191627587-4ab2f949-4ccd-4231-80eb-80dd4d9ad6b9.png)
To take this measurement, I added a new `world_spawn` benchmark.
Unfortunately, optimizing `Commands::spawn` is slightly less trivial, as Commands expose the Entity id of spawned entities prior to actually spawning. Doing the optimization would (naively) require assurances that the `spawn(some_bundle)` command is applied before all other commands involving the entity (which would not necessarily be true, if memory serves). Optimizing `Commands::spawn` this way does feel possible, but it will require careful thought (and maybe some additional checks), which deserves its own PR. For now, it has the same performance characteristics of the current `Commands::spawn_bundle` on main.
**Note that 99% of this PR is simple renames and refactors. The only code that needs careful scrutiny is the new `World::spawn()` impl, which is relatively straightforward, but it has some new unsafe code (which re-uses battle tested BundlerSpawner code path).**
---
## Changelog
- All `spawn` apis (`World::spawn`, `Commands:;spawn`, `ChildBuilder::spawn`, and `WorldChildBuilder::spawn`) now accept a bundle as input
- All instances of `spawn_bundle` have been deprecated in favor of the new `spawn` api
- World and Commands now have `spawn_empty()`, which is equivalent to the old `spawn()` behavior.
## Migration Guide
```rust
// Old (0.8):
commands
.spawn()
.insert_bundle((A, B, C));
// New (0.9)
commands.spawn((A, B, C));
// Old (0.8):
commands.spawn_bundle((A, B, C));
// New (0.9)
commands.spawn((A, B, C));
// Old (0.8):
let entity = commands.spawn().id();
// New (0.9)
let entity = commands.spawn_empty().id();
// Old (0.8)
let entity = world.spawn().id();
// New (0.9)
let entity = world.spawn_empty();
```
2022-09-23 19:55:54 +00:00
|
|
|
});
|
2023-03-18 22:58:17 +00:00
|
|
|
}
|
2022-07-21 14:39:03 +00:00
|
|
|
}
|