Skip to content

Commit

Permalink
removed ExportSettings and changed JsonExport to use JsonObjects (bet…
Browse files Browse the repository at this point in the history
…ter type safety)
  • Loading branch information
benni-tec committed Jan 16, 2024
1 parent ef1e6ab commit e1c4521
Show file tree
Hide file tree
Showing 25 changed files with 130 additions and 136 deletions.
8 changes: 6 additions & 2 deletions packages/tiled/lib/src/chunk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Chunk extends Exportable {
}

@override
ExportElement export(ExportSettings settings) {
ExportElement export({FileEncoding? encoding, Compression? compression}) {
final common = {
'x': x.toExport(),
'y': y.toExport(),
Expand All @@ -73,7 +73,11 @@ class Chunk extends Exportable {
'chunk',
common,
{
'data': TileData(data).export(settings),
'data': TileDataEncoder(
data: data,
compression: compression,
encoding: encoding,
).export(),
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/common/frame.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Frame extends Exportable {
);

@override
ExportResolver export(ExportSettings settings) => ExportElement('frame', {
ExportResolver export() => ExportElement('frame', {
'tileid': tileId.toExport(),
'duration': duration.toExport(),
}, {});
Expand Down
18 changes: 9 additions & 9 deletions packages/tiled/lib/src/common/gid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ part of tiled;
/// When rendering a tile, the order of operation matters. The diagonal flip
/// (x/y axis swap) is done first, followed by the horizontal and vertical
/// flips.
class Gid extends ExportValue {
class Gid extends ExportValue<int> {
static const int flippedHorizontallyFlag = 0x80000000;
static const int flippedVerticallyFlag = 0x40000000;
static const int flippedDiagonallyFlag = 0x20000000;
Expand Down Expand Up @@ -83,16 +83,16 @@ class Gid extends ExportValue {
});
}

String export() => ((tile & ~flagBits) |
(flips.horizontally ? flippedHorizontallyFlag : 0) |
(flips.vertically ? flippedVerticallyFlag : 0) |
(flips.diagonally ? flippedDiagonallyFlag : 0) |
(flips.antiDiagonally ? flippedAntiDiagonallyFlag : 0))
.toString();
int export() =>
(tile & ~flagBits) |
(flips.horizontally ? flippedHorizontallyFlag : 0) |
(flips.vertically ? flippedVerticallyFlag : 0) |
(flips.diagonally ? flippedDiagonallyFlag : 0) |
(flips.antiDiagonally ? flippedAntiDiagonallyFlag : 0);

@override
String get json => export();
int get json => export();

@override
String get xml => export();
String get xml => export().toString();
}
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/common/property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ part of tiled;
/// (default string is “”, default number is 0, default boolean is “false”,
/// default color is #00000000, default file is “.” (the current file’s
/// parent directory))
class Property<T> {
class Property<T> with Exportable {
String name;
PropertyType type;
T value;
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/common/tiled_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class TiledImage with Exportable {
int get hashCode => source.hashCode;

@override
ExportElement export(ExportSettings settings) => ExportElement(
ExportElement export() => ExportElement(
'image',
{
'width': width?.toExport(),
Expand Down
29 changes: 17 additions & 12 deletions packages/tiled/lib/src/data.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
part of tiled;

class TileData extends DelegatingList<int> with Exportable {
TileData(super.base);
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(ExportSettings settings) {
ExportResolver export() {
String? data;
switch (settings.encoding) {
switch (encoding) {
case null:
break;
case FileEncoding.csv:
data = join(', ');
break;
case FileEncoding.base64:
data = _base64(settings);
data = _base64();
break;
}

return ExportFormatSpecific(
xml: ExportElement('data', {
if (settings.encoding != null)
'encoding': settings.encoding!.name.toExport(),
if (settings.compression != null)
'compression': settings.compression!.name.toExport(),
if (encoding != null) 'encoding': encoding!.name.toExport(),
if (compression != null) 'compression': compression!.name.toExport(),
}, {
if (data == null)
'tiles': ExportList(map(
Expand All @@ -39,7 +44,7 @@ class TileData extends DelegatingList<int> with Exportable {
);
}

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

// Compression
List<int> compressed;
print(settings.compression);
switch (settings.compression) {
print(compression);
switch (compression) {
case Compression.zlib:
compressed = const ZLibEncoder().encode(uint8);
break;
Expand Down
30 changes: 15 additions & 15 deletions packages/tiled/lib/src/exporter/export_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ abstract class ExportObject {}
abstract class ExportResolver implements ExportObject {
XmlNode exportXml();

dynamic exportJson();
JsonObject exportJson();
}

class ExportElement implements ExportResolver {
Expand Down Expand Up @@ -49,34 +49,34 @@ class ExportElement implements ExportResolver {
}

@override
Map<String, dynamic> exportJson() => <String, dynamic>{
...fields.map<String, dynamic>(
(key, value) => MapEntry<String, dynamic>(key, value.json),
JsonMap exportJson() => JsonMap({
...fields.map(
(key, value) => MapEntry(key, value.exportJson()),
),
...children.map<String, dynamic>((key, e) {
...children.map((key, e) {
if (e is ExportList) {
return MapEntry<String, Iterable<dynamic>>(
return MapEntry(
key,
e.map<dynamic>((e) => e.exportJson()).toList(),
JsonList(e.map((e) => e.exportJson())),
);
} else if (e is ExportResolver) {
return MapEntry<String, dynamic>(key, e.exportJson());
return MapEntry(key, e.exportJson());
} else {
throw 'Bad State: ExportChild switch should have been exhaustive';
}
}),
'properties': properties.map((e) => e.export().exportJson()).toList(),
};
'properties': JsonList(properties.map((e) => e.exportJson())),
});
}

class ExportDirect implements ExportResolver {
final XmlElement xml;
final dynamic json;
final JsonObject json;

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

@override
dynamic exportJson() => json;
JsonObject exportJson() => json;

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

@override
dynamic exportJson() => json.exportJson();
JsonObject exportJson() => json.exportJson();

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

ExportList.from(Iterable<Exportable> source, ExportSettings settings)
: super(source.map((e) => e.export(settings)).toList());
ExportList.from(Iterable<Exportable> source)
: super(source.map((e) => e.export()).toList());
}
15 changes: 0 additions & 15 deletions packages/tiled/lib/src/exporter/export_settings.dart

This file was deleted.

18 changes: 9 additions & 9 deletions packages/tiled/lib/src/exporter/export_value.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
part of tiled;

abstract class ExportValue implements ExportObject, ExportResolver {
abstract class ExportValue<T> implements ExportObject, ExportResolver {
const ExportValue();

String get xml;
dynamic get json;
T get json;

@override
XmlNode exportXml() => XmlText(xml);

@override
dynamic exportJson() => json;
JsonValue<T> exportJson() => JsonValue(json);
}

class ExportLiteral<T> extends ExportValue {
class ExportLiteral<T> extends ExportValue<T> {
final T value;

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

class _ExportableBool extends ExportValue {
class _ExportableBool extends ExportValue<bool> {
final bool value;

_ExportableBool(this.value);
Expand All @@ -49,7 +49,7 @@ extension ExportableBool on bool {
ExportValue toExport() => _ExportableBool(this);
}

class _ExportableColor extends ExportValue {
class _ExportableColor extends ExportValue<String> {
final Color color;

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

class _ExportablePointList extends ExportValue {
class _ExportablePointList extends ExportValue<List<Map<String, double>>> {
final List<Point> points;

_ExportablePointList(this.points);

@override
Iterable<Map<String, double>> get json => points.map((e) => {
List<Map<String, double>> get json => points.map((e) => {
'x': e.x,
'y': e.y,
});
}).toList();

@override
String get xml => points.map((e) => '${e.x},${e.y}').join(' ');
Expand Down
6 changes: 3 additions & 3 deletions packages/tiled/lib/src/exporter/exportable.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
part of tiled;

abstract class Exportable {
ExportResolver export(ExportSettings settings);
ExportResolver export();

XmlNode exportXml(ExportSettings settings) => export(settings).exportXml();
dynamic exportJson(ExportSettings settings) => export(settings).exportJson();
XmlNode exportXml() => export().exportXml();
JsonObject exportJson() => export().exportJson();
}
8 changes: 5 additions & 3 deletions packages/tiled/lib/src/exporter/json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ abstract class JsonObject {
}

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

@override
List<JsonObject> get value => this;
Expand All @@ -18,7 +18,9 @@ class JsonMap extends DelegatingMap<String, JsonObject> implements JsonObject {
Map<String, JsonObject> get value => this;
}

class JsonValue implements JsonObject {
class JsonValue<T> implements JsonObject {
@override
dynamic get value => this;
final T value;

JsonValue(this.value);
}
Loading

0 comments on commit e1c4521

Please sign in to comment.