From e0d88eea6dcb27b05ff307b210b69249b2a66015 Mon Sep 17 00:00:00 2001 From: Darren Eberly Date: Thu, 22 Aug 2024 23:33:31 -0400 Subject: [PATCH] Fixes for object template properties --- pytiled_parser/parsers/json/tiled_object.py | 17 ++++++++++++++++- pytiled_parser/parsers/tmx/tiled_object.py | 19 ++++++++++++++++++- .../test_data/map_tests/template/expected.py | 2 +- .../template/template-rectangle.json | 14 +++++++++++++- .../map_tests/template/template-rectangle.tx | 7 ++++++- 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/pytiled_parser/parsers/json/tiled_object.py b/pytiled_parser/parsers/json/tiled_object.py index d5f3f08a..260ef90f 100644 --- a/pytiled_parser/parsers/json/tiled_object.py +++ b/pytiled_parser/parsers/json/tiled_object.py @@ -320,7 +320,22 @@ def parse( loaded_template = template["object"] for key in loaded_template: if key != "id": - raw_object[key] = loaded_template[key] # type: ignore + if key == "properties": + if "properties" not in raw_object: + raw_object["properties"] = [] + + for prop in loaded_template["properties"]: + + found = False + for prop2 in raw_object["properties"]: + if prop2["name"] == prop["name"]: + found = True + break + + if not found: + raw_object["properties"].append(prop) + else: + raw_object[key] = loaded_template[key] # type: ignore else: raise NotImplementedError( "Loading TMX object templates inside a JSON map is currently not supported, " diff --git a/pytiled_parser/parsers/tmx/tiled_object.py b/pytiled_parser/parsers/tmx/tiled_object.py index 711664e1..820f37ad 100644 --- a/pytiled_parser/parsers/tmx/tiled_object.py +++ b/pytiled_parser/parsers/tmx/tiled_object.py @@ -277,8 +277,25 @@ def parse(raw_object: etree.Element, parent_dir: Optional[Path] = None) -> Tiled continue new_object.attrib[key] = val + properties_element = raw_object.find("./properties") - if properties_element is not None: + temp_properties_element = new_object.find("./properties") + if properties_element is not None and temp_properties_element is None: + new_object.append(properties_element) + elif properties_element is None and temp_properties_element is not None: + pass + elif properties_element is not None and temp_properties_element is not None: + for prop in temp_properties_element: + + found = False + for prop2 in properties_element: + if prop.attrib["name"] == prop2.attrib["name"]: + found = True + break + + if not found: + properties_element.append(prop) + new_object.remove(temp_properties_element) new_object.append(properties_element) raw_object = new_object diff --git a/tests/test_data/map_tests/template/expected.py b/tests/test_data/map_tests/template/expected.py index a2266e8e..7470be29 100644 --- a/tests/test_data/map_tests/template/expected.py +++ b/tests/test_data/map_tests/template/expected.py @@ -21,7 +21,7 @@ coordinates=common_types.OrderedPair( 98.4987608686521, 46.2385012811358 ), - properties={"test": "hello"}, + properties={"test": "hello", "testtest": "fromtemplate"}, visible=True, class_="", ), diff --git a/tests/test_data/map_tests/template/template-rectangle.json b/tests/test_data/map_tests/template/template-rectangle.json index 3bed3fa1..c18b1f8c 100644 --- a/tests/test_data/map_tests/template/template-rectangle.json +++ b/tests/test_data/map_tests/template/template-rectangle.json @@ -6,7 +6,19 @@ "rotation":0, "class":"", "visible":true, - "width":63.6585878103079 + "width":63.6585878103079, + "properties":[ + { + "name":"test", + "type":"string", + "value":"world" + }, + { + "name":"testtest", + "type":"string", + "value":"fromtemplate" + } + ] }, "type":"template" } \ No newline at end of file diff --git a/tests/test_data/map_tests/template/template-rectangle.tx b/tests/test_data/map_tests/template/template-rectangle.tx index 6daa3644..1959f0f2 100644 --- a/tests/test_data/map_tests/template/template-rectangle.tx +++ b/tests/test_data/map_tests/template/template-rectangle.tx @@ -1,4 +1,9 @@