Use abolute paths for images when loading an external tileset

This commit is contained in:
Darren Eberly
2021-04-25 21:08:30 -04:00
parent 2d3d121c52
commit 4335cb417e
2 changed files with 16 additions and 7 deletions

View File

@@ -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

View File

@@ -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:
@@ -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"]
@@ -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:
@@ -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"]
@@ -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