Skip to content

Commit cd32f74

Browse files
committed
Add support for transformations in Tilesets. Closes #37
1 parent 55d545b commit cd32f74

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

pytiled_parser/tileset.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ class Frame(NamedTuple):
4646
duration: int
4747

4848

49+
@attr.s(auto_attribs=True, kw_only=True)
50+
class Transformations:
51+
"""Transformations Object.
52+
53+
This is used to store what transformations may be performed on Tiles
54+
within a tileset. (This is primarily used with wang sets, however could
55+
be used for any means a game wants really.)
56+
57+
Args:
58+
hflip: Allow horizontal flip?
59+
vflip: Allow vertical flip?
60+
rotate: Allow rotation?
61+
prefer_untransformed: Should untransformed tiles be preferred?
62+
"""
63+
64+
hflip: Optional[bool] = None
65+
vflip: Optional[bool] = None
66+
rotate: Optional[bool] = None
67+
prefer_untransformed: Optional[bool] = None
68+
69+
4970
@attr.s(auto_attribs=True, kw_only=True)
5071
class Tile:
5172
# FIXME: args
@@ -121,6 +142,8 @@ class Tileset:
121142
image_width: Optional[int] = None
122143
image_height: Optional[int] = None
123144

145+
transformations: Optional[Transformations] = None
146+
124147
firstgid: Optional[int] = None
125148
background_color: Optional[Color] = None
126149
tile_offset: Optional[OrderedPair] = None
@@ -145,6 +168,15 @@ class RawTileOffset(TypedDict):
145168
y: int
146169

147170

171+
class RawTransformations(TypedDict):
172+
""" The keys and their types that appear in a Transformations JSON Object."""
173+
174+
hflip: bool
175+
vflip: bool
176+
rotate: bool
177+
preferuntransformed: bool
178+
179+
148180
class RawTile(TypedDict):
149181
""" The keys and their types that appear in a Tile JSON Object."""
150182

@@ -189,7 +221,7 @@ class RawTileSet(TypedDict):
189221
tiles: List[RawTile]
190222
tilewidth: int
191223
transparentcolor: str
192-
type: str
224+
transformations: RawTransformations
193225
version: Union[str, float]
194226
wangsets: List[RawWangSet]
195227

@@ -262,6 +294,24 @@ def _cast_tile(raw_tile: RawTile, external_path: Optional[Path] = None) -> Tile:
262294
return tile
263295

264296

297+
def _cast_transformations(raw_transformations: RawTransformations) -> Transformations:
298+
"""Cast the raw_transformations to a Transformations object.
299+
300+
Args:
301+
raw_transformations: RawTransformations to be casted to a Transformations
302+
303+
Returns:
304+
Transformations: The Transformations created from the raw_transformations
305+
"""
306+
307+
return Transformations(
308+
hflip=raw_transformations["hflip"],
309+
vflip=raw_transformations["vflip"],
310+
rotate=raw_transformations["rotate"],
311+
prefer_untransformed=raw_transformations["preferuntransformed"],
312+
)
313+
314+
265315
def _cast_grid(raw_grid: RawGrid) -> Grid:
266316
"""Cast the raw_grid to a Grid object.
267317
@@ -356,4 +406,7 @@ def cast(raw_tileset: RawTileSet, external_path: Optional[Path] = None) -> Tiles
356406
wangsets.append(cast_wangset(raw_wangset))
357407
tileset.wang_sets = wangsets
358408

409+
if raw_tileset.get("transformations") is not None:
410+
tileset.transformations = _cast_transformations(raw_tileset["transformations"])
411+
359412
return tileset
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from pathlib import Path
2+
3+
from pytiled_parser import tileset
4+
5+
EXPECTED = tileset.Tileset(
6+
columns=8,
7+
image=Path("../../images/tmw_desert_spacing.png"),
8+
image_height=199,
9+
image_width=265,
10+
margin=1,
11+
spacing=1,
12+
name="tile_set_image",
13+
tile_count=48,
14+
tiled_version="1.6.0",
15+
tile_height=32,
16+
tile_width=32,
17+
transformations=tileset.Transformations(
18+
hflip=True,
19+
vflip=False,
20+
prefer_untransformed=False,
21+
rotate=False,
22+
),
23+
version="1.6",
24+
type="tileset",
25+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{ "columns":8,
2+
"image":"..\/..\/images\/tmw_desert_spacing.png",
3+
"imageheight":199,
4+
"imagewidth":265,
5+
"margin":1,
6+
"name":"tile_set_image",
7+
"spacing":1,
8+
"tilecount":48,
9+
"tiledversion":"1.6.0",
10+
"tileheight":32,
11+
"tilewidth":32,
12+
"transformations":
13+
{
14+
"hflip":true,
15+
"preferuntransformed":false,
16+
"rotate":false,
17+
"vflip":false
18+
},
19+
"type":"tileset",
20+
"version":"1.6"
21+
}

tests/test_tileset.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
TILE_SETS / "image_properties",
2121
TILE_SETS / "image_transparent_color",
2222
TILE_SETS / "image_tile_offset",
23+
TILE_SETS / "image_transformations",
2324
TILE_SETS / "individual_images",
2425
TILE_SETS / "terrain",
2526
]

0 commit comments

Comments
 (0)