Skip to content

Commit

Permalink
Merge branch 'main' into test-case-for-CustomSpriteButton
Browse files Browse the repository at this point in the history
  • Loading branch information
spydon authored Oct 3, 2024
2 parents 628cf3f + f36533e commit 367e4b2
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 13 deletions.
3 changes: 2 additions & 1 deletion doc/flame/rendering/images.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ To synchronously retrieve a previously cached image, the `fromCache` method can
with that key was not previously loaded, it will throw an exception.

To add an already loaded image to the cache, the `add` method can be used and you can set the key
that the image should have in the cache.
that the image should have in the cache. You can retrieve all the keys in the cache using the `keys`
getter.

You can also use `ImageExtension.fromPixels()` to dynamically create an image during the game.

Expand Down
1 change: 1 addition & 0 deletions packages/flame/lib/input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export 'src/components/input/joystick_component.dart';
export 'src/components/input/sprite_button_component.dart';
export 'src/events/game_mixins/multi_touch_drag_detector.dart';
export 'src/events/game_mixins/multi_touch_tap_detector.dart';
export 'src/events/tap_config.dart';
export 'src/extensions/vector2.dart';
export 'src/game/mixins/keyboard.dart';
export 'src/gestures/detectors.dart';
3 changes: 3 additions & 0 deletions packages/flame/lib/src/cache/images.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ class Images {
/// Whether the cache contains the specified [key] or not.
bool containsKey(String key) => _assets.containsKey(key);

/// Returns the list of keys in the cache.
List<String> get keys => _assets.keys.toList();

String? findKeyForImage(Image image) {
return _assets.keys.firstWhere(
(k) => _assets[k]?.image?.isCloneOf(image) ?? false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,29 @@ class SpriteButtonComponent extends SpriteGroupComponent<ButtonState>

set button(Sprite value) {
_button = value;
updateSprite(ButtonState.up, value);
if (isLoaded) {
updateSprite(ButtonState.up, value);
}
}

set buttonDown(Sprite value) {
_buttonDown = value;
updateSprite(ButtonState.down, value);
}

@override
void onLoad() {
super.onLoad();
sprites = {
ButtonState.up: button,
ButtonState.down: buttonDown,
};
if (isLoaded) {
updateSprite(ButtonState.down, value);
}
}

@override
void onMount() {
super.onMount();
assert(
_button != null,
'The button sprite has to be set either in onLoad or in the constructor',
);
sprites = {
ButtonState.up: _button!,
ButtonState.down: buttonDown,
};
super.onMount();
}

@override
Expand Down
13 changes: 13 additions & 0 deletions packages/flame/test/cache/images_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ void main() {
}
});

test('keys', () {
final cache = Images();
final images = List.generate(10, (_) => _MockImage());
for (var i = 0; i < images.length; i++) {
cache.add(i.toString(), images[i]);
}
expect(
cache.keys.toSet(),
{for (var i = 0; i < images.length; i++) i.toString()},
);
});

testWithFlameGame(
'prefix on game.images can be changed',
(game) async {
Expand All @@ -111,6 +123,7 @@ void main() {
final img = _MockImage();
game.images.add('my image', img);
expect(Flame.images.containsKey('my image'), isTrue);
expect(Flame.images.keys, hasLength(1));

game.images = Images();
game.images.add('new image', img);
Expand Down
5 changes: 5 additions & 0 deletions packages/flame_fire_atlas/lib/flame_fire_atlas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,9 @@ class FireAtlas {
loop: selection.loop,
);
}

/// Returns the atlas image.
///
/// Throws if called before the image is loaded.
Image get image => _assertImageLoaded();
}
26 changes: 26 additions & 0 deletions packages/flame_fire_atlas/test/flame_fire_atlas_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ void main() {
expect(atlas.id, 'cave_ace');
});

test('image returns the loaded image', () async {
final atlas = await _readTestAtlas();
final image = atlas.image;
expect(image, isA<Image>());
});

test('image throws when the image is not loaded', () async {
final atlas = FireAtlas(
id: '',
tileWidth: 0,
tileHeight: 0,
imageData: '',
);

expect(
() => atlas.image,
throwsA(
isA<Exception>().having(
(e) => e.toString(),
'toString',
'Exception: Atlas is not loaded yet, call "load" before using it',
),
),
);
});

test('can load the asset using the global assets/images', () async {
final assetsMock = _AssetsCacheMock();

Expand Down
1 change: 1 addition & 0 deletions packages/flame_tiled/test/tile_atlas_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ void main() {
expect(atlas.key, 'images/green.png');

expect(images.containsKey('images/green.png'), isTrue);
expect(images.keys, hasLength(1));

expect(
await imageToPng(atlas.atlas!),
Expand Down

0 comments on commit 367e4b2

Please sign in to comment.