Skip to content

Commit

Permalink
fix: ObjectAlignment enum names (#74)
Browse files Browse the repository at this point in the history
Name of some of the enum values of ObjectAlignment were not matching the values from Tiled. As a result, tilesets with topLeft, topRight, bottomLeft and bottomRight alignment couldn't be parsed. https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#tmx-tileset

image

Another problem that was discovered after fixing the names was with the byName method from the EnumByName extension. That method was not using names from ObjectAlignmentExtension that Tiled defines. To fix that, this PR adds a byName static method for ObjectAlignment which uses the correct names.
  • Loading branch information
ufrshubham committed Nov 3, 2023
1 parent 41c9439 commit 628f1f6
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 41 deletions.
22 changes: 17 additions & 5 deletions packages/tiled/lib/src/common/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,19 @@ enum ObjectAlignment {
right,
bottomLeft,
bottom,
bottomRight,
bottomRight;

/// Returns the [ObjectAlignment] based on given [name].
///
/// Throws an [ArgumentError] if no match is found.
static ObjectAlignment fromName(String name) {
for (final value in ObjectAlignment.values) {
if (value.name == name) {
return value;
}
}
throw ArgumentError.value(name, 'name', 'No enum value with that name');
}
}

extension ObjectAlignmentExtension on ObjectAlignment {
Expand All @@ -390,23 +402,23 @@ extension ObjectAlignmentExtension on ObjectAlignment {
case ObjectAlignment.unspecified:
return 'unspecified';
case ObjectAlignment.topLeft:
return 'topLeft';
return 'topleft';
case ObjectAlignment.top:
return 'top';
case ObjectAlignment.topRight:
return 'topRight';
return 'topright';
case ObjectAlignment.left:
return 'left';
case ObjectAlignment.center:
return 'center';
case ObjectAlignment.right:
return 'right';
case ObjectAlignment.bottomLeft:
return 'bottomLeft';
return 'bottomleft';
case ObjectAlignment.bottom:
return 'bottom';
case ObjectAlignment.bottomRight:
return 'bottomRight';
return 'bottomright';
}
}
}
Expand Down
28 changes: 17 additions & 11 deletions packages/tiled/lib/src/common/flips.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ class Flips {
final bool diagonally;
final bool antiDiagonally;

const Flips(
this.horizontally,
this.vertically,
this.diagonally,
this.antiDiagonally,
);
const Flips({
required this.horizontally,
required this.vertically,
required this.diagonally,
required this.antiDiagonally,
});

const Flips.defaults() : this(false, false, false, false);
const Flips.defaults()
: this(
horizontally: false,
vertically: false,
diagonally: false,
antiDiagonally: false,
);

Flips copyWith({
bool? horizontally,
Expand All @@ -22,10 +28,10 @@ class Flips {
bool? antiDiagonally,
}) {
return Flips(
horizontally ?? this.horizontally,
vertically ?? this.vertically,
diagonally ?? this.diagonally,
antiDiagonally ?? this.antiDiagonally,
horizontally: horizontally ?? this.horizontally,
vertically: vertically ?? this.vertically,
diagonally: diagonally ?? this.diagonally,
antiDiagonally: antiDiagonally ?? this.antiDiagonally,
);
}
}
8 changes: 4 additions & 4 deletions packages/tiled/lib/src/common/gid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ class Gid {
flippedDiagonallyFlag |
flippedAntiDiagonallyFlag);
final flip = Flips(
flippedHorizontally,
flippedVertically,
flippedDiagonally,
flippedAntiDiagonally,
horizontally: flippedHorizontally,
vertically: flippedVertically,
diagonally: flippedDiagonally,
antiDiagonally: flippedAntiDiagonally,
);
return Gid(tileId, flip);
}
Expand Down
26 changes: 13 additions & 13 deletions packages/tiled/lib/src/layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ abstract class Layer {
CustomProperties properties;

Layer({
this.id,
required this.name,
required this.type,
this.id,
this.class_,
this.x = 0,
this.y = 0,
Expand Down Expand Up @@ -298,7 +298,7 @@ abstract class Layer {
}
final text = xml.element.children.first;
if (text is XmlText) {
return text.text;
return text.value;
}
return null;
},
Expand Down Expand Up @@ -380,8 +380,10 @@ class TileLayer extends Layer {
List<List<Gid>>? tileData;

TileLayer({
super.id,
required super.name,
required this.width,
required this.height,
super.id,
super.class_,
super.x,
super.y,
Expand All @@ -396,8 +398,6 @@ class TileLayer extends Layer {
super.opacity,
super.visible,
super.properties,
required this.width,
required this.height,
this.compression,
this.encoding = FileEncoding.csv,
this.chunks,
Expand Down Expand Up @@ -441,8 +441,9 @@ class ObjectGroup extends Layer {
Color color;

ObjectGroup({
super.id,
required super.name,
required this.objects,
super.id,
super.class_,
super.x,
super.y,
Expand All @@ -458,7 +459,6 @@ class ObjectGroup extends Layer {
super.visible,
super.properties,
this.drawOrder = DrawOrder.topDown,
required this.objects,
this.colorHex = defaultColorHex,
this.color = defaultColor,
}) : super(
Expand Down Expand Up @@ -487,8 +487,11 @@ class ImageLayer extends Layer {
bool repeatY;

ImageLayer({
super.id,
required super.name,
required this.image,
required this.repeatX,
required this.repeatY,
super.id,
super.class_,
super.x,
super.y,
Expand All @@ -503,9 +506,6 @@ class ImageLayer extends Layer {
super.opacity,
super.visible,
super.properties,
required this.image,
required this.repeatX,
required this.repeatY,
this.transparentColorHex,
this.transparentColor,
}) : super(
Expand All @@ -518,8 +518,9 @@ class Group extends Layer {
List<Layer> layers;

Group({
super.id,
required super.name,
required this.layers,
super.id,
super.class_,
super.x,
super.y,
Expand All @@ -534,7 +535,6 @@ class Group extends Layer {
super.opacity,
super.visible,
super.properties,
required this.layers,
}) : super(
type: LayerType.imageLayer,
);
Expand Down
8 changes: 4 additions & 4 deletions packages/tiled/lib/src/tiled_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ class TiledMap {
StaggerIndex? staggerIndex;

TiledMap({
this.type = TileMapType.map,
this.version = '1.0',
this.tiledVersion,
required this.width,
required this.height,
this.infinite = false,
required this.tileWidth,
required this.tileHeight,
this.type = TileMapType.map,
this.version = '1.0',
this.tiledVersion,
this.infinite = false,
this.tilesets = const [],
this.layers = const [],
this.backgroundColorHex,
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/tileset/tileset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Tileset {
final firstGid = parser.getIntOrNull('firstgid');
final margin = parser.getInt('margin', defaults: 0);
final name = parser.getStringOrNull('name');
final objectAlignment = ObjectAlignment.values.byName(
final objectAlignment = ObjectAlignment.fromName(
parser.getString('objectalignment', defaults: 'unspecified'),
);
final source = parser.getStringOrNull('source');
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ dependencies:

dev_dependencies:
dartdoc: ^6.0.1
flame_lint: ^0.2.0
flame_lint: ^1.1.1
flutter_test:
sdk: flutter
2 changes: 1 addition & 1 deletion packages/tiled/test/map_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ void main() {
image: const TiledImage(),
),
],
)
),
],
);
});
Expand Down
28 changes: 28 additions & 0 deletions packages/tiled/test/object_alignment_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';

void main() {
group('ObjectAlignment', () {
test('ObjectAlignment.byName', () {
expect(
ObjectAlignment.fromName('unspecified'),
ObjectAlignment.unspecified,
);
expect(ObjectAlignment.fromName('topleft'), ObjectAlignment.topLeft);
expect(ObjectAlignment.fromName('top'), ObjectAlignment.top);
expect(ObjectAlignment.fromName('topright'), ObjectAlignment.topRight);
expect(ObjectAlignment.fromName('left'), ObjectAlignment.left);
expect(ObjectAlignment.fromName('center'), ObjectAlignment.center);
expect(ObjectAlignment.fromName('right'), ObjectAlignment.right);
expect(
ObjectAlignment.fromName('bottomleft'),
ObjectAlignment.bottomLeft,
);
expect(ObjectAlignment.fromName('bottom'), ObjectAlignment.bottom);
expect(
ObjectAlignment.fromName('bottomright'),
ObjectAlignment.bottomRight,
);
});
});
}
4 changes: 3 additions & 1 deletion packages/tiled/test/tileset_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ void main() {
test(
'first objectgroup object = ellipsis',
() => expect(
((tileset.tiles.first.objectGroup as ObjectGroup?)!.objects.first)
(tileset.tiles.first.objectGroup as ObjectGroup?)!
.objects
.first
.isEllipse,
true,
),
Expand Down
7 changes: 7 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: tiled_workspace

environment:
sdk: ">=3.0.0 <4.0.0"

dev_dependencies:
melos: ^3.0.0

0 comments on commit 628f1f6

Please sign in to comment.