-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72d561e
commit a5f0a11
Showing
8 changed files
with
56 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters