diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e777b8..35ca071 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,30 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +## 2023-12-10 + +### Changes + +--- + +Packages with breaking changes: + + - There are no breaking changes in this release. + +Packages with other changes: + + - [`tiled` - `v0.10.2`](#tiled---v0102) + +--- + +#### `tiled` - `v0.10.2` + + - **FIX**: `ObjectAlignment` enum names ([#74](https://github.com/flame-engine/tiled.dart/issues/74)). ([628f1f6c](https://github.com/flame-engine/tiled.dart/commit/628f1f6cc89f6dd9b0a9cadcdd619549cf180e35)) + - **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)) + - **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)) + - **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)) + + ## 2022-11-26 ### Changes diff --git a/packages/tiled/CHANGELOG.md b/packages/tiled/CHANGELOG.md index 5d7dc91..ef006ac 100644 --- a/packages/tiled/CHANGELOG.md +++ b/packages/tiled/CHANGELOG.md @@ -1,3 +1,10 @@ +## 0.10.2 + + - **FIX**: `ObjectAlignment` enum names ([#74](https://github.com/flame-engine/tiled.dart/issues/74)). ([628f1f6c](https://github.com/flame-engine/tiled.dart/commit/628f1f6cc89f6dd9b0a9cadcdd619549cf180e35)) + - **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)) + - **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)) + - **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)) + ## 0.10.1 - **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)) diff --git a/packages/tiled/lib/src/common/enums.dart b/packages/tiled/lib/src/common/enums.dart index 26dc44b..502598c 100644 --- a/packages/tiled/lib/src/common/enums.dart +++ b/packages/tiled/lib/src/common/enums.dart @@ -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 { @@ -390,11 +402,11 @@ 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: @@ -402,11 +414,11 @@ extension ObjectAlignmentExtension on ObjectAlignment { case ObjectAlignment.right: return 'right'; case ObjectAlignment.bottomLeft: - return 'bottomLeft'; + return 'bottomleft'; case ObjectAlignment.bottom: return 'bottom'; case ObjectAlignment.bottomRight: - return 'bottomRight'; + return 'bottomright'; } } } diff --git a/packages/tiled/lib/src/common/flips.dart b/packages/tiled/lib/src/common/flips.dart index 7691bec..13bc652 100644 --- a/packages/tiled/lib/src/common/flips.dart +++ b/packages/tiled/lib/src/common/flips.dart @@ -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, @@ -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, ); } } diff --git a/packages/tiled/lib/src/common/gid.dart b/packages/tiled/lib/src/common/gid.dart index 69fdd34..5eccd21 100644 --- a/packages/tiled/lib/src/common/gid.dart +++ b/packages/tiled/lib/src/common/gid.dart @@ -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); } diff --git a/packages/tiled/lib/src/layer.dart b/packages/tiled/lib/src/layer.dart index 1169251..37341c4 100644 --- a/packages/tiled/lib/src/layer.dart +++ b/packages/tiled/lib/src/layer.dart @@ -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, @@ -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; }, @@ -380,8 +380,10 @@ class TileLayer extends Layer { List>? tileData; TileLayer({ - super.id, required super.name, + required this.width, + required this.height, + super.id, super.class_, super.x, super.y, @@ -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, @@ -441,8 +441,9 @@ class ObjectGroup extends Layer { ColorData color; ObjectGroup({ - super.id, required super.name, + required this.objects, + super.id, super.class_, super.x, super.y, @@ -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( @@ -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, @@ -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( @@ -518,8 +518,9 @@ class Group extends Layer { List layers; Group({ - super.id, required super.name, + required this.layers, + super.id, super.class_, super.x, super.y, @@ -534,7 +535,6 @@ class Group extends Layer { super.opacity, super.visible, super.properties, - required this.layers, }) : super( type: LayerType.imageLayer, ); diff --git a/packages/tiled/lib/src/tiled_map.dart b/packages/tiled/lib/src/tiled_map.dart index 19dbeac..0ce3500 100644 --- a/packages/tiled/lib/src/tiled_map.dart +++ b/packages/tiled/lib/src/tiled_map.dart @@ -91,20 +91,23 @@ class TiledMap { List editorSettings; CustomProperties properties; + // Cache the object by ID when accessed. + Map? _cachedObjects; + // only for hexagonal maps: int? hexSideLength; StaggerAxis? staggerAxis; 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, @@ -279,6 +282,27 @@ class TiledMap { throw ArgumentError('Layer $name not found'); } + /// Finds the [TiledObject] in this map with the unique [id]. + /// Objects have map wide unique IDs which are never reused. + /// https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#object + /// + /// This reads through a cached map of all the objects so it does not + /// need to loop through all the object layers each time. + /// + /// Returns null if not found. + TiledObject? objectById(int id) { + if (_cachedObjects == null) { + _cachedObjects = {}; + layers.whereType().forEach((objectGroup) { + for (final object in objectGroup.objects) { + _cachedObjects![object.id] = object; + } + }); + } + + return _cachedObjects?[id]; + } + Tileset tilesetByName(String name) { return tilesets.firstWhere( (element) => element.name == name, diff --git a/packages/tiled/lib/src/tileset/tileset.dart b/packages/tiled/lib/src/tileset/tileset.dart index 49d64f3..bc67500 100644 --- a/packages/tiled/lib/src/tileset/tileset.dart +++ b/packages/tiled/lib/src/tileset/tileset.dart @@ -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'); diff --git a/packages/tiled/pubspec.yaml b/packages/tiled/pubspec.yaml index a84f600..4b8fe87 100644 --- a/packages/tiled/pubspec.yaml +++ b/packages/tiled/pubspec.yaml @@ -1,5 +1,5 @@ name: tiled -version: 0.10.1 +version: 0.10.2 description: A Dart Tiled library. Parse your TMX files into useful representations. Compatible with Flame. homepage: https://github.com/flame-engine/tiled.dart diff --git a/packages/tiled/test/map_test.dart b/packages/tiled/test/map_test.dart index 2288cc5..b051f8c 100644 --- a/packages/tiled/test/map_test.dart +++ b/packages/tiled/test/map_test.dart @@ -330,7 +330,7 @@ void main() { image: const TiledImage(), ), ], - ) + ), ], ); }); @@ -380,4 +380,55 @@ void main() { expect(map.tilesetByName('Humans'), equals(tileset)); }); }); + + group('Map.objectById', () { + late TiledMap map; + setUp(() { + map = TiledMap( + width: 2, + height: 2, + tileWidth: 8, + tileHeight: 8, + layers: [ + TileLayer( + name: 'tile layer 1', + width: 2, + height: 2, + data: [1, 0, 2, 0], + ), + ObjectGroup( + name: 'object layer 1', + objects: [ + TiledObject(id: 1, name: 'object one'), + TiledObject(id: 5, name: 'object five'), + ], + ), + ], + tilesets: [ + Tileset( + name: 'TileSet_1', + image: const TiledImage(source: 'tileset_1.png'), + firstGid: 1, + columns: 1, + tileCount: 2, + tiles: [ + Tile(localId: 0), + Tile(localId: 1), + ], + ), + ], + ); + }); + + test('gets images only in use on each TileLayer', () { + final object1 = map.objectById(1); + expect(object1?.name, equals('object one')); + + final object5 = map.objectById(5); + expect(object5?.name, equals('object five')); + + final object3 = map.objectById(3); + expect(object3, equals(null)); + }); + }); } diff --git a/packages/tiled/test/object_alignment_test.dart b/packages/tiled/test/object_alignment_test.dart new file mode 100644 index 0000000..c65dc29 --- /dev/null +++ b/packages/tiled/test/object_alignment_test.dart @@ -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, + ); + }); + }); +} diff --git a/packages/tiled/test/tileset_test.dart b/packages/tiled/test/tileset_test.dart index d5bed4e..516fd97 100644 --- a/packages/tiled/test/tileset_test.dart +++ b/packages/tiled/test/tileset_test.dart @@ -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, ), diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 0000000..dd13865 --- /dev/null +++ b/pubspec.yaml @@ -0,0 +1,7 @@ +name: tiled_workspace + +environment: + sdk: ">=3.0.0 <4.0.0" + +dev_dependencies: + melos: ^3.0.0