diff --git a/pytiled_parser/parsers/json/tileset.py b/pytiled_parser/parsers/json/tileset.py index 1750b02..bbe9d7b 100644 --- a/pytiled_parser/parsers/json/tileset.py +++ b/pytiled_parser/parsers/json/tileset.py @@ -47,6 +47,10 @@ RawTile = TypedDict( "type": str, "properties": List[RawProperty], "objectgroup": RawLayer, + "x": int, + "y": int, + "width": int, + "height": int }, ) RawTile.__doc__ = """ @@ -191,11 +195,17 @@ def _parse_tile(raw_tile: RawTile, external_path: Optional[Path] = None) -> Tile # image is set, but these aren't, so the branches will never fully be hit. # However, leaving these checks in place is nice to prevent fatal errors on # a manually edited map that has an "incorrect" but not "unusable" structure + # + # We also set the width and height attributes here as the values for these in + # Tiled defaults to the same value as imagewidth and imageheight if no custom value + # is set. We then later load in the custom value if it exists. if raw_tile.get("imagewidth") is not None: # pragma: no cover tile.image_width = raw_tile["imagewidth"] + tile.width = tile.image_width if raw_tile.get("imageheight") is not None: # pragma: no cover tile.image_height = raw_tile["imageheight"] + tile.height = tile.image_height if raw_tile.get("type") is not None: tile.class_ = raw_tile["type"] @@ -203,6 +213,18 @@ def _parse_tile(raw_tile: RawTile, external_path: Optional[Path] = None) -> Tile if raw_tile.get("class") is not None: tile.class_ = raw_tile["class"] + if raw_tile.get("x") is not None: + tile.x = raw_tile["x"] + + if raw_tile.get("y") is not None: + tile.y = raw_tile["y"] + + if raw_tile.get("width") is not None: + tile.width = raw_tile["width"] + + if raw_tile.get("height") is not None: + tile.height = raw_tile["height"] + return tile diff --git a/pytiled_parser/parsers/tmx/tileset.py b/pytiled_parser/parsers/tmx/tileset.py index d77c713..0db9232 100644 --- a/pytiled_parser/parsers/tmx/tileset.py +++ b/pytiled_parser/parsers/tmx/tileset.py @@ -107,7 +107,21 @@ def _parse_tile(raw_tile: etree.Element, external_path: Optional[Path] = None) - tile.image = Path(image_element.attrib["source"]) tile.image_width = int(image_element.attrib["width"]) + tile.width = tile.image_width tile.image_height = int(image_element.attrib["height"]) + tile.height = tile.image_height + + if raw_tile.attrib.get("x") is not None: + tile.x = int(raw_tile.attrib["x"]) + + if raw_tile.attrib.get("y") is not None: + tile.y = int(raw_tile.attrib["y"]) + + if raw_tile.attrib.get("width") is not None: + tile.width = int(raw_tile.attrib["width"]) + + if raw_tile.attrib.get("height") is not None: + tile.height = int(raw_tile.attrib["height"]) return tile diff --git a/pytiled_parser/tileset.py b/pytiled_parser/tileset.py index 99557e6..8e153af 100644 --- a/pytiled_parser/tileset.py +++ b/pytiled_parser/tileset.py @@ -119,6 +119,10 @@ class Tile: id: int opacity: int = 1 + x: int = 0 + y: int = 0 + width: Optional[int] = None + height: Optional[int] = None class_: Optional[str] = None animation: Optional[List[Frame]] = None objects: Optional[layer.Layer] = None diff --git a/tests/test_data/images/tile_05.png b/tests/test_data/images/tile_05.png new file mode 100644 index 0000000..46f7225 Binary files /dev/null and b/tests/test_data/images/tile_05.png differ diff --git a/tests/test_data/map_tests/external_tileset_dif_dir/expected.py b/tests/test_data/map_tests/external_tileset_dif_dir/expected.py index 40d7945..02ddf04 100644 --- a/tests/test_data/map_tests/external_tileset_dif_dir/expected.py +++ b/tests/test_data/map_tests/external_tileset_dif_dir/expected.py @@ -105,6 +105,8 @@ EXPECTED = tiled_map.TiledMap( image_width=32, properties={"float property": 2.2}, class_="tile", + width=32, + height=32, ), 1: tileset.Tile( id=1, @@ -147,6 +149,8 @@ EXPECTED = tiled_map.TiledMap( ), ], ), + width=32, + height=32, properties={"string property": "testing"}, class_="tile", ), @@ -159,6 +163,8 @@ EXPECTED = tiled_map.TiledMap( image_width=32, properties={"bool property": True}, class_="tile", + width=32, + height=32, ), 3: tileset.Tile( id=3, @@ -167,6 +173,8 @@ EXPECTED = tiled_map.TiledMap( .resolve(), image_height=32, image_width=32, + width=32, + height=32, class_="tile", ), }, diff --git a/tests/test_data/map_tests/template/expected.py b/tests/test_data/map_tests/template/expected.py index cb227a5..a6c7aad 100644 --- a/tests/test_data/map_tests/template/expected.py +++ b/tests/test_data/map_tests/template/expected.py @@ -106,6 +106,8 @@ EXPECTED = tiled_map.TiledMap( .resolve(), image_height=32, image_width=32, + width=32, + height=32 ) }, tile_count=1, diff --git a/tests/test_data/tilesets/individual_images/expected.py b/tests/test_data/tilesets/individual_images/expected.py index 1d19ade..05c9f7e 100644 --- a/tests/test_data/tilesets/individual_images/expected.py +++ b/tests/test_data/tilesets/individual_images/expected.py @@ -7,12 +7,12 @@ EXPECTED = tileset.Tileset( margin=0, spacing=0, name="tileset", - tile_count=4, - tiled_version="1.6.0", + tile_count=5, + tiled_version="1.9.1", tile_height=32, tile_width=32, firstgid=1, - version="1.6", + version="1.9", type="tileset", grid=tileset.Grid(orientation="orthogonal", width=1, height=1), tiles={ @@ -29,6 +29,8 @@ EXPECTED = tileset.Tileset( image_width=32, properties={"float property": 2.2}, class_="tile", + width=32, + height=32, ), 1: tileset.Tile( id=1, @@ -67,6 +69,8 @@ EXPECTED = tileset.Tileset( ), properties={"string property": "testing"}, class_="tile", + width=32, + height=32, ), 2: tileset.Tile( id=2, @@ -75,6 +79,8 @@ EXPECTED = tileset.Tileset( image_width=32, properties={"bool property": True}, class_="tile", + width=32, + height=32, ), 3: tileset.Tile( id=3, @@ -82,6 +88,18 @@ EXPECTED = tileset.Tileset( image_height=32, image_width=32, class_="tile", + width=32, + height=32, ), + 4: tileset.Tile( + id=4, + image=Path("../../images/tile_05.png"), + image_height=32, + image_width=64, + x=32, + y=0, + width=32, + height=32 + ) }, ) diff --git a/tests/test_data/tilesets/individual_images/tileset.json b/tests/test_data/tilesets/individual_images/tileset.json index 3d7b412..2e79cad 100644 --- a/tests/test_data/tilesets/individual_images/tileset.json +++ b/tests/test_data/tilesets/individual_images/tileset.json @@ -8,8 +8,8 @@ "margin":0, "name":"tileset", "spacing":0, - "tilecount":4, - "tiledversion":"1.9.0", + "tilecount":5, + "tiledversion":"1.9.1", "tileheight":32, "tiles":[ { @@ -108,8 +108,18 @@ "image":"..\/..\/images\/tile_04.png", "imageheight":32, "imagewidth":32 + }, + { + "height":32, + "id":4, + "image":"..\/..\/images\/tile_05.png", + "imageheight":32, + "imagewidth":64, + "width":32, + "x":32, + "y":0 }], "tilewidth":32, "type":"tileset", - "version":"1.8" + "version":"1.9" } \ No newline at end of file diff --git a/tests/test_data/tilesets/individual_images/tileset.tsx b/tests/test_data/tilesets/individual_images/tileset.tsx index 3e8a0f9..1c48286 100644 --- a/tests/test_data/tilesets/individual_images/tileset.tsx +++ b/tests/test_data/tilesets/individual_images/tileset.tsx @@ -1,5 +1,5 @@ - + @@ -34,4 +34,7 @@ + + +