From 9e1f5fd019c420bb927b100357196d125c9384f4 Mon Sep 17 00:00:00 2001 From: mothnox <> Date: Tue, 8 Apr 2025 15:00:04 -0500 Subject: [PATCH] Re-add class properties Removed 2 years ago with intention of being reimplemented better, but this did not happen. --- pytiled_parser/parsers/tmx/properties.py | 8 +++++++- pytiled_parser/properties.py | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pytiled_parser/parsers/tmx/properties.py b/pytiled_parser/parsers/tmx/properties.py index 26b82433..57731d25 100644 --- a/pytiled_parser/parsers/tmx/properties.py +++ b/pytiled_parser/parsers/tmx/properties.py @@ -1,7 +1,7 @@ import xml.etree.ElementTree as etree from pathlib import Path -from pytiled_parser.properties import Properties, Property +from pytiled_parser.properties import Properties, Property, ClassProperty from pytiled_parser.util import parse_color @@ -11,6 +11,12 @@ def parse(raw_properties: etree.Element) -> Properties: for raw_property in raw_properties.findall("property"): type_ = raw_property.attrib.get("type") + if type_ == "class": + children_nodes = raw_property.find("./properties") + x = ClassProperty( + raw_property.attrib["propertytype"], parse(children_nodes) if children_nodes is not None else {}) + final[raw_property.attrib["name"]] = x + continue value_ = raw_property.attrib.get("value", raw_property.text) if value_ is None: diff --git a/pytiled_parser/properties.py b/pytiled_parser/properties.py index a8d26b41..f9b94f71 100644 --- a/pytiled_parser/properties.py +++ b/pytiled_parser/properties.py @@ -13,6 +13,11 @@ from .common_types import Color -Property = Union[int, float, Path, str, bool, Color] +class ClassProperty(dict): + def __init__(self, propertytype:str, *args, **kwargs): + self.propertytype = propertytype or '' + dict.__init__(self, *args, **kwargs) + +Property = Union[int, float, Path, str, bool, Color, ClassProperty] Properties = Dict[str, Property]