From 159f9037d45ece3037bd006ed4cc60f9e4490f49 Mon Sep 17 00:00:00 2001 From: mgi388 <135186256+mgi388@users.noreply.github.com> Date: Fri, 20 Dec 2024 16:53:20 +1100 Subject: [PATCH] Add missing support for ImageNode --- Cargo.toml | 3 ++- README.md | 2 +- src/animator.rs | 21 +++++++++++++++++++-- src/systems/spritesheet_animation.rs | 2 ++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 60bc16d..a48fdde 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,10 +15,11 @@ exclude = ["assets/example.gif", "assets/example3d.gif"] bevy = { version = "0.15.0", default-features = false, features = [ "bevy_pbr", "bevy_sprite", + "bevy_ui", ] } # Temporary dep until the bevy_image export is fixed # https://github.com/bevyengine/bevy/issues/16563 -bevy_internal = { version = "0.15", features = [ "bevy_image" ]} +bevy_internal = { version = "0.15", features = ["bevy_image"] } [dev-dependencies] approx = "0.5.1" diff --git a/README.md b/README.md index 0ad4a4e..58e8d08 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ bevy_spritesheet_animation is a [Bevy](https://bevyengine.org/) plugin for easil # Features -- Animate 2D and [3D sprites](#3d-sprites)! 🎉 +- Animate 2D sprites, [3D sprites](#3d-sprites) and UI images! 🎉 - A single Bevy [component](https://docs.rs/bevy_spritesheet_animation/latest/bevy_spritesheet_animation/components/spritesheet_animation/struct.SpritesheetAnimation.html) to add to your entities to play animations. - Tunable parameters: [duration](https://docs.rs/bevy_spritesheet_animation/latest/bevy_spritesheet_animation/animation/enum.AnimationDuration.html), [repetitions](https://docs.rs/bevy_spritesheet_animation/latest/bevy_spritesheet_animation/animation/enum.AnimationRepeat.html), [direction](https://docs.rs/bevy_spritesheet_animation/latest/bevy_spritesheet_animation/animation/enum.AnimationDirection.html), [easing](https://docs.rs/bevy_spritesheet_animation/latest/bevy_spritesheet_animation/easing/enum.Easing.html). - [Composable animations](https://docs.rs/bevy_spritesheet_animation/latest/bevy_spritesheet_animation/animation/struct.Animation.html) from multiple clips. diff --git a/src/animator.rs b/src/animator.rs index 12bf824..c51d16b 100644 --- a/src/animator.rs +++ b/src/animator.rs @@ -21,6 +21,7 @@ use bevy::{ reflect::prelude::*, sprite::Sprite, time::Time, + ui::widget::ImageNode, }; use iterator::AnimationIteratorEvent; use std::{collections::HashMap, time::Duration}; @@ -60,6 +61,7 @@ impl Animator { &mut SpritesheetAnimation, Option<&mut Sprite>, Option<&mut Sprite3d>, + Option<&mut ImageNode>, )>, ) { // Clear outdated animation instances associated to entities that do not have the component anymore @@ -69,8 +71,13 @@ impl Animator { // Run animations for all the entities - for (entity, mut entity_animation, mut maybe_entity_sprite, mut maybe_entity_3dsprite) in - query.iter_mut() + for ( + entity, + mut entity_animation, + mut maybe_entity_sprite, + mut maybe_entity_3dsprite, + mut maybe_entity_image_node, + ) in query.iter_mut() { // Create a new animation instance if: let needs_new_animation_instance = match self.animation_instances.get(&entity) { @@ -104,6 +111,7 @@ impl Animator { &entity, maybe_entity_sprite.as_deref_mut(), maybe_entity_3dsprite.as_deref_mut(), + maybe_entity_image_node.as_deref_mut(), event_writer, ); @@ -135,6 +143,7 @@ impl Animator { &entity, maybe_entity_sprite.as_deref_mut(), maybe_entity_3dsprite.as_deref_mut(), + maybe_entity_image_node.as_deref_mut(), event_writer, ) .inspect(|new_frame| { @@ -181,6 +190,7 @@ impl Animator { &entity, maybe_entity_sprite.as_deref_mut(), maybe_entity_3dsprite.as_deref_mut(), + maybe_entity_image_node.as_deref_mut(), event_writer, ) .or_else(|| { @@ -224,6 +234,7 @@ impl Animator { entity: &Entity, maybe_sprite: Option<&mut Sprite>, maybe_3dsprite: Option<&mut Sprite3d>, + maybe_image_node: Option<&mut ImageNode>, event_writer: &mut EventWriter, ) -> Option<(IteratorFrame, AnimationProgress)> { let maybe_frame = iterator.next(); @@ -244,6 +255,12 @@ impl Animator { } } + if let Some(atlas) = maybe_image_node.and_then(|image| image.texture_atlas.as_mut()) { + if atlas.index != frame.atlas_index { + atlas.index = frame.atlas_index; + } + } + animation.progress = *progress; // Emit events diff --git a/src/systems/spritesheet_animation.rs b/src/systems/spritesheet_animation.rs index c0c6fef..c3da841 100644 --- a/src/systems/spritesheet_animation.rs +++ b/src/systems/spritesheet_animation.rs @@ -6,6 +6,7 @@ use bevy::{ }, sprite::Sprite, time::Time, + ui::widget::ImageNode, }; use crate::{ @@ -25,6 +26,7 @@ pub fn play_animations( &mut SpritesheetAnimation, Option<&mut Sprite>, Option<&mut Sprite3d>, + Option<&mut ImageNode>, )>, ) { animator.update(&time, &library, &mut event_writer, &mut query);