Skip to content
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 a way to specify grid starting point in TextureAtlas #3775

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions crates/bevy_sprite/src/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Image>,
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<Image>,
tile_size: Vec2,
columns: usize,
rows: usize,
padding: Vec2,
origin: Vec2
) -> TextureAtlas {
let mut sprites = Vec::new();
let mut x_padding = 0.0;
Expand All @@ -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 {
Expand Down