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

feat: Add margin and spacing properties to SpriteSheet #2925

Merged
merged 6 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
32 changes: 25 additions & 7 deletions packages/flame/lib/src/sprite_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,46 @@ class SpriteSheet {
/// size. If it's an animation sheet, this would be the frame size.
final Vector2 srcSize;

/// The empty space around the edges of the image.
final double margin;

/// This empty space in between adjacent tiles within the image.
final double spacing;

final int _rows;
final int _columns;
final Map<int, Sprite> _spriteCache = {};

/// Creates a sprite sheet given the image and the tile size.
SpriteSheet({
required this.image,
required this.srcSize,
});
this.margin = 0,
this.spacing = 0,
}) : _columns =
(image.width - 2 * margin + spacing) ~/ (srcSize.x + spacing),
_rows = (image.height - 2 * margin + spacing) ~/ (srcSize.y + spacing);

SpriteSheet.fromColumnsAndRows({
required this.image,
required int columns,
required int rows,
}) : srcSize = Vector2(
image.width / columns,
image.height / rows,
this.spacing = 0,
this.margin = 0,
}) : _columns = columns,
_rows = rows,
srcSize = Vector2(
(image.width - 2 * margin - (columns - 1) * spacing) / columns,
(image.height - 2 * margin - (rows - 1) * spacing) / rows,
);

/// Compute the number of columns the image has
/// by using the image width and tile size.
int get columns => image.width ~/ srcSize.x;
int get columns => _columns;
spydon marked this conversation as resolved.
Show resolved Hide resolved

/// Compute the number of rows the image has
/// by using the image height and tile size.
int get rows => image.height ~/ srcSize.y;
int get rows => _rows;

/// Gets the sprite in the position (row, column) on the sprite sheet grid.
///
Expand Down Expand Up @@ -101,7 +117,9 @@ class SpriteSheet {
final j = spriteId ~/ columns;
return Sprite(
image,
srcPosition: Vector2Extension.fromInts(i, j)..multiply(srcSize),
srcPosition: Vector2Extension.fromInts(i, j)
..multiply(srcSize)
..translate(margin + i * spacing, margin + j * spacing),
srcSize: srcSize,
);
}
Expand Down
25 changes: 25 additions & 0 deletions packages/flame/test/spritesheet_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ void main() {
expect(spriteSheet.columns, 100);
});

test('calculates columns and rows with margin and spacing', () {
final spriteSheet = SpriteSheet(
image: image,
srcSize: Vector2(1, 2),
margin: 3,
spacing: 2,
);

expect(spriteSheet.rows, 24);
expect(spriteSheet.columns, 32);
});

test('calculates srcSize with margin and spacing', () {
final spriteSheet = SpriteSheet.fromColumnsAndRows(
image: image,
columns: 32,
rows: 24,
margin: 3,
spacing: 2,
);

expect(spriteSheet.srcSize.x, 1);
expect(spriteSheet.srcSize.y, 2);
});

test('assign the correct time in sprite', () {
final spriteSheet = SpriteSheet(
image: image,
Expand Down