mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 22:50:19 +00:00
1bb751cb8d
# Objective Fixes #5884 #2879 Alternative to #2988 #5885 #2886 "Immutable" Plugin settings are currently represented as normal ECS resources, which are read as part of plugin init. This presents a number of problems: 1. If a user inserts the plugin settings resource after the plugin is initialized, it will be silently ignored (and use the defaults instead) 2. Users can modify the plugin settings resource after the plugin has been initialized. This creates a false sense of control over settings that can no longer be changed. (1) and (2) are especially problematic and confusing for the `WindowDescriptor` resource, but this is a general problem. ## Solution Immutable Plugin settings now live on each Plugin struct (ex: `WindowPlugin`). PluginGroups have been reworked to support overriding plugin values. This also removes the need for the `add_plugins_with` api, as the `add_plugins` api can use the builder pattern directly. Settings that can be used at runtime continue to be represented as ECS resources. Plugins are now configured like this: ```rust app.add_plugin(AssetPlugin { watch_for_changes: true, ..default() }) ``` PluginGroups are now configured like this: ```rust app.add_plugins(DefaultPlugins .set(AssetPlugin { watch_for_changes: true, ..default() }) ) ``` This is an alternative to #2988, which is similar. But I personally prefer this solution for a couple of reasons: * ~~#2988 doesn't solve (1)~~ #2988 does solve (1) and will panic in that case. I was wrong! * This PR directly ties plugin settings to Plugin types in a 1:1 relationship, rather than a loose "setup resource" <-> plugin coupling (where the setup resource is consumed by the first plugin that uses it). * I'm not a huge fan of overloading the ECS resource concept and implementation for something that has very different use cases and constraints. ## Changelog - PluginGroups can now be configured directly using the builder pattern. Individual plugin values can be overridden by using `plugin_group.set(SomePlugin {})`, which enables overriding default plugin values. - `WindowDescriptor` plugin settings have been moved to `WindowPlugin` and `AssetServerSettings` have been moved to `AssetPlugin` - `app.add_plugins_with` has been replaced by using `add_plugins` with the builder pattern. ## Migration Guide The `WindowDescriptor` settings have been moved from a resource to `WindowPlugin::window`: ```rust // Old (Bevy 0.8) app .insert_resource(WindowDescriptor { width: 400.0, ..default() }) .add_plugins(DefaultPlugins) // New (Bevy 0.9) app.add_plugins(DefaultPlugins.set(WindowPlugin { window: WindowDescriptor { width: 400.0, ..default() }, ..default() })) ``` The `AssetServerSettings` resource has been removed in favor of direct `AssetPlugin` configuration: ```rust // Old (Bevy 0.8) app .insert_resource(AssetServerSettings { watch_for_changes: true, ..default() }) .add_plugins(DefaultPlugins) // New (Bevy 0.9) app.add_plugins(DefaultPlugins.set(AssetPlugin { watch_for_changes: true, ..default() })) ``` `add_plugins_with` has been replaced by `add_plugins` in combination with the builder pattern: ```rust // Old (Bevy 0.8) app.add_plugins_with(DefaultPlugins, |group| group.disable::<AssetPlugin>()); // New (Bevy 0.9) app.add_plugins(DefaultPlugins.build().disable::<AssetPlugin>()); ```
168 lines
4.7 KiB
Rust
168 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::{core_pipeline::clear_color::ClearColorConfig, prelude::*};
|
|
|
|
// 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()
|
|
.insert_resource(Dimensions {
|
|
width: MAX_WIDTH,
|
|
height: MAX_HEIGHT,
|
|
})
|
|
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
|
window: WindowDescriptor {
|
|
width: MAX_WIDTH.try_into().unwrap(),
|
|
height: MAX_HEIGHT.try_into().unwrap(),
|
|
scale_factor_override: Some(1.),
|
|
title: "Resizing".into(),
|
|
..Default::default()
|
|
},
|
|
..default()
|
|
}))
|
|
.insert_resource(Phase::ContractingY)
|
|
.add_system(change_window_size)
|
|
.add_system(sync_dimensions)
|
|
.add_system(bevy::window::close_on_esc)
|
|
.add_startup_system(setup_3d)
|
|
.add_startup_system(setup_2d)
|
|
.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 {
|
|
Phase::ContractingY => {
|
|
if height <= MIN_HEIGHT {
|
|
*phase = ContractingX;
|
|
} else {
|
|
windows.height -= RESIZE_STEP;
|
|
}
|
|
}
|
|
Phase::ContractingX => {
|
|
if width <= MIN_WIDTH {
|
|
*phase = ExpandingY;
|
|
} else {
|
|
windows.width -= RESIZE_STEP;
|
|
}
|
|
}
|
|
Phase::ExpandingY => {
|
|
if height >= MAX_HEIGHT {
|
|
*phase = ExpandingX;
|
|
} else {
|
|
windows.height += RESIZE_STEP;
|
|
}
|
|
}
|
|
Phase::ExpandingX => {
|
|
if width >= MAX_WIDTH {
|
|
*phase = ContractingY;
|
|
} else {
|
|
windows.width += RESIZE_STEP;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn sync_dimensions(dim: Res<Dimensions>, mut windows: ResMut<Windows>) {
|
|
if dim.is_changed() {
|
|
windows.primary_mut().set_resolution(
|
|
dim.width.try_into().unwrap(),
|
|
dim.height.try_into().unwrap(),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// 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(Mesh::from(shape::Plane { size: 5.0 })),
|
|
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
|
|
..default()
|
|
});
|
|
// cube
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
|
|
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
|
|
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
|
|
priority: 1,
|
|
..default()
|
|
},
|
|
camera_2d: Camera2d {
|
|
// do not use a clear color
|
|
clear_color: ClearColorConfig::None,
|
|
},
|
|
..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()
|
|
});
|
|
}
|