Skip to content

Commit

Permalink
Non-string labels (#1423 continued) (#1473)
Browse files Browse the repository at this point in the history
Non-string labels
  • Loading branch information
Ratysz committed Feb 18, 2021
1 parent 82d0e84 commit c2a427f
Show file tree
Hide file tree
Showing 36 changed files with 711 additions and 440 deletions.
92 changes: 46 additions & 46 deletions crates/bevy_app/src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use crate::{
app::{App, AppExit},
event::Events,
plugin::Plugin,
stage, startup_stage, PluginGroup, PluginGroupBuilder,
CoreStage, PluginGroup, PluginGroupBuilder, StartupStage,
};
use bevy_ecs::{
clear_trackers_system, FromResources, IntoExclusiveSystem, IntoSystem, Resource, Resources,
RunOnce, Schedule, Stage, StateStage, SystemDescriptor, SystemStage, World,
RunOnce, Schedule, Stage, StageLabel, StateStage, SystemDescriptor, SystemStage, World,
};
use bevy_utils::tracing::debug;

Expand All @@ -24,7 +24,7 @@ impl Default for AppBuilder {
app_builder
.add_default_stages()
.add_event::<AppExit>()
.add_system_to_stage(stage::LAST, clear_trackers_system.exclusive_system());
.add_system_to_stage(CoreStage::Last, clear_trackers_system.exclusive_system());
app_builder
}
}
Expand Down Expand Up @@ -54,110 +54,110 @@ impl AppBuilder {
self
}

