#![deny(unsafe_code)]
use bevy::{prelude::*, sprite::TextureAtlas, utils::HashMap};
use derive_more::IsVariant;
use std::marker::PhantomData;
pub use self::definitions::*;
pub use self::systems::*;
mod definitions;
mod systems;
pub struct AtlasTexturePlugin<T>(PhantomData<T>);
impl<T> Plugin for AtlasTexturePlugin<T>
where
T: Send + Sync + Eq + std::str::FromStr + std::hash::Hash + strum::VariantNames + 'static,
<T as std::str::FromStr>::Err: std::fmt::Debug,
{
fn build(&self, app: &mut App) {
app.add_system(process_atlas_definitions::<T>)
.add_asset::<GenericAtlasDefinitions>()
.add_event::<AtlasTexturesEvent<T>>();
}
}
impl<T> Default for AtlasTexturePlugin<T> {
fn default() -> Self {
Self(Default::default())
}
}
#[derive(Debug)]
pub struct AtlasTextures<T>(HashMap<T, CreatedAtlas>)
where
T: Eq + std::hash::Hash;
#[derive(Debug, Default, Clone)]
struct CreatedAtlas {
handle: Handle<TextureAtlas>,
len: usize,
}
#[derive(Debug, Clone, Copy)]
pub struct AtlasTexturesEvent<T>(ResourceStatus, PhantomData<T>);
impl<T> AtlasTexturesEvent<T> {
pub fn status(&self) -> ResourceStatus {
self.0
}
}
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, IsVariant)]
pub enum ResourceStatus {
Created,
Failed,
}
impl<T> AtlasTextures<T>
where
T: Eq + std::hash::Hash,
{
pub fn handle<B: std::borrow::Borrow<T>>(&self, index: B) -> Handle<TextureAtlas> {
self.0[index.borrow()].handle.clone_weak()
}
#[allow(clippy::len_without_is_empty)]
pub fn len<B: std::borrow::Borrow<T>>(&self, index: B) -> usize {
self.0[index.borrow()].len
}
}
impl<T> AtlasTexturesEvent<T> {
pub fn state(&self) -> ResourceStatus {
self.0
}
}