Skip to content

Development->Master #31

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

Merged
merged 2 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pytiled_parser/tiled_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ def parse_map(file: Path) -> TiledMap:
for raw_tileset in raw_tilesets:
if raw_tileset.get("source") is not None:
# Is an external Tileset
with open(parent_dir / raw_tileset["source"]) as raw_tileset_file:
tileset_path = Path(parent_dir / raw_tileset["source"])
with open(tileset_path) as raw_tileset_file:
tilesets[raw_tileset["firstgid"]] = tileset.cast(
json.load(raw_tileset_file)
json.load(raw_tileset_file), external_path=tileset_path.parent
)
else:
# Is an embedded Tileset
Expand Down
18 changes: 13 additions & 5 deletions pytiled_parser/tileset.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def _cast_terrain(raw_terrain: RawTerrain) -> Terrain:
)


def _cast_tile(raw_tile: RawTile) -> Tile:
def _cast_tile(raw_tile: RawTile, external_path: Optional[Path] = None) -> Tile:
"""Cast the raw_tile to a Tile object.

Args:
Expand All @@ -312,7 +312,10 @@ def _cast_tile(raw_tile: RawTile) -> Tile:
tile.properties = properties_.cast(raw_tile["properties"])

if raw_tile.get("image") is not None:
tile.image = Path(raw_tile["image"])
if external_path:
tile.image = Path(external_path / raw_tile["image"]).absolute().resolve()
else:
tile.image = Path(raw_tile["image"])

if raw_tile.get("imagewidth") is not None:
tile.image_width = raw_tile["imagewidth"]
Expand Down Expand Up @@ -353,7 +356,7 @@ def _cast_grid(raw_grid: RawGrid) -> Grid:
)


def cast(raw_tileset: RawTileSet) -> Tileset:
def cast(raw_tileset: RawTileSet, external_path: Optional[Path] = None) -> Tileset:
"""Cast the raw tileset into a pytiled_parser type

Args:
Expand Down Expand Up @@ -383,7 +386,12 @@ def cast(raw_tileset: RawTileSet) -> Tileset:
tileset.tiled_version = raw_tileset["tiledversion"]

if raw_tileset.get("image") is not None:
tileset.image = Path(raw_tileset["image"])
if external_path:
tileset.image = (
Path(external_path / raw_tileset["image"]).absolute().resolve()
)
else:
tileset.image = Path(raw_tileset["image"])

if raw_tileset.get("imagewidth") is not None:
tileset.image_width = raw_tileset["imagewidth"]
Expand Down Expand Up @@ -418,7 +426,7 @@ def cast(raw_tileset: RawTileSet) -> Tileset:
if raw_tileset.get("tiles") is not None:
tiles = {}
for raw_tile in raw_tileset["tiles"]:
tiles[raw_tile["id"]] = _cast_tile(raw_tile)
tiles[raw_tile["id"]] = _cast_tile(raw_tile, external_path=external_path)
tileset.tiles = tiles

return tileset
182 changes: 182 additions & 0 deletions tests/test_data/map_tests/external_tileset_dif_dir/expected.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
from pathlib import Path

from pytiled_parser import common_types, layer, tiled_map, tiled_object, tileset

