diff --git a/pytiled_parser/parsers/tmx/tiled_object.py b/pytiled_parser/parsers/tmx/tiled_object.py index 455c2c5..9d272ea 100644 --- a/pytiled_parser/parsers/tmx/tiled_object.py +++ b/pytiled_parser/parsers/tmx/tiled_object.py @@ -123,9 +123,11 @@ def _parse_polyline(raw_object: etree.Element) -> Polyline: Polyline: The Polyline object created from the raw object """ polyline = [] - for raw_point in raw_object.attrib["polyline"].split(" "): - point = raw_point.split(",") - polyline.append(OrderedPair(float(point[0]), float(point[1]))) + polyline_element = raw_object.find("./polyline") + if polyline_element is not None: + for raw_point in polyline_element.attrib["points"].split(" "): + point = raw_point.split(",") + polyline.append(OrderedPair(float(point[0]), float(point[1]))) return Polyline(points=polyline, **_parse_common(raw_object).__dict__) @@ -161,46 +163,50 @@ def _parse_text(raw_object: etree.Element) -> Text: Text: The Text object created from the raw object """ # required attributes - text = raw_object.text + text_element = raw_object.find("./text") - if not text: - text = "" - # create base Text object - text_object = Text(text=text, **_parse_common(raw_object).__dict__) + if text_element is not None: + text = text_element.text - # optional attributes - if raw_object.attrib.get("color") is not None: - text_object.color = parse_color(raw_object.attrib["color"]) + if not text: + text = "" + # create base Text object + text_object = Text(text=text, **_parse_common(raw_object).__dict__) - if raw_object.attrib.get("fontfamily") is not None: - text_object.font_family = raw_object.attrib["fontfamily"] + # optional attributes - if raw_object.attrib.get("pixelsize") is not None: - text_object.font_size = float(raw_object.attrib["pixelsize"]) + if text_element.attrib.get("color") is not None: + text_object.color = parse_color(text_element.attrib["color"]) - if raw_object.attrib.get("bold") is not None: - text_object.bold = bool(int(raw_object.attrib["bold"])) + if text_element.attrib.get("fontfamily") is not None: + text_object.font_family = text_element.attrib["fontfamily"] - if raw_object.attrib.get("italic") is not None: - text_object.italic = bool(int(raw_object.attrib["italic"])) + if text_element.attrib.get("pixelsize") is not None: + text_object.font_size = float(text_element.attrib["pixelsize"]) - if raw_object.attrib.get("kerning") is not None: - text_object.kerning = bool(int(raw_object.attrib["kerning"])) + if text_element.attrib.get("bold") is not None: + text_object.bold = bool(int(text_element.attrib["bold"])) - if raw_object.attrib.get("strikeout") is not None: - text_object.strike_out = bool(int(raw_object.attrib["strikeout"])) + if text_element.attrib.get("italic") is not None: + text_object.italic = bool(int(text_element.attrib["italic"])) - if raw_object.attrib.get("underline") is not None: - text_object.underline = bool(int(raw_object.attrib["underline"])) + if text_element.attrib.get("kerning") is not None: + text_object.kerning = bool(int(text_element.attrib["kerning"])) - if raw_object.attrib.get("halign") is not None: - text_object.horizontal_align = raw_object.attrib["halign"] + if text_element.attrib.get("strikeout") is not None: + text_object.strike_out = bool(int(text_element.attrib["strikeout"])) - if raw_object.attrib.get("valign") is not None: - text_object.vertical_align = raw_object.attrib["valign"] + if text_element.attrib.get("underline") is not None: + text_object.underline = bool(int(text_element.attrib["underline"])) - if raw_object.attrib.get("wrap") is not None: - text_object.wrap = bool(int(raw_object.attrib["wrap"])) + if text_element.attrib.get("halign") is not None: + text_object.horizontal_align = text_element.attrib["halign"] + + if text_element.attrib.get("valign") is not None: + text_object.vertical_align = text_element.attrib["valign"] + + if text_element.attrib.get("wrap") is not None: + text_object.wrap = bool(int(text_element.attrib["wrap"])) return text_object diff --git a/tests/test_tiled_object.py b/tests/test_tiled_object_json.py similarity index 100% rename from tests/test_tiled_object.py rename to tests/test_tiled_object_json.py diff --git a/tests/test_tiled_object_tmx.py b/tests/test_tiled_object_tmx.py new file mode 100644 index 0000000..d277759 --- /dev/null +++ b/tests/test_tiled_object_tmx.py @@ -0,0 +1,492 @@ +"""Tests for objects""" +import xml.etree.ElementTree as etree +from contextlib import ExitStack as does_not_raise +from pathlib import Path + +import pytest + +from pytiled_parser import common_types +from pytiled_parser.parsers.tmx.tiled_object import parse +from pytiled_parser.tiled_object import ( + Ellipse, + Point, + Polygon, + Polyline, + Rectangle, + Text, + Tile, +) + +ELLIPSES = [ + ( + """ + + + + """, + Ellipse( + id=6, + size=common_types.Size(57.4014, 18.5518), + name="ellipse", + coordinates=common_types.OrderedPair(37.5401, 81.1913), + ), + ), + ( + """ + + + + """, + Ellipse( + id=7, + size=common_types.Size(6.3294, 31.4289), + name="ellipse - invisible", + visible=False, + coordinates=common_types.OrderedPair(22.6986, 53.9093), + ), + ), + ( + """ + + + + """, + Ellipse( + id=8, + size=common_types.Size(29.6828, 24.2264), + name="ellipse - rotated", + rotation=111, + coordinates=common_types.OrderedPair(35.7940, 120.0409), + ), + ), + ( + """ + + + + """, + Ellipse( + id=29, + name="ellipse - no width or height", + coordinates=common_types.OrderedPair(72.4611, 127.6799), + ), + ), +] + +RECTANGLES = [ + ( + """ + + """, + Rectangle( + id=1, + size=common_types.Size(45.3973, 41.4687), + coordinates=common_types.OrderedPair(27.7185, 23.5717), + name="rectangle", + ), + ), + ( + """ + + """, + Rectangle( + id=4, + size=common_types.Size(30.9924, 32.7384), + coordinates=common_types.OrderedPair(163.9104, 91.0128), + name="rectangle - invisible", + visible=False, + ), + ), + ( + """ + + """, + Rectangle( + id=5, + size=common_types.Size(10, 22), + coordinates=common_types.OrderedPair(183.3352, 23.3534), + name="rectangle - rotated", + rotation=10, + ), + ), + ( + """ + + """, + Rectangle( + id=28, + coordinates=common_types.OrderedPair(131.1720, 53.4728), + name="rectangle - no width or height", + ), + ), + ( + r""" + + + + + + + + + + + """, + Rectangle( + id=30, + size=common_types.Size(21.1709, 13.7501), + coordinates=common_types.OrderedPair(39.0679, 131.8268), + name="rectangle - properties", + properties={ + "bool property": False, + "color property": common_types.Color(170, 0, 0, 255), + "file property": Path("../../../../../../dev/null"), + "float property": 42.1, + "int property": 8675309, + "string property": "pytiled_parser rulez!1!!", + }, + ), + ), +] + +POINTS = [ + ( + """ + + + + """, + Point( + id=2, coordinates=common_types.OrderedPair(159.9818, 82.9374), name="point" + ), + ), + ( + """ + + + + """, + Point( + id=2, + coordinates=common_types.OrderedPair(159.9818, 82.9374), + name="point - invisible", + visible=False, + ), + ), +] + +POLYGONS = [ + ( + """ + + + + """, + Polygon( + id=9, + coordinates=common_types.OrderedPair(89.4851, 38.6314), + name="polygon", + points=[ + common_types.OrderedPair(0, 0), + common_types.OrderedPair(19.4248, 27.0638), + common_types.OrderedPair(19.6431, 3.0556), + common_types.OrderedPair(-2.6191, 15.9327), + common_types.OrderedPair(25.3177, 16.3692), + ], + ), + ), + ( + """ + + + + """, + Polygon( + id=9, + coordinates=common_types.OrderedPair(89.4851, 38.6314), + name="polygon - invisible", + points=[ + common_types.OrderedPair(0, 0), + common_types.OrderedPair(19.4248, 27.0638), + common_types.OrderedPair(19.6431, 3.0556), + common_types.OrderedPair(-2.6191, 15.9327), + common_types.OrderedPair(25.3177, 16.3692), + ], + visible=False, + ), + ), + ( + """ + + + + """, + Polygon( + id=9, + coordinates=common_types.OrderedPair(89.4851, 38.6314), + name="polygon - rotated", + points=[ + common_types.OrderedPair(0, 0), + common_types.OrderedPair(19.4248, 27.0638), + common_types.OrderedPair(19.6431, 3.0556), + common_types.OrderedPair(-2.6191, 15.9327), + common_types.OrderedPair(25.3177, 16.3692), + ], + rotation=123, + ), + ), +] + +POLYLINES = [ + ( + """ + + + + """, + Polyline( + id=12, + coordinates=common_types.OrderedPair(124.1878, 90.1398), + name="polyline", + points=[ + common_types.OrderedPair(0, 0), + common_types.OrderedPair(-13.3136, 41.0321), + common_types.OrderedPair(21.3891, 16.8057), + ], + ), + ), + ( + """ + + + + """, + Polyline( + id=12, + coordinates=common_types.OrderedPair(124.1878, 90.1398), + name="polyline - invisible", + points=[ + common_types.OrderedPair(0, 0), + common_types.OrderedPair(-13.3136, 41.0321), + common_types.OrderedPair(21.3891, 16.8057), + ], + visible=False, + ), + ), + ( + """ + + + + """, + Polyline( + id=12, + coordinates=common_types.OrderedPair(124.1878, 90.1398), + name="polyline - rotated", + points=[ + common_types.OrderedPair(0, 0), + common_types.OrderedPair(-13.3136, 41.0321), + common_types.OrderedPair(21.3891, 16.8057), + ], + rotation=110, + ), + ), +] + +TEXTS = [ + ( + """ + + Hello World + + """, + Text( + id=19, + name="text", + text="Hello World", + size=common_types.Size(92.375, 19), + coordinates=common_types.OrderedPair(93.2987, 81.7106), + ), + ), + ( + """ + + Hello World + + """, + Text( + id=19, + name="text - wrap", + text="Hello World", + wrap=True, + size=common_types.Size(92.375, 19), + coordinates=common_types.OrderedPair(93.2987, 81.7106), + ), + ), + ( + """ + + Hello World + + """, + Text( + id=19, + name="text - rotated", + text="Hello World", + rotation=110, + size=common_types.Size(92.375, 19), + coordinates=common_types.OrderedPair(93.2987, 81.7106), + ), + ), + ( + """ + + Hello World + + """, + Text( + id=19, + name="text - different font", + text="Hello World", + font_size=19, + font_family="DejaVu Sans", + rotation=110, + size=common_types.Size(92.375, 19), + coordinates=common_types.OrderedPair(93.2987, 81.7106), + ), + ), + ( + """ + + Hello World + + """, + Text( + id=19, + name="text - right bottom align", + text="Hello World", + horizontal_align="right", + vertical_align="bottom", + size=common_types.Size(92.375, 19), + coordinates=common_types.OrderedPair(93.2987, 81.7106), + ), + ), + ( + """ + + Hello World + + """, + Text( + id=19, + name="text - center center align", + text="Hello World", + horizontal_align="center", + vertical_align="center", + size=common_types.Size(92.375, 19), + coordinates=common_types.OrderedPair(93.2987, 81.7106), + ), + ), + ( + """ + + Hello World + + """, + Text( + id=19, + name="text - justified", + text="Hello World", + horizontal_align="justify", + size=common_types.Size(92.375, 19), + coordinates=common_types.OrderedPair(93.2987, 81.7106), + ), + ), + ( + """ + + Hello World + + """, + Text( + id=19, + name="text - colored", + text="Hello World", + color=common_types.Color(170, 0, 0, 255), + size=common_types.Size(92.375, 19), + coordinates=common_types.OrderedPair(93.2987, 81.7106), + ), + ), + ( + """ + + Hello World + + """, + Text( + id=19, + name="text - font options", + text="Hello World", + size=common_types.Size(92.375, 19), + bold=True, + italic=True, + kerning=True, + strike_out=True, + underline=True, + wrap=True, + coordinates=common_types.OrderedPair(93.2987, 81.7106), + ), + ), +] + +TILES = [ + ( + """ + + """, + Tile( + id=13, + size=common_types.Size(32, 32), + name="tile", + coordinates=common_types.OrderedPair(111.8981, 48.3019), + gid=79, + ), + ), + ( + """ + + """, + Tile( + id=13, + size=common_types.Size(32, 32), + name="tile - invisible", + type="tile", + coordinates=common_types.OrderedPair(111.8981, 48.3019), + gid=79, + visible=False, + ), + ), + ( + """ + + """, + Tile( + id=13, + size=common_types.Size(32, 32), + name="tile - rotated", + coordinates=common_types.OrderedPair(111.8981, 48.3019), + gid=79, + rotation=110, + ), + ), +] + +OBJECTS = ELLIPSES + RECTANGLES + POINTS + POLYGONS + POLYLINES + TEXTS + TILES + + +@pytest.mark.parametrize("raw_object_tmx,expected", OBJECTS) +def test_parse_layer(raw_object_tmx, expected): + raw_object = etree.fromstring(raw_object_tmx) + result = parse(raw_object) + + assert result == expected