mirror of
https://github.com/bevyengine/bevy
synced 2024-12-23 11:33:06 +00:00
dc3f801239
# Objective The [Stageless RFC](https://github.com/bevyengine/rfcs/pull/45) involves allowing exclusive systems to be referenced and ordered relative to parallel systems. We've agreed that unifying systems under `System` is the right move. This is an alternative to #4166 (see rationale in the comments I left there). Note that this builds on the learnings established there (and borrows some patterns). ## Solution This unifies parallel and exclusive systems under the shared `System` trait, removing the old `ExclusiveSystem` trait / impls. This is accomplished by adding a new `ExclusiveFunctionSystem` impl similar to `FunctionSystem`. It is backed by `ExclusiveSystemParam`, which is similar to `SystemParam`. There is a new flattened out SystemContainer api (which cuts out a lot of trait and type complexity). This means you can remove all cases of `exclusive_system()`: ```rust // before commands.add_system(some_system.exclusive_system()); // after commands.add_system(some_system); ``` I've also implemented `ExclusiveSystemParam` for `&mut QueryState` and `&mut SystemState`, which makes this possible in exclusive systems: ```rust fn some_exclusive_system( world: &mut World, transforms: &mut QueryState<&Transform>, state: &mut SystemState<(Res<Time>, Query<&Player>)>, ) { for transform in transforms.iter(world) { println!("{transform:?}"); } let (time, players) = state.get(world); for player in players.iter() { println!("{player:?}"); } } ``` Note that "exclusive function systems" assume `&mut World` is present (and the first param). I think this is a fair assumption, given that the presence of `&mut World` is what defines the need for an exclusive system. I added some targeted SystemParam `static` constraints, which removed the need for this: ``` rust fn some_exclusive_system(state: &mut SystemState<(Res<'static, Time>, Query<&'static Player>)>) {} ``` ## Related - #2923 - #3001 - #3946 ## Changelog - `ExclusiveSystem` trait (and implementations) has been removed in favor of sharing the `System` trait. - `ExclusiveFunctionSystem` and `ExclusiveSystemParam` were added, enabling flexible exclusive function systems - `&mut SystemState` and `&mut QueryState` now implement `ExclusiveSystemParam` - Exclusive and parallel System configuration is now done via a unified `SystemDescriptor`, `IntoSystemDescriptor`, and `SystemContainer` api. ## Migration Guide Calling `.exclusive_system()` is no longer required (or supported) for converting exclusive system functions to exclusive systems: ```rust // Old (0.8) app.add_system(some_exclusive_system.exclusive_system()); // New (0.9) app.add_system(some_exclusive_system); ``` Converting "normal" parallel systems to exclusive systems is done by calling the exclusive ordering apis: ```rust // Old (0.8) app.add_system(some_system.exclusive_system().at_end()); // New (0.9) app.add_system(some_system.at_end()); ``` Query state in exclusive systems can now be cached via ExclusiveSystemParams, which should be preferred for clarity and performance reasons: ```rust // Old (0.8) fn some_system(world: &mut World) { let mut transforms = world.query::<&Transform>(); for transform in transforms.iter(world) { } } // New (0.9) fn some_system(world: &mut World, transforms: &mut QueryState<&Transform>) { for transform in transforms.iter(world) { } } ```
126 lines
4.1 KiB
Rust
126 lines
4.1 KiB
Rust
#[warn(missing_docs)]
|
|
mod cursor;
|
|
mod event;
|
|
mod raw_window_handle;
|
|
mod system;
|
|
mod window;
|
|
mod windows;
|
|
|
|
pub use crate::raw_window_handle::*;
|
|
pub use cursor::*;
|
|
pub use event::*;
|
|
pub use system::*;
|
|
pub use window::*;
|
|
pub use windows::*;
|
|
|
|
pub mod prelude {
|
|
#[doc(hidden)]
|
|
pub use crate::{
|
|
CursorEntered, CursorIcon, CursorLeft, CursorMoved, FileDragAndDrop, MonitorSelection,
|
|
ReceivedCharacter, Window, WindowDescriptor, WindowMode, WindowMoved, WindowPosition,
|
|
Windows,
|
|
};
|
|
}
|
|
|
|
use bevy_app::prelude::*;
|
|
use bevy_ecs::{
|
|
event::Events,
|
|
schedule::{IntoSystemDescriptor, SystemLabel},
|
|
system::Resource,
|
|
};
|
|
|
|
/// The configuration information for the [`WindowPlugin`].
|
|
///
|
|
/// It can be added as a [`Resource`](bevy_ecs::system::Resource) before the [`WindowPlugin`]
|
|
/// runs, to configure how it behaves.
|
|
#[derive(Resource, Clone)]
|
|
pub struct WindowSettings {
|
|
/// Whether to create a window when added.
|
|
///
|
|
/// Note that if there are no windows, by default the App will exit,
|
|
/// due to [`exit_on_all_closed`].
|
|
pub add_primary_window: bool,
|
|
/// Whether to exit the app when there are no open windows.
|
|
///
|
|
/// If disabling this, ensure that you send the [`bevy_app::AppExit`]
|
|
/// event when the app should exit. If this does not occur, you will
|
|
/// create 'headless' processes (processes without windows), which may
|
|
/// surprise your users. It is recommended to leave this setting as `true`.
|
|
///
|
|
/// If true, this plugin will add [`exit_on_all_closed`] to [`CoreStage::Update`].
|
|
pub exit_on_all_closed: bool,
|
|
/// Whether to close windows when they are requested to be closed (i.e.
|
|
/// when the close button is pressed).
|
|
///
|
|
/// If true, this plugin will add [`close_when_requested`] to [`CoreStage::Update`].
|
|
/// If this system (or a replacement) is not running, the close button will have no effect.
|
|
/// This may surprise your users. It is recommended to leave this setting as `true`.
|
|
pub close_when_requested: bool,
|
|
}
|
|
|
|
impl Default for WindowSettings {
|
|
fn default() -> Self {
|
|
WindowSettings {
|
|
add_primary_window: true,
|
|
exit_on_all_closed: true,
|
|
close_when_requested: true,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A [`Plugin`] that defines an interface for windowing support in Bevy.
|
|
#[derive(Default)]
|
|
pub struct WindowPlugin;
|
|
|
|
impl Plugin for WindowPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.add_event::<WindowResized>()
|
|
.add_event::<CreateWindow>()
|
|
.add_event::<WindowCreated>()
|
|
.add_event::<WindowClosed>()
|
|
.add_event::<WindowCloseRequested>()
|
|
.add_event::<RequestRedraw>()
|
|
.add_event::<CursorMoved>()
|
|
.add_event::<CursorEntered>()
|
|
.add_event::<CursorLeft>()
|
|
.add_event::<ReceivedCharacter>()
|
|
.add_event::<WindowFocused>()
|
|
.add_event::<WindowScaleFactorChanged>()
|
|
.add_event::<WindowBackendScaleFactorChanged>()
|
|
.add_event::<FileDragAndDrop>()
|
|
.add_event::<WindowMoved>()
|
|
.init_resource::<Windows>();
|
|
|
|
let settings = app
|
|
.world
|
|
.get_resource::<WindowSettings>()
|
|
.cloned()
|
|
.unwrap_or_default();
|
|
|
|
if settings.add_primary_window {
|
|
let window_descriptor = app
|
|
.world
|
|
.get_resource::<WindowDescriptor>()
|
|
.cloned()
|
|
.unwrap_or_default();
|
|
let mut create_window_event = app.world.resource_mut::<Events<CreateWindow>>();
|
|
create_window_event.send(CreateWindow {
|
|
id: WindowId::primary(),
|
|
descriptor: window_descriptor,
|
|
});
|
|
}
|
|
|
|
if settings.exit_on_all_closed {
|
|
app.add_system_to_stage(
|
|
CoreStage::PostUpdate,
|
|
exit_on_all_closed.after(ModifiesWindows),
|
|
);
|
|
}
|
|
if settings.close_when_requested {
|
|
app.add_system(close_when_requested);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
|
|
pub struct ModifiesWindows;
|