Test case and fix for object template overrides

This commit is contained in:
Darren Eberly
2023-03-10 18:04:53 -05:00
parent 481bf3e71c
commit ae2ab80d5d
5 changed files with 363 additions and 349 deletions

223
.gitignore vendored
View File

@@ -1,111 +1,114 @@
# Tiled Session file from using a project file
*.tiled-session
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.idea/
# VS Code Directory
# Tiled Session file from using a project file
*.tiled-session
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.idea/
# ruff
.ruff_cache
# VS Code Directory
.vscode

View File

@@ -272,14 +272,14 @@ def parse(raw_object: etree.Element, parent_dir: Optional[Path] = None) -> Tiled
if isinstance(template, etree.Element):
new_object = template.find("./object")
if new_object is not None:
if raw_object.attrib.get("id") is not None:
new_object.attrib["id"] = raw_object.attrib["id"]
for key, val in raw_object.attrib.items():
if key == "template":
continue
new_object.attrib[key] = val
if raw_object.attrib.get("x") is not None:
new_object.attrib["x"] = raw_object.attrib["x"]
if raw_object.attrib.get("y") is not None:
new_object.attrib["y"] = raw_object.attrib["y"]
properties_element = raw_object.find("./properties")
if properties_element is not None:
new_object.append(properties_element)
raw_object = new_object
elif isinstance(template, dict):

View File

