Skip to content

Commit

Permalink
Renamed SpriteSheet to TextureSheet
Browse files Browse the repository at this point in the history
  • Loading branch information
ManevilleF committed Jun 28, 2022
1 parent 72d561e commit a5f0a11
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 50 deletions.
4 changes: 2 additions & 2 deletions crates/bevy_sprite/src/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Sprite, SpriteSheet};
use crate::{Sprite, TextureSheet};
use bevy_asset::Handle;
use bevy_ecs::bundle::Bundle;
use bevy_render::{
Expand Down Expand Up @@ -40,7 +40,7 @@ pub struct SpriteSheetBundle {
/// The sprite sheet base texture
pub texture: Handle<Image>,
/// 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,
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_sprite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Expand All @@ -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};
Expand Down Expand Up @@ -57,6 +60,7 @@ impl Plugin for SpritePlugin {
shaders.set_untracked(SPRITE_SHADER_HANDLE, sprite_shader);
app.add_asset::<TextureAtlas>()
.register_type::<Sprite>()
.register_type::<TextureSheet>()
.register_type::<Mesh2dHandle>()
.add_plugin(Mesh2dRenderPlugin)
.add_plugin(ColorMaterialPlugin);
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -226,7 +226,7 @@ pub fn extract_sprites(
&Sprite,
&GlobalTransform,
&Handle<Image>,
Option<&SpriteSheet>,
Option<&TextureSheet>,
)>,
) {
let mut extracted_sprites = render_world.resource_mut::<ExtractedSprites>();
Expand Down
37 changes: 0 additions & 37 deletions crates/bevy_sprite/src/sprite.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<TextureAtlas>,
/// Texture atlas section index
pub index: usize,
}

impl From<Handle<TextureAtlas>> for SpriteSheet {
fn from(texture_atlas: Handle<TextureAtlas>) -> 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<TextureAtlas>) -> Option<Rect> {
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)]
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_sprite/src/texture_atlas_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
/// });
Expand Down
39 changes: 39 additions & 0 deletions crates/bevy_sprite/src/texture_sheet.rs
Original file line number Diff line number Diff line change
@@ -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<TextureAtlas>,
/// Texture atlas section index
pub texture_index: usize,
}

impl From<Handle<TextureAtlas>> for TextureSheet {
fn from(texture_atlas: Handle<TextureAtlas>) -> 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<TextureAtlas>) -> Option<Rect> {
let atlas = texture_atlases.get(&self.texture_atlas)?;
atlas.textures.get(self.texture_index).copied()
}
}
8 changes: 4 additions & 4 deletions examples/2d/sprite_sheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ struct AnimationTimer(Timer);
fn animate_sprite(
time: Res<Time>,
texture_atlases: Res<Assets<TextureAtlas>>,
mut query: Query<(&mut AnimationTimer, &mut SpriteSheet)>,
mut query: Query<(&mut AnimationTimer, &mut TextureSheet)>,
) {
for (mut timer, mut sprite) in query.iter_mut() {
timer.tick(time.delta());
if timer.just_finished() {
let texture_atlas = texture_atlases.get(&sprite.texture_atlas).unwrap();
sprite.index = (sprite.index + 1) % texture_atlas.textures.len();
sprite.texture_index = (sprite.texture_index + 1) % texture_atlas.textures.len();
}
}
}
Expand All @@ -41,9 +41,9 @@ fn setup(
commands
.spawn_bundle(SpriteSheetBundle {
texture,
sheet: SpriteSheet {
sheet: TextureSheet {
texture_atlas,
index: 0,
texture_index: 0,
},
transform: Transform::from_scale(Vec3::splat(6.0)),
..default()
Expand Down
4 changes: 2 additions & 2 deletions examples/2d/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ fn setup(
scale: Vec3::splat(4.0),
..default()
},
sheet: SpriteSheet {
index: vendor_index,
sheet: TextureSheet {
texture_index: vendor_index,
texture_atlas: atlas_handle,
},
..default()
Expand Down

0 comments on commit a5f0a11

Please sign in to comment.