File tree Expand file tree Collapse file tree 5 files changed +79
-49
lines changed
Expand file tree Collapse file tree 5 files changed +79
-49
lines changed Original file line number Diff line number Diff line change 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 ` .
Original file line number Diff line number Diff 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) {
Original file line number Diff line number Diff line change 11library tiled;
22
3+ import 'dart:collection' ;
34import 'dart:convert' ;
45import 'dart:math' show Rectangle;
56import 'dart:typed_data' ;
Original file line number Diff line number Diff 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 ));
You can’t perform that action at this time.
0 commit comments