mirror of
https://github.com/bevyengine/bevy
synced 2025-02-16 14:08:32 +00:00
# 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.
172 lines
4.7 KiB
Rust
172 lines
4.7 KiB
Rust
//! A test to confirm that `bevy` allows setting the window to arbitrary small sizes
|
|
//! This is run in CI to ensure that this doesn't regress again.
|
|
|
|
use bevy::{prelude::*, window::WindowResolution};
|
|
|
|
// The smallest size reached is 1x1, as X11 doesn't support windows with a 0 dimension
|
|
// TODO: Add a check for platforms other than X11 for 0xk and kx0, despite those currently unsupported on CI.
|
|
const MAX_WIDTH: u16 = 401;
|
|
const MAX_HEIGHT: u16 = 401;
|
|
const MIN_WIDTH: u16 = 1;
|
|
const MIN_HEIGHT: u16 = 1;
|
|
const RESIZE_STEP: u16 = 4;
|
|
|
|
#[derive(Resource)]
|
|
struct Dimensions {
|
|
width: u16,
|
|
height: u16,
|
|
}
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(
|
|
DefaultPlugins.set(WindowPlugin {
|
|
primary_window: Some(Window {
|
|
resolution: WindowResolution::new(MAX_WIDTH as f32, MAX_HEIGHT as f32)
|
|
.with_scale_factor_override(1.0),
|
|
title: "Resizing".into(),
|
|
..default()
|
|
}),
|
|
..default()
|
|
}),
|
|
)
|
|
.insert_resource(Dimensions {
|
|
width: MAX_WIDTH,
|
|
height: MAX_HEIGHT,
|
|
})
|
|
.insert_resource(ContractingY)
|
|
.add_systems(Startup, (setup_3d, setup_2d))
|
|
.add_systems(
|
|
Update,
|
|
(
|
|
change_window_size,
|
|
sync_dimensions,
|
|
bevy::window::close_on_esc,
|
|
),
|
|
)
|
|
.run();
|
|
}
|
|
|
|
#[derive(Resource)]
|
|
enum Phase {
|
|
ContractingY,
|
|
ContractingX,
|
|
ExpandingY,
|
|
ExpandingX,
|
|
}
|
|
|
|
use Phase::*;
|
|
|
|
fn change_window_size(
|
|
mut windows: ResMut<Dimensions>,
|
|
mut phase: ResMut<Phase>,
|
|
mut first_complete: Local<bool>,
|
|
) {
|
|
// Put off rendering for one frame, as currently for a frame where
|
|
// resizing happens, nothing is presented.
|
|
// TODO: Debug and fix this if feasible
|
|
if !*first_complete {
|
|
*first_complete = true;
|
|
return;
|
|
}
|
|
let height = windows.height;
|
|
let width = windows.width;
|
|
match *phase {
|
|
ContractingY => {
|
|
if height <= MIN_HEIGHT {
|
|
*phase = ContractingX;
|
|
} else {
|
|
windows.height -= RESIZE_STEP;
|
|
}
|
|
}
|
|
ContractingX => {
|
|
if width <= MIN_WIDTH {
|
|
*phase = ExpandingY;
|
|
} else {
|
|
windows.width -= RESIZE_STEP;
|
|
}
|
|
}
|
|
ExpandingY => {
|
|
if height >= MAX_HEIGHT {
|
|
*phase = ExpandingX;
|
|
} else {
|
|
windows.height += RESIZE_STEP;
|
|
}
|
|
}
|
|
ExpandingX => {
|
|
if width >= MAX_WIDTH {
|
|
*phase = ContractingY;
|
|
} else {
|
|
windows.width += RESIZE_STEP;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn sync_dimensions(dim: Res<Dimensions>, mut windows: Query<&mut Window>) {
|
|
if dim.is_changed() {
|
|
let mut window = windows.single_mut();
|
|
window.resolution.set(dim.width as f32, dim.height as f32);
|
|
}
|
|
}
|
|
|
|
/// A simple 3d scene, taken from the `3d_scene` example
|
|
fn setup_3d(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
) {
|
|
// plane
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(shape::Plane {
|
|
size: 5.0,
|
|
subdivisions: 0,
|
|
}),
|
|
material: materials.add(Color::rgb(0.3, 0.5, 0.3)),
|
|
..default()
|
|
});
|
|
// cube
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(shape::Cube { size: 1.0 }),
|
|
material: materials.add(Color::rgb(0.8, 0.7, 0.6)),
|
|
transform: Transform::from_xyz(0.0, 0.5, 0.0),
|
|
..default()
|
|
});
|
|
// light
|
|
commands.spawn(PointLightBundle {
|
|
point_light: PointLight {
|
|
intensity: 1500.0,
|
|
shadows_enabled: true,
|
|
..default()
|
|
},
|
|
transform: Transform::from_xyz(4.0, 8.0, 4.0),
|
|
..default()
|
|
});
|
|
// camera
|
|
commands.spawn(Camera3dBundle {
|
|
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
|
|
..default()
|
|
});
|
|
}
|
|
|
|
/// A simple 2d scene, taken from the `rect` example
|
|
fn setup_2d(mut commands: Commands) {
|
|
commands.spawn(Camera2dBundle {
|
|
camera: Camera {
|
|
// render the 2d camera after the 3d camera
|
|
order: 1,
|
|
// do not use a clear color
|
|
clear_color: ClearColorConfig::None,
|
|
..default()
|
|
},
|
|
..default()
|
|
});
|
|
commands.spawn(SpriteBundle {
|
|
sprite: Sprite {
|
|
color: Color::rgb(0.25, 0.25, 0.75),
|
|
custom_size: Some(Vec2::new(50.0, 50.0)),
|
|
..default()
|
|
},
|
|
..default()
|
|
});
|
|
}
|