forked from flame-engine/tiled.dart
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/flame-engine#71-exporter'
- Loading branch information
Showing
28 changed files
with
1,051 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
part of tiled; | ||
|
||
extension Grouping<V> on Iterable<V> { | ||
Map<K, List<V>> groupBy<K>(K Function(V value) key) { | ||
final out = <K, List<V>>{}; | ||
for (final v in this) { | ||
final k = key(v); | ||
if (!out.containsKey(k)) { | ||
out[k] = [v]; | ||
} else { | ||
out[k]!.add(v); | ||
} | ||
} | ||
|
||
return out; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
part of tiled; | ||
|
||
extension Null<K, V> on Map<K, V?> { | ||
Map<K, V> nonNulls() => {for (final e in entries.where((e) => e.value is V)) e.key : e.value as V}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
part of tiled; | ||
|
||
class TileDataEncoder extends DelegatingList<int> with Exportable { | ||
final FileEncoding? encoding; | ||
final Compression? compression; | ||
|
||
TileDataEncoder({ | ||
required List<int> data, | ||
required this.encoding, | ||
required this.compression, | ||
}) : super(data); | ||
|
||
@override | ||
ExportResolver export() { | ||
String? data; | ||
switch (encoding) { | ||
case null: | ||
break; | ||
case FileEncoding.csv: | ||
data = join(', '); | ||
break; | ||
case FileEncoding.base64: | ||
data = _base64(); | ||
break; | ||
} | ||
|
||
return ExportFormatSpecific( | ||
xml: ExportElement('data', { | ||
if (encoding != null) 'encoding': encoding!.name.toExport(), | ||
if (compression != null) 'compression': compression!.name.toExport(), | ||
}, { | ||
if (data == null) | ||
'tiles': ExportList(map( | ||
(gid) => ExportElement( | ||
'tile', | ||
{'gid': gid.toExport()}, | ||
{}, | ||
), | ||
)) | ||
else | ||
'data': data.toExport(), | ||
}), | ||
json: ExportLiteral(this), | ||
); | ||
} | ||
|
||
String _base64() { | ||
// Conversion to Uint8List | ||
final uint32 = Uint32List.fromList(this); | ||
final dv = ByteData(this.length * 4); | ||
|
||
for (var i = 0; i < this.length; ++i) { | ||
dv.setInt32(i * 4, uint32[i], Endian.little); | ||
} | ||
|
||
final uint8 = dv.buffer.asUint8List(); | ||
|
||
// Compression | ||
List<int> compressed; | ||
print(compression); | ||
switch (compression) { | ||
case Compression.zlib: | ||
compressed = const ZLibEncoder().encode(uint8); | ||
break; | ||
case Compression.gzip: | ||
compressed = GZipEncoder().encode(uint8)!; | ||
break; | ||
case Compression.zstd: | ||
throw UnsupportedError('zstd is an unsupported compression'); | ||
case null: | ||
compressed = uint8; | ||
break; | ||
} | ||
|
||
// encoding | ||
return base64Encode(compressed); | ||
} | ||
} |
Oops, something went wrong.