mirror of
https://github.com/OMGeeky/pytiled_parser.git
synced 2025-12-26 17:02:28 +01:00
Add support for image tile sub-rect
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
BIN
tests/test_data/images/tile_05.png
Normal file
BIN
tests/test_data/images/tile_05.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
@@ -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",
|
||||
),
|
||||
},
|
||||
|
||||
@@ -106,6 +106,8 @@ EXPECTED = tiled_map.TiledMap(
|
||||
.resolve(),
|
||||
image_height=32,
|
||||
image_width=32,
|
||||
width=32,
|
||||
height=32
|
||||
)
|
||||
},
|
||||
tile_count=1,
|
||||
|
||||
@@ -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
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<tileset version="1.9" tiledversion="1.9.0" name="tileset" tilewidth="32" tileheight="32" tilecount="4" columns="0">
|
||||
<tileset version="1.9" tiledversion="1.9.1" name="tileset" tilewidth="32" tileheight="32" tilecount="5" columns="0">
|
||||
<grid orientation="orthogonal" width="1" height="1"/>
|
||||
<tile id="0" class="tile">
|
||||
<properties>
|
||||
@@ -34,4 +34,7 @@
|
||||
<tile id="3" class="tile">
|
||||
<image width="32" height="32" source="../../images/tile_04.png"/>
|
||||
</tile>
|
||||
<tile id="4" x="32" y="0" width="32" height="32">
|
||||
<image width="64" height="32" source="../../images/tile_05.png"/>
|
||||
</tile>
|
||||
</tileset>
|
||||
|
||||
Reference in New Issue
Block a user