-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add index_range for TextureAtlas #16611
base: main
Are you sure you want to change the base?
Conversation
Welcome, new contributor! Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like these changes, but I had a hard time understanding the docs at first. I've left a simple suggestion to improve that.
It looks like your PR is a breaking change, but you didn't provide a migration guide. Could you add some context on what users should update when this change get released in a new version of Bevy? |
Co-authored-by: Alice Cecile <[email protected]>
self.index += 1; | ||
if self.index > self.index_range.1 { | ||
self.index = self.index_range.0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it should just use a saturating add.
@@ -31,7 +31,7 @@ fn button_system( | |||
Interaction::Pressed => { | |||
**text = "Press".to_string(); | |||
if let Some(atlas) = &mut image.texture_atlas { | |||
atlas.index = (atlas.index + 1) % 30; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is supposed to cycle
Sort of opposed to this change. I feel like it's of way too limited utility to be given prime real estate in the Definitely want to add stuff to help with 2d animations... I just think that whatever we end up with will look nothing like what is being done in that PR. I'd maybe implement it as an optional |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me this change doesn't stand out as a net improvement.
The name advance_index
implies to me that it advances the index, not that it advances and possibly wraps back around.
It feels weird to me that you'd now have to pass index_range
to your TextureAtlas
(even if using Default
to have it receive a default value).
Some problems I have with index_range
:
index_range
does not imply that it's a range for the first and last index. Some valid ranges might just be a section of the atlas (e.g. index 0 to 7 out of an atlas with 256 frames), soindex_range
's name and functionality seems limited. Further, thenadvance_index
only works on the range 0 to 7.- An entity with a
TextureAtlas
probably has multiple valid ranges, not just one. A texture atlas can have animations in it for walking north, south, etc., running north, running south, etc. See https://docs.rs/bevy_spritesheet_animation/1.0.0/bevy_spritesheet_animation/spritesheet/struct.Spritesheet.html for an example crate which can create different "ranges" and how there is a wide variety, certainly more than just "start to end incrementing by 1". - All my existing code that constructs
TextureAtlas
would now have to passindex_range
even though it may not even be relevant for my entity. A concrete example follows. I have a UI image with aTextureAtlas
. I have an atlas of textures and my UI image comes from index 48.
commands
.spawn((
ImageBundle {
style: Style {
// ...
..Default::default()
},
image: UiImage::new(hud_sprite_sheet.texture.clone()),
..Default::default()
},
TextureAtlas {
layout: hud_sprite_sheet.texture_atlas_layout.clone(),
index: 48,
},
))
.id();
I never need to animate this UI image. So if I was forced to pass index_range
here, even if indirectly through Default
, it doesn't really make sense to me.
} else { | ||
// ...and it is NOT the last frame, then we move to the next frame... | ||
atlas.index += 1; | ||
atlas.advance_index(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, this change looks subtly wrong to me. Previously the logic was:
- If the index is not the last, then increment it (inline).
Now the logic is (due to advance_index
):
- If the index is not the last, then increment it (inside
advance_index
). - (Inside
advance_index
) If the index is more than the last set it to the index of the first.
Even if this particular change is the same as before (I didn't completely check because it looks subtly wrong and it's no longer intuitive, at least to me), my main concerns are left in a comment at the end of the review.
Objective
To simplify index updating for TextureAtlas when playing animation.
Solution
Add index_range and advance_index() for TextureAtlas.
So
will become
Migration Guide
index_range: (my_first_index, my_last_index)
for all yourTextureAtlas
initializations.just call
Testing
Tested all examples related to TextureAtlas.