pub fn add_stage<S: Stage>(&mut self, name: &'static str, stage: S) -> &mut Self {
self.app.schedule.add_stage(name, stage);
pub fn add_stage<S: Stage>(&mut self, label: impl StageLabel, stage: S) -> &mut Self {
self.app.schedule.add_stage(label, stage);
self
}

pub fn add_stage_after<S: Stage>(
&mut self,
target: &'static str,
name: &'static str,
target: impl StageLabel,
label: impl StageLabel,
stage: S,
) -> &mut Self {
self.app.schedule.add_stage_after(target, name, stage);
self.app.schedule.add_stage_after(target, label, stage);
self
}

pub fn add_stage_before<S: Stage>(
&mut self,
target: &'static str,
name: &'static str,
target: impl StageLabel,
label: impl StageLabel,
stage: S,
) -> &mut Self {
self.app.schedule.add_stage_before(target, name, stage);
self.app.schedule.add_stage_before(target, label, stage);
self
}

pub fn add_startup_stage<S: Stage>(&mut self, name: &'static str, stage: S) -> &mut Self {
pub fn add_startup_stage<S: Stage>(&mut self, label: impl StageLabel, stage: S) -> &mut Self {
self.app
.schedule
.stage(stage::STARTUP, |schedule: &mut Schedule| {
schedule.add_stage(name, stage)
.stage(CoreStage::Startup, |schedule: &mut Schedule| {
schedule.add_stage(label, stage)
});
self
}

pub fn add_startup_stage_after<S: Stage>(
&mut self,
target: &'static str,
name: &'static str,
target: impl StageLabel,
label: impl StageLabel,
stage: S,
) -> &mut Self {
self.app
.schedule
.stage(stage::STARTUP, |schedule: &mut Schedule| {
schedule.add_stage_after(target, name, stage)
.stage(CoreStage::Startup, |schedule: &mut Schedule| {
schedule.add_stage_after(target, label, stage)
});
self
}

pub fn add_startup_stage_before<S: Stage>(
&mut self,
target: &'static str,
name: &'static str,
target: impl StageLabel,
label: impl StageLabel,
stage: S,
) -> &mut Self {
self.app
.schedule
.stage(stage::STARTUP, |schedule: &mut Schedule| {
schedule.add_stage_before(target, name, stage)
.stage(CoreStage::Startup, |schedule: &mut Schedule| {
schedule.add_stage_before(target, label, stage)
});
self
}

pub fn stage<T: Stage, F: FnOnce(&mut T) -> &mut T>(
&mut self,
name: &str,
label: impl StageLabel,
func: F,
) -> &mut Self {
self.app.schedule.stage(name, func);
self.app.schedule.stage(label, func);
self
}

pub fn add_system(&mut self, system: impl Into<SystemDescriptor>) -> &mut Self {
self.add_system_to_stage(stage::UPDATE, system)
self.add_system_to_stage(CoreStage::Update, system)
}

pub fn add_system_to_stage(
&mut self,
stage_name: &'static str,
stage_label: impl StageLabel,
system: impl Into<SystemDescriptor>,
) -> &mut Self {
self.app.schedule.add_system_to_stage(stage_name, system);
self.app.schedule.add_system_to_stage(stage_label, system);
self
}

pub fn add_startup_system(&mut self, system: impl Into<SystemDescriptor>) -> &mut Self {
self.add_startup_system_to_stage(startup_stage::STARTUP, system)
self.add_startup_system_to_stage(StartupStage::Startup, system)
}

pub fn add_startup_system_to_stage(
&mut self,
stage_name: &'static str,
stage_label: impl StageLabel,
system: impl Into<SystemDescriptor>,
) -> &mut Self {
self.app
.schedule
.stage(stage::STARTUP, |schedule: &mut Schedule| {
schedule.add_system_to_stage(stage_name, system)
.stage(CoreStage::Startup, |schedule: &mut Schedule| {
schedule.add_system_to_stage(stage_label, system)
});
self
}

pub fn on_state_enter<T: Clone + Resource>(
&mut self,
stage: &str,
stage: impl StageLabel,
state: T,
system: impl Into<SystemDescriptor>,
) -> &mut Self {
Expand All @@ -168,7 +168,7 @@ impl AppBuilder {

pub fn on_state_update<T: Clone + Resource>(
&mut self,
stage: &str,
stage: impl StageLabel,
state: T,
system: impl Into<SystemDescriptor>,
) -> &mut Self {
Expand All @@ -179,7 +179,7 @@ impl AppBuilder {

pub fn on_state_exit<T: Clone + Resource>(
&mut self,
stage: &str,
stage: impl StageLabel,
state: T,
system: impl Into<SystemDescriptor>,
) -> &mut Self {
Expand All @@ -190,28 +190,28 @@ impl AppBuilder {

pub fn add_default_stages(&mut self) -> &mut Self {
self.add_stage(
stage::STARTUP,
CoreStage::Startup,
Schedule::default()
.with_run_criteria(RunOnce::default())
.with_stage(startup_stage::PRE_STARTUP, SystemStage::parallel())
.with_stage(startup_stage::STARTUP, SystemStage::parallel())
.with_stage(startup_stage::POST_STARTUP, SystemStage::parallel()),
.with_stage(StartupStage::PreStartup, SystemStage::parallel())
.with_stage(StartupStage::Startup, SystemStage::parallel())
.with_stage(StartupStage::PostStartup, SystemStage::parallel()),
)
.add_stage(stage::FIRST, SystemStage::parallel())
.add_stage(stage::PRE_EVENT, SystemStage::parallel())
.add_stage(stage::EVENT, SystemStage::parallel())
.add_stage(stage::PRE_UPDATE, SystemStage::parallel())
.add_stage(stage::UPDATE, SystemStage::parallel())
.add_stage(stage::POST_UPDATE, SystemStage::parallel())
.add_stage(stage::LAST, SystemStage::parallel())
.add_stage(CoreStage::First, SystemStage::parallel())
.add_stage(CoreStage::PreEvent, SystemStage::parallel())
.add_stage(CoreStage::Event, SystemStage::parallel())
.add_stage(CoreStage::PreUpdate, SystemStage::parallel())
.add_stage(CoreStage::Update, SystemStage::parallel())
.add_stage(CoreStage::PostUpdate, SystemStage::parallel())
.add_stage(CoreStage::Last, SystemStage::parallel())
}

pub fn add_event<T>(&mut self) -> &mut Self
where
T: Send + Sync + 'static,
{
self.insert_resource(Events::<T>::default())
.add_system_to_stage(stage::EVENT, Events::<T>::update_system.system())
.add_system_to_stage(CoreStage::Event, Events::<T>::update_system.system())
}

/// Inserts a resource to the current [App] and overwrites any resource previously added of the same type.
Expand Down
40 changes: 34 additions & 6 deletions crates/bevy_app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/// The names of the default App stages
pub mod stage;
/// The names of the default App startup stages
pub mod startup_stage;

mod app;
mod app_builder;
mod event;
Expand All @@ -23,6 +18,39 @@ pub mod prelude {
app::App,
app_builder::AppBuilder,
event::{EventReader, Events},
stage, DynamicPlugin, Plugin, PluginGroup,
CoreStage, DynamicPlugin, Plugin, PluginGroup, StartupStage,
};
}

use bevy_ecs::StageLabel;

/// The names of the default App stages
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
pub enum CoreStage {
/// Runs once at the beginning of the app.
Startup,
/// Name of app stage that runs before all other app stages
First,
/// Name of app stage that runs before EVENT
PreEvent,
/// Name of app stage that updates events. Runs before UPDATE
Event,
/// Name of app stage responsible for performing setup before an update. Runs before UPDATE.
PreUpdate,
/// Name of app stage responsible for doing most app logic. Systems should be registered here by default.
Update,
/// Name of app stage responsible for processing the results of UPDATE. Runs after UPDATE.
PostUpdate,
/// Name of app stage that runs after all other app stages
Last,
}
/// The names of the default App startup stages
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
pub enum StartupStage {
/// Name of app stage that runs once before the startup stage
PreStartup,
/// Name of app stage that runs once when an app starts up
Startup,
/// Name of app stage that runs once after the startup stage
PostStartup,
}
23 changes: 0 additions & 23 deletions crates/bevy_app/src/stage.rs

This file was deleted.

8 changes: 0 additions & 8 deletions crates/bevy_app/src/startup_stage.rs

This file was deleted.

7 changes: 4 additions & 3 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
update_asset_storage_system, Asset, AssetLoader, AssetServer, Handle, HandleId, RefChange,
update_asset_storage_system, Asset, AssetLoader, AssetServer, AssetStage, Handle, HandleId,
RefChange,
};
use bevy_app::{prelude::Events, AppBuilder};
use bevy_ecs::{FromResources, IntoSystem, ResMut};
Expand Down Expand Up @@ -219,11 +220,11 @@ impl AddAsset for AppBuilder {

self.insert_resource(assets)
.add_system_to_stage(
super::stage::ASSET_EVENTS,
AssetStage::AssetEvents,
Assets::<T>::asset_event_system.system(),
)
.add_system_to_stage(
crate::stage::LOAD_ASSETS,
AssetStage::LoadAssets,
update_asset_storage_system::<T>.system(),
)
.register_type::<Handle<T>>()
Expand Down
34 changes: 19 additions & 15 deletions crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,27 @@ mod path;

pub use asset_server::*;
pub use assets::*;
use bevy_ecs::{IntoSystem, SystemStage};
use bevy_reflect::RegisterTypeBuilder;
use bevy_tasks::IoTaskPool;
pub use handle::*;
pub use info::*;
pub use io::*;
pub use loader::*;
pub use path::*;

/// The names of asset stages in an App Schedule
pub mod stage {
pub const LOAD_ASSETS: &str = "load_assets";
pub const ASSET_EVENTS: &str = "asset_events";
}

pub mod prelude {
pub use crate::{AddAsset, AssetEvent, AssetServer, Assets, Handle, HandleUntyped};
}

use bevy_app::{prelude::Plugin, AppBuilder};
use bevy_ecs::{IntoSystem, StageLabel, SystemStage};
use bevy_reflect::RegisterTypeBuilder;
use bevy_tasks::IoTaskPool;

/// The names of asset stages in an App Schedule
#[derive(Debug, Hash, PartialEq, Eq, Clone, StageLabel)]
pub enum AssetStage {
LoadAssets,
AssetEvents,
}

/// Adds support for Assets to an App. Assets are typed collections with change tracking, which are added as App Resources.
/// Examples of assets: textures, sounds, 3d models, maps, scenes
Expand Down Expand Up @@ -89,25 +90,28 @@ impl Plugin for AssetPlugin {
}

app.add_stage_before(
bevy_app::stage::PRE_UPDATE,
stage::LOAD_ASSETS,
bevy_app::CoreStage::PreUpdate,
AssetStage::LoadAssets,
SystemStage::parallel(),
)
.add_stage_after(
bevy_app::stage::POST_UPDATE,
stage::ASSET_EVENTS,
bevy_app::CoreStage::PostUpdate,
AssetStage::AssetEvents,
SystemStage::parallel(),
)
.register_type::<HandleId>()
.add_system_to_stage(
bevy_app::stage::PRE_UPDATE,
bevy_app::CoreStage::PreUpdate,
asset_server::free_unused_assets_system.system(),
);

#[cfg(all(
feature = "filesystem_watcher",
all(not(target_arch = "wasm32"), not(target_os = "android"))
))]
app.add_system_to_stage(stage::LOAD_ASSETS, io::filesystem_watcher_system.system());
app.add_system_to_stage(
AssetStage::LoadAssets,
io::filesystem_watcher_system.system(),
);
}
}
Loading

0 comments on commit c2a427f

Please sign in to comment.