Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,34 @@ default = [
# In practice, this feature flag is not required as we use optional arguments for our Bevy systems.
# However, the Github Action's environment does not support winit, which is a transitive dependency of bevy/custom_cursor.
# So hiding this behind a feature allows us to run tests with --no-default-features in that specific context and circumvent the issue.
"custom_cursor"
"custom_cursor",
"3d"
]
custom_cursor = ["bevy/custom_cursor"]
3d = ["bevy/bevy_pbr"]

[dependencies]
bevy = { version = "0.17.0", default-features = false, features = [
"bevy_log",
"bevy_sprite",
"bevy_pbr",
"bevy_ui"
] }

[[example]]
name = "3d"
required-features = ["3d"]

[[example]]
name = "stress"
# stress example uses both 2d and 3d depending on CLI args,
# but we need to guard the 3d code inside it.
# Actually, it's easier to just require 3d feature to run it if we don't want to overcomplicate the code.
# Or we can just guard the code.

[[example]]
name = "cursor"
required-features = ["custom_cursor"]

[dev-dependencies]
approx = "0.5.1"
bevy = { version = "0.17.0", features = [
Expand Down
29 changes: 18 additions & 11 deletions examples/stress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,24 @@ fn spawn_sprites(
let transform = Transform::from_translation(random_position(&window));

match cli.mode {
Mode::TwoD => commands.spawn((
component_generator.sprite(&mut atlas_layouts),
SpritesheetAnimation::new(animation_handle.clone()),
transform,
)),
Mode::ThreeD => commands.spawn((
component_generator.sprite3d(&mut atlas_layouts),
SpritesheetAnimation::new(animation_handle.clone()),
transform,
)),
};
Mode::TwoD => {
commands.spawn((
component_generator.sprite(&mut atlas_layouts),
SpritesheetAnimation::new(animation_handle.clone()),
transform,
));
}
Mode::ThreeD => {
#[cfg(feature = "3d")]
commands.spawn((
component_generator.sprite3d(&mut atlas_layouts),
SpritesheetAnimation::new(animation_handle.clone()),
transform,
));
#[cfg(not(feature = "3d"))]
panic!("3D mode requires the '3d' feature to be enabled");
}
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/animator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ use bevy::{
ui::widget::ImageNode,
};

#[cfg(feature = "3d")]
use crate::components::sprite3d::Sprite3d;
use crate::{
animation::Animation,
animator::{
cache::AnimationCache,
iterator::{AnimationIterator, IteratorFrame},
},
components::{
sprite3d::Sprite3d,
spritesheet_animation::{AnimationProgress, SpritesheetAnimation},
},
components::spritesheet_animation::{AnimationProgress, SpritesheetAnimation},
events::AnimationEvent,
};
use iterator::AnimationIteratorEvent;
Expand Down Expand Up @@ -73,6 +72,7 @@ pub(crate) struct SpritesheetAnimationQuery {
entity: Entity,
spritesheet_animation: &'static mut SpritesheetAnimation,
sprite: Option<&'static mut Sprite>,
#[cfg(feature = "3d")]
sprite3d: Option<&'static mut Sprite3d>,
image_node: Option<&'static mut ImageNode>,
#[cfg(feature = "custom_cursor")]
Expand Down Expand Up @@ -262,6 +262,7 @@ impl Animator {

// 3D sprites

#[cfg(feature = "3d")]
if let Some(atlas) = item
.sprite3d
.as_deref_mut()
Expand Down
1 change: 1 addition & 0 deletions src/components.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod generator;
#[cfg(feature = "3d")]
pub mod sprite3d;
pub mod spritesheet_animation;
5 changes: 4 additions & 1 deletion src/components/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use bevy::{
utils::default,
};

use crate::{components::sprite3d::Sprite3d, spritesheet::Spritesheet};
#[cfg(feature = "3d")]
use crate::components::sprite3d::Sprite3d;
use crate::spritesheet::Spritesheet;

/// A helper to generate animation-ready components such as sprites, texture atlases, UI images and cursors.
///
Expand Down Expand Up @@ -67,6 +69,7 @@ impl ComponentGenerator {
Sprite::from_atlas_image(self.spritesheet.image().clone(), self.atlas(atlas_layouts))
}

#[cfg(feature = "3d")]
/// Creates an animation-ready [Sprite3d].
///
/// # Arguments
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! # Features
//!
//! - Animate 2D sprites, [3D sprites](crate::prelude::Sprite3d), UI images and cursors! 🎉
//! - Animate 2D sprites, 3D sprites, UI images and cursors! 🎉
//! - [Easily build](crate::prelude::AnimationBuilder) animations from [spritesheets](crate::prelude::Spritesheet) with custom parameters like [duration](crate::prelude::AnimationDuration), [repetitions](crate::prelude::AnimationRepeat), [direction](crate::prelude::AnimationDirection), [easing](crate::prelude::Easing).
//! - Trigger [events](crate::prelude::AnimationEvent) when animations end or reach specific points.
//!
Expand Down Expand Up @@ -37,7 +37,6 @@ pub mod prelude {
clip::{Clip, ClipId},
components::{
generator::ComponentGenerator,
sprite3d::Sprite3d,
spritesheet_animation::{AnimationProgress, SpritesheetAnimation},
},
easing::{Easing, EasingVariety},
Expand All @@ -46,6 +45,9 @@ pub mod prelude {
plugin::SpritesheetAnimationPlugin,
spritesheet::Spritesheet,
};

#[cfg(feature = "3d")]
pub use crate::components::sprite3d::Sprite3d;
}

mod animator;
Expand Down
19 changes: 13 additions & 6 deletions src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
use bevy::prelude::*;

use crate::{
animation::Animation,
animator::Animator,
components::{sprite3d::Sprite3d, spritesheet_animation::SpritesheetAnimation},
events::AnimationEvent,
systems::{sprite3d, spritesheet_animation},
animation::Animation, animator::Animator,
components::spritesheet_animation::SpritesheetAnimation, events::AnimationEvent,
systems::spritesheet_animation,
};

#[cfg(feature = "3d")]
use crate::components::sprite3d::Sprite3d;
#[cfg(feature = "3d")]
use crate::systems::sprite3d;

/// Set for systems that update animations
#[derive(Debug, PartialEq, Eq, Clone, Hash, SystemSet)]
pub struct AnimationSystemSet;

/// Set for systems that manage 3D sprites
#[cfg(feature = "3d")]
#[derive(Debug, PartialEq, Eq, Clone, Hash, SystemSet)]
pub struct Sprite3dSystemSet;

Expand Down Expand Up @@ -68,7 +72,10 @@ impl Plugin for SpritesheetAnimationPlugin {
spritesheet_animation::play_animations.in_set(AnimationSystemSet),
)
// Animations events
.add_message::<AnimationEvent>()
.add_message::<AnimationEvent>();

#[cfg(feature = "3d")]
app
// 3D sprites
.init_resource::<sprite3d::Cache>()
.register_type::<sprite3d::Cache>()
Expand Down
1 change: 1 addition & 0 deletions src/systems.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#[cfg(feature = "3d")]
pub mod sprite3d;
pub mod spritesheet_animation;
15 changes: 12 additions & 3 deletions tests/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,18 @@ impl Context {
let atlas = entity_ref
.get::<Sprite>()
.and_then(|sprite| sprite.texture_atlas.as_ref())
.or(entity_ref
.get::<Sprite3d>()
.and_then(|sprite| sprite.texture_atlas.as_ref()))
.or_else(|| {
#[cfg(feature = "3d")]
{
entity_ref
.get::<Sprite3d>()
.and_then(|sprite| sprite.texture_atlas.as_ref())
}
#[cfg(not(feature = "3d"))]
{
None
}
})
.unwrap();

assert_eq!(atlas.index, expected_atlas_index);
Expand Down