Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add SpriteBatch.replace to allow the replacement of the batch i…
…nformation (#3079) This PR adds a new method to the `SpriteBatch` class which allow users to replace the information, based on an index of the batch. This is useful when you want to change some of the rendering calls without having to rebuild the whole batch. --------- Co-authored-by: Jochum van der Ploeg <jochum@vdploeg.net>
- Loading branch information
1 parent
76498eb
commit bf3c282
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:flame/sprite.dart'; | ||
import 'package:flutter/material.dart' hide Image; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
|
||
class _MockImage extends Mock implements Image {} | ||
|
||
void main() { | ||
group('SpriteBatch', () { | ||
test('can add to the batch', () { | ||
final image = _MockImage(); | ||
final spriteBatch = SpriteBatch(image); | ||
spriteBatch.add(source: Rect.zero); | ||
|
||
expect(spriteBatch.transforms, hasLength(1)); | ||
}); | ||
|
||
test('can replace the color of a batch', () { | ||
final image = _MockImage(); | ||
final spriteBatch = SpriteBatch(image); | ||
spriteBatch.add(source: Rect.zero, color: Colors.blue); | ||
|
||
spriteBatch.replace(0, color: Colors.red); | ||
|
||
expect(spriteBatch.colors, hasLength(1)); | ||
expect(spriteBatch.colors.first, Colors.red); | ||
}); | ||
|
||
test('can replace the source of a batch', () { | ||
final image = _MockImage(); | ||
final spriteBatch = SpriteBatch(image); | ||
spriteBatch.add(source: Rect.zero); | ||
|
||
spriteBatch.replace(0, source: const Rect.fromLTWH(1, 1, 1, 1)); | ||
|
||
expect(spriteBatch.sources, hasLength(1)); | ||
expect(spriteBatch.sources.first, const Rect.fromLTWH(1, 1, 1, 1)); | ||
}); | ||
|
||
test('can replace the transform of a batch', () { | ||
final image = _MockImage(); | ||
final spriteBatch = SpriteBatch(image); | ||
spriteBatch.add(source: Rect.zero); | ||
|
||
spriteBatch.replace(0, transform: RSTransform(1, 1, 1, 1)); | ||
|
||
expect(spriteBatch.transforms, hasLength(1)); | ||
expect( | ||
spriteBatch.transforms.first, | ||
isA<RSTransform>() | ||
.having((t) => t.scos, 'scos', 1) | ||
.having((t) => t.ssin, 'ssin', 1) | ||
.having((t) => t.tx, 'tx', 1) | ||
.having((t) => t.ty, 'ty', 1), | ||
); | ||
}); | ||
}); | ||
} |