# 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) { } } ```
134 lines
4.3 KiB
Rust
134 lines
4.3 KiB
Rust
//! This crate contains Bevy's UI system, which can be used to create UI for both 2D and 3D games
|
|
//! # Basic usage
|
|
//! Spawn UI elements with [`entity::ButtonBundle`], [`entity::ImageBundle`], [`entity::TextBundle`] and [`entity::NodeBundle`]
|
|
//! This UI is laid out with the Flexbox paradigm (see <https://cssreference.io/flexbox/> ) except the vertical axis is inverted
|
|
mod flex;
|
|
mod focus;
|
|
mod geometry;
|
|
mod render;
|
|
mod ui_node;
|
|
|
|
pub mod entity;
|
|
pub mod update;
|
|
pub mod widget;
|
|
|
|
use bevy_render::extract_component::ExtractComponentPlugin;
|
|
pub use flex::*;
|
|
pub use focus::*;
|
|
pub use geometry::*;
|
|
pub use render::*;
|
|
pub use ui_node::*;
|
|
|
|
#[doc(hidden)]
|
|
pub mod prelude {
|
|
#[doc(hidden)]
|
|
pub use crate::{entity::*, geometry::*, ui_node::*, widget::Button, Interaction, UiScale};
|
|
}
|
|
|
|
use bevy_app::prelude::*;
|
|
use bevy_ecs::{
|
|
schedule::{IntoSystemDescriptor, SystemLabel},
|
|
system::Resource,
|
|
};
|
|
use bevy_input::InputSystem;
|
|
use bevy_transform::TransformSystem;
|
|
use bevy_window::ModifiesWindows;
|
|
use update::{ui_z_system, update_clipping_system};
|
|
|
|
use crate::prelude::UiCameraConfig;
|
|
|
|
/// The basic plugin for Bevy UI
|
|
#[derive(Default)]
|
|
pub struct UiPlugin;
|
|
|
|
/// The label enum labeling the types of systems in the Bevy UI
|
|
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
|
|
pub enum UiSystem {
|
|
/// After this label, the ui flex state has been updated
|
|
Flex,
|
|
/// After this label, input interactions with UI entities have been updated for this frame
|
|
Focus,
|
|
}
|
|
|
|
/// The current scale of the UI.
|
|
///
|
|
/// A multiplier to fixed-sized ui values.
|
|
/// **Note:** This will only affect fixed ui values like [`Val::Px`]
|
|
#[derive(Debug, Resource)]
|
|
pub struct UiScale {
|
|
/// The scale to be applied.
|
|
pub scale: f64,
|
|
}
|
|
|
|
impl Default for UiScale {
|
|
fn default() -> Self {
|
|
Self { scale: 1.0 }
|
|
}
|
|
}
|
|
|
|
impl Plugin for UiPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.add_plugin(ExtractComponentPlugin::<UiCameraConfig>::default())
|
|
.init_resource::<FlexSurface>()
|
|
.init_resource::<UiScale>()
|
|
.register_type::<AlignContent>()
|
|
.register_type::<AlignItems>()
|
|
.register_type::<AlignSelf>()
|
|
.register_type::<CalculatedSize>()
|
|
.register_type::<Direction>()
|
|
.register_type::<Display>()
|
|
.register_type::<FlexDirection>()
|
|
.register_type::<FlexWrap>()
|
|
.register_type::<FocusPolicy>()
|
|
.register_type::<Interaction>()
|
|
.register_type::<JustifyContent>()
|
|
.register_type::<Node>()
|
|
// NOTE: used by Style::aspect_ratio
|
|
.register_type::<Option<f32>>()
|
|
.register_type::<Overflow>()
|
|
.register_type::<PositionType>()
|
|
.register_type::<Size>()
|
|
.register_type::<UiRect>()
|
|
.register_type::<Style>()
|
|
.register_type::<BackgroundColor>()
|
|
.register_type::<UiImage>()
|
|
.register_type::<Val>()
|
|
.register_type::<widget::Button>()
|
|
.register_type::<widget::ImageMode>()
|
|
.add_system_to_stage(
|
|
CoreStage::PreUpdate,
|
|
ui_focus_system.label(UiSystem::Focus).after(InputSystem),
|
|
)
|
|
// add these stages to front because these must run before transform update systems
|
|
.add_system_to_stage(
|
|
CoreStage::PostUpdate,
|
|
widget::text_system
|
|
.before(UiSystem::Flex)
|
|
.after(ModifiesWindows),
|
|
)
|
|
.add_system_to_stage(
|
|
CoreStage::PostUpdate,
|
|
widget::image_node_system.before(UiSystem::Flex),
|
|
)
|
|
.add_system_to_stage(
|
|
CoreStage::PostUpdate,
|
|
flex_node_system
|
|
.label(UiSystem::Flex)
|
|
.before(TransformSystem::TransformPropagate)
|
|
.after(ModifiesWindows),
|
|
)
|
|
.add_system_to_stage(
|
|
CoreStage::PostUpdate,
|
|
ui_z_system
|
|
.after(UiSystem::Flex)
|
|
.before(TransformSystem::TransformPropagate),
|
|
)
|
|
.add_system_to_stage(
|
|
CoreStage::PostUpdate,
|
|
update_clipping_system.after(TransformSystem::TransformPropagate),
|
|
);
|
|
|
|
crate::render::build_ui_render(app);
|
|
}
|
|
}
|