Skip to content

Commit e1c4521

Browse files
author
benni-tec
committed
removed ExportSettings and changed JsonExport to use JsonObjects (better type safety)
1 parent ef1e6ab commit e1c4521

25 files changed

+130
-136
lines changed

packages/tiled/lib/src/chunk.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Chunk extends Exportable {
6161
}
6262

6363
@override
64-
ExportElement export(ExportSettings settings) {
64+
ExportElement export({FileEncoding? encoding, Compression? compression}) {
6565
final common = {
6666
'x': x.toExport(),
6767
'y': y.toExport(),
@@ -73,7 +73,11 @@ class Chunk extends Exportable {
7373
'chunk',
7474
common,
7575
{
76-
'data': TileData(data).export(settings),
76+
'data': TileDataEncoder(
77+
data: data,
78+
compression: compression,
79+
encoding: encoding,
80+
).export(),
7781
},
7882
);
7983
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Frame extends Exportable {
2424
);
2525

2626
@override
27-
ExportResolver export(ExportSettings settings) => ExportElement('frame', {
27+
ExportResolver export() => ExportElement('frame', {
2828
'tileid': tileId.toExport(),
2929
'duration': duration.toExport(),
3030
}, {});

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ part of tiled;
3232
/// When rendering a tile, the order of operation matters. The diagonal flip
3333
/// (x/y axis swap) is done first, followed by the horizontal and vertical
3434
/// flips.
35-
class Gid extends ExportValue {
35+
class Gid extends ExportValue<int> {
3636
static const int flippedHorizontallyFlag = 0x80000000;
3737
static const int flippedVerticallyFlag = 0x40000000;
3838
static const int flippedDiagonallyFlag = 0x20000000;
@@ -83,16 +83,16 @@ class Gid extends ExportValue {
8383
});
8484
}
8585

86-
String export() => ((tile & ~flagBits) |
87-
(flips.horizontally ? flippedHorizontallyFlag : 0) |
88-
(flips.vertically ? flippedVerticallyFlag : 0) |
89-
(flips.diagonally ? flippedDiagonallyFlag : 0) |
90-
(flips.antiDiagonally ? flippedAntiDiagonallyFlag : 0))
91-
.toString();
86+
int export() =>
87+
(tile & ~flagBits) |
88+
(flips.horizontally ? flippedHorizontallyFlag : 0) |
89+
(flips.vertically ? flippedVerticallyFlag : 0) |
90+
(flips.diagonally ? flippedDiagonallyFlag : 0) |
91+
(flips.antiDiagonally ? flippedAntiDiagonallyFlag : 0);
9292

9393
@override
94-
String get json => export();
94+
int get json => export();
9595

9696
@override
97-
String get xml => export();
97+
String get xml => export().toString();
9898
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ part of tiled;
1212
/// (default string is “”, default number is 0, default boolean is “false”,
1313
/// default color is #00000000, default file is “.” (the current file’s
1414
/// parent directory))
15-
class Property<T> {
15+
class Property<T> with Exportable {
1616
String name;
1717
PropertyType type;
1818
T value;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class TiledImage with Exportable {
5555
int get hashCode => source.hashCode;
5656

5757
@override
58-
ExportElement export(ExportSettings settings) => ExportElement(
58+
ExportElement export() => ExportElement(
5959
'image',
6060
{
6161
'width': width?.toExport(),

packages/tiled/lib/src/data.dart

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
part of tiled;
22

3-
class TileData extends DelegatingList<int> with Exportable {
4-
TileData(super.base);
3+
class TileDataEncoder extends DelegatingList<int> with Exportable {
4+
final FileEncoding? encoding;
5+
final Compression? compression;
6+
7+
TileDataEncoder({
8+
required List<int> data,
9+
required this.encoding,
10+
required this.compression,
11+
}) : super(data);
512

613
@override
7-
ExportResolver export(ExportSettings settings) {
14+
ExportResolver export() {
815
String? data;
9-
switch (settings.encoding) {
16+
switch (encoding) {
1017
case null:
1118
break;
1219
case FileEncoding.csv:
1320
data = join(', ');
1421
break;
1522
case FileEncoding.base64:
16-
data = _base64(settings);
23+
data = _base64();
1724
break;
1825
}
1926

2027
return ExportFormatSpecific(
2128
xml: ExportElement('data', {
22-
if (settings.encoding != null)
23-
'encoding': settings.encoding!.name.toExport(),
24-
if (settings.compression != null)
25-
'compression': settings.compression!.name.toExport(),
29+
if (encoding != null) 'encoding': encoding!.name.toExport(),
30+
if (compression != null) 'compression': compression!.name.toExport(),
2631
}, {
2732
if (data == null)
2833
'tiles': ExportList(map(
@@ -39,7 +44,7 @@ class TileData extends DelegatingList<int> with Exportable {
3944
);
4045
}
4146

42-
String _base64(ExportSettings settings) {
47+
String _base64() {
4348
// Conversion to Uint8List
4449
final uint32 = Uint32List.fromList(this);
4550
final dv = ByteData(this.length * 4);
@@ -52,8 +57,8 @@ class TileData extends DelegatingList<int> with Exportable {
5257

5358
// Compression
5459
List<int> compressed;
55-
print(settings.compression);
56-
switch (settings.compression) {
60+
print(compression);
61+
switch (compression) {
5762
case Compression.zlib:
5863
compressed = const ZLibEncoder().encode(uint8);
5964
break;

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ abstract class ExportObject {}
55
abstract class ExportResolver implements ExportObject {
66
XmlNode exportXml();
77

8-
dynamic exportJson();
8+
JsonObject exportJson();
99
}
1010

1111
class ExportElement implements ExportResolver {
@@ -49,34 +49,34 @@ class ExportElement implements ExportResolver {
4949
}
5050

5151
@override
52-
Map<String, dynamic> exportJson() => <String, dynamic>{
53-
...fields.map<String, dynamic>(
54-
(key, value) => MapEntry<String, dynamic>(key, value.json),
52+
JsonMap exportJson() => JsonMap({
53+
...fields.map(
54+
(key, value) => MapEntry(key, value.exportJson()),
5555
),
56-
...children.map<String, dynamic>((key, e) {
56+
...children.map((key, e) {
5757
if (e is ExportList) {
58-
return MapEntry<String, Iterable<dynamic>>(
58+
return MapEntry(
5959
key,
60-
e.map<dynamic>((e) => e.exportJson()).toList(),
60+
JsonList(e.map((e) => e.exportJson())),
6161
);
6262
} else if (e is ExportResolver) {
63-
return MapEntry<String, dynamic>(key, e.exportJson());
63+
return MapEntry(key, e.exportJson());
6464
} else {
6565
throw 'Bad State: ExportChild switch should have been exhaustive';
6666
}
6767
}),
68-
'properties': properties.map((e) => e.export().exportJson()).toList(),
69-
};
68+
'properties': JsonList(properties.map((e) => e.exportJson())),
69+
});
7070
}
7171

7272
class ExportDirect implements ExportResolver {
7373
final XmlElement xml;
74-
final dynamic json;
74+
final JsonObject json;
7575

7676
ExportDirect({required this.xml, required this.json});
7777

7878
@override
79-
dynamic exportJson() => json;
79+
JsonObject exportJson() => json;
8080

8181
@override
8282
XmlElement exportXml() => xml;
@@ -89,7 +89,7 @@ class ExportFormatSpecific implements ExportResolver {
8989
ExportFormatSpecific({required this.xml, required this.json});
9090

9191
@override
92-
dynamic exportJson() => json.exportJson();
92+
JsonObject exportJson() => json.exportJson();
9393

9494
@override
9595
XmlNode exportXml() => xml.exportXml();
@@ -99,6 +99,6 @@ class ExportList extends DelegatingList<ExportResolver>
9999
implements ExportObject {
100100
ExportList(Iterable<ExportResolver> base) : super(base.toList());
101101

102-
ExportList.from(Iterable<Exportable> source, ExportSettings settings)
103-
: super(source.map((e) => e.export(settings)).toList());
102+
ExportList.from(Iterable<Exportable> source)
103+
: super(source.map((e) => e.export()).toList());
104104
}

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

Lines changed: 0 additions & 15 deletions
This file was deleted.

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

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

3-
abstract class ExportValue implements ExportObject, ExportResolver {
3+
abstract class ExportValue<T> implements ExportObject, ExportResolver {
44
const ExportValue();
55

66
String get xml;
7-
dynamic get json;
7+
T get json;
88

99
@override
1010
XmlNode exportXml() => XmlText(xml);
1111

1212
@override
13-
dynamic exportJson() => json;
13+
JsonValue<T> exportJson() => JsonValue(json);
1414
}
1515

16-
class ExportLiteral<T> extends ExportValue {
16+
class ExportLiteral<T> extends ExportValue<T> {
1717
final T value;
1818

1919
const ExportLiteral(this.value);
@@ -33,7 +33,7 @@ extension ExportableNum on num {
3333
ExportValue toExport() => ExportLiteral<num>(this);
3434
}
3535

36-
class _ExportableBool extends ExportValue {
36+
class _ExportableBool extends ExportValue<bool> {
3737
final bool value;
3838

3939
_ExportableBool(this.value);
@@ -49,7 +49,7 @@ extension ExportableBool on bool {
4949
ExportValue toExport() => _ExportableBool(this);
5050
}
5151

52-
class _ExportableColor extends ExportValue {
52+
class _ExportableColor extends ExportValue<String> {
5353
final Color color;
5454

5555
const _ExportableColor(this.color);
@@ -72,16 +72,16 @@ extension ExportableColor on Color {
7272
ExportValue toExport() => _ExportableColor(this);
7373
}
7474

75-
class _ExportablePointList extends ExportValue {
75+
class _ExportablePointList extends ExportValue<List<Map<String, double>>> {
7676
final List<Point> points;
7777

7878
_ExportablePointList(this.points);
7979

8080
@override
81-
Iterable<Map<String, double>> get json => points.map((e) => {
81+
List<Map<String, double>> get json => points.map((e) => {
8282
'x': e.x,
8383
'y': e.y,
84-
});
84+
}).toList();
8585

8686
@override
8787
String get xml => points.map((e) => '${e.x},${e.y}').join(' ');
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
part of tiled;
22

33
abstract class Exportable {
4-
ExportResolver export(ExportSettings settings);
4+
ExportResolver export();
55

6-
XmlNode exportXml(ExportSettings settings) => export(settings).exportXml();
7-
dynamic exportJson(ExportSettings settings) => export(settings).exportJson();
6+
XmlNode exportXml() => export().exportXml();
7+
JsonObject exportJson() => export().exportJson();
88
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ abstract class JsonObject {
55
}
66

77
class JsonList extends DelegatingList<JsonObject> implements JsonObject {
8-
JsonList([super.base = const []]);
8+
JsonList([Iterable<JsonObject>? base]) : super(base?.toList() ?? []);
99

1010
@override
1111
List<JsonObject> get value => this;
@@ -18,7 +18,9 @@ class JsonMap extends DelegatingMap<String, JsonObject> implements JsonObject {
1818
Map<String, JsonObject> get value => this;
1919
}
2020

21-
class JsonValue implements JsonObject {
21+
class JsonValue<T> implements JsonObject {
2222
@override
23-
dynamic get value => this;
23+
final T value;
24+
25+
JsonValue(this.value);
2426
}

0 commit comments

Comments
 (0)