From 3c94bc7f16b8c6e0d11aca5f01c7ab5f808d040c Mon Sep 17 00:00:00 2001 From: Darren Eberly Date: Sat, 13 Aug 2022 00:46:54 -0400 Subject: [PATCH] Add support for repeatx and repeaty on layeres --- pytiled_parser/layer.py | 8 ++++++++ pytiled_parser/parsers/json/layer.py | 8 ++++++++ pytiled_parser/parsers/tmx/layer.py | 8 +++++++- tests/test_data/layer_tests/all_layer_types/expected.py | 2 ++ tests/test_data/layer_tests/all_layer_types/map.json | 4 +++- tests/test_data/layer_tests/all_layer_types/map.tmx | 6 +++--- 6 files changed, 31 insertions(+), 5 deletions(-) diff --git a/pytiled_parser/layer.py b/pytiled_parser/layer.py index f1488b7..8ad7646 100644 --- a/pytiled_parser/layer.py +++ b/pytiled_parser/layer.py @@ -44,12 +44,20 @@ class Layer: properties: Properties for the layer. tint_color: Tint color that is multiplied with any graphics in this layer. class_: The Tiled class of this Layer. + repeat_x: Repeat drawing on the X Axis(Currently only applies to image layers) + repeat_y: Repeat drawing on the Y Axis(Currently only applies to image layers) """ name: str opacity: float = 1 visible: bool = True + # These technically only apply to image layers as of now, however Tiled has indicated + # that is only at this time, and there's no reason they couldn't apply to other + # types of layers in the future. For this reason they are stored in the common class. + repeat_x: bool = False + repeat_y: bool = False + coordinates: OrderedPair = OrderedPair(0, 0) parallax_factor: OrderedPair = OrderedPair(1, 1) offset: OrderedPair = OrderedPair(0, 0) diff --git a/pytiled_parser/parsers/json/layer.py b/pytiled_parser/parsers/json/layer.py index 3c3e5a6..5f8208e 100644 --- a/pytiled_parser/parsers/json/layer.py +++ b/pytiled_parser/parsers/json/layer.py @@ -81,6 +81,8 @@ RawLayer = TypedDict( "width": int, "x": int, "y": int, + "repeatx": bool, + "repeaty": bool, }, ) RawLayer.__doc__ = """ @@ -248,6 +250,12 @@ def _parse_common(raw_layer: RawLayer) -> Layer: if raw_layer.get("tintcolor") is not None: common.tint_color = parse_color(raw_layer["tintcolor"]) + if raw_layer.get("repeatx") is not None: + common.repeat_x = raw_layer["repeatx"] + + if raw_layer.get("repeaty") is not None: + common.repeat_y = raw_layer["repeaty"] + return common diff --git a/pytiled_parser/parsers/tmx/layer.py b/pytiled_parser/parsers/tmx/layer.py index d539d67..df219bb 100644 --- a/pytiled_parser/parsers/tmx/layer.py +++ b/pytiled_parser/parsers/tmx/layer.py @@ -195,6 +195,12 @@ def _parse_common(raw_layer: etree.Element) -> Layer: if raw_layer.attrib.get("class") is not None: common.class_ = raw_layer.attrib["class"] + if raw_layer.attrib.get("repeatx") is not None: + common.repeat_x = bool(int(raw_layer.attrib["repeatx"])) + + if raw_layer.attrib.get("repeaty") is not None: + common.repeat_y = bool(int(raw_layer.attrib["repeaty"])) + return common @@ -302,7 +308,7 @@ def _parse_image_layer(raw_layer: etree.Element) -> ImageLayer: transparent_color=transparent_color, **_parse_common(raw_layer).__dict__, ) - print(image_layer.size) + return image_layer raise RuntimeError("Tried to parse an image layer that doesn't have an image!") diff --git a/tests/test_data/layer_tests/all_layer_types/expected.py b/tests/test_data/layer_tests/all_layer_types/expected.py index 8bb9068..6485438 100644 --- a/tests/test_data/layer_tests/all_layer_types/expected.py +++ b/tests/test_data/layer_tests/all_layer_types/expected.py @@ -115,6 +115,7 @@ EXPECTED = [ image=Path("../../images/tile_04.png"), transparent_color=common_types.Color(0, 0, 0, 255), tint_color=common_types.Color(255, 0, 0, 255), + repeat_y=True, ), layer.ImageLayer( name="Image Layer 2", @@ -124,5 +125,6 @@ EXPECTED = [ id=5, parallax_factor=common_types.OrderedPair(1.0, 1.4), image=Path("../../images/tile_04.png"), + repeat_x=True, ), ] diff --git a/tests/test_data/layer_tests/all_layer_types/map.json b/tests/test_data/layer_tests/all_layer_types/map.json index f828ebd..c8c8ca5 100644 --- a/tests/test_data/layer_tests/all_layer_types/map.json +++ b/tests/test_data/layer_tests/all_layer_types/map.json @@ -71,6 +71,7 @@ "offsetx":1, "offsety":4, "opacity":1, + "repeaty":true, "tintcolor":"#ff0000", "transparentcolor":"#000000", "type":"imagelayer", @@ -84,6 +85,7 @@ "name":"Image Layer 2", "opacity":1, "parallaxy":1.4, + "repeatx":true, "type":"imagelayer", "visible":true, "x":0, @@ -93,7 +95,7 @@ "nextobjectid":3, "orientation":"orthogonal", "renderorder":"right-down", - "tiledversion":"1.9.0", + "tiledversion":"1.9.1", "tileheight":32, "tilesets":[ { diff --git a/tests/test_data/layer_tests/all_layer_types/map.tmx b/tests/test_data/layer_tests/all_layer_types/map.tmx index ce7a3de..b629128 100644 --- a/tests/test_data/layer_tests/all_layer_types/map.tmx +++ b/tests/test_data/layer_tests/all_layer_types/map.tmx @@ -1,5 +1,5 @@ - + @@ -19,10 +19,10 @@ - + - +