Skip to content
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

json support #18

Merged
merged 28 commits into from May 12, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -29,6 +29,17 @@ Load a TMX file into a string by any means, and then pass the string to an insta
final TileMap map = parser.parse(tmxBody);
```


Alternatively load a json file and use the json or tmx structure.

```dart
final String jsonBody = /* ... */;
final TileMapJsonParser parser = TileMapJsonParser();
final MapJson map = parser.parse(jsonBody);
// To use the tmx structure
final TileMap map = map.toTileMap();
```

## Credits

TMX support is work of @radicaled and we have got his code from [tmx.dart](https://github.com/radicaled/tmx.dart) lib.
257 changes: 257 additions & 0 deletions json_structure.puml
@@ -0,0 +1,257 @@
@startuml
luanpotter marked this conversation as resolved.
Show resolved Hide resolved
set namespaceSeparator ::

class "tiled::tiled.dart::MapJson" {
+String backgroundcolor
+int compressionlevel
+EditorsettingJson editorsettings
+int height
+int hexsidelength
+bool infinite
+List<LayerJson> layers
+int nextlayerid
+int nextobjectid
+String orientation
+List<PropertyJson> properties
+String renderorder
+String staggeraxis
+String staggerindex
+String tiledversion
+int tileheight
+List<TilesetJson> tilesets
+int tilewidth
+String type
+num version
+int width
+Map toJson()
}

"tiled::tiled.dart::MapJson" o-- "tiled::tiled.dart::EditorsettingJson"
"tiled::tiled.dart::MapJson" o-- "tiled::tiled.dart::LayerJson"
"tiled::tiled.dart::MapJson" o-- "tiled::tiled.dart::PropertyJson"
"tiled::tiled.dart::MapJson" o-- "tiled::tiled.dart::TilesetJson"

class "tiled::tiled.dart::EditorsettingJson" {
+ChunkJson chunksize
+ExportJson export
+Map toJson()
}

"tiled::tiled.dart::EditorsettingJson" o-- "tiled::tiled.dart::ChunkJson"
"tiled::tiled.dart::EditorsettingJson" o-- "tiled::tiled.dart::ExportJson"

class "tiled::tiled.dart::ChunkJson" {
+String data
+int height
+int width
+int x
+int y
+Map toJson()
}

class "tiled::tiled.dart::ExportJson" {
+String format
+String target
+Map toJson()
}

class "tiled::tiled.dart::LayerJson" {
+List<ChunkJson> chunks
+String compression
+List<int> data
+String draworder
+String encoding
+int height
+int id
+String image
+List<LayerJson> layers
+String name
+List<ObjectJson> objects
+double offsetx
+double offsety
+double opacity
+List<PropertyJson> properties
+int startx
+int starty
+String tintcolor
+String transparentcolor
+String type
+bool visible
+int width
+int x
+int y
+Map toJson()
}

"tiled::tiled.dart::LayerJson" o-- "tiled::tiled.dart::ChunkJson"
"tiled::tiled.dart::LayerJson" o-- "tiled::tiled.dart::LayerJson"
"tiled::tiled.dart::LayerJson" o-- "tiled::tiled.dart::ObjectJson"
"tiled::tiled.dart::LayerJson" o-- "tiled::tiled.dart::PropertyJson"


class "tiled::tiled.dart::ObjectJson" {
+bool ellipse
+int gid
+double height
+int id
+String name
+bool point
+List<PointJson> polygon
+List<PointJson> polyline
+List<PropertyJson> properties
+double rotation
+String template
+TextJson text
+String type
+bool visible
+double width
+double x
+double y
+Map toJson()
}

"tiled::tiled.dart::ObjectJson" o-- "tiled::tiled.dart::PointJson"
"tiled::tiled.dart::ObjectJson" o-- "tiled::tiled.dart::PropertyJson"
"tiled::tiled.dart::ObjectJson" o-- "tiled::tiled.dart::TextJson"

class "tiled::tiled.dart::PointJson" {
+num x
+num y
+Map toJson()
}

class "tiled::tiled.dart::TextJson" {
+bool bold
+String color
+String fontfamily
+String halign
+bool italic
+bool kerning
+int pixelsize
+bool strikeout
+String text
+bool underline
+String valign
+bool wrap
+Map toJson()
}

class "tiled::tiled.dart::PropertyJson" {
+String name
+String type
+String value
+Map toJson()
}

class "tiled::tiled.dart::TilesetJson" {
+String backgroundcolor
+int columns
+int firstgid
+GridJson grid
+String image
+int imageheight
+int imagewidth
+int margin
+String name
+String objectalignment
+List<PropertyJson> properties
+String source
+int spacing
+List<TerrainJson> terrains
+int tilecount
+String tiledversion
+int tileheight
+TileOffsetJson tileoffset
+List<TileJson> tiles
+int tilewidth
+String transparentcolor
+String type
+num version
+List<WangSetJson> wangsets
+Map toJson()
}

"tiled::tiled.dart::TilesetJson" o-- "tiled::tiled.dart::GridJson"
"tiled::tiled.dart::TilesetJson" o-- "tiled::tiled.dart::TileOffsetJson"
"tiled::tiled.dart::TilesetJson" o-- "tiled::tiled.dart::PropertyJson"
"tiled::tiled.dart::TilesetJson" o-- "tiled::tiled.dart::TerrainJson"
"tiled::tiled.dart::TilesetJson" o-- "tiled::tiled.dart::TileJson"
"tiled::tiled.dart::TilesetJson" o-- "tiled::tiled.dart::WangSetJson"

class "tiled::tiled.dart::GridJson" {
+int height
+String orientation
+int width
+Map toJson()
}

class "tiled::tiled.dart::TileOffsetJson" {
+int x
+int y
+Map toJson()
}

class "tiled::tiled.dart::FrameJson" {
+int duration
+int tileid
+Map toJson()
}

class "tiled::tiled.dart::TerrainJson" {
+String name
+List<PropertyJson> properties
+int tile
+Map toJson()
}
"tiled::tiled.dart::TerrainJson" o-- "tiled::tiled.dart::PropertyJson"

class "tiled::tiled.dart::WangSetJson" {
+List<WangColorJson> cornercolors
+List<WangColorJson> edgecolors
+String name
+List<PropertyJson> properties
+int tile
+List<WangTileJson> wangtiles
+Map toJson()
}
"tiled::tiled.dart::WangSetJson" o-- "tiled::tiled.dart::WangColorJson"
"tiled::tiled.dart::WangSetJson" o-- "tiled::tiled.dart::PropertyJson"
"tiled::tiled.dart::WangSetJson" o-- "tiled::tiled.dart::WangTileJson"

class "tiled::tiled.dart::WangColorJson" {
+String color
+String name
+double probability
+int tile
+Map toJson()
}

class "tiled::tiled.dart::WangTileJson" {
+bool dflip
+bool hflip
+int tile
+bool vflip
+List<int> wangid
+Map toJson()
}

class "tiled::tiled.dart::TileJson" {
+List<FrameJson> animation
+int id
+String image
+int imageheight
+int imagewidth
+LayerJson objectgroup
+double probability
+List<PropertyJson> properties
+List<int> terrain
+String type
+Map toJson()
}

"tiled::tiled.dart::TileJson" o-- "tiled::tiled.dart::Layer"
"tiled::tiled.dart::TileJson" o-- "tiled::tiled.dart::FrameJson"
"tiled::tiled.dart::TileJson" o-- "tiled::tiled.dart::PropertyJson"


@enduml
5 changes: 5 additions & 0 deletions lib/src/image.dart
Expand Up @@ -6,4 +6,9 @@ class Image {
num height;

Image(this.source, this.width, this.height);

@override
String toString() {
return 'Image{source: $source, width: $width, height: $height}';
}
}
6 changes: 6 additions & 0 deletions lib/src/layer.dart
Expand Up @@ -26,6 +26,12 @@ class Layer {

Layer(this.name, this.width, this.height);


@override
String toString() {
synchronisator marked this conversation as resolved.
Show resolved Hide resolved
return 'Layer{name: $name, width: $width, height: $height, visible: $visible, properties: $properties, map: ${map != null}, tileMatrix: $tileMatrix, tileFlips: $tileFlips, _tiles: $_tiles}';
}

Layer.fromXML(XmlElement element) {
if (element == null) {
throw 'arg "element" cannot be null';
Expand Down
7 changes: 7 additions & 0 deletions lib/src/object_group.dart
Expand Up @@ -12,6 +12,13 @@ class ObjectGroup {
Map<String, dynamic> properties = {};
List<TmxObject> tmxObjects = [];

ObjectGroup();
synchronisator marked this conversation as resolved.
Show resolved Hide resolved

@override
String toString() {
return 'ObjectGroup{name: $name, color: $color, opacity: $opacity, visible: $visible, map: ${map != null}, properties: $properties, tmxObjects: $tmxObjects}';
}

ObjectGroup.fromXML(XmlElement element) {
if (element == null) {
throw 'arg "element" cannot be null';
Expand Down
12 changes: 12 additions & 0 deletions lib/src/tile.dart
Expand Up @@ -19,9 +19,15 @@ class Tile {

Image _image;

set image(Image value) {
_image = value;
}

Image get image {
if (_image == null) {
return tileset.image;
} else {

}
return _image;
}
Expand All @@ -47,6 +53,12 @@ class Tile {
_image = tileset.tileImage[gid];
}


@override
String toString() {
return 'Tile{tileId: $tileId, tileset: ${tileset != null}, gid: $gid, width: $width, height: $height, spacing: $spacing, margin: $margin, flips: $flips, properties: $properties, _image: $_image, x: $x, y: $y, px: $px, py: $py}';
}

Tile.emptyTile() {
gid = 0;
}
Expand Down
6 changes: 6 additions & 0 deletions lib/src/tile_map.dart
Expand Up @@ -10,6 +10,12 @@ class TileMap {
List<ObjectGroup> objectGroups = [];
Map<String, dynamic> properties = {};


@override
String toString() {
return 'TileMap{tileWidth: $tileWidth, tileHeight: $tileHeight, width: $width, height: $height, tilesets: $tilesets, layers: $layers, objectGroups: $objectGroups, properties: $properties}';
}

/// Retrieve a tile based on its GID
///
/// GID is 1-based
Expand Down