Skip to content

Commit 2395dd3

Browse files
committed
More 2.0/tmx parsing work. TMX is nearly done
1 parent 65bfc2a commit 2395dd3

File tree

7 files changed

+93
-58
lines changed

7 files changed

+93
-58
lines changed

pytiled_parser/layer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class Layer:
3838
"""
3939

4040
name: str
41-
opacity: float
42-
visible: bool
41+
opacity: float = 1
42+
visible: bool = True
4343

4444
coordinates: OrderedPair = OrderedPair(0, 0)
4545
parallax_factor: OrderedPair = OrderedPair(1, 1)

pytiled_parser/parsers/tmx/layer.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,19 @@ def _parse_common(raw_layer: etree.Element) -> Layer:
144144
Returns:
145145
Layer: The attributes in common of all layer types
146146
"""
147+
if raw_layer.attrib.get("name") is None:
148+
raw_layer.attrib["name"] = ""
149+
147150
common = Layer(
148151
name=raw_layer.attrib["name"],
149-
opacity=float(raw_layer.attrib["opacity"]),
150-
visible=bool(int(raw_layer.attrib["visible"])),
151152
)
152153

154+
if raw_layer.attrib.get("opacity") is not None:
155+
common.opacity = float(raw_layer.attrib["opacity"])
156+
157+
if raw_layer.attrib.get("visible") is not None:
158+
common.visible = bool(int(raw_layer.attrib["visible"]))
159+
153160
if raw_layer.attrib.get("id") is not None:
154161
common.id = int(raw_layer.attrib["id"])
155162

@@ -159,7 +166,7 @@ def _parse_common(raw_layer: etree.Element) -> Layer:
159166
)
160167

161168
properties_element = raw_layer.find("./properties")
162-
if properties_element:
169+
if properties_element is not None:
163170
common.properties = parse_properties(properties_element)
164171

165172
parallax = [1.0, 1.0]
@@ -187,13 +194,15 @@ def _parse_tile_layer(raw_layer: etree.Element) -> TileLayer:
187194
Returns:
188195
TileLayer: The TileLayer created from raw_layer
189196
"""
197+
common = _parse_common(raw_layer).__dict__
198+
del common["size"]
190199
tile_layer = TileLayer(
191200
size=Size(int(raw_layer.attrib["width"]), int(raw_layer.attrib["height"])),
192-
**_parse_common(raw_layer).__dict__,
201+
**common,
193202
)
194203

195-
data_element = raw_layer.find("./data")
196-
if data_element:
204+
data_element = raw_layer.find("data")
205+
if data_element is not None:
197206
encoding = None
198207
if data_element.attrib.get("encoding") is not None:
199208
encoding = data_element.attrib["encoding"]
@@ -202,18 +211,17 @@ def _parse_tile_layer(raw_layer: etree.Element) -> TileLayer:
202211
if data_element.attrib.get("compression") is not None:
203212
compression = data_element.attrib["compression"]
204213

205-
raw_chunks = data_element.findall("./chunk")
206-
214+
raw_chunks = data_element.findall("chunk")
207215
if not raw_chunks:
208-
if encoding:
216+
if encoding and encoding != "csv":
209217
tile_layer.data = _decode_tile_layer_data(
210218
data=data_element.text, # type: ignore
211219
compression=compression,
212220
layer_width=int(raw_layer.attrib["width"]),
213221
)
214222
else:
215223
tile_layer.data = _convert_raw_tile_layer_data(
216-
[int(v.strip()) for v in data_element.text], # type: ignore
224+
[int(v.strip()) for v in data_element.text.split(",")], # type: ignore
217225
int(raw_layer.attrib["width"]),
218226
)
219227
else:
@@ -248,12 +256,16 @@ def _parse_object_layer(
248256
for object_ in raw_layer.findall("./object"):
249257
objects.append(parse_object(object_, parent_dir))
250258

251-
return ObjectLayer(
259+
object_layer = ObjectLayer(
252260
tiled_objects=objects,
253-
draw_order=raw_layer.attrib["draworder"],
254261
**_parse_common(raw_layer).__dict__,
255262
)
256263

264+
if raw_layer.attrib.get("draworder") is not None:
265+
object_layer.draw_order = raw_layer.attrib["draworder"]
266+
267+
return object_layer
268+
257269

258270
def _parse_image_layer(raw_layer: etree.Element) -> ImageLayer:
259271
"""Parse the raw_layer to an ImageLayer.
@@ -274,6 +286,8 @@ def _parse_image_layer(raw_layer: etree.Element) -> ImageLayer:
274286
if image_element.attrib.get("trans") is not None:
275287
transparent_color = parse_color(image_element.attrib["trans"])
276288

