Skip to content

Commit 58deab5

Browse files
committed
Add support for image tile sub-rect
1 parent 95ed454 commit 58deab5

File tree

9 files changed

+88
-7
lines changed

9 files changed

+88
-7
lines changed

pytiled_parser/parsers/json/tileset.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
"type": str,
4848
"properties": List[RawProperty],
4949
"objectgroup": RawLayer,
50+
"x": int,
51+
"y": int,
52+
"width": int,
53+
"height": int
5054
},
5155
)
5256
RawTile.__doc__ = """
@@ -191,18 +195,36 @@ def _parse_tile(raw_tile: RawTile, external_path: Optional[Path] = None) -> Tile
191195
# image is set, but these aren't, so the branches will never fully be hit.
192196
# However, leaving these checks in place is nice to prevent fatal errors on
193197
# a manually edited map that has an "incorrect" but not "unusable" structure
198+
#
199+
# We also set the width and height attributes here as the values for these in
200+
# Tiled defaults to the same value as imagewidth and imageheight if no custom value
201+
# is set. We then later load in the custom value if it exists.
194202
if raw_tile.get("imagewidth") is not None: # pragma: no cover
195203
tile.image_width = raw_tile["imagewidth"]
204+
tile.width = tile.image_width
196205

197206
if raw_tile.get("imageheight") is not None: # pragma: no cover
198207
tile.image_height = raw_tile["imageheight"]
208+
tile.height = tile.image_height
199209

200210
if raw_tile.get("type") is not None:
201211
tile.class_ = raw_tile["type"]
202212

203213
if raw_tile.get("class") is not None:
204214
tile.class_ = raw_tile["class"]
205215

216+
if raw_tile.get("x") is not None:
217+
tile.x = raw_tile["x"]
218+
219+
if raw_tile.get("y") is not None:
220+
tile.y = raw_tile["y"]
221+
222+
if raw_tile.get("width") is not None:
223+
tile.width = raw_tile["width"]
224+
225+
if raw_tile.get("height") is not None:
226+
tile.height = raw_tile["height"]
227+
206228
return tile
207229

208230

pytiled_parser/parsers/tmx/tileset.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,21 @@ def _parse_tile(raw_tile: etree.Element, external_path: Optional[Path] = None) -
107107
tile.image = Path(image_element.attrib["source"])
108108

109109
tile.image_width = int(image_element.attrib["width"])
110+
tile.width = tile.image_width
110111
tile.image_height = int(image_element.attrib["height"])
112+
tile.height = tile.image_height
113+
114+
if raw_tile.attrib.get("x") is not None:
115+
tile.x = int(raw_tile.attrib["x"])
116+
117+
if raw_tile.attrib.get("y") is not None:
118+
tile.y = int(raw_tile.attrib["y"])
119+
120+
if raw_tile.attrib.get("width") is not None:
121+
tile.width = int(raw_tile.attrib["width"])
122+
123+
if raw_tile.attrib.get("height") is not None:
124+
tile.height = int(raw_tile.attrib["height"])
111125

112126
return tile
113127

pytiled_parser/tileset.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ class Tile:
119119

120120
id: int
121121
opacity: int = 1
122+
x: int = 0
123+
y: int = 0
124+
width: Optional[int] = None
125+
height: Optional[int] = None
122126
class_: Optional[str] = None
123127
animation: Optional[List[Frame]] = None
124128
objects: Optional[layer.Layer] = None

tests/test_data/images/tile_05.png

14.4 KB
Loading

tests/test_data/map_tests/external_tileset_dif_dir/expected.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@
105105
image_width=32,
106106
properties={"float property": 2.2},
107107
class_="tile",
108+
width=32,
109+
height=32,
108110
),
109111
1: tileset.Tile(
110112
id=1,
@@ -147,6 +149,8 @@
147149
),
148150
],
149151
),
152+
width=32,
153+
height=32,
150154
properties={"string property": "testing"},
151155
class_="tile",
152156
),
@@ -159,6 +163,8 @@
159163
image_width=32,
160164
properties={"bool property": True},
161165
class_="tile",
166+
width=32,
167+
height=32,
162168
),
163169
3: tileset.Tile(
164170
id=3,
@@ -167,6 +173,8 @@
167173
.resolve(),
168174
image_height=32,
169175
image_width=32,
176+
width=32,
177+
height=32,
170178
class_="tile",
171179
),
172180
},

tests/test_data/map_tests/template/expected.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@
106106
.resolve(),
107107
image_height=32,
108108
image_width=32,
109+
width=32,
110+
height=32
109111
)
110112
},
111113
tile_count=1,

tests/test_data/tilesets/individual_images/expected.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
margin=0,
88
spacing=0,
99
name="tileset",
10-
tile_count=4,
11-
tiled_version="1.6.0",
10+
tile_count=5,
11+
tiled_version="1.9.1",
1212
tile_height=32,
1313
tile_width=32,
1414
firstgid=1,
15-
version="1.6",
15+
version="1.9",
1616
type="tileset",
1717
grid=tileset.Grid(orientation="orthogonal", width=1, height=1),
1818
tiles={
@@ -29,6 +29,8 @@
2929
image_width=32,
3030
properties={"float property": 2.2},
3131
class_="tile",
32+
width=32,
33+
height=32,
3234
),
3335
1: tileset.Tile(
3436
id=1,
@@ -67,6 +69,8 @@
6769
),
6870
properties={"string property": "testing"},
6971
class_="tile",
72+
width=32,
73+
height=32,
7074
),
7175
2: tileset.Tile(
7276
id=2,
@@ -75,13 +79,27 @@
7579
image_width=32,
7680
properties={"bool property": True},
7781
class_="tile",
82+
width=32,
83+
height=32,
7884
),
7985
3: tileset.Tile(
8086
id=3,
8187
image=Path("../../images/tile_04.png"),
8288
image_height=32,
8389
image_width=32,
8490
class_="tile",
91+
width=32,
92+
height=32,
8593
),
94+
4: tileset.Tile(
95+
id=4,
96+
image=Path("../../images/tile_05.png"),
97+
image_height=32,
98+
image_width=64,
99+
x=32,
100+
y=0,
101+
width=32,
102+
height=32
103+
)
86104
},
87105
)

tests/test_data/tilesets/individual_images/tileset.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"margin":0,
99
"name":"tileset",
1010
"spacing":0,
11-
"tilecount":4,
12-
"tiledversion":"1.9.0",
11+
"tilecount":5,
12+
"tiledversion":"1.9.1",
1313
"tileheight":32,
1414
"tiles":[
1515
{
@@ -108,8 +108,18 @@
108108
"image":"..\/..\/images\/tile_04.png",
109109
"imageheight":32,
110110
"imagewidth":32
111+
},
112+
{
113+
"height":32,
114+
"id":4,
115+
"image":"..\/..\/images\/tile_05.png",
116+
"imageheight":32,
117+
"imagewidth":64,
118+
"width":32,
119+
"x":32,
120+
"y":0
111121
}],
112122
"tilewidth":32,
113123
"type":"tileset",
114-
"version":"1.8"
124+
"version":"1.9"
115125
}

tests/test_data/tilesets/individual_images/tileset.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<tileset version="1.9" tiledversion="1.9.0" name="tileset" tilewidth="32" tileheight="32" tilecount="4" columns="0">
2+
<tileset version="1.9" tiledversion="1.9.1" name="tileset" tilewidth="32" tileheight="32" tilecount="5" columns="0">
33
<grid orientation="orthogonal" width="1" height="1"/>
44
<tile id="0" class="tile">
55
<properties>
@@ -34,4 +34,7 @@
3434
<tile id="3" class="tile">
3535
<image width="32" height="32" source="../../images/tile_04.png"/>
3636
</tile>
37+
<tile id="4" x="32" y="0" width="32" height="32">
38+
<image width="64" height="32" source="../../images/tile_05.png"/>
39+
</tile>
3740
</tileset>

0 commit comments

Comments
 (0)