@@ -1,130 +1,131 @@
from pathlib import Path
from pytiled_parser import common_types, layer, tiled_map, tiled_object, tileset
EXPECTED = tiled_map.TiledMap(
infinite=False,
layers=[
layer.ObjectLayer(
name="Object Layer 1",
opacity=1,
visible=True,
id=2,
draw_order="topdown",
tiled_objects=[
tiled_object.Rectangle(
id=2,
name="",
rotation=0,
size=common_types.Size(63.6585878103079, 38.2811778048473),
coordinates=common_types.OrderedPair(
98.4987608686521, 46.2385012811358
),
visible=True,
class_="",
),
tiled_object.Tile(
id=6,
coordinates=common_types.OrderedPair(46, 136.666666666667),
name="",
rotation=0,
class_="",
visible=True,
size=common_types.Size(32, 32),
gid=49,
),
tiled_object.Tile(
id=7,
coordinates=common_types.OrderedPair(141.333333333333, 148),
name="",
rotation=0,
class_="",
visible=True,
size=common_types.Size(32, 32),
gid=50,
),
],
)
],
map_size=common_types.Size(8, 6),
next_layer_id=3,
next_object_id=8,
orientation="orthogonal",
render_order="right-down",
tiled_version="1.7.1",
tile_size=common_types.Size(32, 32),
version="1.6",
background_color=common_types.Color(255, 0, 4, 255),
tilesets={
1: tileset.Tileset(
columns=8,
image=Path(Path(__file__).parent / "../../images/tmw_desert_spacing.png")
.absolute()
.resolve(),
image_width=265,
image_height=199,
firstgid=1,
margin=1,
spacing=1,
name="tile_set_image",
tile_count=48,
tiled_version="1.6.0",
tile_height=32,
tile_width=32,
version="1.6",
type="tileset",
),
49: tileset.Tileset(
columns=1,
image=Path(Path(__file__).parent / "../../images/tile_04.png")
.absolute()
.resolve(),
image_width=32,
image_height=32,
firstgid=49,
margin=0,
spacing=0,
name="tile_set_image_for_template",
tile_count=1,
tiled_version="1.7.1",
tile_height=32,
tile_width=32,
version="1.6",
type="tileset",
),
50: tileset.Tileset(
columns=0,
margin=0,
spacing=0,
name="tile_set_single_image",
grid=tileset.Grid(orientation="orthogonal", width=1, height=1),
tiles={
0: tileset.Tile(
id=0,
image=Path(Path(__file__).parent / "../../images/tile_02.png")
.absolute()
.resolve(),
image_height=32,
image_width=32,
width=32,
height=32
)
},
tile_count=1,
tiled_version="1.7.1",
tile_height=32,
tile_width=32,
firstgid=50,
type="tileset",
version="1.6",
),
},
properties={
"bool property - true": True,
"color property": common_types.Color(73, 252, 255, 255),
"file property": Path("../../../../../../var/log/syslog"),
"float property": 1.23456789,
"int property": 13,
"string property": "Hello, World!!",
},
)
from pathlib import Path
from pytiled_parser import common_types, layer, tiled_map, tiled_object, tileset
EXPECTED = tiled_map.TiledMap(
infinite=False,
layers=[
layer.ObjectLayer(
name="Object Layer 1",
opacity=1,
visible=True,
id=2,
draw_order="topdown",
tiled_objects=[
tiled_object.Rectangle(
id=2,
name="",
rotation=0,
size=common_types.Size(63.6585878103079, 38.2811778048473),
coordinates=common_types.OrderedPair(
98.4987608686521, 46.2385012811358
),
properties={"test": "hello"},
visible=True,
class_="",
),
tiled_object.Tile(
id=6,
coordinates=common_types.OrderedPair(46, 136.666666666667),
name="",
rotation=0,
class_="",
visible=True,
size=common_types.Size(32, 32),
gid=49,
),
tiled_object.Tile(
id=7,
coordinates=common_types.OrderedPair(141.333333333333, 148),
name="",
rotation=0,
class_="",
visible=True,
size=common_types.Size(32, 32),
gid=50,
),
],
)
],
map_size=common_types.Size(8, 6),
next_layer_id=3,
next_object_id=8,
orientation="orthogonal",
render_order="right-down",
tiled_version="1.9.2",
tile_size=common_types.Size(32, 32),
version="1.9",
background_color=common_types.Color(255, 0, 4, 255),
tilesets={
1: tileset.Tileset(
columns=8,
image=Path(Path(__file__).parent / "../../images/tmw_desert_spacing.png")
.absolute()
.resolve(),
image_width=265,
image_height=199,
firstgid=1,
margin=1,
spacing=1,
name="tile_set_image",
tile_count=48,
tiled_version="1.6.0",
tile_height=32,
tile_width=32,
version="1.6",
type="tileset",
),
49: tileset.Tileset(
columns=1,
image=Path(Path(__file__).parent / "../../images/tile_04.png")
.absolute()
.resolve(),
image_width=32,
image_height=32,
firstgid=49,
margin=0,
spacing=0,
name="tile_set_image_for_template",
tile_count=1,
tiled_version="1.7.1",
tile_height=32,
tile_width=32,
version="1.6",
type="tileset",
),
50: tileset.Tileset(
columns=0,
margin=0,
spacing=0,
name="tile_set_single_image",
grid=tileset.Grid(orientation="orthogonal", width=1, height=1),
tiles={
0: tileset.Tile(
id=0,
image=Path(Path(__file__).parent / "../../images/tile_02.png")
.absolute()
.resolve(),
image_height=32,
image_width=32,
width=32,
height=32,
)
},
tile_count=1,
tiled_version="1.7.1",
tile_height=32,
tile_width=32,
firstgid=50,
type="tileset",
version="1.6",
),
},
properties={
"bool property - true": True,
"color property": common_types.Color(73, 252, 255, 255),
"file property": Path("../../../../../../var/log/syslog"),
"float property": 1.23456789,
"int property": 13,
"string property": "Hello, World!!",
},
)

View File

