From c604c058a9c94a3a742d95fa3d7f753d849da25d Mon Sep 17 00:00:00 2001 From: oledfish <88390729+oledfish@users.noreply.github.com> Date: Wed, 26 Jan 2022 21:40:38 -0300 Subject: [PATCH 1/5] Add methods to specify grid origin in TextureAtlas --- crates/bevy_sprite/src/texture_atlas.rs | 34 ++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/crates/bevy_sprite/src/texture_atlas.rs b/crates/bevy_sprite/src/texture_atlas.rs index 49d366fa64ff1..bc99c9ac5ccc9 100644 --- a/crates/bevy_sprite/src/texture_atlas.rs +++ b/crates/bevy_sprite/src/texture_atlas.rs @@ -74,7 +74,7 @@ impl TextureAtlas { columns: usize, rows: usize, ) -> TextureAtlas { - Self::from_grid_with_padding(texture, tile_size, columns, rows, Vec2::new(0f32, 0f32)) + Self::from_grid_with_padding_and_origin(texture, tile_size, columns, rows, Vec2::new(0f32, 0f32), Vec2::new(0f32, 0f32)) } /// Generate a `TextureAtlas` by splitting a texture into a grid where each @@ -86,6 +86,34 @@ impl TextureAtlas { columns: usize, rows: usize, padding: Vec2, + ) -> TextureAtlas { + Self::from_grid_with_padding_and_origin(texture, tile_size, columns, rows, padding, Vec2::new(0f32, 0f32)) + } + + /// Generate a `TextureAtlas` by splitting a texture into a grid where each + /// cell of the grid of `tile_size` is one of the textures in the atlas and the grid starts + /// at a point `origin` starting from the top-left corner + pub fn from_grid_with_origin( + texture: Handle, + tile_size: Vec2, + columns: usize, + rows: usize, + origin: Vec2, + ) -> TextureAtlas { + Self::from_grid_with_padding_and_origin(texture, tile_size, columns, rows, Vec2::new(0f32, 0f32), origin) + } + + /// Generate a `TextureAtlas` by splitting a texture into a grid where each + /// cell of the grid of `tile_size` is one of the textures in the atlas, is separated by + /// some `padding` in the texture and the grid starts at a point `origin` starting from + /// the top-left corner + pub fn from_grid_with_padding_and_origin( + texture: Handle, + tile_size: Vec2, + columns: usize, + rows: usize, + padding: Vec2, + origin: Vec2 ) -> TextureAtlas { let mut sprites = Vec::new(); let mut x_padding = 0.0; @@ -101,8 +129,8 @@ impl TextureAtlas { } let rect_min = Vec2::new( - (tile_size.x + x_padding) * x as f32, - (tile_size.y + y_padding) * y as f32, + origin.x + (tile_size.x + x_padding) * x as f32, + origin.y + (tile_size.y + y_padding) * y as f32, ); sprites.push(Rect { From 33335928582a045e208776e0ce47317b74aa908d Mon Sep 17 00:00:00 2001 From: oledfish <88390729+oledfish@users.noreply.github.com> Date: Thu, 27 Jan 2022 01:18:08 -0300 Subject: [PATCH 2/5] Merge all TextureAtlas grid methods into `from_grid` --- crates/bevy_sprite/src/texture_atlas.rs | 47 ++++--------------------- 1 file changed, 6 insertions(+), 41 deletions(-) diff --git a/crates/bevy_sprite/src/texture_atlas.rs b/crates/bevy_sprite/src/texture_atlas.rs index bc99c9ac5ccc9..abcf2d7748de7 100644 --- a/crates/bevy_sprite/src/texture_atlas.rs +++ b/crates/bevy_sprite/src/texture_atlas.rs @@ -66,55 +66,20 @@ impl TextureAtlas { } } - /// Generate a `TextureAtlas` by splitting a texture into a grid where each - /// cell of the grid of `tile_size` is one of the textures in the atlas - pub fn from_grid( - texture: Handle, - tile_size: Vec2, - columns: usize, - rows: usize, - ) -> TextureAtlas { - Self::from_grid_with_padding_and_origin(texture, tile_size, columns, rows, Vec2::new(0f32, 0f32), Vec2::new(0f32, 0f32)) - } - - /// Generate a `TextureAtlas` by splitting a texture into a grid where each - /// cell of the grid of `tile_size` is one of the textures in the atlas and is separated by - /// some `padding` in the texture - pub fn from_grid_with_padding( - texture: Handle, - tile_size: Vec2, - columns: usize, - rows: usize, - padding: Vec2, - ) -> TextureAtlas { - Self::from_grid_with_padding_and_origin(texture, tile_size, columns, rows, padding, Vec2::new(0f32, 0f32)) - } - - /// Generate a `TextureAtlas` by splitting a texture into a grid where each - /// cell of the grid of `tile_size` is one of the textures in the atlas and the grid starts - /// at a point `origin` starting from the top-left corner - pub fn from_grid_with_origin( - texture: Handle, - tile_size: Vec2, - columns: usize, - rows: usize, - origin: Vec2, - ) -> TextureAtlas { - Self::from_grid_with_padding_and_origin(texture, tile_size, columns, rows, Vec2::new(0f32, 0f32), origin) - } - /// Generate a `TextureAtlas` by splitting a texture into a grid where each /// cell of the grid of `tile_size` is one of the textures in the atlas, is separated by - /// some `padding` in the texture and the grid starts at a point `origin` starting from + /// some `padding` in the texture and the grid starts at a point `origin` starting from /// the top-left corner - pub fn from_grid_with_padding_and_origin( + pub fn from_grid( texture: Handle, tile_size: Vec2, columns: usize, rows: usize, - padding: Vec2, - origin: Vec2 + padding: Option, + origin: Option, ) -> TextureAtlas { + let padding = padding.unwrap_or_default(); + let origin = origin.unwrap_or_default(); let mut sprites = Vec::new(); let mut x_padding = 0.0; let mut y_padding = 0.0; From 509e18915b2868343812dfcf53c693cb54879dd7 Mon Sep 17 00:00:00 2001 From: oledfish <88390729+oledfish@users.noreply.github.com> Date: Thu, 27 Jan 2022 01:23:18 -0300 Subject: [PATCH 3/5] Fix sprite_sheet example --- examples/2d/sprite_sheet.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/2d/sprite_sheet.rs b/examples/2d/sprite_sheet.rs index acf025a02ab76..1d90d4c78ce16 100644 --- a/examples/2d/sprite_sheet.rs +++ b/examples/2d/sprite_sheet.rs @@ -28,7 +28,7 @@ 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_bundle(OrthographicCameraBundle::new_2d()); commands From 0de89680d5e0d9fe7bfdedb15a18a72a24d53be5 Mon Sep 17 00:00:00 2001 From: oledfish <88390729+oledfish@users.noreply.github.com> Date: Thu, 27 Jan 2022 01:27:14 -0300 Subject: [PATCH 4/5] Fix sprite_sheet example formatting --- examples/2d/sprite_sheet.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/2d/sprite_sheet.rs b/examples/2d/sprite_sheet.rs index 1d90d4c78ce16..4e594f2dedd18 100644 --- a/examples/2d/sprite_sheet.rs +++ b/examples/2d/sprite_sheet.rs @@ -28,7 +28,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, None, None); + 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_bundle(OrthographicCameraBundle::new_2d()); commands From 90e54a98988df0788640ae53daa89fe5bc9a9778 Mon Sep 17 00:00:00 2001 From: oledfish <88390729+oledfish@users.noreply.github.com> Date: Thu, 27 Jan 2022 12:59:30 -0300 Subject: [PATCH 5/5] Fix docs, rename `origin` to `offset` --- crates/bevy_sprite/src/texture_atlas.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/crates/bevy_sprite/src/texture_atlas.rs b/crates/bevy_sprite/src/texture_atlas.rs index abcf2d7748de7..88c5dba6fef41 100644 --- a/crates/bevy_sprite/src/texture_atlas.rs +++ b/crates/bevy_sprite/src/texture_atlas.rs @@ -66,20 +66,22 @@ impl TextureAtlas { } } - /// Generate a `TextureAtlas` by splitting a texture into a grid where each - /// cell of the grid of `tile_size` is one of the textures in the atlas, is separated by - /// some `padding` in the texture and the grid starts at a point `origin` starting from - /// the top-left corner + /// Generate a `TextureAtlas` by splitting a texture into a grid. + /// + /// Each cell in the grid of `tile_size` corresponds to one texture in the atlas. + /// + /// * Cells can have horizontal and/or vertical separation specified by `padding` + /// * The grid can start at a point `offset` relative to the top-left corner of the texture pub fn from_grid( texture: Handle, tile_size: Vec2, columns: usize, rows: usize, padding: Option, - origin: Option, + offset: Option, ) -> TextureAtlas { let padding = padding.unwrap_or_default(); - let origin = origin.unwrap_or_default(); + let offset = offset.unwrap_or_default(); let mut sprites = Vec::new(); let mut x_padding = 0.0; let mut y_padding = 0.0; @@ -94,8 +96,8 @@ impl TextureAtlas { } let rect_min = Vec2::new( - origin.x + (tile_size.x + x_padding) * x as f32, - origin.y + (tile_size.y + y_padding) * y as f32, + offset.x + (tile_size.x + x_padding) * x as f32, + offset.y + (tile_size.y + y_padding) * y as f32, ); sprites.push(Rect {