Skip to content

Commit e64d512

Browse files
author
benni-tec
committed
Merge branch 'flame-engine#70-tsx-provider'
2 parents e389a08 + f8658e1 commit e64d512

File tree

5 files changed

+61
-12
lines changed

5 files changed

+61
-12
lines changed

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ Load a TMX file into a string by any means, and then pass the string to TileMapP
2929
final TiledMap mapTmx = TileMapParser.parseTmx(tmxBody);
3030
```
3131

32-
If your tmx file includes a external tsx reference, you have to add a CustomParser
32+
If your tmx file includes a external tsx reference, you have to add a CustomParser. This can either be done by using extending the TsxProviderBase, which can match multiple files, or by extending TsxProvider, which only matches on file by its name.
3333
```dart
34-
class CustomTsxProvider extends TsxProvider {
34+
class MultipleTsxProvider extends TsxProviderBase {
35+
@override
36+
bool checkProvidable(String filename) => ["external1.tsx", "external2.tsx"].contains(filename);
37+
38+
@override
39+
Parser? getCachedSource(String filename) => null;
40+
3541
@override
3642
Parser getSource(String fileName) {
3743
final xml = File(fileName).readAsStringSync();
@@ -40,6 +46,23 @@ class CustomTsxProvider extends TsxProvider {
4046
}
4147
}
4248
```
49+
50+
```dart
51+
class SingleTsxProvider extends TsxProvider {
52+
@override
53+
String get filename => "external.tsx";
54+
55+
@override
56+
Parser? getCachedSource() => null;
57+
58+
@override
59+
Parser getSource(String _) {
60+
final xml = File(filename).readAsStringSync();
61+
final node = XmlDocument.parse(xml).rootElement;
62+
return XmlParser(node);
63+
}
64+
}
65+
```
4366
And use it in the parseTmx method
4467
```dart
4568
final String tmxBody = /* ... */;

packages/tiled/lib/src/tile_map_parser.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TileMapParser {
1010
///
1111
/// Accepts an optional list of external TsxProviders for external tilesets
1212
/// referenced in the map file.
13-
static TiledMap parseTmx(String xml, {List<TsxProvider>? tsxList}) {
13+
static TiledMap parseTmx(String xml, {List<TsxProviderBase>? tsxList}) {
1414
final xmlElement = XmlDocument.parse(xml).rootElement;
1515
if (xmlElement.name.local != 'map') {
1616
throw 'XML is not in TMX format';

packages/tiled/lib/src/tiled_map.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ class TiledMap {
286286
);
287287
}
288288

289-
factory TiledMap.parse(Parser parser, {List<TsxProvider>? tsxList}) {
289+
factory TiledMap.parse(Parser parser, {List<TsxProviderBase>? tsxList}) {
290290
final backgroundColorHex = parser.getStringOrNull('backgroundcolor');
291291
final backgroundColor = parser.getColorOrNull('backgroundcolor');
292292
final compressionLevel = parser.getInt('compressionlevel', defaults: -1);
@@ -317,7 +317,7 @@ class TiledMap {
317317
return Tileset.parse(tilesetData);
318318
}
319319
final matchingTsx = tsxList.where(
320-
(tsx) => tsx.filename == tilesetSource,
320+
(tsx) => tsx.checkProvidable(tilesetSource),
321321
);
322322

323323
return Tileset.parse(

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class Tileset {
103103
tileCount = this.tiles.length;
104104
}
105105

106-
factory Tileset.parse(Parser parser, {TsxProvider? tsx}) {
106+
factory Tileset.parse(Parser parser, {TsxProviderBase? tsx}) {
107107
final backgroundColor = parser.getStringOrNull('backgroundcolor');
108108
final columns = parser.getIntOrNull('columns');
109109
final firstGid = parser.getIntOrNull('firstgid');
@@ -169,10 +169,10 @@ class Tileset {
169169
return result;
170170
}
171171

172-
void _checkIfExternalTsx(String? source, TsxProvider? tsx) {
172+
void _checkIfExternalTsx(String? source, TsxProviderBase? tsx) {
173173
if (tsx != null && source != null) {
174174
final tileset = Tileset.parse(
175-
tsx.getCachedSource() ?? tsx.getSource(source),
175+
tsx.getCachedSourceBase(source) ?? tsx.getSourceBase(source),
176176
);
177177
// Copy attributes if not null
178178
backgroundColor = tileset.backgroundColor ?? backgroundColor;

packages/tiled/lib/src/tsx_provider.dart

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,44 @@
11
part of tiled;
22

33
/// abstract class to be implemented for an external tileset data provider.
4-
abstract class TsxProvider {
4+
abstract class TsxProviderBase {
5+
/// Given a filename this function should check whether this Provider
6+
/// can provide this source.
7+
bool checkProvidable(String filename);
8+
9+
/// Retrieves the external tileset data given the tileset filename.
10+
Parser getSourceBase(String filename);
11+
12+
/// Used when provider implementations cache the data. Returns the cached
13+
/// data for the external tileset by filename.
14+
Parser? getCachedSourceBase(String filename);
15+
}
16+
17+
/// abstract convenience class to be implemented for an external tileset data
18+
/// provider, which only provides one file and can therefore be matched by the
19+
/// filename alone.
20+
abstract class TsxProvider extends TsxProviderBase {
521
/// Unique filename for the tileset to be loaded. This should match the
6-
/// 'source' property in the map.tmx file.
22+
/// 'source' property in the map.tmx file. This is used to check if this
23+
/// Provider can provide a source!
724
String get filename;
825

9-
/// Retrieves the external tileset data given the tileset filename.
10-
Parser getSource(String filename);
26+
@override
27+
bool checkProvidable(String filename) => filename == this.filename;
1128

1229
/// Used when provider implementations cache the data. Returns the cached
1330
/// data for the exernal tileset.
1431
Parser? getCachedSource();
1532

33+
@override
34+
Parser? getCachedSourceBase(String _) => getCachedSource();
35+
36+
/// Retrieves the external tileset data given the tileset filename.
37+
Parser getSource(String filename);
38+
39+
@override
40+
Parser getSourceBase(String filename) => getSource(filename);
41+
1642
/// Parses a file returning a [TsxProvider].
1743
static Future<TsxProvider> parse(String key) {
1844
throw UnimplementedError();

0 commit comments

Comments
 (0)