Skip to content

Commit

Permalink
add events on texture create/dispose, and event debug traits
Browse files Browse the repository at this point in the history
  • Loading branch information
jabuwu committed Oct 26, 2022
1 parent dff4925 commit 1065a3f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 13 deletions.
29 changes: 20 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ use crate::{
rusty_spine::{
draw::CullDirection, AnimationStateData, BoneHandle, EventType, SkeletonControllerSettings,
},
textures::SpineTextures,
textures::{SpineTexture, SpineTextures},
};

pub use assets::*;
pub use crossfades::Crossfades;
pub use entity_sync::*;
pub use rusty_spine::SkeletonController;
pub use textures::SpineTexture;
pub use crate::{
assets::*,
crossfades::Crossfades,
entity_sync::*,
rusty_spine::SkeletonController,
textures::{SpineTextureCreateEvent, SpineTextureDisposeEvent},
};

pub use rusty_spine;

Expand Down Expand Up @@ -69,6 +71,8 @@ impl Plugin for SpinePlugin {
.add_plugin(Material2dPlugin::<SpineScreenPmaMaterial>::default())
.add_plugin(SpineSyncPlugin::default())
.insert_resource(SpineTextures::init())
.add_event::<SpineTextureCreateEvent>()
.add_event::<SpineTextureDisposeEvent>()
.add_asset::<Atlas>()
.add_asset::<SkeletonJson>()
.add_asset::<SkeletonBinary>()
Expand Down Expand Up @@ -157,13 +161,13 @@ pub struct SpineBundle {
pub computed_visibility: ComputedVisibility,
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct SpineReadyEvent {
pub entity: Entity,
pub bones: HashMap<String, Entity>,
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub enum SpineEvent {
Start { entity: Entity, animation: String },
Interrupt { entity: Entity, animation: String },
Expand Down Expand Up @@ -193,6 +197,8 @@ fn spine_load(
mut local: Local<SpineLoadLocal>,
mut skeleton_data_assets: ResMut<Assets<SkeletonData>>,
mut images: ResMut<Assets<Image>>,
mut texture_create_events: EventWriter<SpineTextureCreateEvent>,
mut texture_dispose_events: EventWriter<SpineTextureDisposeEvent>,
atlases: Res<Assets<Atlas>>,
jsons: Res<Assets<SkeletonJson>>,
binaries: Res<Assets<SkeletonBinary>>,
Expand Down Expand Up @@ -347,7 +353,12 @@ fn spine_load(
}
}

spine_textures.update(asset_server.as_ref(), images.as_mut());
spine_textures.update(
asset_server.as_ref(),
images.as_mut(),
&mut texture_create_events,
&mut texture_dispose_events,
);
}

fn spawn_bones(
Expand Down
34 changes: 30 additions & 4 deletions src/textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,26 @@ use bevy::{
};

#[derive(Debug)]
pub struct SpineTexture(pub String);
pub(crate) struct SpineTexture(pub String);

pub(crate) struct SpineTextures {
data: Arc<Mutex<SpineTexturesData>>,
}

#[derive(Debug, Clone)]
pub struct SpineTextureCreateEvent {
pub path: String,
pub handle: Handle<Image>,
}

#[derive(Debug, Clone)]
pub struct SpineTextureDisposeEvent {
pub path: String,
pub handle: Handle<Image>,
}

#[derive(Default)]
pub struct SpineTexturesData {
pub(crate) struct SpineTexturesData {
handles: Vec<(String, Handle<Image>)>,
initialize: Vec<Handle<Image>>,
remember: Vec<String>,
Expand Down Expand Up @@ -47,12 +59,22 @@ impl SpineTextures {
Self { data }
}

pub fn update(&self, asset_server: &AssetServer, images: &mut Assets<Image>) {
pub fn update(
&self,
asset_server: &AssetServer,
images: &mut Assets<Image>,
create_events: &mut EventWriter<SpineTextureCreateEvent>,
dispose_events: &mut EventWriter<SpineTextureDisposeEvent>,
) {
let mut data = self.data.lock().unwrap();
while let Some(image) = data.remember.pop() {
let handle = asset_server.load(&image);
data.handles.push((image.clone(), handle.clone()));
data.initialize.push(handle);
data.initialize.push(handle.clone());
create_events.send(SpineTextureCreateEvent {
path: image,
handle,
});
}
while let Some(image) = data.forget.pop() {
if let Some(index) = data.handles.iter().position(|i| i.0 == image) {
Expand All @@ -63,6 +85,10 @@ impl SpineTextures {
if let Some(initialize_position) = initialize_position {
data.initialize.remove(initialize_position);
}
dispose_events.send(SpineTextureDisposeEvent {
path: image,
handle: data.handles[index].1.clone(),
});
data.handles.remove(index);
}
}
Expand Down

0 comments on commit 1065a3f

Please sign in to comment.