|
|
|
|
@ -1,7 +1,8 @@
|
|
|
|
|
use std::str::Utf8Error;
|
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
|
|
use bevy::{
|
|
|
|
|
asset::{AssetLoader, LoadContext, LoadedAsset},
|
|
|
|
|
asset::{io::Reader, AssetLoader, LoadContext},
|
|
|
|
|
reflect::{TypePath, TypeUuid},
|
|
|
|
|
utils::BoxedFuture,
|
|
|
|
|
};
|
|
|
|
|
@ -12,8 +13,8 @@ pub(crate) struct CreditsPlugin;
|
|
|
|
|
|
|
|
|
|
impl Plugin for CreditsPlugin {
|
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
|
app.add_asset::<CreditsText>()
|
|
|
|
|
.init_asset_loader::<CreditsTextLoader>()
|
|
|
|
|
app.register_asset_loader(CreditsTextLoader)
|
|
|
|
|
.init_asset::<CreditsText>()
|
|
|
|
|
.add_systems(Startup, init_credits_ui)
|
|
|
|
|
.add_systems(
|
|
|
|
|
Update,
|
|
|
|
|
@ -27,25 +28,36 @@ impl Plugin for CreditsPlugin {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, TypeUuid, TypePath)]
|
|
|
|
|
#[derive(Debug, TypeUuid, TypePath, Asset)]
|
|
|
|
|
#[uuid = "43df4a09-b5f0-4619-9223-8cf67dc9e844"]
|
|
|
|
|
pub struct CreditsText {
|
|
|
|
|
sections: Vec<TextSection>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
|
enum CreditsError {
|
|
|
|
|
IO(#[from] std::io::Error),
|
|
|
|
|
Parse(#[from] Utf8Error),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
|
struct CreditsTextLoader;
|
|
|
|
|
|
|
|
|
|
impl AssetLoader for CreditsTextLoader {
|
|
|
|
|
type Asset = CreditsText;
|
|
|
|
|
type Settings = ();
|
|
|
|
|
type Error = CreditsError;
|
|
|
|
|
|
|
|
|
|
fn load<'a>(
|
|
|
|
|
&'a self,
|
|
|
|
|
bytes: &'a [u8],
|
|
|
|
|
reader: &'a mut Reader,
|
|
|
|
|
settings: &'a Self::Settings,
|
|
|
|
|
load_context: &'a mut LoadContext,
|
|
|
|
|
) -> BoxedFuture<'a, Result<(), bevy::asset::Error>> {
|
|
|
|
|
) -> BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
|
|
|
|
|
Box::pin(async move {
|
|
|
|
|
let custom_asset = parse_credits(bytes)?;
|
|
|
|
|
load_context.set_default_asset(LoadedAsset::new(custom_asset));
|
|
|
|
|
Ok(())
|
|
|
|
|
let mut bytes = Vec::new();
|
|
|
|
|
reader.read_to_end(&mut bytes).await?;
|
|
|
|
|
parse_credits(bytes.as_slice())
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -112,10 +124,12 @@ fn update_credits(
|
|
|
|
|
mut query: Query<(&mut Text, &Handle<CreditsText>)>,
|
|
|
|
|
) {
|
|
|
|
|
reader.iter().for_each(|event| match event {
|
|
|
|
|
AssetEvent::Created { handle } | AssetEvent::Modified { handle } => {
|
|
|
|
|
AssetEvent::Added { id }
|
|
|
|
|
| AssetEvent::LoadedWithDependencies { id }
|
|
|
|
|
| AssetEvent::Modified { id } => {
|
|
|
|
|
query
|
|
|
|
|
.iter_mut()
|
|
|
|
|
.filter(|(_, this_handle)| handle.clone() == (*this_handle).clone())
|
|
|
|
|
.filter(|(_, this_handle)| id == (*this_handle).id())
|
|
|
|
|
.for_each(|(mut text, this_handle)| {
|
|
|
|
|
if let Some(credits_text) = credits_texts.get(this_handle) {
|
|
|
|
|
text.sections = credits_text.sections.clone();
|
|
|
|
|
|