Skip to content

Commit 50025a0

Browse files
author
benni-tec
committed
added exports to I think everything
1 parent 54c3878 commit 50025a0

27 files changed

+819
-188
lines changed

packages/tiled/lib/src/chunk.dart

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ part of tiled;
1717
/// The data inside is a compressed (encoded) representation of a list
1818
/// (that sequentially represents a matrix) of integers representing
1919
/// [Gid]s.
20-
class Chunk {
20+
class Chunk extends Exportable {
2121
List<int> data;
2222

2323
int x;
@@ -59,4 +59,22 @@ class Chunk {
5959

6060
return Chunk(data: data, x: x, y: y, width: width, height: height);
6161
}
62+
63+
@override
64+
ExportElement export(ExportSettings settings) {
65+
final common = {
66+
'x': x.toExport(),
67+
'y': y.toExport(),
68+
'width': width.toExport(),
69+
'height': height.toExport(),
70+
};
71+
72+
return ExportElement(
73+
'chunk',
74+
common,
75+
{
76+
'data': TileData(data).export(settings),
77+
},
78+
);
79+
}
6280
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ part of tiled;
88
/// * tileid: The local ID of a tile within the parent <tileset>.
99
/// * duration: How long (in milliseconds) this frame should be displayed
1010
/// before advancing to the next frame.
11-
class Frame {
11+
class Frame extends Exportable {
1212
int tileId;
1313
int duration;
1414

@@ -22,4 +22,10 @@ class Frame {
2222
tileId: parser.getInt('tileid'),
2323
duration: parser.getInt('duration'),
2424
);
25+
26+
@override
27+
ExportResolver export(ExportSettings settings) => ExportElement('frame', {
28+
'tileid': tileId.toExport(),
29+
'duration': duration.toExport(),
30+
}, {});
2531
}

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

Lines changed: 7 additions & 2 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 UnaryExportable {
35+
class Gid extends ExportValue {
3636
static const int flippedHorizontallyFlag = 0x80000000;
3737
static const int flippedVerticallyFlag = 0x40000000;
3838
static const int flippedDiagonallyFlag = 0x20000000;
@@ -83,11 +83,16 @@ class Gid extends UnaryExportable {
8383
});
8484
}
8585

86-
@override
8786
String export() => ((tile & ~flagBits) |
8887
(flips.horizontally ? flippedHorizontallyFlag : 0) |
8988
(flips.vertically ? flippedVerticallyFlag : 0) |
9089
(flips.diagonally ? flippedDiagonallyFlag : 0) |
9190
(flips.antiDiagonally ? flippedAntiDiagonallyFlag : 0))
9291
.toString();
92+
93+
@override
94+
String get json => export();
95+
96+
@override
97+
String get xml => export();
9398
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
part of tiled;
2+
3+
extension Grouping<V> on Iterable<V> {
4+
Map<K, List<V>> groupBy<K>(K Function(V value) key) {
5+
final out = <K, List<V>>{};
6+
for (final v in this) {
7+
final k = key(v);
8+
if (!out.containsKey(k)) {
9+
out[k] = [v];
10+
} else {
11+
out[k]!.add(v);
12+
}
13+
}
14+
15+
return out;
16+
}
17+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
part of tiled;
2+
13
extension Null<K, V> on Map<K, V?> {
24
Map<K, V> nonNulls() => {for (final e in entries.where((e) => e.value is V)) e.key : e.value as V};
35
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ class Property<T> {
8686
);
8787
}
8888
}
89+
90+
ExportValue get exportValue => value.toString().toExport();
91+
92+
ExportElement export() => ExportElement('property', {
93+
'name': name.toExport(),
94+
'type': type.name.toExport(),
95+
'value': exportValue,
96+
}, {});
8997
}
9098

9199
/// A wrapper for a Tiled property set
@@ -164,6 +172,9 @@ class ColorProperty extends Property<ColorData> {
164172
required super.value,
165173
required this.hexValue,
166174
}) : super(type: PropertyType.color);
175+
176+
@override
177+
ExportValue get exportValue => value.toExport();
167178
}
168179

169180
/// [value] is the string text
@@ -180,6 +191,10 @@ class FileProperty extends Property<String> {
180191
required super.name,
181192
required super.value,
182193
}) : super(type: PropertyType.file);
194+
195+
@override
196+
ExportValue get exportValue =>
197+
value.isNotEmpty ? value.toExport() : '.'.toExport();
183198
}
184199

185200
/// [value] is the integer number

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

Lines changed: 16 additions & 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 {
21+
class TiledImage implements Exportable {
2222
final String? source;
2323
final String? format;
2424
final int? width;
@@ -53,4 +53,19 @@ class TiledImage {
5353

5454
@override
5555
int get hashCode => source.hashCode;
56+
57+
@override
58+
ExportElement export(ExportSettings settings) => ExportElement(
59+
'image',
60+
{
61+
'width': width?.toExport(),
62+
'height': height?.toExport(),
63+
'format': format?.toExport(),
64+
'source': source?.toExport(),
65+
'trans': trans?.toExport(),
66+
}.nonNulls(),
67+
{
68+
// missing data, tiled.dart does not support embedded images
69+
},
70+
);
5671
}

packages/tiled/lib/src/data.dart

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
part of tiled;
2+
3+
class TileData extends DelegatingList<int> implements Exportable {
4+
TileData(super.base);
5+
6+
@override
7+
ExportResolver export(ExportSettings settings) {
8+
String? data;
9+
switch (settings.encoding) {
10+
case null:
11+
break;
12+
case FileEncoding.csv:
13+
data = join(', ');
14+
break;
15+
case FileEncoding.base64:
16+
data = _base64(settings);
17+
break;
18+
}
19+
20+
return ExportFormatSpecific(
21+
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(),
26+
}, {
27+
if (data == null)
28+
'tiles': ExportList(map(
29+
(gid) => ExportElement(
30+
'tile',
31+
{'gid': gid.toExport()},
32+
{},
33+
),
34+
))
35+
else
36+
'data': data.toExport(),
37+
}),
38+
json: ExportLiteral(this),
39+
);
40+
}
41+
42+
String _base64(ExportSettings settings) {
43+
// Compression
44+
List<int> compressed;
45+
switch (settings.compression) {
46+
case Compression.zlib:
47+
compressed = const ZLibEncoder().encode(this);
48+
break;
49+
case Compression.gzip:
50+
compressed = GZipEncoder().encode(this)!;
51+
break;
52+
case Compression.zstd:
53+
throw UnsupportedError('zstd is an unsupported compression');
54+
case null:
55+
compressed = this;
56+
break;
57+
}
58+
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+
69+
// encoding
70+
return base64Encode(uint8);
71+
}
72+
}

packages/tiled/lib/src/exportable.dart

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

packages/tiled/lib/src/exporter.dart

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

0 commit comments

Comments
 (0)