EXPECTED = tiled_map.TiledMap(
infinite=False,
map_size=common_types.Size(8, 6),
next_layer_id=3,
next_object_id=1,
orientation="orthogonal",
render_order="right-down",
tiled_version="1.5.0",
tile_size=common_types.Size(32, 32),
version=1.5,
background_color=common_types.Color(255, 0, 4, 255),
layers=[
layer.TileLayer(
name="Layer 1",
opacity=1,
visible=True,
id=2,
size=common_types.Size(8, 6),
data=[
[4, 3, 2, 1, 0, 0, 0, 0],
[
0,
0,
0,
0,
0,
0,
0,
0,
],
[
0,
0,
0,
0,
0,
0,
0,
0,
],
[
0,
0,
0,
0,
0,
0,
0,
0,
],
[
0,
0,
0,
0,
0,
0,
0,
0,
],
[
0,
0,
0,
0,
0,
0,
0,
0,
],
],
),
],
tilesets={
1: tileset.Tileset(
columns=0,
margin=0,
spacing=0,
name="tileset",
tile_count=4,
tiled_version="1.3.5",
tile_height=32,
tile_width=32,
version=1.2,
type="tileset",
grid=tileset.Grid(orientation="orthogonal", width=1, height=1),
tiles={
0: tileset.Tile(
animation=[
tileset.Frame(duration=100, tile_id=0),
tileset.Frame(duration=100, tile_id=1),
tileset.Frame(duration=100, tile_id=2),
tileset.Frame(duration=100, tile_id=3),
],
id=0,
image=Path(Path(__file__).parent / "../../images/tile_01.png")
.absolute()
.resolve(),
image_height=32,
image_width=32,
properties={"float property": 2.2},
type="tile",
),
1: tileset.Tile(
id=1,
image=Path(Path(__file__).parent / "../../images/tile_02.png")
.absolute()
.resolve(),
image_height=32,
image_width=32,
objects=layer.ObjectLayer(
name="",
opacity=1,
visible=True,
draw_order="index",
tiled_objects=[
tiled_object.Rectangle(
id=2,
name="",
size=common_types.Size(
14.4766410408043, 13.7196924896511
),
rotation=0,
type="",
visible=True,
coordinates=common_types.OrderedPair(
13.4358367829687, 13.5304553518628
),
),
tiled_object.Ellipse(
id=3,
name="",
size=common_types.Size(
14.287403903016, 11.070372560615
),
rotation=0,
type="",
visible=True,
coordinates=common_types.OrderedPair(
13.8143110585452, 1.98698994677705
),
),
],
),
properties={"string property": "testing"},
type="tile",
),
2: tileset.Tile(
id=2,
image=Path(Path(__file__).parent / "../../images/tile_03.png")
.absolute()
.resolve(),
image_height=32,
image_width=32,
properties={"bool property": True},
type="tile",
),
3: tileset.Tile(
id=3,
image=Path(Path(__file__).parent / "../../images/tile_04.png")
.absolute()
.resolve(),
image_height=32,
image_width=32,
type="tile",
),
},
)
},
properties={
"bool property - true": True,
"color property": common_types.Color(255, 73, 252, 255),
"file property": Path("../../../../../../var/log/syslog"),
"float property": 1.23456789,
"int property": 13,
"string property": "Hello, World!!",
},
)
66 changes: 66 additions & 0 deletions tests/test_data/map_tests/external_tileset_dif_dir/map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{ "backgroundcolor":"#ff0004",
"compressionlevel":0,
"height":6,
"infinite":false,
"layers":[
{
"compression":"zlib",
"data":"eAFjYWBgYAZiJiBmBOKhBgAIGAAL",
"encoding":"base64",
"height":6,
"id":2,
"name":"Layer 1",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":8,
"x":0,
"y":0
}],
"nextlayerid":3,
"nextobjectid":1,
"orientation":"orthogonal",
"properties":[
{
"name":"bool property - true",
"type":"bool",
"value":true
},
{
"name":"color property",
"type":"color",
"value":"#ff49fcff"
},
{
"name":"file property",
"type":"file",
"value":"..\/..\/..\/..\/..\/..\/var\/log\/syslog"
},
{
"name":"float property",
"type":"float",
"value":1.23456789
},
{
"name":"int property",
"type":"int",
"value":13
},
{
"name":"string property",
"type":"string",
"value":"Hello, World!!"
}],
"renderorder":"right-down",
"tiledversion":"1.5.0",
"tileheight":32,
"tilesets":[
{
"firstgid":1,
"source":"tileset\/tileset.json"
}],
"tilewidth":32,
"type":"map",
"version":1.5,
"width":8
}
Loading