Skip to content

Commit 628f1f6

Browse files
authored
fix: ObjectAlignment enum names (#74)
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.
1 parent 41c9439 commit 628f1f6

File tree

11 files changed

+96
-41
lines changed

11 files changed

+96
-41
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,19 @@ enum ObjectAlignment {
381381
right,
382382
bottomLeft,
383383
bottom,
384-
bottomRight,
384+
bottomRight;
385+
386+
/// Returns the [ObjectAlignment] based on given [name].
387+
///
388+
/// Throws an [ArgumentError] if no match is found.
389+
static ObjectAlignment fromName(String name) {
390+
for (final value in ObjectAlignment.values) {
391+
if (value.name == name) {
392+
return value;
393+
}
394+
}
395+
throw ArgumentError.value(name, 'name', 'No enum value with that name');
396+
}
385397
}
386398

387399
extension ObjectAlignmentExtension on ObjectAlignment {
@@ -390,23 +402,23 @@ extension ObjectAlignmentExtension on ObjectAlignment {
390402
case ObjectAlignment.unspecified:
391403
return 'unspecified';
392404
case ObjectAlignment.topLeft:
393-
return 'topLeft';
405+
return 'topleft';
394406
case ObjectAlignment.top:
395407
return 'top';
396408
case ObjectAlignment.topRight:
397-
return 'topRight';
409+
return 'topright';
398410
case ObjectAlignment.left:
399411
return 'left';
400412
case ObjectAlignment.center:
401413
return 'center';
402414
case ObjectAlignment.right:
403415
return 'right';
404416
case ObjectAlignment.bottomLeft:
405-
return 'bottomLeft';
417+
return 'bottomleft';
406418
case ObjectAlignment.bottom:
407419
return 'bottom';
408420
case ObjectAlignment.bottomRight:
409-
return 'bottomRight';
421+
return 'bottomright';
410422
}
411423
}
412424
}

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ class Flips {
66
final bool diagonally;
77
final bool antiDiagonally;
88

9-
const Flips(
10-
this.horizontally,
11-
this.vertically,
12-
this.diagonally,
13-
this.antiDiagonally,
14-
);
9+
const Flips({
10+
required this.horizontally,
11+
required this.vertically,
12+
required this.diagonally,
13+
required this.antiDiagonally,
14+
});
1515

16-
const Flips.defaults() : this(false, false, false, false);
16+
const Flips.defaults()
17+
: this(
18+
horizontally: false,
19+
vertically: false,
20+
diagonally: false,
21+
antiDiagonally: false,
22+
);
1723

1824
Flips copyWith({
1925
bool? horizontally,
@@ -22,10 +28,10 @@ class Flips {
2228
bool? antiDiagonally,
2329
}) {
2430
return Flips(
25-
horizontally ?? this.horizontally,
26-
vertically ?? this.vertically,
27-
diagonally ?? this.diagonally,
28-
antiDiagonally ?? this.antiDiagonally,
31+
horizontally: horizontally ?? this.horizontally,
32+
vertically: vertically ?? this.vertically,
33+
diagonally: diagonally ?? this.diagonally,
34+
antiDiagonally: antiDiagonally ?? this.antiDiagonally,
2935
);
3036
}
3137
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ class Gid {
6060
flippedDiagonallyFlag |
6161
flippedAntiDiagonallyFlag);
6262
final flip = Flips(
63-
flippedHorizontally,
64-
flippedVertically,
65-
flippedDiagonally,
66-
flippedAntiDiagonally,
63+
horizontally: flippedHorizontally,
64+
vertically: flippedVertically,
65+
diagonally: flippedDiagonally,
66+
antiDiagonally: flippedAntiDiagonally,
6767
);
6868
return Gid(tileId, flip);
6969
}

packages/tiled/lib/src/layer.dart

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ abstract class Layer {
9393
CustomProperties properties;
9494

9595
Layer({
96-
this.id,
9796
required this.name,
9897
required this.type,
98+
this.id,
9999
this.class_,
100100
this.x = 0,
101101
this.y = 0,
@@ -298,7 +298,7 @@ abstract class Layer {
298298
}
299299
final text = xml.element.children.first;
300300
if (text is XmlText) {
301-
return text.text;
301+
return text.value;
302302
}
303303
return null;
304304
},
@@ -380,8 +380,10 @@ class TileLayer extends Layer {
380380
List<List<Gid>>? tileData;
381381

382382
TileLayer({
383-
super.id,
384383
required super.name,
384+
required this.width,
385+
required this.height,
386+
super.id,
385387
super.class_,
386388
super.x,
387389
super.y,
@@ -396,8 +398,6 @@ class TileLayer extends Layer {
396398
super.opacity,
397399
super.visible,
398400
super.properties,
399-
required this.width,
400-
required this.height,
401401
this.compression,
402402
this.encoding = FileEncoding.csv,
403403
this.chunks,
@@ -441,8 +441,9 @@ class ObjectGroup extends Layer {
441441
Color color;
442442

443443
ObjectGroup({
444-
super.id,
445444
required super.name,
445+
required this.objects,
446+
super.id,
446447
super.class_,
447448
super.x,
448449
super.y,
@@ -458,7 +459,6 @@ class ObjectGroup extends Layer {
458459
super.visible,
459460
super.properties,
460461
this.drawOrder = DrawOrder.topDown,
461-
required this.objects,
462462
this.colorHex = defaultColorHex,
463463
this.color = defaultColor,
464464
}) : super(
@@ -487,8 +487,11 @@ class ImageLayer extends Layer {
487487
bool repeatY;
488488

489489
ImageLayer({
490-
super.id,
491490
required super.name,
491+
required this.image,
492+
required this.repeatX,
493+
required this.repeatY,
494+
super.id,
492495
super.class_,
493496
super.x,
494497
super.y,
@@ -503,9 +506,6 @@ class ImageLayer extends Layer {
503506
super.opacity,
504507
super.visible,
505508
super.properties,
506-
required this.image,
507-
required this.repeatX,
508-
required this.repeatY,
509509
this.transparentColorHex,
510510
this.transparentColor,
511511
}) : super(
@@ -518,8 +518,9 @@ class Group extends Layer {
518518
List<Layer> layers;
519519

520520
Group({
521-
super.id,
522521
required super.name,
522+
required this.layers,
523+
super.id,
523524
super.class_,
524525
super.x,
525526
super.y,
@@ -534,7 +535,6 @@ class Group extends Layer {
534535
super.opacity,
535536
super.visible,
536537
super.properties,
537-
required this.layers,
538538
}) : super(
539539
type: LayerType.imageLayer,
540540
);

packages/tiled/lib/src/tiled_map.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ class TiledMap {
9797
StaggerIndex? staggerIndex;
9898

9999
TiledMap({
100-
this.type = TileMapType.map,
101-
this.version = '1.0',
102-
this.tiledVersion,
103100
required this.width,
104101
required this.height,
105-
this.infinite = false,
106102
required this.tileWidth,
107103
required this.tileHeight,
104+
this.type = TileMapType.map,
105+
this.version = '1.0',
106+
this.tiledVersion,
107+
this.infinite = false,
108108
this.tilesets = const [],
109109
this.layers = const [],
110110
this.backgroundColorHex,

packages/tiled/lib/src/tileset/tileset.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Tileset {
109109
final firstGid = parser.getIntOrNull('firstgid');
110110
final margin = parser.getInt('margin', defaults: 0);
111111
final name = parser.getStringOrNull('name');
112-
final objectAlignment = ObjectAlignment.values.byName(
112+
final objectAlignment = ObjectAlignment.fromName(
113113
parser.getString('objectalignment', defaults: 'unspecified'),
114114
);
115115
final source = parser.getStringOrNull('source');

packages/tiled/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ dependencies:
1515

1616
dev_dependencies:
1717
dartdoc: ^6.0.1
18-
flame_lint: ^0.2.0
18+
flame_lint: ^1.1.1
1919
flutter_test:
2020
sdk: flutter

packages/tiled/test/map_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ void main() {
330330
image: const TiledImage(),
331331
),
332332
],
333-
)
333+
),
334334
],
335335
);
336336
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:tiled/tiled.dart';
3+
4+
void main() {
5+
group('ObjectAlignment', () {
6+
test('ObjectAlignment.byName', () {
7+
expect(
8+
ObjectAlignment.fromName('unspecified'),
9+
ObjectAlignment.unspecified,
10+
);
11+
expect(ObjectAlignment.fromName('topleft'), ObjectAlignment.topLeft);
12+
expect(ObjectAlignment.fromName('top'), ObjectAlignment.top);
13+
expect(ObjectAlignment.fromName('topright'), ObjectAlignment.topRight);
14+
expect(ObjectAlignment.fromName('left'), ObjectAlignment.left);
15+
expect(ObjectAlignment.fromName('center'), ObjectAlignment.center);
16+
expect(ObjectAlignment.fromName('right'), ObjectAlignment.right);
17+
expect(
18+
ObjectAlignment.fromName('bottomleft'),
19+
ObjectAlignment.bottomLeft,
20+
);
21+
expect(ObjectAlignment.fromName('bottom'), ObjectAlignment.bottom);
22+
expect(
23+
ObjectAlignment.fromName('bottomright'),
24+
ObjectAlignment.bottomRight,
25+
);
26+
});
27+
});
28+
}

packages/tiled/test/tileset_test.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ void main() {
5757
test(
5858
'first objectgroup object = ellipsis',
5959
() => expect(
60-
((tileset.tiles.first.objectGroup as ObjectGroup?)!.objects.first)
60+
(tileset.tiles.first.objectGroup as ObjectGroup?)!
61+
.objects
62+
.first
6163
.isEllipse,
6264
true,
6365
),

0 commit comments

Comments
 (0)