Skip to content

Commit

Permalink
Fixes for object template properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Cleptomania committed Aug 23, 2024
1 parent c88c3cc commit e0d88ee
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
17 changes: 16 additions & 1 deletion pytiled_parser/parsers/json/tiled_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, "
Expand Down
19 changes: 18 additions & 1 deletion pytiled_parser/parsers/tmx/tiled_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_data/map_tests/template/expected.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
coordinates=common_types.OrderedPair(
98.4987608686521, 46.2385012811358
),
properties={"test": "hello"},
properties={"test": "hello", "testtest": "fromtemplate"},
visible=True,
class_="",
),
Expand Down
14 changes: 13 additions & 1 deletion tests/test_data/map_tests/template/template-rectangle.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
7 changes: 6 additions & 1 deletion tests/test_data/map_tests/template/template-rectangle.tx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<template>
<object width="63.6586" height="38.2812"/>
<object width="63.6586" height="38.2812">
<properties>
<property name="test" value="world"/>
<property name="testtest" value="fromtemplate"/>
</properties>
</object>
</template>

0 comments on commit e0d88ee

Please sign in to comment.