bevy/examples/window/low_power.rs
Joona Aalto a795de30b4
Use impl Into<A> for Assets::add (#10878)
# Motivation

When spawning entities into a scene, it is very common to create assets
like meshes and materials and to add them via asset handles. A common
setup might look like this:

```rust
fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    commands.spawn(PbrBundle {
        mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
        material: materials.add(StandardMaterial::from(Color::RED)),
        ..default()
    });
}
```

Let's take a closer look at the part that adds the assets using `add`.

```rust
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(StandardMaterial::from(Color::RED)),
```

Here, "mesh" and "material" are both repeated three times. It's very
explicit, but I find it to be a bit verbose. In addition to being more
code to read and write, the extra characters can sometimes also lead to
the code being formatted to span multiple lines even though the core
task, adding e.g. a primitive mesh, is extremely simple.

A way to address this is by using `.into()`:

```rust
mesh: meshes.add(shape::Cube { size: 1.0 }.into()),
material: materials.add(Color::RED.into()),
```

This is fine, but from the names and the type of `meshes`, we already
know what the type should be. It's very clear that `Cube` should be
turned into a `Mesh` because of the context it's used in. `.into()` is
just seven characters, but it's so common that it quickly adds up and
gets annoying.

It would be nice if you could skip all of the conversion and let Bevy
handle it for you:

```rust
mesh: meshes.add(shape::Cube { size: 1.0 }),
material: materials.add(Color::RED),
```

# Objective

Make adding assets more ergonomic by making `Assets::add` take an `impl
Into<A>` instead of `A`.

## Solution

`Assets::add` now takes an `impl Into<A>` instead of `A`, so e.g. this
works:

```rust
    commands.spawn(PbrBundle {
        mesh: meshes.add(shape::Cube { size: 1.0 }),
        material: materials.add(Color::RED),
        ..default()
    });
```

I also changed all examples to use this API, which increases consistency
as well because `Mesh::from` and `into` were being used arbitrarily even
in the same file. This also gets rid of some lines of code because
formatting is nicer.

---

## Changelog

- `Assets::add` now takes an `impl Into<A>` instead of `A`
- Examples don't use `T::from(K)` or `K.into()` when adding assets

## Migration Guide

Some `into` calls that worked previously might now be broken because of
the new trait bounds. You need to either remove `into` or perform the
conversion explicitly with `from`:

```rust
// Doesn't compile
let mesh_handle = meshes.add(shape::Cube { size: 1.0 }.into()),

// These compile
let mesh_handle = meshes.add(shape::Cube { size: 1.0 }),
let mesh_handle = meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
```

## Concerns

I believe the primary concerns might be:

1. Is this too implicit?
2. Does this increase codegen bloat?

Previously, the two APIs were using `into` or `from`, and now it's
"nothing" or `from`. You could argue that `into` is slightly more
explicit than "nothing" in cases like the earlier examples where a
`Color` gets converted to e.g. a `StandardMaterial`, but I personally
don't think `into` adds much value even in this case, and you could
still see the actual type from the asset type.

As for codegen bloat, I doubt it adds that much, but I'm not very
familiar with the details of codegen. I personally value the user-facing
code reduction and ergonomics improvements that these changes would
provide, but it might be worth checking the other effects in more
detail.

Another slight concern is migration pain; apps might have a ton of
`into` calls that would need to be removed, and it did take me a while
to do so for Bevy itself (maybe around 20-40 minutes). However, I think
the fact that there *are* so many `into` calls just highlights that the
API could be made nicer, and I'd gladly migrate my own projects for it.
2024-01-08 22:14:43 +00:00

211 lines
7.4 KiB
Rust

//! This example illustrates how to run a winit window in a reactive, low power mode.
//!
//! This is useful for making desktop applications, or any other program that doesn't need to be
//! running the event loop non-stop.
use bevy::{
prelude::*,
utils::Duration,
window::{PresentMode, RequestRedraw, WindowPlugin},
winit::WinitSettings,
};
fn main() {
App::new()
// Continuous rendering for games - bevy's default.
.insert_resource(WinitSettings::game())
// Power-saving reactive rendering for applications.
.insert_resource(WinitSettings::desktop_app())
// You can also customize update behavior with the fields of [`WinitConfig`]
.insert_resource(WinitSettings {
focused_mode: bevy::winit::UpdateMode::Continuous,
unfocused_mode: bevy::winit::UpdateMode::ReactiveLowPower {
wait: Duration::from_millis(10),
},
})
.insert_resource(ExampleMode::Game)
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
// Turn off vsync to maximize CPU/GPU usage
present_mode: PresentMode::AutoNoVsync,
..default()
}),
..default()
}))
.add_systems(Startup, test_setup::setup)
.add_systems(
Update,
(
test_setup::cycle_modes,
test_setup::rotate_cube,
test_setup::update_text,
update_winit,
),
)
.run();
}
#[derive(Resource, Debug)]
enum ExampleMode {
Game,
Application,
ApplicationWithRedraw,
}
/// Update winit based on the current `ExampleMode`
fn update_winit(
mode: Res<ExampleMode>,
mut event: EventWriter<RequestRedraw>,
mut winit_config: ResMut<WinitSettings>,
) {
use ExampleMode::*;
*winit_config = match *mode {
Game => {
// In the default `WinitConfig::game()` mode:
// * When focused: the event loop runs as fast as possible
// * When not focused: the event loop runs as fast as possible
WinitSettings::game()
}
Application => {
// While in `WinitConfig::desktop_app()` mode:
// * When focused: the app will update any time a winit event (e.g. the window is
// moved/resized, the mouse moves, a button is pressed, etc.), a [`RequestRedraw`]
// event is received, or after 5 seconds if the app has not updated.
// * When not focused: the app will update when the window is directly interacted with
// (e.g. the mouse hovers over a visible part of the out of focus window), a
// [`RequestRedraw`] event is received, or one minute has passed without the app
// updating.
WinitSettings::desktop_app()
}
ApplicationWithRedraw => {
// Sending a `RequestRedraw` event is useful when you want the app to update the next
// frame regardless of any user input. For example, your application might use
// `WinitConfig::desktop_app()` to reduce power use, but UI animations need to play even
// when there are no inputs, so you send redraw requests while the animation is playing.
event.send(RequestRedraw);
WinitSettings::desktop_app()
}
};
}
/// Everything in this module is for setting up and animating the scene, and is not important to the
/// demonstrated features.
pub(crate) mod test_setup {
use crate::ExampleMode;
use bevy::{prelude::*, window::RequestRedraw};
/// Switch between update modes when the mouse is clicked.
pub(crate) fn cycle_modes(
mut mode: ResMut<ExampleMode>,
mouse_button_input: Res<ButtonInput<KeyCode>>,
) {
if mouse_button_input.just_pressed(KeyCode::Space) {
*mode = match *mode {
ExampleMode::Game => ExampleMode::Application,
ExampleMode::Application => ExampleMode::ApplicationWithRedraw,
ExampleMode::ApplicationWithRedraw => ExampleMode::Game,
};
}
}
#[derive(Component)]
pub(crate) struct Rotator;
/// Rotate the cube to make it clear when the app is updating
pub(crate) fn rotate_cube(
time: Res<Time>,
mut cube_transform: Query<&mut Transform, With<Rotator>>,
) {
for mut transform in &mut cube_transform {
transform.rotate_x(time.delta_seconds());
transform.rotate_local_y(time.delta_seconds());
}
}
#[derive(Component)]
pub struct ModeText;
pub(crate) fn update_text(
mut frame: Local<usize>,
mode: Res<ExampleMode>,
mut query: Query<&mut Text, With<ModeText>>,
) {
*frame += 1;
let mode = match *mode {
ExampleMode::Game => "game(), continuous, default",
ExampleMode::Application => "desktop_app(), reactive",
ExampleMode::ApplicationWithRedraw => "desktop_app(), reactive, RequestRedraw sent",
};
let mut text = query.single_mut();
text.sections[1].value = mode.to_string();
text.sections[3].value = frame.to_string();
}
/// Set up a scene with a cube and some text
pub fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut event: EventWriter<RequestRedraw>,
) {
commands.spawn((
PbrBundle {
mesh: meshes.add(shape::Cube { size: 0.5 }),
material: materials.add(Color::rgb(0.8, 0.7, 0.6)),
..default()
},
Rotator,
));
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.0, 2.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
event.send(RequestRedraw);
commands.spawn((
TextBundle::from_sections([
TextSection::new(
"Press spacebar to cycle modes\n",
TextStyle {
font_size: 50.0,
..default()
},
),
TextSection::from_style(TextStyle {
font_size: 50.0,
color: Color::GREEN,
..default()
}),
TextSection::new(
"\nFrame: ",
TextStyle {
font_size: 50.0,
color: Color::YELLOW,
..default()
},
),
TextSection::from_style(TextStyle {
font_size: 50.0,
color: Color::YELLOW,
..default()
}),
])
.with_style(Style {
align_self: AlignSelf::FlexStart,
position_type: PositionType::Absolute,
top: Val::Px(5.0),
left: Val::Px(5.0),
..default()
}),
ModeText,
));
}
}