-
-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: TsxProviderBase with test function #72
base: main
Are you sure you want to change the base?
Changes from 2 commits
f8658e1
51ba2a7
6bf2527
838bb52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,9 +29,15 @@ Load a TMX file into a string by any means, and then pass the string to TileMapP | |
final TiledMap mapTmx = TileMapParser.parseTmx(tmxBody); | ||
``` | ||
|
||
If your tmx file includes a external tsx reference, you have to add a CustomParser | ||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like we don't have the markdown linter activated here, the max line length on markdown should be 100. |
||
```dart | ||
class CustomTsxProvider extends TsxProvider { | ||
class MultipleTsxProvider extends TsxProviderBase { | ||
@override | ||
bool checkProvidable(String filename) => ["external1.tsx", "external2.tsx"].contains(filename); | ||
|
||
@override | ||
Parser? getCachedSource(String filename) => null; | ||
|
||
@override | ||
Parser getSource(String fileName) { | ||
final xml = File(fileName).readAsStringSync(); | ||
|
@@ -40,10 +46,27 @@ class CustomTsxProvider extends TsxProvider { | |
} | ||
} | ||
``` | ||
And use it in the parseTmx method | ||
|
||
```dart | ||
class SingleTsxProvider extends TsxProvider { | ||
@override | ||
String get filename => "external.tsx"; | ||
|
||
@override | ||
Parser? getCachedSource() => null; | ||
|
||
@override | ||
Parser getSource(String _) { | ||
final xml = File(filename).readAsStringSync(); | ||
final node = XmlDocument.parse(xml).rootElement; | ||
return XmlParser(node); | ||
} | ||
} | ||
``` | ||
And use it in the parseTmx method. Keep in mind that the first TsxProvider that can provide a source is used! | ||
```dart | ||
final String tmxBody = /* ... */; | ||
final TiledMap mapTmx = TileMapParser.parseTmx(tmxBody, tsx: CustomTsxProvider()); | ||
final TiledMap mapTmx = TileMapParser.parseTmx(tmxBody, tsxProviders: [SingleTsxProvider(), MultipleTsxProvider()]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Split up line with trailing comma |
||
|
||
``` | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,44 @@ | ||
part of tiled; | ||
|
||
/// abstract class to be implemented for an external tileset data provider. | ||
abstract class TsxProvider { | ||
abstract class TsxProviderBase { | ||
/// Given a filename this function should check whether this Provider | ||
/// can provide this source. | ||
bool checkProvidable(String filename); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The word "check" in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes that does sound better 👍 |
||
|
||
/// Retrieves the external tileset data given the tileset filename. | ||
Parser getSourceBase(String filename); | ||
|
||
/// Used when provider implementations cache the data. Returns the cached | ||
/// data for the external tileset by filename. | ||
Parser? getCachedSourceBase(String filename); | ||
} | ||
|
||
/// abstract convenience class to be implemented for an external tileset data | ||
/// provider, which only provides one file and can therefore be matched by the | ||
/// filename alone. | ||
abstract class TsxProvider extends TsxProviderBase { | ||
/// Unique filename for the tileset to be loaded. This should match the | ||
/// 'source' property in the map.tmx file. | ||
/// 'source' property in the map.tmx file. This is used to check if this | ||
/// Provider can provide a source! | ||
String get filename; | ||
|
||
/// Retrieves the external tileset data given the tileset filename. | ||
Parser getSource(String filename); | ||
@override | ||
bool checkProvidable(String filename) => filename == this.filename; | ||
|
||
/// Used when provider implementations cache the data. Returns the cached | ||
/// data for the exernal tileset. | ||
Parser? getCachedSource(); | ||
|
||
@override | ||
Parser? getCachedSourceBase(String _) => getCachedSource(); | ||
|
||
/// Retrieves the external tileset data given the tileset filename. | ||
Parser getSource(String filename); | ||
|
||
@override | ||
Parser getSourceBase(String filename) => getSource(filename); | ||
|
||
/// Parses a file returning a [TsxProvider]. | ||
static Future<TsxProvider> parse(String key) { | ||
throw UnimplementedError(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops missed that on 😅