Skip to content

Commit a5f0a11

Browse files
committed
Renamed SpriteSheet to TextureSheet
1 parent 72d561e commit a5f0a11

File tree

8 files changed

+56
-50
lines changed

8 files changed

+56
-50
lines changed

crates/bevy_sprite/src/bundle.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Sprite, SpriteSheet};
1+
use crate::{Sprite, TextureSheet};
22
use bevy_asset::Handle;
33
use bevy_ecs::bundle::Bundle;
44
use bevy_render::{
@@ -40,7 +40,7 @@ pub struct SpriteSheetBundle {
4040
/// The sprite sheet base texture
4141
pub texture: Handle<Image>,
4242
/// The sprite sheet texture atlas and the section to draw
43-
pub sheet: SpriteSheet,
43+
pub sheet: TextureSheet,
4444
/// Data pertaining to how the sprite is drawn on the screen
4545
pub transform: Transform,
4646
pub global_transform: GlobalTransform,

crates/bevy_sprite/src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ mod texture_atlas;
88
mod texture_atlas_builder;
99

1010
pub mod collide_aabb;
11+
mod texture_sheet;
1112

1213
pub mod prelude {
1314
#[doc(hidden)]
1415
pub use crate::{
1516
bundle::{SpriteBundle, SpriteSheetBundle},
16-
sprite::{Sprite, SpriteSheet},
17+
sprite::Sprite,
1718
texture_atlas::TextureAtlas,
19+
texture_sheet::TextureSheet,
1820
ColorMaterial, ColorMesh2dBundle, TextureAtlasBuilder,
1921
};
2022
}
@@ -27,6 +29,7 @@ pub use render::*;
2729
pub use sprite::*;
2830
pub use texture_atlas::*;
2931
pub use texture_atlas_builder::*;
32+
pub use texture_sheet::*;
3033

3134
use bevy_app::prelude::*;
3235
use bevy_asset::{AddAsset, Assets, HandleUntyped};
@@ -57,6 +60,7 @@ impl Plugin for SpritePlugin {
5760
shaders.set_untracked(SPRITE_SHADER_HANDLE, sprite_shader);
5861
app.add_asset::<TextureAtlas>()
5962
.register_type::<Sprite>()
63+
.register_type::<TextureSheet>()
6064
.register_type::<Mesh2dHandle>()
6165
.add_plugin(Mesh2dRenderPlugin)
6266
.add_plugin(ColorMaterialPlugin);

crates/bevy_sprite/src/render/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::cmp::Ordering;
22

3-
use crate::{texture_atlas::TextureAtlas, Rect, Sprite, SpriteSheet, SPRITE_SHADER_HANDLE};
3+
use crate::{texture_atlas::TextureAtlas, Rect, Sprite, TextureSheet, SPRITE_SHADER_HANDLE};
44
use bevy_asset::{AssetEvent, Assets, Handle, HandleId};
55
use bevy_core_pipeline::core_2d::Transparent2d;
66
use bevy_ecs::{
@@ -226,7 +226,7 @@ pub fn extract_sprites(
226226
&Sprite,
227227
&GlobalTransform,
228228
&Handle<Image>,
229-
Option<&SpriteSheet>,
229+
Option<&TextureSheet>,
230230
)>,
231231
) {
232232
let mut extracted_sprites = render_world.resource_mut::<ExtractedSprites>();

crates/bevy_sprite/src/sprite.rs

-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use crate::{Rect, TextureAtlas};
2-
use bevy_asset::{Assets, Handle};
31
use bevy_ecs::component::Component;
42
use bevy_math::Vec2;
53
use bevy_reflect::Reflect;
@@ -21,41 +19,6 @@ pub struct Sprite {
2119
pub anchor: Anchor,
2220
}
2321

24-
/// Component for sprite sheets containing a handle to [`TextureAtlas`] and the index of the current
25-
/// section of the sheet.
26-
///
27-
/// A texture atlas contains various *sections* or *cuts* of a given texture, allowing users to have a single
28-
/// image file for sprite animation or various elements.
29-
/// You can change the [`index`](Self::index) of the sheet to animate the sprite or to pick a *section* of the texture.
30-
///
31-
/// Check the following examples for usage:
32-
/// - [`animated sprite sheet example`](https://github.com/bevyengine/bevy/blob/main/examples/2d/sprite_sheet.rs)
33-
/// - [`texture atlas example`](https://github.com/bevyengine/bevy/blob/main/examples/2d/texture_atlas.rs)
34-
#[derive(Component, Default, Debug, Clone, Reflect)]
35-
pub struct SpriteSheet {
36-
/// Texture atlas handle
37-
pub texture_atlas: Handle<TextureAtlas>,
38-
/// Texture atlas section index
39-
pub index: usize,
40-
}
41-
42-
impl From<Handle<TextureAtlas>> for SpriteSheet {
43-
fn from(texture_atlas: Handle<TextureAtlas>) -> Self {
44-
Self {
45-
texture_atlas,
46-
index: 0,
47-
}
48-
}
49-
}
50-
51-
impl SpriteSheet {
52-
/// Retrieves the current texture [`Rect`] of the sprite sheet according to the section `index`
53-
pub fn texture_rect(&self, texture_atlases: &Assets<TextureAtlas>) -> Option<Rect> {
54-
let atlas = texture_atlases.get(&self.texture_atlas)?;
55-
atlas.textures.get(self.index).copied()
56-
}
57-
}
58-
5922
/// How a sprite is positioned relative to its [`Transform`](bevy_transform::components::Transform).
6023
/// It defaults to `Anchor::Center`.
6124
#[derive(Debug, Clone, Reflect)]

crates/bevy_sprite/src/texture_atlas_builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ impl TextureAtlasBuilder {
162162
/// // Spawn your sprite
163163
/// commands.spawn_bundle(SpriteSheetBundle {
164164
/// texture,
165-
/// sheet: SpriteSheet {
165+
/// sheet: TextureSheet {
166166
/// texture_atlas,
167-
/// index: 0
167+
/// texture_index: 0
168168
/// },
169169
/// ..Default::default()
170170
/// });
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use crate::{Rect, TextureAtlas};
2+
use bevy_asset::{Assets, Handle};
3+
use bevy_ecs::component::Component;
4+
use bevy_reflect::Reflect;
5+
6+
/// Component used to draw a specific section of a texture.
7+
///
8+
/// It stores a handle to [`TextureAtlas`] and the index of the current section of the atlas.
9+
/// The texture atlas contains various *sections* or *cuts* of a given texture, allowing users to have a single
10+
/// image file for either sprite animation or global mapping.
11+
/// You can change the [`texture_index`](Self::texture_index) of the sheet to animate the sprite or to pick a *section* of the texture.
12+
///
13+
/// Check the following examples for usage:
14+
/// - [`animated sprite sheet example`](https://github.com/bevyengine/bevy/blob/main/examples/2d/sprite_sheet.rs)
15+
/// - [`texture atlas example`](https://github.com/bevyengine/bevy/blob/main/examples/2d/texture_atlas.rs)
16+
#[derive(Component, Default, Debug, Clone, Reflect)]
17+
pub struct TextureSheet {
18+
/// Texture atlas handle
19+
pub texture_atlas: Handle<TextureAtlas>,
20+
/// Texture atlas section index
21+
pub texture_index: usize,
22+
}
23+
24+
impl From<Handle<TextureAtlas>> for TextureSheet {
25+
fn from(texture_atlas: Handle<TextureAtlas>) -> Self {
26+
Self {
27+
texture_atlas,
28+
texture_index: 0,
29+
}
30+
}
31+
}
32+
33+
impl TextureSheet {
34+
/// Retrieves the current texture [`Rect`] of the sprite sheet according to the section `index`
35+
pub fn texture_rect(&self, texture_atlases: &Assets<TextureAtlas>) -> Option<Rect> {
36+
let atlas = texture_atlases.get(&self.texture_atlas)?;
37+
atlas.textures.get(self.texture_index).copied()
38+
}
39+
}

examples/2d/sprite_sheet.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ struct AnimationTimer(Timer);
1818
fn animate_sprite(
1919
time: Res<Time>,
2020
texture_atlases: Res<Assets<TextureAtlas>>,
21-
mut query: Query<(&mut AnimationTimer, &mut SpriteSheet)>,
21+
mut query: Query<(&mut AnimationTimer, &mut TextureSheet)>,
2222
) {
2323
for (mut timer, mut sprite) in query.iter_mut() {
2424
timer.tick(time.delta());
2525
if timer.just_finished() {
2626
let texture_atlas = texture_atlases.get(&sprite.texture_atlas).unwrap();
27-
sprite.index = (sprite.index + 1) % texture_atlas.textures.len();
27+
sprite.texture_index = (sprite.texture_index + 1) % texture_atlas.textures.len();
2828
}
2929
}
3030
}
@@ -41,9 +41,9 @@ fn setup(
4141
commands
4242
.spawn_bundle(SpriteSheetBundle {
4343
texture,
44-
sheet: SpriteSheet {
44+
sheet: TextureSheet {
4545
texture_atlas,
46-
index: 0,
46+
texture_index: 0,
4747
},
4848
transform: Transform::from_scale(Vec3::splat(6.0)),
4949
..default()

examples/2d/texture_atlas.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ fn setup(
7171
scale: Vec3::splat(4.0),
7272
..default()
7373
},
74-
sheet: SpriteSheet {
75-
index: vendor_index,
74+
sheet: TextureSheet {
75+
texture_index: vendor_index,
7676
texture_atlas: atlas_handle,
7777
},
7878
..default()

0 commit comments

Comments
 (0)