Skip to content

Commit

Permalink
configure_loading_state with_collection
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasEi committed Nov 23, 2023
1 parent 157e231 commit 03a4620
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
5 changes: 4 additions & 1 deletion bevy_asset_loader/examples/atlas_from_grid.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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::<MyAssets>(),
)
.add_systems(OnEnter(MyStates::Next), draw_atlas)
.add_systems(
Update,
Expand Down
58 changes: 58 additions & 0 deletions bevy_asset_loader/src/loading_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -663,6 +664,54 @@ pub trait LoadingStateAppExt {
&mut self,
loading_state: S,
) -> &mut Self;

fn configure_loading_state<S: States>(
&mut self,
configuration: LoadingStateConfig<S>,
) -> &mut Self;
}

pub struct LoadingStateConfig<S: States> {
state: S,
start_loading: Vec<SystemConfigs>,
check_loading: Vec<SystemConfigs>,
}

impl<S: States> LoadingStateConfig<S> {
pub fn new(state: S) -> Self {
Self {
state,
start_loading: vec![],
check_loading: vec![],
}
}

pub fn with_collection<A: AssetCollection>(mut self) -> Self {
self.start_loading
.push(start_loading_collection::<S, A>.into_configs());
self.check_loading.push(
check_loading_collection::<S, A>
.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 {
Expand Down Expand Up @@ -727,6 +776,15 @@ impl LoadingStateAppExt for App {
init_resource::<A>,
)
}

fn configure_loading_state<S: States>(
&mut self,
configuration: LoadingStateConfig<S>,
) -> &mut Self {
configuration.build(self);

self
}
}

struct InternalAssetLoaderPlugin<S> {
Expand Down

0 comments on commit 03a4620

Please sign in to comment.