From 29e17c76c9261f593e36756ad2338f0ae699a4f6 Mon Sep 17 00:00:00 2001 From: xc Date: Tue, 3 Dec 2024 01:33:13 +0800 Subject: [PATCH 1/2] add index_range for TextureAtlas --- crates/bevy_sprite/src/texture_atlas.rs | 12 ++++++++++++ examples/2d/sprite_animation.rs | 6 ++++-- examples/2d/sprite_sheet.rs | 23 ++++++----------------- examples/picking/sprite_picking.rs | 23 ++++++----------------- examples/ui/ui_texture_atlas_slice.rs | 3 ++- 5 files changed, 30 insertions(+), 37 deletions(-) diff --git a/crates/bevy_sprite/src/texture_atlas.rs b/crates/bevy_sprite/src/texture_atlas.rs index 797fb4aa206ff..1b3714680a554 100644 --- a/crates/bevy_sprite/src/texture_atlas.rs +++ b/crates/bevy_sprite/src/texture_atlas.rs @@ -32,6 +32,7 @@ impl TextureAtlasSources { Some(TextureAtlas { layout, index: self.texture_index(texture)?, + index_range: (0, 0), }) } @@ -170,6 +171,8 @@ pub struct TextureAtlas { pub layout: Handle, /// Texture atlas section index pub index: usize, + /// Texture atlas default index range (first, last) + pub index_range: (usize, usize), } impl TextureAtlas { @@ -178,6 +181,14 @@ impl TextureAtlas { let atlas = texture_atlases.get(&self.layout)?; atlas.textures.get(self.index).copied() } + + pub fn advance_index(&mut self) -> &mut Self { + self.index += 1; + if self.index > self.index_range.1 { + self.index = self.index_range.0; + } + self + } } impl From> for TextureAtlas { @@ -185,6 +196,7 @@ impl From> for TextureAtlas { Self { layout: texture_atlas, index: 0, + index_range: (0, 0), } } } diff --git a/examples/2d/sprite_animation.rs b/examples/2d/sprite_animation.rs index 712865af56cc2..384ccbf3fdab9 100644 --- a/examples/2d/sprite_animation.rs +++ b/examples/2d/sprite_animation.rs @@ -64,10 +64,10 @@ fn execute_animations(time: Res