289+
common = _parse_common(raw_layer).__dict__
290+
del common["size"]
277291
return ImageLayer(
278292
image=source,
279293
size=Size(width, height),

pytiled_parser/parsers/tmx/properties.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ def parse(raw_properties: etree.Element) -> Properties:
1111
final: Properties = {}
1212
value: Property
1313

14-
for raw_property in raw_properties.findall("./property"):
15-
type_ = raw_property.attrib["type"]
14+
for raw_property in raw_properties.findall("property"):
15+
16+
type_ = raw_property.attrib.get("type")
1617
value_ = raw_property.attrib["value"]
1718
if type_ == "file":
1819
value = Path(value_)

pytiled_parser/parsers/tmx/tiled_map.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def parse(file: Path) -> TiledMap:
1818
Returns:
1919
TiledMap: A parsed TiledMap.
2020
"""
21+
print(file)
2122
with open(file) as map_file:
2223
raw_map = etree.parse(map_file).getroot()
2324

@@ -71,10 +72,10 @@ def parse(file: Path) -> TiledMap:
7172
for my_layer in layers:
7273
for tiled_object in my_layer.tiled_objects:
7374
if hasattr(tiled_object, "new_tileset"):
74-
if tiled_object.new_tileset:
75+
if tiled_object.new_tileset is not None:
7576
already_loaded = None
7677
for val in map_.tilesets.values():
77-
if val.name == tiled_object.new_tileset["name"]:
78+
if val.name == tiled_object.new_tileset.attrib["name"]:
7879
already_loaded = val
7980
break
8081

pytiled_parser/parsers/tmx/tiled_object.py

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,25 @@ def _parse_common(raw_object: etree.Element) -> TiledObject:
3232
coordinates=OrderedPair(
3333
float(raw_object.attrib["x"]), float(raw_object.attrib["y"])
3434
),
35-
visible=bool(int(raw_object.attrib["visible"])),
36-
size=Size(
37-
float(raw_object.attrib["width"]), float(raw_object.attrib["height"])
38-
),
39-
rotation=float(raw_object.attrib["rotation"]),
40-
name=raw_object.attrib["name"],
41-
type=raw_object.attrib["type"],
4235
)
4336

37+
if raw_object.attrib.get("width") is not None:
38+
common.size = Size(
39+
float(raw_object.attrib["width"]), float(raw_object.attrib["height"])
40+
)
41+
42+
if raw_object.attrib.get("visible") is not None:
43+
common.visible = bool(int(raw_object.attrib["visible"]))
44+
45+
if raw_object.attrib.get("rotation") is not None:
46+
common.rotation = float(raw_object.attrib["rotation"])
47+
48+
if raw_object.attrib.get("name") is not None:
49+
common.name = raw_object.attrib["name"]
50+
51+
if raw_object.attrib.get("type") is not None:
52+
common.type = raw_object.attrib["type"]
53+
4454
properties_element = raw_object.find("./properties")
4555
if properties_element:
4656
common.properties = parse_properties(properties_element)
@@ -94,9 +104,11 @@ def _parse_polygon(raw_object: etree.Element) -> Polygon:
94104
Polygon: The Polygon object created from the raw object
95105
"""
96106
polygon = []
97-
for raw_point in raw_object.attrib["points"].split(" "):
98-
point = raw_point.split(",")
99-
polygon.append(OrderedPair(float(point[0]), float(point[1])))
107+
polygon_element = raw_object.find("./polygon")
108+
if polygon_element is not None:
109+
for raw_point in polygon_element.attrib["points"].split(" "):
110+
point = raw_point.split(",")
111+
polygon.append(OrderedPair(float(point[0]), float(point[1])))
100112

101113
return Polygon(points=polygon, **_parse_common(raw_object).__dict__)
102114

@@ -204,23 +216,19 @@ def _get_parser(raw_object: etree.Element) -> Callable[[etree.Element], TiledObj
204216
Returns:
205217
Callable[[Element], Object]: The parser function.
206218
"""
207-
if raw_object.find("./ellipse"):
219+
if raw_object.find("./ellipse") is not None:
208220
return _parse_ellipse
209221

210-
if raw_object.find("./point"):
222+
if raw_object.find("./point") is not None:
211223
return _parse_point
212224

213-
if raw_object.attrib.get("gid"):
214-
# Only tile objects have the `gid` attribute
215-
return _parse_tile
216-
217-
if raw_object.find("./polygon"):
225+
if raw_object.find("./polygon") is not None:
218226
return _parse_polygon
219227

220-
if raw_object.find("./polyline"):
228+
if raw_object.find("./polyline") is not None:
221229
return _parse_polyline
222230

223-
if raw_object.find("./text"):
231+
if raw_object.find("./text") is not None:
224232
return _parse_text
225233

226234
# If it's none of the above, rectangle is the only one left.
@@ -263,13 +271,22 @@ def parse(raw_object: etree.Element, parent_dir: Optional[Path] = None) -> Tiled
263271
new_tileset_path = tileset_path.parent
264272

265273
new_object = template.find("./object")
266-
if raw_object.attrib.get("id") and new_object:
267-
new_object.attrib["id"] = raw_object.attrib["id"]
274+
if new_object is not None:
275+
if raw_object.attrib.get("id") is not None:
276+
new_object.attrib["id"] = raw_object.attrib["id"]
277+
278+
if raw_object.attrib.get("x") is not None:
279+
new_object.attrib["x"] = raw_object.attrib["x"]
280+
281+
if raw_object.attrib.get("y") is not None:
282+
new_object.attrib["y"] = raw_object.attrib["y"]
268283

269-
if new_object:
270284
raw_object = new_object
271285

286+
if raw_object.attrib.get("gid"):
287+
return _parse_tile(raw_object, new_tileset, new_tileset_path)
288+
272289
if raw_object.attrib.get("gid"):
273-
return _parse_tile(raw_object, new_tileset, new_tileset_path)
290+
return _parse_tile(raw_object)
274291

275292
return _get_parser(raw_object)(raw_object)

pytiled_parser/parsers/tmx/tileset.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,21 @@ def _parse_tile(raw_tile: etree.Element, external_path: Optional[Path] = None) -
7979
tile.type = raw_tile.attrib["type"]
8080

8181
animation_element = raw_tile.find("./animation")
82-
if animation_element:
82+
if animation_element is not None:
8383
tile.animation = []
8484
for raw_frame in animation_element.findall("./frame"):
8585
tile.animation.append(_parse_frame(raw_frame))
8686

8787
object_element = raw_tile.find("./objectgroup")
88-
if object_element:
88+
if object_element is not None:
8989
tile.objects = parse_layer(object_element)
9090

9191
properties_element = raw_tile.find("./properties")
92-
if properties_element:
92+
if properties_element is not None:
9393
tile.properties = parse_properties(properties_element)
9494

9595
image_element = raw_tile.find("./image")
96-
if image_element:
96+
if image_element is not None:
9797
if external_path:
9898
tile.image = (
9999
Path(external_path / image_element.attrib["source"])
@@ -120,8 +120,6 @@ def parse(
120120
tile_width=int(raw_tileset.attrib["tilewidth"]),
121121
tile_height=int(raw_tileset.attrib["tileheight"]),
122122
columns=int(raw_tileset.attrib["columns"]),
123-
spacing=int(raw_tileset.attrib["spacing"]),
124-
margin=int(raw_tileset.attrib["margin"]),
125123
firstgid=firstgid,
126124
)
127125

@@ -134,9 +132,14 @@ def parse(
134132
if raw_tileset.attrib.get("backgroundcolor") is not None:
135133
tileset.background_color = parse_color(raw_tileset.attrib["backgroundcolor"])
136134

135+
if raw_tileset.attrib.get("spacing") is not None:
136+
tileset.spacing = int(raw_tileset.attrib["spacing"])
137+
138+
if raw_tileset.attrib.get("margin") is not None:
139+
tileset.margin = int(raw_tileset.attrib["margin"])
140+
137141
image_element = raw_tileset.find("image")
138142
if image_element is not None:
139-
print("here")
140143
if external_path:
141144
tileset.image = (
142145
Path(external_path / image_element.attrib["source"])
@@ -156,36 +159,36 @@ def parse(
156159
tileset.transparent_color = parse_color(my_string)
157160

158161
tileoffset_element = raw_tileset.find("./tileoffset")
159-
if tileoffset_element:
162+
if tileoffset_element is not None:
160163
tileset.tile_offset = OrderedPair(
161164
int(tileoffset_element.attrib["x"]), int(tileoffset_element.attrib["y"])
162165
)
163166

164167
grid_element = raw_tileset.find("./grid")
165-
if grid_element:
168+
if grid_element is not None:
166169
tileset.grid = _parse_grid(grid_element)
167170

168171
properties_element = raw_tileset.find("./properties")
169-
if properties_element:
172+
if properties_element is not None:
170173
tileset.properties = parse_properties(properties_element)
171174

172175
tiles = {}
173-
for tile_element in raw_tileset.findall("./tiles"):
176+
for tile_element in raw_tileset.findall("./tile"):
174177
tiles[int(tile_element.attrib["id"])] = _parse_tile(
175178
tile_element, external_path=external_path
176179
)
177180
if tiles:
178181
tileset.tiles = tiles
179182

180183
wangsets_element = raw_tileset.find("./wangsets")
181-
if wangsets_element:
184+
if wangsets_element is not None:
182185
wangsets = []
183186
for raw_wangset in wangsets_element.findall("./wangset"):
184187
wangsets.append(parse_wangset(raw_wangset))
185188
tileset.wang_sets = wangsets
186189

187190
transformations_element = raw_tileset.find("./transformations")
188-
if transformations_element:
191+
if transformations_element is not None:
189192
tileset.transformations = _parse_transformations(transformations_element)
190193

191194
return tileset

pytiled_parser/tiled_object.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ class TiledObject:
3535
coordinates: OrderedPair
3636
size: Size = Size(0, 0)
3737
rotation: float = 0
38-
visible: bool
39-
40-
name: Optional[str] = None
41-
type: Optional[str] = None
38+
visible: bool = True
39+
name: str = ""
40+
type: str = ""
4241

4342
properties: properties_.Properties = {}
4443

0 commit comments

Comments
 (0)