From a97d3071dd2b25316609dda2c5b6ca256f3e4cd1 Mon Sep 17 00:00:00 2001 From: Boutillier Date: Sat, 24 Sep 2022 12:58:06 +0000 Subject: [PATCH] Merge TextureAtlas::from_grid_with_padding into TextureAtlas::from_grid through option arguments (#6057) This is an adoption of #3775 This merges `TextureAtlas` `from_grid_with_padding` into `from_grid` , adding optional padding and optional offset. Since the orignal PR, the offset had already been added to from_grid_with_padding through #4836 ## Changelog - Added `padding` and `offset` arguments to `TextureAtlas::from_grid` - Removed `TextureAtlas::from_grid_with_padding` ## Migration Guide `TextureAtlas::from_grid_with_padding` was merged into `from_grid` which takes two additional parameters for padding and an offset. ``` // 0.8 TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1); // 0.9 TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1, None, None) // 0.8 TextureAtlas::from_grid_with_padding(texture_handle, Vec2::new(24.0, 24.0), 7, 1, Vec2::new(4.0, 4.0)); // 0.9 TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1, Some(Vec2::new(4.0, 4.0)), None) ``` Co-authored-by: olefish <88390729+oledfish@users.noreply.github.com> --- crates/bevy_sprite/src/texture_atlas.rs | 20 +++++-------------- examples/2d/sprite_sheet.rs | 3 ++- .../stress_tests/many_animated_sprites.rs | 3 ++- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/crates/bevy_sprite/src/texture_atlas.rs b/crates/bevy_sprite/src/texture_atlas.rs index ae94e77b2bad6..53cc9cadd6be2 100644 --- a/crates/bevy_sprite/src/texture_atlas.rs +++ b/crates/bevy_sprite/src/texture_atlas.rs @@ -67,31 +67,21 @@ impl TextureAtlas { } } - /// Generate a `TextureAtlas` by splitting a texture into a grid where each - /// `tile_size` by `tile_size` grid-cell is one of the textures in the - /// atlas. Resulting `TextureAtlas` is indexed left to right, top to bottom. - pub fn from_grid( - texture: Handle, - tile_size: Vec2, - columns: usize, - rows: usize, - ) -> TextureAtlas { - Self::from_grid_with_padding(texture, tile_size, columns, rows, Vec2::ZERO, Vec2::ZERO) - } - /// Generate a `TextureAtlas` by splitting a texture into a grid where each /// `tile_size` by `tile_size` grid-cell is one of the textures in the /// atlas. Grid cells are separated by some `padding`, and the grid starts /// at `offset` pixels from the top left corner. Resulting `TextureAtlas` is /// indexed left to right, top to bottom. - pub fn from_grid_with_padding( + pub fn from_grid( texture: Handle, tile_size: Vec2, columns: usize, rows: usize, - padding: Vec2, - offset: Vec2, + padding: Option, + offset: Option, ) -> TextureAtlas { + let padding = padding.unwrap_or_default(); + let offset = offset.unwrap_or_default(); let mut sprites = Vec::new(); let mut current_padding = Vec2::ZERO; diff --git a/examples/2d/sprite_sheet.rs b/examples/2d/sprite_sheet.rs index b1164286daf09..c5366f55116dd 100644 --- a/examples/2d/sprite_sheet.rs +++ b/examples/2d/sprite_sheet.rs @@ -39,7 +39,8 @@ fn setup( mut texture_atlases: ResMut>, ) { let texture_handle = asset_server.load("textures/rpg/chars/gabe/gabe-idle-run.png"); - let texture_atlas = TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1); + let texture_atlas = + TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1, None, None); let texture_atlas_handle = texture_atlases.add(texture_atlas); commands.spawn(Camera2dBundle::default()); commands.spawn(( diff --git a/examples/stress_tests/many_animated_sprites.rs b/examples/stress_tests/many_animated_sprites.rs index 296a4e5454a01..4ea6d9fae01bf 100644 --- a/examples/stress_tests/many_animated_sprites.rs +++ b/examples/stress_tests/many_animated_sprites.rs @@ -46,7 +46,8 @@ fn setup( let half_y = (map_size.y / 2.0) as i32; let texture_handle = assets.load("textures/rpg/chars/gabe/gabe-idle-run.png"); - let texture_atlas = TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1); + let texture_atlas = + TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1, None, None); let texture_atlas_handle = texture_atlases.add(texture_atlas); // Spawns the camera