Skip to content

Commit ef1e6ab

Browse files
author
benni-tec
committed
added some tests and fixes
1 parent 50025a0 commit ef1e6ab

File tree

14 files changed

+283
-100
lines changed

14 files changed

+283
-100
lines changed

packages/tiled/lib/src/common/tiled_image.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ part of tiled;
1818
/// when the image changes)
1919
/// * height: The image height in pixels (optional)
2020
@immutable
21-
class TiledImage implements Exportable {
21+
class TiledImage with Exportable {
2222
final String? source;
2323
final String? format;
2424
final int? width;

packages/tiled/lib/src/data.dart

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
part of tiled;
22

3-
class TileData extends DelegatingList<int> implements Exportable {
3+
class TileData extends DelegatingList<int> with Exportable {
44
TileData(super.base);
55

66
@override
@@ -40,33 +40,34 @@ class TileData extends DelegatingList<int> implements Exportable {
4040
}
4141

4242
String _base64(ExportSettings settings) {
43+
// Conversion to Uint8List
44+
final uint32 = Uint32List.fromList(this);
45+
final dv = ByteData(this.length * 4);
46+
47+
for (var i = 0; i < this.length; ++i) {
48+
dv.setInt32(i * 4, uint32[i], Endian.little);
49+
}
50+
51+
final uint8 = dv.buffer.asUint8List();
52+
4353
// Compression
4454
List<int> compressed;
55+
print(settings.compression);
4556
switch (settings.compression) {
4657
case Compression.zlib:
47-
compressed = const ZLibEncoder().encode(this);
58+
compressed = const ZLibEncoder().encode(uint8);
4859
break;
4960
case Compression.gzip:
50-
compressed = GZipEncoder().encode(this)!;
61+
compressed = GZipEncoder().encode(uint8)!;
5162
break;
5263
case Compression.zstd:
5364
throw UnsupportedError('zstd is an unsupported compression');
5465
case null:
55-
compressed = this;
66+
compressed = uint8;
5667
break;
5768
}
5869

59-
// Conversion to Uint8List
60-
final uint32 = Uint32List.fromList(compressed);
61-
final dv = ByteData(compressed.length * 4);
62-
63-
for (var i = 0; i < compressed.length; ++i) {
64-
dv.setInt32(i * 4, uint32[i], Endian.little);
65-
}
66-
67-
final uint8 = dv.buffer.asUint8List();
68-
6970
// encoding
70-
return base64Encode(uint8);
71+
return base64Encode(compressed);
7172
}
7273
}

packages/tiled/lib/src/exporter/export_element.dart

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,12 @@ class ExportElement implements ExportResolver {
2424
@override
2525
XmlElement exportXml() {
2626
final _children = children.values.expand((e) {
27-
switch (e.runtimeType) {
28-
case ExportList:
29-
return (e as ExportList).map((e) => e.exportXml());
30-
case ExportResolver:
31-
return [(e as ExportResolver).exportXml()];
32-
case ExportValue:
33-
return [XmlText((e as ExportValue).xml)];
34-
default:
35-
throw 'Bad State: ExportChild switch should have been exhaustive';
27+
if (e is ExportList) {
28+
return e.map((e) => e.exportXml());
29+
} else if (e is ExportResolver) {
30+
return [e.exportXml()];
31+
} else {
32+
throw 'Bad State: ExportObject switch should have been exhaustive';
3633
}
3734
});
3835

@@ -41,11 +38,12 @@ class ExportElement implements ExportResolver {
4138
fields.entries.map((e) => XmlAttribute(XmlName(e.key), e.value.xml)),
4239
[
4340
..._children,
44-
XmlElement(
45-
XmlName('properties'),
46-
[],
47-
properties.map((e) => e.export().exportXml()),
48-
),
41+
if (properties.isNotEmpty)
42+
XmlElement(
43+
XmlName('properties'),
44+
[],
45+
properties.map((e) => e.export().exportXml()).toList(),
46+
),
4947
],
5048
);
5149
}
@@ -56,22 +54,18 @@ class ExportElement implements ExportResolver {
5654
(key, value) => MapEntry<String, dynamic>(key, value.json),
5755
),
5856
...children.map<String, dynamic>((key, e) {
59-
switch (e.runtimeType) {
60-
case ExportList:
61-
return MapEntry<String, Iterable<dynamic>>(
62-
key,
63-
(e as ExportList).map<dynamic>((e) => e.exportJson()),
64-
);
65-
case ExportResolver:
66-
return MapEntry<String, dynamic>(
67-
key,
68-
(e as ExportElement).exportJson(),
69-
);
70-
default:
71-
throw 'Bad State: ExportChild switch should have been exhaustive';
57+
if (e is ExportList) {
58+
return MapEntry<String, Iterable<dynamic>>(
59+
key,
60+
e.map<dynamic>((e) => e.exportJson()).toList(),
61+
);
62+
} else if (e is ExportResolver) {
63+
return MapEntry<String, dynamic>(key, e.exportJson());
64+
} else {
65+
throw 'Bad State: ExportChild switch should have been exhaustive';
7266
}
7367
}),
74-
'properties': properties.map((e) => e.export().exportJson()),
68+
'properties': properties.map((e) => e.export().exportJson()).toList(),
7569
};
7670
}
7771

packages/tiled/lib/src/exporter/export_value.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ class _ExportableColor extends ExportValue {
5555
const _ExportableColor(this.color);
5656

5757
static String _hex(int value) {
58-
final str = value.toRadixString(16).padLeft(2, '0');
59-
return str.substring(str.length - 2, str.length - 1);
58+
return value.toRadixString(16).padLeft(2, '0');
6059
}
6160

6261
String get export =>

packages/tiled/lib/src/exporter/exportable.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ part of tiled;
22

33
abstract class Exportable {
44
ExportResolver export(ExportSettings settings);
5+
6+
XmlNode exportXml(ExportSettings settings) => export(settings).exportXml();
7+
dynamic exportJson(ExportSettings settings) => export(settings).exportJson();
58
}

packages/tiled/lib/src/objects/tiled_object.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,10 @@ class TiledObject extends Exportable {
213213
'points': polygon.toExport(),
214214
},
215215
{},
216-
properties,
217216
),
218217
if (text != null) 'text': text!.export(settings),
219218
},
219+
properties
220220
),
221221
json: ExportElement(
222222
'object',

packages/tiled/lib/src/tiled_map.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ part of tiled;
5555
///
5656
/// Can contain any number: <tileset>, <layer>, <objectgroup>, <imagelayer>,
5757
/// <group> (since 1.0), <editorsettings> (since 1.3)
58-
class TiledMap {
58+
class TiledMap with Exportable {
5959
TileMapType type;
6060
String version;
6161
String? tiledVersion;
@@ -359,6 +359,7 @@ class TiledMap {
359359
);
360360
}
361361

362+
@override
362363
ExportElement export(ExportSettings settings) => ExportElement(
363364
'map',
364365
{
@@ -367,7 +368,7 @@ class TiledMap {
367368

368369
'orientation': orientation?.name.toExport(),
369370
'renderorder': renderOrder.name.toExport(),
370-
'compressionLevel': '-1'.toExport(),
371+
'compressionlevel': '-1'.toExport(),
371372

372373
'width': width.toExport(),
373374
'height': height.toExport(),
@@ -379,14 +380,14 @@ class TiledMap {
379380
'staggerindex': staggerIndex?.name.toExport(),
380381
// 'parallaxoriginx': , 'parallaxoriginy': , Not supplied by this class
381382

382-
'backgroundColor': backgroundColor?.toExport(),
383+
'backgroundcolor': backgroundColor?.toExport(),
383384
'nextlayerid': nextLayerId?.toExport(),
384385
'nextobjectid': nextObjectId?.toExport(),
385386
'infinite': infinite.toExport(),
386387
}.nonNulls(),
387388
{
388-
'layers': ExportList.from(layers, settings),
389389
'tilesets': ExportList.from(tilesets, settings),
390+
'layers': ExportList.from(layers, settings),
390391
},
391392
properties,
392393
);

packages/tiled/lib/src/tileset/tile.dart

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ part of tiled;
1818
///
1919
/// Can contain at most one: <properties>, <image> (since 0.9), <objectgroup>,
2020
/// <animation>.
21-
class Tile implements Exportable {
21+
class Tile with Exportable {
2222
int localId;
2323
String? type;
2424
double probability;
@@ -50,40 +50,44 @@ class Tile implements Exportable {
5050
/// Will be same as [type].
5151
String? get class_ => type;
5252

53-
Tile.parse(Parser parser)
54-
: this(
55-
localId: parser.getInt('id'),
53+
factory Tile.parse(Parser parser) {
54+
final image = parser.getSingleChildOrNullAs('image', TiledImage.parse);
55+
final x = parser.getDoubleOrNull('x');
56+
final y = parser.getDoubleOrNull('y');
57+
final width = parser.getDoubleOrNull('width');
58+
final height = parser.getDoubleOrNull('height');
5659

57-
/// Tiled 1.9 "type" has been moved to "class"
58-
type:
59-
parser.getStringOrNull('class') ?? parser.getStringOrNull('type'),
60+
final imageRect = [image, x, y, width, height].contains(null)
61+
? null
62+
: Rectangle(x!, y!, width!, height!);
6063

61-
probability: parser.getDouble('probability', defaults: 0),
62-
terrain: parser
63-
.getStringOrNull('terrain')
64-
?.split(',')
65-
.map((str) => str.isEmpty ? null : int.parse(str))
66-
.toList() ??
67-
[],
68-
image: parser.getSingleChildOrNullAs('image', TiledImage.parse),
69-
imageRect: Rectangle(
70-
parser.getDoubleOrNull('x') ?? 0,
71-
parser.getDoubleOrNull('y') ?? 0,
72-
parser.getDoubleOrNull('width') ?? 0,
73-
parser.getDoubleOrNull('height') ?? 0,
74-
),
75-
objectGroup:
76-
parser.getSingleChildOrNullAs('objectgroup', Layer.parse),
77-
animation: parser.formatSpecificParsing(
78-
(json) => json.getChildrenAs('animation', Frame.parse),
79-
(xml) =>
80-
xml
81-
.getSingleChildOrNull('animation')
82-
?.getChildrenAs('frame', Frame.parse) ??
83-
[],
84-
),
85-
properties: parser.getProperties(),
86-
);
64+
return Tile(
65+
localId: parser.getInt('id'),
66+
67+
/// Tiled 1.9 "type" has been moved to "class"
68+
type: parser.getStringOrNull('class') ?? parser.getStringOrNull('type'),
69+
70+
probability: parser.getDouble('probability', defaults: 0),
71+
terrain: parser
72+
.getStringOrNull('terrain')
73+
?.split(',')
74+
.map((str) => str.isEmpty ? null : int.parse(str))
75+
.toList() ??
76+
[],
77+
image: image,
78+
imageRect: imageRect,
79+
objectGroup: parser.getSingleChildOrNullAs('objectgroup', Layer.parse),
80+
animation: parser.formatSpecificParsing(
81+
(json) => json.getChildrenAs('animation', Frame.parse),
82+
(xml) =>
83+
xml
84+
.getSingleChildOrNull('animation')
85+
?.getChildrenAs('frame', Frame.parse) ??
86+
[],
87+
),
88+
properties: parser.getProperties(),
89+
);
90+
}
8791

8892
@override
8993
ExportResolver export(ExportSettings settings) {
@@ -98,7 +102,7 @@ class Tile implements Exportable {
98102
}.nonNulls();
99103

100104
final children = {
101-
'image': image!.export(settings),
105+
'image': image?.export(settings),
102106
'objectgroup': objectGroup?.export(settings)
103107
}.nonNulls();
104108

packages/tiled/lib/src/tileset/tileset.dart

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ part of tiled;
4242
/// (since 1.5)
4343
///
4444
/// Can contain any number: <tile>
45-
class Tileset implements Exportable {
45+
class Tileset with Exportable {
4646
int? firstGid;
4747
String? source;
4848
String? name;
@@ -284,23 +284,21 @@ class Tileset implements Exportable {
284284
'class': type.name.toExport(),
285285
'type': type.name.toExport(),
286286

287-
'tilewidth': tileWidth!.toExport(),
288-
'tileheigth': tileHeight!.toExport(),
287+
'tilewidth': tileWidth?.toExport(),
288+
'tileheigth': tileHeight?.toExport(),
289289
'spacing': spacing.toExport(),
290290
'margin': margin.toExport(),
291291

292-
'tilecount': tileCount!.toExport(),
293-
'columns': columns!.toExport(),
292+
'tilecount': tileCount?.toExport(),
293+
'columns': columns?.toExport(),
294294
'objectalignment': objectAlignment.name.toExport(),
295295
// 'tilerendersize': , Not supported by this class
296296
// 'fillmode': , Not supported by this class
297-
};
297+
}.nonNulls();
298298

299299
final common = {
300-
if (image != null)
301-
'image': image!.export(settings)
302-
else
303-
'tiles': ExportList.from(tiles, settings),
300+
'image': image?.export(settings),
301+
'tiles': ExportList.from(tiles, settings),
304302
'tileoffset': tileOffset?.export(settings),
305303
'grid': grid?.export(settings),
306304
// 'terraintypes': , DEPRECATED
@@ -316,15 +314,22 @@ class Tileset implements Exportable {
316314

317315
return ExportFormatSpecific(
318316
xml: ExportElement(
319-
'wangsets',
317+
'tileset',
320318
fields,
321319
{
322320
...common,
323-
'wangsets': wangsets,
321+
if (wangSets.isNotEmpty) 'wangsets': wangsets,
324322
},
325323
properties,
326324
),
327-
json: wangsets,
325+
json: ExportElement(
326+
'tileset',
327+
fields,
328+
{
329+
...common,
330+
'wangsets': ExportList.from(wangSets, settings),
331+
},
332+
),
328333
);
329334
}
330335
}

packages/tiled/lib/src/tileset/wang/wang_color.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ part of tiled;
1313
/// others in case of multiple options. (defaults to 0)
1414
///
1515
/// Can contain at most one: <properties>
16-
class WangColor implements Exportable {
16+
class WangColor with Exportable {
1717
String name;
1818
String color;
1919
int tile;

packages/tiled/lib/src/tileset/wang/wang_tile.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ part of tiled;
1919
/// * hflip: Whether the tile is flipped horizontally (removed in Tiled 1.5).
2020
/// * vflip: Whether the tile is flipped vertically (removed in Tiled 1.5).
2121
/// * dflip: Whether the tile is flipped on its diagonal (removed in Tiled 1.5).
22-
class WangTile implements Exportable {
22+
class WangTile with Exportable {
2323
int tileId;
2424
List<int> wangId;
2525
bool hFlip;

0 commit comments

Comments
 (0)