Skip to content

Commit ab5611f

Browse files
author
benni-tec
committed
Merge branch 'main' into #69-dart-sdk
# Conflicts: # packages/tiled/pubspec.yaml
2 parents 6a8e362 + e297f55 commit ab5611f

File tree

13 files changed

+202
-41
lines changed

13 files changed

+202
-41
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@
33
All notable changes to this project will be documented in this file.
44
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
55

6+
## 2023-12-10
7+
8+
### Changes
9+
10+
---
11+
12+
Packages with breaking changes:
13+
14+
- There are no breaking changes in this release.
15+
16+
Packages with other changes:
17+
18+
- [`tiled` - `v0.10.2`](#tiled---v0102)
19+
20+
---
21+
22+
#### `tiled` - `v0.10.2`
23+
24+
- **FIX**: `ObjectAlignment` enum names ([#74](https://github.com/flame-engine/tiled.dart/issues/74)). ([628f1f6c](https://github.com/flame-engine/tiled.dart/commit/628f1f6cc89f6dd9b0a9cadcdd619549cf180e35))
25+
- **FEAT**: Adding a method to get any object in a map by its unique ID ([#75](https://github.com/flame-engine/tiled.dart/issues/75)). ([4faf43b4](https://github.com/flame-engine/tiled.dart/commit/4faf43b45002e19c8fdbf2af8dd09969bcf4781c))
26+
- **FEAT**: Omit TiledImage without source from TiledMap.tiledImages ([#68](https://github.com/flame-engine/tiled.dart/issues/68)). ([41c9439f](https://github.com/flame-engine/tiled.dart/commit/41c9439f9c0f1345b8b803b9b33d3a507e45bf1a))
27+
- **FEAT**: Add convenience method for getting images in each layer ([#66](https://github.com/flame-engine/tiled.dart/issues/66)). ([1d3043f7](https://github.com/flame-engine/tiled.dart/commit/1d3043f75dc59449e98c9f2f637141b8ac127508))
28+
29+
630
## 2022-11-26
731

832
### Changes

packages/tiled/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.10.2
2+
3+
- **FIX**: `ObjectAlignment` enum names ([#74](https://github.com/flame-engine/tiled.dart/issues/74)). ([628f1f6c](https://github.com/flame-engine/tiled.dart/commit/628f1f6cc89f6dd9b0a9cadcdd619549cf180e35))
4+
- **FEAT**: Adding a method to get any object in a map by its unique ID ([#75](https://github.com/flame-engine/tiled.dart/issues/75)). ([4faf43b4](https://github.com/flame-engine/tiled.dart/commit/4faf43b45002e19c8fdbf2af8dd09969bcf4781c))
5+
- **FEAT**: Omit TiledImage without source from TiledMap.tiledImages ([#68](https://github.com/flame-engine/tiled.dart/issues/68)). ([41c9439f](https://github.com/flame-engine/tiled.dart/commit/41c9439f9c0f1345b8b803b9b33d3a507e45bf1a))
6+
- **FEAT**: Add convenience method for getting images in each layer ([#66](https://github.com/flame-engine/tiled.dart/issues/66)). ([1d3043f7](https://github.com/flame-engine/tiled.dart/commit/1d3043f75dc59449e98c9f2f637141b8ac127508))
7+
18
## 0.10.1
29

310
- **FEAT**: Add `imageRect` for `Tile` ([#64](https://github.com/flame-engine/tiled.dart/issues/64)). ([33d99b70](https://github.com/flame-engine/tiled.dart/commit/33d99b70e9c0c9b11483d9a25abfc1375869c87f))

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
ColorData 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: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,23 @@ class TiledMap {
9191
List<EditorSetting> editorSettings;
9292
CustomProperties properties;
9393

94+
// Cache the object by ID when accessed.
95+
Map<int, TiledObject>? _cachedObjects;
96+
9497
// only for hexagonal maps:
9598
int? hexSideLength;
9699
StaggerAxis? staggerAxis;
97100
StaggerIndex? staggerIndex;
98101

99102
TiledMap({
100-
this.type = TileMapType.map,
101-
this.version = '1.0',
102-
this.tiledVersion,
103103
required this.width,
104104
required this.height,
105-
this.infinite = false,
106105
required this.tileWidth,
107106
required this.tileHeight,
107+
this.type = TileMapType.map,
108+
this.version = '1.0',
109+
this.tiledVersion,
110+
this.infinite = false,
108111
this.tilesets = const [],
109112
this.layers = const [],
110113
this.backgroundColorHex,
@@ -279,6 +282,27 @@ class TiledMap {
279282
throw ArgumentError('Layer $name not found');
280283
}
281284

285+
/// Finds the [TiledObject] in this map with the unique [id].
286+
/// Objects have map wide unique IDs which are never reused.
287+
/// https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#object
288+
///
289+
/// This reads through a cached map of all the objects so it does not
290+
/// need to loop through all the object layers each time.
291+
///
292+
/// Returns null if not found.
293+
TiledObject? objectById(int id) {
294+
if (_cachedObjects == null) {
295+
_cachedObjects = {};
296+
layers.whereType<ObjectGroup>().forEach((objectGroup) {
297+
for (final object in objectGroup.objects) {
298+
_cachedObjects![object.id] = object;
299+
}
300+
});
301+
}
302+
303+
return _cachedObjects?[id];
304+
}
305+
282306
Tileset tilesetByName(String name) {
283307
return tilesets.firstWhere(
284308
(element) => element.name == name,

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
@@ -1,5 +1,5 @@
11
name: tiled
2-
version: 0.10.1
2+
version: 0.10.2
33
description: A Dart Tiled library. Parse your TMX files into useful representations. Compatible with Flame.
44
homepage: https://github.com/flame-engine/tiled.dart
55

packages/tiled/test/map_test.dart

Lines changed: 52 additions & 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
});
@@ -380,4 +380,55 @@ void main() {
380380
expect(map.tilesetByName('Humans'), equals(tileset));
381381
});
382382
});
383+
384+
group('Map.objectById', () {
385+
late TiledMap map;
386+
setUp(() {
387+
map = TiledMap(
388+
width: 2,
389+
height: 2,
390+
tileWidth: 8,
391+
tileHeight: 8,
392+
layers: [
393+
TileLayer(
394+
name: 'tile layer 1',
395+
width: 2,
396+
height: 2,
397+
data: [1, 0, 2, 0],
398+
),
399+
ObjectGroup(
400+
name: 'object layer 1',
401+
objects: [
402+
TiledObject(id: 1, name: 'object one'),
403+
TiledObject(id: 5, name: 'object five'),
404+
],
405+
),
406+
],
407+
tilesets: [
408+
Tileset(
409+
name: 'TileSet_1',
410+
image: const TiledImage(source: 'tileset_1.png'),
411+
firstGid: 1,
412+
columns: 1,
413+
tileCount: 2,
414+
tiles: [
415+
Tile(localId: 0),
416+
Tile(localId: 1),
417+
],
418+
),
419+
],
420+
);
421+
});
422+
423+
test('gets images only in use on each TileLayer', () {
424+
final object1 = map.objectById(1);
425+
expect(object1?.name, equals('object one'));
426+
427+
final object5 = map.objectById(5);
428+
expect(object5?.name, equals('object five'));
429+
430+
final object3 = map.objectById(3);
431+
expect(object3, equals(null));
432+
});
433+
});
383434
}

0 commit comments

Comments
 (0)