diff --git a/bevy_asset_loader/examples/atlas_from_grid.rs b/bevy_asset_loader/examples/atlas_from_grid.rs index b595b76..39c3325 100644 --- a/bevy_asset_loader/examples/atlas_from_grid.rs +++ b/bevy_asset_loader/examples/atlas_from_grid.rs @@ -1,4 +1,5 @@ use bevy::prelude::*; +use bevy_asset_loader::loading_state::LoadingStateConfig; use bevy_asset_loader::prelude::*; /// This example demonstrates how to load a texture atlas from a sprite sheet @@ -11,7 +12,9 @@ fn main() { .add_loading_state( LoadingState::new(MyStates::AssetLoading).continue_to_state(MyStates::Next), ) - .add_collection_to_loading_state::<_, MyAssets>(MyStates::AssetLoading) + .configure_loading_state( + LoadingStateConfig::new(MyStates::AssetLoading).with_collection::(), + ) .add_systems(OnEnter(MyStates::Next), draw_atlas) .add_systems( Update, diff --git a/bevy_asset_loader/src/loading_state.rs b/bevy_asset_loader/src/loading_state.rs index 3f8973e..3adb122 100644 --- a/bevy_asset_loader/src/loading_state.rs +++ b/bevy_asset_loader/src/loading_state.rs @@ -3,6 +3,7 @@ mod systems; use bevy::app::{App, Plugin}; use bevy::asset::{Asset, UntypedHandle}; +use bevy::ecs::schedule::SystemConfigs; use bevy::ecs::{ schedule::{ common_conditions::in_state, InternedScheduleLabel, IntoSystemConfigs, @@ -663,6 +664,54 @@ pub trait LoadingStateAppExt { &mut self, loading_state: S, ) -> &mut Self; + + fn configure_loading_state( + &mut self, + configuration: LoadingStateConfig, + ) -> &mut Self; +} + +pub struct LoadingStateConfig { + state: S, + start_loading: Vec, + check_loading: Vec, +} + +impl LoadingStateConfig { + pub fn new(state: S) -> Self { + Self { + state, + start_loading: vec![], + check_loading: vec![], + } + } + + pub fn with_collection(mut self) -> Self { + self.start_loading + .push(start_loading_collection::.into_configs()); + self.check_loading.push( + check_loading_collection:: + .in_set(InternalLoadingStateSet::CheckAssets) + .into_configs(), + ); + + self + } + + fn build(self, app: &mut App) { + for config in self.start_loading { + app.add_systems( + OnEnterInternalLoadingState( + self.state.clone(), + InternalLoadingState::LoadingAssets, + ), + config, + ); + } + for config in self.check_loading { + app.add_systems(LoadingStateSchedule(self.state.clone()), config); + } + } } impl LoadingStateAppExt for App { @@ -727,6 +776,15 @@ impl LoadingStateAppExt for App { init_resource::, ) } + + fn configure_loading_state( + &mut self, + configuration: LoadingStateConfig, + ) -> &mut Self { + configuration.build(self); + + self + } } struct InternalAssetLoaderPlugin {