diff --git a/crates/bevy_sprite/src/bundle.rs b/crates/bevy_sprite/src/bundle.rs index 47dc596165cafb..5c85db657170a0 100644 --- a/crates/bevy_sprite/src/bundle.rs +++ b/crates/bevy_sprite/src/bundle.rs @@ -1,4 +1,4 @@ -use crate::{Sprite, SpriteSheet}; +use crate::{Sprite, TextureSheet}; use bevy_asset::Handle; use bevy_ecs::bundle::Bundle; use bevy_render::{ @@ -40,7 +40,7 @@ pub struct SpriteSheetBundle { /// The sprite sheet base texture pub texture: Handle, /// The sprite sheet texture atlas and the section to draw - pub sheet: SpriteSheet, + pub sheet: TextureSheet, /// Data pertaining to how the sprite is drawn on the screen pub transform: Transform, pub global_transform: GlobalTransform, diff --git a/crates/bevy_sprite/src/lib.rs b/crates/bevy_sprite/src/lib.rs index eb19c033fc85c2..348a5657d4050f 100644 --- a/crates/bevy_sprite/src/lib.rs +++ b/crates/bevy_sprite/src/lib.rs @@ -8,13 +8,15 @@ mod texture_atlas; mod texture_atlas_builder; pub mod collide_aabb; +mod texture_sheet; pub mod prelude { #[doc(hidden)] pub use crate::{ bundle::{SpriteBundle, SpriteSheetBundle}, - sprite::{Sprite, SpriteSheet}, + sprite::Sprite, texture_atlas::TextureAtlas, + texture_sheet::TextureSheet, ColorMaterial, ColorMesh2dBundle, TextureAtlasBuilder, }; } @@ -27,6 +29,7 @@ pub use render::*; pub use sprite::*; pub use texture_atlas::*; pub use texture_atlas_builder::*; +pub use texture_sheet::*; use bevy_app::prelude::*; use bevy_asset::{AddAsset, Assets, HandleUntyped}; @@ -57,6 +60,7 @@ impl Plugin for SpritePlugin { shaders.set_untracked(SPRITE_SHADER_HANDLE, sprite_shader); app.add_asset::() .register_type::() + .register_type::() .register_type::() .add_plugin(Mesh2dRenderPlugin) .add_plugin(ColorMaterialPlugin); diff --git a/crates/bevy_sprite/src/render/mod.rs b/crates/bevy_sprite/src/render/mod.rs index 6706f7f4517f96..7b0ca7e4719f3e 100644 --- a/crates/bevy_sprite/src/render/mod.rs +++ b/crates/bevy_sprite/src/render/mod.rs @@ -1,6 +1,6 @@ use std::cmp::Ordering; -use crate::{texture_atlas::TextureAtlas, Rect, Sprite, SpriteSheet, SPRITE_SHADER_HANDLE}; +use crate::{texture_atlas::TextureAtlas, Rect, Sprite, TextureSheet, SPRITE_SHADER_HANDLE}; use bevy_asset::{AssetEvent, Assets, Handle, HandleId}; use bevy_core_pipeline::core_2d::Transparent2d; use bevy_ecs::{ @@ -226,7 +226,7 @@ pub fn extract_sprites( &Sprite, &GlobalTransform, &Handle, - Option<&SpriteSheet>, + Option<&TextureSheet>, )>, ) { let mut extracted_sprites = render_world.resource_mut::(); diff --git a/crates/bevy_sprite/src/sprite.rs b/crates/bevy_sprite/src/sprite.rs index 9344d0de62e6e2..e18c9f4ab4d3ad 100644 --- a/crates/bevy_sprite/src/sprite.rs +++ b/crates/bevy_sprite/src/sprite.rs @@ -1,5 +1,3 @@ -use crate::{Rect, TextureAtlas}; -use bevy_asset::{Assets, Handle}; use bevy_ecs::component::Component; use bevy_math::Vec2; use bevy_reflect::Reflect; @@ -21,41 +19,6 @@ pub struct Sprite { pub anchor: Anchor, } -/// Component for sprite sheets containing a handle to [`TextureAtlas`] and the index of the current -/// section of the sheet. -/// -/// A texture atlas contains various *sections* or *cuts* of a given texture, allowing users to have a single -/// image file for sprite animation or various elements. -/// You can change the [`index`](Self::index) of the sheet to animate the sprite or to pick a *section* of the texture. -/// -/// Check the following examples for usage: -/// - [`animated sprite sheet example`](https://github.com/bevyengine/bevy/blob/main/examples/2d/sprite_sheet.rs) -/// - [`texture atlas example`](https://github.com/bevyengine/bevy/blob/main/examples/2d/texture_atlas.rs) -#[derive(Component, Default, Debug, Clone, Reflect)] -pub struct SpriteSheet { - /// Texture atlas handle - pub texture_atlas: Handle, - /// Texture atlas section index - pub index: usize, -} - -impl From> for SpriteSheet { - fn from(texture_atlas: Handle) -> Self { - Self { - texture_atlas, - index: 0, - } - } -} - -impl SpriteSheet { - /// Retrieves the current texture [`Rect`] of the sprite sheet according to the section `index` - pub fn texture_rect(&self, texture_atlases: &Assets) -> Option { - let atlas = texture_atlases.get(&self.texture_atlas)?; - atlas.textures.get(self.index).copied() - } -} - /// How a sprite is positioned relative to its [`Transform`](bevy_transform::components::Transform). /// It defaults to `Anchor::Center`. #[derive(Debug, Clone, Reflect)] diff --git a/crates/bevy_sprite/src/texture_atlas_builder.rs b/crates/bevy_sprite/src/texture_atlas_builder.rs index b555d0aa4f8415..2cacfd0084af9c 100644 --- a/crates/bevy_sprite/src/texture_atlas_builder.rs +++ b/crates/bevy_sprite/src/texture_atlas_builder.rs @@ -162,9 +162,9 @@ impl TextureAtlasBuilder { /// // Spawn your sprite /// commands.spawn_bundle(SpriteSheetBundle { /// texture, - /// sheet: SpriteSheet { + /// sheet: TextureSheet { /// texture_atlas, - /// index: 0 + /// texture_index: 0 /// }, /// ..Default::default() /// }); diff --git a/crates/bevy_sprite/src/texture_sheet.rs b/crates/bevy_sprite/src/texture_sheet.rs new file mode 100644 index 00000000000000..d04f1195c09f7c --- /dev/null +++ b/crates/bevy_sprite/src/texture_sheet.rs @@ -0,0 +1,39 @@ +use crate::{Rect, TextureAtlas}; +use bevy_asset::{Assets, Handle}; +use bevy_ecs::component::Component; +use bevy_reflect::Reflect; + +/// Component used to draw a specific section of a texture. +/// +/// It stores a handle to [`TextureAtlas`] and the index of the current section of the atlas. +/// The texture atlas contains various *sections* or *cuts* of a given texture, allowing users to have a single +/// image file for either sprite animation or global mapping. +/// You can change the [`texture_index`](Self::texture_index) of the sheet to animate the sprite or to pick a *section* of the texture. +/// +/// Check the following examples for usage: +/// - [`animated sprite sheet example`](https://github.com/bevyengine/bevy/blob/main/examples/2d/sprite_sheet.rs) +/// - [`texture atlas example`](https://github.com/bevyengine/bevy/blob/main/examples/2d/texture_atlas.rs) +#[derive(Component, Default, Debug, Clone, Reflect)] +pub struct TextureSheet { + /// Texture atlas handle + pub texture_atlas: Handle, + /// Texture atlas section index + pub texture_index: usize, +} + +impl From> for TextureSheet { + fn from(texture_atlas: Handle) -> Self { + Self { + texture_atlas, + texture_index: 0, + } + } +} + +impl TextureSheet { + /// Retrieves the current texture [`Rect`] of the sprite sheet according to the section `index` + pub fn texture_rect(&self, texture_atlases: &Assets) -> Option { + let atlas = texture_atlases.get(&self.texture_atlas)?; + atlas.textures.get(self.texture_index).copied() + } +} diff --git a/examples/2d/sprite_sheet.rs b/examples/2d/sprite_sheet.rs index f1e7a5055f061e..99e619e9f1489f 100644 --- a/examples/2d/sprite_sheet.rs +++ b/examples/2d/sprite_sheet.rs @@ -18,13 +18,13 @@ struct AnimationTimer(Timer); fn animate_sprite( time: Res