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 SpriteBatch.replace to allow the replacement of the batch information #3079

Merged
merged 6 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
39 changes: 39 additions & 0 deletions packages/flame/lib/src/sprite_batch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,45 @@ class SpriteBatch {
return picture.toImageSafe(image.width * 2, image.height);
}

int get length => _sources.length;

/// Replace a batch item at the [index] the new provided values, when a
/// value is not informed, the method will keep the current information.
erickzanardo marked this conversation as resolved.
Show resolved Hide resolved
///
/// Throws an [ArgumentError] if the [index] is out of bounds.
/// At least one of the parameters must be different from null.
void replace(
int index, {
Rect? source,
Color? color,
RSTransform? transform,
}) {
assert(
source != null || color != null || transform != null,
'At least one of the parameters must be different from null.',
);

if (index < 0 || index >= length) {
throw ArgumentError('Index out of bounds: $index');
}

final currentBatchItem = _batchItems[index];
final newBatchItem = BatchItem(
source: source ?? currentBatchItem.source,
transform: transform ?? currentBatchItem.transform,
color: color ?? currentBatchItem.paint.color,
flip: currentBatchItem.flip,
);

_batchItems[index] = newBatchItem;

_sources[index] = newBatchItem.source;
_transforms[index] = newBatchItem.transform;
if (color != null) {
_colors[index] = color;
}
}

/// Add a new batch item using a RSTransform.
///
/// The [source] parameter is the source location on the [atlas].
Expand Down
60 changes: 60 additions & 0 deletions packages/flame/test/sprite_batch_test.dart
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),
);
});
});
}
Loading