Skip to content

Commit 7ef732b

Browse files
authored
feat: adding Group layer support for layerByName (#51)
* adding Group layer support for layerByName * adding CHANGELOG entry * changing CHANGELOG entry to [Next]
1 parent d566dfc commit 7ef732b

File tree

5 files changed

+79
-49
lines changed

5 files changed

+79
-49
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## [Next]
2+
* Adding support for searching `Group` layer children when using `map.layerByName`.
23
* Add parser support for Tiled hex colors as `ui.Color`
34
* BREAKING CHANGE: re-named string color fields to `colorHex` such as `layer.tintColorHex` and
45
added a new `color` field paired with each `colorHex` field of type `ui.Color`.

lib/src/tiled_map.dart

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,32 @@ class TiledMap {
220220
return imageSet.toList();
221221
}
222222

223+
/// Finds the first layer with the matching [name], or throw an
224+
/// [ArgumentError] if one cannot be found.
225+
/// Will search recursively through [Group] children.
223226
Layer layerByName(String name) {
224-
return layers.firstWhere(
225-
(element) => element.name == name,
226-
orElse: () => throw ArgumentError('Layer $name not found'),
227-
);
227+
final toSearch = Queue<List<Layer>>();
228+
toSearch.add(layers);
229+
230+
Layer? found;
231+
while (found == null && toSearch.isNotEmpty) {
232+
final currentLayers = toSearch.removeFirst();
233+
currentLayers.forEach((layer) {
234+
if (layer.name == name) {
235+
found = layer;
236+
return;
237+
} else if (layer is Group) {
238+
toSearch.add(layer.layers);
239+
}
240+
});
241+
}
242+
243+
if (found != null) {
244+
return found!;
245+
}
246+
247+
// Couldn't find it in any layer
248+
throw ArgumentError('Layer $name not found');
228249
}
229250

230251
Tileset tilesetByName(String name) {

lib/tiled.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
library tiled;
22

3+
import 'dart:collection';
34
import 'dart:convert';
45
import 'dart:math' show Rectangle;
56
import 'dart:typed_data';

test/complexmap_infinite_test.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ void main() {
1616

1717
group('Layer.tiles Json', () {
1818
late TileLayer layer;
19+
late Group groupLayer;
1920
setUp(() {
21+
groupLayer = complexMapInfinite.layerByName('group_layer') as Group;
2022
layer = complexMapInfinite.layerByName('top') as TileLayer;
2123
});
2224
test('is expected to be infinite with chunks', () {
2325
expect(complexMapInfinite.infinite, isTrue);
26+
expect(groupLayer, isNotNull);
27+
expect(groupLayer.layers[1], equals(layer));
28+
expect(layer, isNotNull);
2429
expect(layer.tileData, isNull);
2530
expect(layer.chunks!.length, equals(9));
2631
expect(layer.chunks![0].tileData.length, equals(16));

0 commit comments

Comments
 (0)