@@ -1,85 +1,91 @@
{ "backgroundcolor":"#ff0004",
"compressionlevel":0,
"height":6,
"infinite":false,
"layers":[
{
"draworder":"topdown",
"id":2,
"name":"Object Layer 1",
"objects":[
{
"id":2,
"template":"template-rectangle.json",
"x":98.4987608686521,
"y":46.2385012811358
},
{
"id":6,
"template":"template-tile-spritesheet.json",
"x":46,
"y":136.666666666667
},
{
"id":7,
"template":"template-tile-image.json",
"x":141.333333333333,
"y":148
}],
"opacity":1,
"type":"objectgroup",
"visible":true,
"x":0,
"y":0
}],
"nextlayerid":3,
"nextobjectid":8,
"orientation":"orthogonal",
"properties":[
{
"name":"bool property - true",
"type":"bool",
"value":true
},
{
"name":"color property",
"type":"color",
"value":"#ff49fcff"
},
{
"name":"file property",
"type":"file",
"value":"..\/..\/..\/..\/..\/..\/var\/log\/syslog"
},
{
"name":"float property",
"type":"float",
"value":1.23456789
},
{
"name":"int property",
"type":"int",
"value":13
},
{
"name":"string property",
"type":"string",
"value":"Hello, World!!"
}],
"renderorder":"right-down",
"tiledversion":"1.9.0",
"tileheight":32,
"tilesets":[
{
"firstgid":1,
"source":"tileset.json"
},
{
"firstgid":49,
"source":"tile_set_image_for_template.json"
}],
"tilewidth":32,
"type":"map",
"version":"1.9",
"width":8
{ "backgroundcolor":"#ff0004",
"compressionlevel":0,
"height":6,
"infinite":false,
"layers":[
{
"draworder":"topdown",
"id":2,
"name":"Object Layer 1",
"objects":[
{
"id":2,
"properties":[
{
"name":"test",
"type":"string",
"value":"hello"
}],
"template":"template-rectangle.json",
"x":98.4987608686521,
"y":46.2385012811358
},
{
"id":6,
"template":"template-tile-spritesheet.json",
"x":46,
"y":136.666666666667
},
{
"id":7,
"template":"template-tile-image.json",
"x":141.333333333333,
"y":148
}],
"opacity":1,
"type":"objectgroup",
"visible":true,
"x":0,
"y":0
}],
"nextlayerid":3,
"nextobjectid":8,
"orientation":"orthogonal",
"properties":[
{
"name":"bool property - true",
"type":"bool",
"value":true
},
{
"name":"color property",
"type":"color",
"value":"#ff49fcff"
},
{
"name":"file property",
"type":"file",
"value":"..\/..\/..\/..\/..\/..\/var\/log\/syslog"
},
{
"name":"float property",
"type":"float",
"value":1.23456789
},
{
"name":"int property",
"type":"int",
"value":13
},
{
"name":"string property",
"type":"string",
"value":"Hello, World!!"
}],
"renderorder":"right-down",
"tiledversion":"1.9.2",
"tileheight":32,
"tilesets":[
{
"firstgid":1,
"source":"tileset.json"
},
{
"firstgid":49,
"source":"tile_set_image_for_template.json"
}],
"tilewidth":32,
"type":"map",
"version":"1.9",
"width":8
}

View File

@@ -1,18 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.9" tiledversion="1.9.0" orientation="orthogonal" renderorder="right-down" compressionlevel="0" width="8" height="6" tilewidth="32" tileheight="32" infinite="0" backgroundcolor="#ff0004" nextlayerid="3" nextobjectid="8">
<properties>
<property name="bool property - true" type="bool" value="true"/>
<property name="color property" type="color" value="#ff49fcff"/>
<property name="file property" type="file" value="../../../../../../var/log/syslog"/>
<property name="float property" type="float" value="1.23456789"/>
<property name="int property" type="int" value="13"/>
<property name="string property" value="Hello, World!!"/>
</properties>
<tileset firstgid="1" source="tileset.tsx"/>
<tileset firstgid="49" source="tile_set_image_for_template.tsx"/>
<objectgroup id="2" name="Object Layer 1">
<object id="2" template="template-rectangle.tx" x="98.4988" y="46.2385"/>
<object id="6" template="template-tile-spritesheet.tx" x="46" y="136.667"/>
<object id="7" template="template-tile-image.tx" x="141.333" y="148"/>
</objectgroup>
</map>
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.9" tiledversion="1.9.2" orientation="orthogonal" renderorder="right-down" compressionlevel="0" width="8" height="6" tilewidth="32" tileheight="32" infinite="0" backgroundcolor="#ff0004" nextlayerid="3" nextobjectid="8">
<properties>
<property name="bool property - true" type="bool" value="true"/>
<property name="color property" type="color" value="#ff49fcff"/>
<property name="file property" type="file" value="../../../../../../var/log/syslog"/>
<property name="float property" type="float" value="1.23456789"/>
<property name="int property" type="int" value="13"/>
<property name="string property" value="Hello, World!!"/>
</properties>
<tileset firstgid="1" source="tileset.tsx"/>
<tileset firstgid="49" source="tile_set_image_for_template.tsx"/>
<objectgroup id="2" name="Object Layer 1">
<object id="2" template="template-rectangle.tx" x="98.4988" y="46.2385">
<properties>
<property name="test" value="hello"/>
</properties>
</object>
<object id="6" template="template-tile-spritesheet.tx" x="46" y="136.667"/>
<object id="7" template="template-tile-image.tx" x="141.333" y="148"/>
</objectgroup>
</map>