-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
105 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use crate::proto::Prototypical; | ||
use bevy::asset::Handle; | ||
|
||
/// Asset lifecycle events for [prototype] assets. | ||
/// | ||
/// This is analogous to [`AssetEvent`], but accounts for prototype | ||
/// caching and registration. | ||
/// This event should be preferred over using the `AssetEvent` directly. | ||
/// | ||
/// [prototype]: Prototypical | ||
/// [`AssetEvent`]: bevy::asset::AssetEvent | ||
pub enum ProtoAssetEvent<T: Prototypical> { | ||
/// This event is fired when a prototype has been successfully created, | ||
/// registered, and cached. | ||
Created { | ||
/// The ID of the created prototype. | ||
id: T::Id, | ||
/// A weak handle to the prototype asset. | ||
handle: Handle<T>, | ||
}, | ||
/// This event is fired when a prototype has been modified, such as when | ||
/// a change is made to the file while hot-reloading is enabled. | ||
Modified { | ||
/// The ID of the modified prototype. | ||
id: T::Id, | ||
/// A weak handle to the prototype asset. | ||
handle: Handle<T>, | ||
}, | ||
/// This event is fired when a prototype has been fully unloaded. | ||
Removed { | ||
/// The ID of the removed prototype. | ||
/// | ||
/// This will almost always be `Some` unless a duplicate removal happens | ||
/// to occur at the same time, in which case it may be `None`. | ||
id: Option<T::Id>, | ||
/// A weak handle to the prototype asset. | ||
handle: Handle<T>, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,35 @@ | ||
use bevy::asset::AssetEvent; | ||
use bevy::prelude::{error, EventReader}; | ||
use bevy::prelude::{error, EventReader, EventWriter}; | ||
|
||
use crate::proto::Prototypical; | ||
use crate::proto::{ProtoAssetEvent, Prototypical}; | ||
use crate::registration::ProtoManager; | ||
|
||
/// Handles the registration of loaded, modified, and removed prototypes. | ||
pub(crate) fn on_proto_asset_event<T: Prototypical>( | ||
mut events: EventReader<AssetEvent<T>>, | ||
mut manager: ProtoManager<T>, | ||
mut proto_events: EventWriter<ProtoAssetEvent<T>>, | ||
) { | ||
for event in events.iter() { | ||
match event { | ||
AssetEvent::Created { handle } => { | ||
if let Err(err) = manager.register(handle) { | ||
error!("could not register prototype: {}", err); | ||
} | ||
} | ||
AssetEvent::Modified { handle } => { | ||
if let Err(err) = manager.register(handle) { | ||
error!("could not re-register modified prototype: {}", err); | ||
} | ||
} | ||
AssetEvent::Removed { handle } => { | ||
manager.unregister(handle); | ||
} | ||
AssetEvent::Created { handle } => match manager.register(handle) { | ||
Ok(proto) => proto_events.send(ProtoAssetEvent::Created { | ||
id: proto.id().clone(), | ||
handle: handle.clone_weak(), | ||
}), | ||
Err(err) => error!("could not register prototype: {}", err), | ||
}, | ||
AssetEvent::Modified { handle } => match manager.register(handle) { | ||
Ok(proto) => proto_events.send(ProtoAssetEvent::Modified { | ||
id: proto.id().clone(), | ||
handle: handle.clone_weak(), | ||
}), | ||
Err(err) => error!("could not re-register modified prototype: {}", err), | ||
}, | ||
AssetEvent::Removed { handle } => proto_events.send(ProtoAssetEvent::Removed { | ||
id: manager.unregister(handle), | ||
handle: handle.clone_weak(), | ||
}), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters