Add support for repeatx and repeaty on layeres

This commit is contained in:
Darren Eberly
2022-08-13 00:46:54 -04:00
parent 9a92a32661
commit 3c94bc7f16
6 changed files with 31 additions and 5 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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!")

View File

@@ -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,
),
]

View File

@@ -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":[
{

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.9" tiledversion="1.9.0" orientation="orthogonal" renderorder="right-down" width="8" height="6" tilewidth="32" tileheight="32" infinite="0" nextlayerid="6" nextobjectid="3">
<map version="1.9" tiledversion="1.9.1" orientation="orthogonal" renderorder="right-down" width="8" height="6" tilewidth="32" tileheight="32" infinite="0" nextlayerid="6" nextobjectid="3">
<tileset firstgid="1" source="tileset.tsx"/>
<layer id="1" name="Tile Layer 1" width="8" height="6" tintcolor="#aaffff" offsetx="1" offsety="3" parallaxx="1.4" parallaxy="1.3">
<properties>
@@ -19,10 +19,10 @@
<object id="1" x="46.3333" y="39" width="69.3333" height="52.6667"/>
</objectgroup>
</group>
<imagelayer id="3" name="Image Layer 1" tintcolor="#ff0000" offsetx="1" offsety="4">
<imagelayer id="3" name="Image Layer 1" tintcolor="#ff0000" offsetx="1" offsety="4" repeaty="1">
<image source="../../images/tile_04.png" trans="000000" width="32" height="32"/>
</imagelayer>
<imagelayer id="5" name="Image Layer 2" parallaxy="1.4">
<imagelayer id="5" name="Image Layer 2" parallaxy="1.4" repeatx="1">
<image source="../../images/tile_04.png" width="32" height="32"/>
</imagelayer>
</map>