Skip to content

Commit

Permalink
feat: Add convenience method for getting images in each layer (#66)
Browse files Browse the repository at this point in the history
This PR adds the convenience method to the TiledMap that gets all TiledImage in the specific layer.

If the layer is Group, the method gets all images in the group recursively.
  • Loading branch information
Hwan-seok committed Mar 28, 2023
1 parent d71d0c8 commit 1d3043f
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 18 deletions.
27 changes: 27 additions & 0 deletions packages/tiled/lib/src/tiled_map.dart
Expand Up @@ -219,6 +219,33 @@ class TiledMap {
return imageSet.toList();
}

List<TiledImage> collectImagesInLayer(Layer layer) {
if (layer is ImageLayer) {
return [layer.image];
} else if (layer is Group) {
return layer.layers.expand(collectImagesInLayer).toList();
} else if (layer is TileLayer) {
const emptyTile = 0;
final rows = layer.tileData ?? <List<Gid>>[];
final gids = rows
.expand((row) => row.map((gid) => gid.tile))
.where((gid) => gid != emptyTile)
.toSet();

return gids
.map(tilesetByTileGId)
.toSet() // The different gid can be in the same tileset
.expand(
(tileset) =>
[tileset.image, ...tileset.tiles.map((tile) => tile.image)],
)
.whereNotNull()
.toList();
}

return [];
}

/// Finds the first layer with the matching [name], or throw an
/// [ArgumentError] if one cannot be found.
/// Will search recursively through [Group] children.
Expand Down
7 changes: 4 additions & 3 deletions packages/tiled/pubspec.yaml
Expand Up @@ -14,6 +14,7 @@ dependencies:
xml: ^6.1.0

dev_dependencies:
dartdoc: ^4.1.0
flame_lint: ^0.1.2
test: any
dartdoc: ^6.0.1
flame_lint: ^0.2.0
flutter_test:
sdk: flutter
2 changes: 1 addition & 1 deletion packages/tiled/test/complexmap_infinite_test.dart
@@ -1,6 +1,6 @@
import 'dart:io';

import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/image_layer_test.dart
@@ -1,6 +1,6 @@
import 'dart:io';

import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/isometric_staggered_test.dart
@@ -1,6 +1,6 @@
import 'dart:io';

import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/isometric_test.dart
@@ -1,6 +1,6 @@
import 'dart:io';

import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
3 changes: 1 addition & 2 deletions packages/tiled/test/layer_test.dart
@@ -1,8 +1,7 @@
import 'dart:async';
import 'dart:io';
import 'dart:ui';

import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
138 changes: 137 additions & 1 deletion packages/tiled/test/map_test.dart
@@ -1,4 +1,4 @@
import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down Expand Up @@ -151,6 +151,142 @@ void main() {
});
});

group('Map.collectImagesInLayer', () {
late TiledMap map;
setUp(() {
map = TiledMap(
width: 2,
height: 2,
tileWidth: 8,
tileHeight: 8,
layers: [
TileLayer(
name: 'tile layer 1',
width: 2,
height: 2,
data: [1, 0, 2, 0],
),
Group(
name: 'group layer 1',
layers: [
TileLayer(
name: 'group - tile layer 1',
width: 2,
height: 2,
data: [1, 2, 3, 0],
),
TileLayer(
name: 'group - tile layer 2',
width: 2,
height: 2,
data: [5, 0, 0, 0],
),
],
),
ImageLayer(
name: 'image layer 1',
image: const TiledImage(source: 'image_layer.png'),
repeatX: false,
repeatY: false,
),
ObjectGroup(name: 'object layer 1', objects: []),
TileLayer(
name: 'tile layer 3 (empty)',
width: 2,
height: 2,
data: [0, 0, 0, 0],
),
],
tilesets: [
Tileset(
name: 'TileSet_1',
image: const TiledImage(source: 'tileset_1.png'),
firstGid: 1,
columns: 1,
tileCount: 2,
tiles: [
Tile(localId: 0),
Tile(localId: 1),
],
),
Tileset(
name: 'TileSet_2',
image: const TiledImage(source: 'tileset_2.png'),
firstGid: 3,
columns: 1,
tileCount: 2,
tiles: [
Tile(localId: 0),
Tile(localId: 1),
],
),
Tileset(
name: 'TileSet_3',
image: const TiledImage(source: 'tileset_3.png'),
firstGid: 5,
columns: 1,
tileCount: 2,
tiles: [
Tile(localId: 0),
Tile(localId: 1),
],
),
],
);
});

test('gets images only in use on each TileLayer', () {
final tileLayer1 = map.layerByName('tile layer 1');
final tileLayer2 = map.layerByName('group - tile layer 1');

final images1 = map.collectImagesInLayer(tileLayer1);
final images2 = map.collectImagesInLayer(tileLayer2);

expect(images1, hasLength(1));
expect(images1[0].source, equals('tileset_1.png'));

expect(images2, hasLength(2));
expect(images2[0].source, equals('tileset_1.png'));
expect(images2[1].source, equals('tileset_2.png'));
});

test('gets no image if TileLayer is empty', () {
final tileLayer = map.layerByName('tile layer 3 (empty)');

final images = map.collectImagesInLayer(tileLayer);

expect(images, hasLength(0));
});

test('gets all images recursively in the Group', () {
final tileLayerInsideGroup = map.layerByName('group layer 1');

final images3 = map.collectImagesInLayer(tileLayerInsideGroup);

expect(images3, hasLength(3));
expect(images3[0].source, equals('tileset_1.png'));
expect(images3[1].source, equals('tileset_2.png'));
expect(images3[2].source, equals('tileset_3.png'));
});

test('gets a image in the ImageLayer', () {
final imageLayer = map.layerByName('image layer 1');

final images = map.collectImagesInLayer(imageLayer);

expect(images, hasLength(1));
expect(images[0].source, equals('image_layer.png'));
});

test('gets no image in the ObjectLayer', () {
final imageLayer = map.layerByName('object layer 1');

final images = map.collectImagesInLayer(imageLayer);

expect(images, hasLength(0));
});
});

group('Map.getTileSet', () {
late TiledMap map;
final tileset = Tileset(
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/object_group_test.dart
@@ -1,7 +1,7 @@
import 'dart:io';
import 'dart:ui';

import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/overflow_bug_test.dart
Expand Up @@ -2,7 +2,7 @@

import 'dart:io';

import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/parser_compare_test.dart
@@ -1,6 +1,6 @@
import 'dart:io';

import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/parser_test.dart
Expand Up @@ -2,7 +2,7 @@ import 'dart:io';
import 'dart:math' show Rectangle;
import 'dart:ui';

import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';
import 'package:xml/xml.dart';

Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/rectangle_map_test.dart
@@ -1,6 +1,6 @@
import 'dart:io';

import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/tile_test.dart
@@ -1,7 +1,7 @@
import 'dart:io';
import 'dart:ui';

import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';
import 'package:xml/xml.dart';

Expand Down
3 changes: 2 additions & 1 deletion packages/tiled/test/tileset_test.dart
@@ -1,5 +1,6 @@
import 'dart:io';
import 'package:test/test.dart';

import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';
import 'package:xml/xml.dart';

Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/tmx_object_test.dart
@@ -1,6 +1,6 @@
import 'dart:io';

import 'package:test/test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down

0 comments on commit 1d3043f

Please sign in to comment.