mirror of
https://github.com/OMGeeky/pytiled_parser.git
synced 2025-12-26 17:02:28 +01:00
major clean-up for linting
This commit is contained in:
2
make.sh
2
make.sh
@@ -11,5 +11,5 @@ do
|
||||
pip3 install $file
|
||||
done
|
||||
# sphinx-build -b html doc doc/build/html
|
||||
coverage run --source arcade setup.py test
|
||||
coverage run --source pytiled_parser setup.py test
|
||||
coverage report -m
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from . import utilities
|
||||
from . import objects
|
||||
"""init for pytiled_parser"""
|
||||
|
||||
from . import objects, utilities
|
||||
from .xml_parser import parse_tile_map
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
"""
|
||||
pytiled_parser objects for Tiled maps.
|
||||
"""pytiled_parser objects for Tiled maps.
|
||||
"""
|
||||
|
||||
import functools
|
||||
import re
|
||||
import xml.etree.ElementTree as etree
|
||||
from collections import OrderedDict
|
||||
# pylint: disable=too-few-public-methods
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, NamedTuple, Optional, Union
|
||||
|
||||
@@ -16,10 +13,10 @@ class Color(NamedTuple):
|
||||
"""Color object.
|
||||
|
||||
Attributes:
|
||||
:red (int): Red, between 1 and 255.
|
||||
:green (int): Green, between 1 and 255.
|
||||
:blue (int): Blue, between 1 and 255.
|
||||
:alpha (int): Alpha, between 1 and 255.
|
||||
red (int): Red, between 1 and 255.
|
||||
green (int): Green, between 1 and 255.
|
||||
blue (int): Blue, between 1 and 255.
|
||||
alpha (int): Alpha, between 1 and 255.
|
||||
"""
|
||||
|
||||
red: int
|
||||
@@ -64,24 +61,22 @@ class Size(NamedTuple):
|
||||
height: Union[int, float]
|
||||
|
||||
|
||||
@attr.s(auto_attribs=True)
|
||||
class Template:
|
||||
"""
|
||||
FIXME TODO
|
||||
"""
|
||||
"""FIXME TODO"""
|
||||
|
||||
|
||||
@attr.s(auto_attribs=True)
|
||||
class Chunk:
|
||||
"""
|
||||
Chunk object for infinite maps.
|
||||
"""Chunk object for infinite maps.
|
||||
|
||||
See: https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#chunk
|
||||
|
||||
Attributes:
|
||||
:location (OrderedPair): Location of chunk in tiles.
|
||||
:width (int): The width of the chunk in tiles.
|
||||
:height (int): The height of the chunk in tiles.
|
||||
:layer_data (List[List(int)]): The global tile IDs in chunky
|
||||
location (OrderedPair): Location of chunk in tiles.
|
||||
width (int): The width of the chunk in tiles.
|
||||
height (int): The height of the chunk in tiles.
|
||||
layer_data (List[List(int)]): The global tile IDs in chunky
|
||||
according to row.
|
||||
"""
|
||||
|
||||
@@ -93,19 +88,18 @@ class Chunk:
|
||||
|
||||
@attr.s(auto_attribs=True)
|
||||
class Image:
|
||||
"""
|
||||
Image object.
|
||||
"""Image object.
|
||||
|
||||
This module does not support embedded data in image elements.
|
||||
|
||||
Attributes:
|
||||
:source (Optional[str]): The reference to the tileset image file.
|
||||
source (Optional[str]): The reference to the tileset image file.
|
||||
Not that this is a relative path compared to FIXME
|
||||
:trans (Optional[Color]): Defines a specific color that is treated
|
||||
trans (Optional[Color]): Defines a specific color that is treated
|
||||
as transparent.
|
||||
:width (Optional[str]): The image width in pixels
|
||||
width (Optional[str]): The image width in pixels
|
||||
(optional, used for tile index correction when the image changes).
|
||||
:height (Optional[str]): The image height in pixels (optional).
|
||||
height (Optional[str]): The image height in pixels (optional).
|
||||
"""
|
||||
|
||||
source: str
|
||||
@@ -117,8 +111,7 @@ Properties = Dict[str, Union[int, float, Color, Path, str]]
|
||||
|
||||
|
||||
class Grid(NamedTuple):
|
||||
"""
|
||||
Contains info for isometric maps.
|
||||
"""Contains info for isometric maps.
|
||||
|
||||
This element is only used in case of isometric orientation, and
|
||||
determines how tile overlays for terrain and collision information
|
||||
@@ -131,12 +124,11 @@ class Grid(NamedTuple):
|
||||
|
||||
|
||||
class Terrain(NamedTuple):
|
||||
"""
|
||||
Terrain object.
|
||||
"""Terrain object.
|
||||
|
||||
Args:
|
||||
:name (str): The name of the terrain type.
|
||||
:tile (int): The local tile-id of the tile that represents the
|
||||
name (str): The name of the terrain type.
|
||||
tile (int): The local tile-id of the tile that represents the
|
||||
terrain visually.
|
||||
"""
|
||||
|
||||
@@ -145,15 +137,14 @@ class Terrain(NamedTuple):
|
||||
|
||||
|
||||
class Frame(NamedTuple):
|
||||
"""
|
||||
Animation Frame object.
|
||||
"""Animation Frame object.
|
||||
|
||||
This is only used as a part of an animation for Tile objects.
|
||||
|
||||
Args:
|
||||
:tile_id (int): The local ID of a tile within the parent tile set
|
||||
tile_id (int): The local ID of a tile within the parent tile set
|
||||
object.
|
||||
:duration (int): How long in milliseconds this frame should be
|
||||
duration (int): How long in milliseconds this frame should be
|
||||
displayed before advancing to the next frame.
|
||||
"""
|
||||
|
||||
@@ -163,17 +154,16 @@ class Frame(NamedTuple):
|
||||
|
||||
@attr.s(auto_attribs=True)
|
||||
class TileTerrain:
|
||||
"""
|
||||
Defines each corner of a tile by Terrain index in
|
||||
"""Defines each corner of a tile by Terrain index in
|
||||
'TileSet.terrain_types'.
|
||||
|
||||
Defaults to 'None'. 'None' means that corner has no terrain.
|
||||
|
||||
Attributes:
|
||||
:top_left (Optional[int]): Top left terrain type.
|
||||
:top_right (Optional[int]): Top right terrain type.
|
||||
:bottom_left (Optional[int]): Bottom left terrain type.
|
||||
:bottom_right (Optional[int]): Bottom right terrain type.
|
||||
top_left (Optional[int]): Top left terrain type.
|
||||
top_right (Optional[int]): Top right terrain type.
|
||||
bottom_left (Optional[int]): Bottom left terrain type.
|
||||
bottom_right (Optional[int]): Bottom right terrain type.
|
||||
"""
|
||||
|
||||
top_left: Optional[int] = None
|
||||
@@ -214,8 +204,7 @@ class Layer:
|
||||
|
||||
|
||||
LayerData = Union[List[List[int]], List[Chunk]]
|
||||
"""
|
||||
The tile data for one layer.
|
||||
"""The tile data for one layer.
|
||||
|
||||
Either a 2 dimensional array of integers representing the global tile IDs
|
||||
for a map layer, or a lists of chunks for an infinite map layer.
|
||||
@@ -242,27 +231,26 @@ class TileLayer(Layer):
|
||||
|
||||
@attr.s(auto_attribs=True, kw_only=True)
|
||||
class TiledObject:
|
||||
"""
|
||||
TiledObject object.
|
||||
"""TiledObject object.
|
||||
|
||||
See:
|
||||
https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#object
|
||||
|
||||
Args:
|
||||
:id_ (int): Unique ID of the object. Each object that is placed on a
|
||||
id_ (int): Unique ID of the object. Each object that is placed on a
|
||||
map gets a unique id. Even if an object was deleted, no object
|
||||
gets the same ID.
|
||||
:gid (Optional[int]): Global tiled object ID
|
||||
:location (OrderedPair): The location of the object in pixels.
|
||||
:size (Size): The width of the object in pixels
|
||||
gid (Optional[int]): Global tiled object ID
|
||||
location (OrderedPair): The location of the object in pixels.
|
||||
size (Size): The width of the object in pixels
|
||||
(default: (0, 0)).
|
||||
:rotation (int): The rotation of the object in degrees clockwise
|
||||
rotation (int): The rotation of the object in degrees clockwise
|
||||
(default: 0).
|
||||
:opacity (int): The opacity of the object. (default: 255)
|
||||
:name (Optional[str]): The name of the object.
|
||||
:type (Optional[str]): The type of the object.
|
||||
:properties (Properties): The properties of the TiledObject.
|
||||
:template Optional[Template]: A reference to a Template object FIXME
|
||||
opacity (int): The opacity of the object. (default: 255)
|
||||
name (Optional[str]): The name of the object.
|
||||
type (Optional[str]): The type of the object.
|
||||
properties (Properties): The properties of the TiledObject.
|
||||
template Optional[Template]: A reference to a Template object FIXME
|
||||
"""
|
||||
|
||||
id_: int
|
||||
@@ -282,8 +270,7 @@ class TiledObject:
|
||||
|
||||
@attr.s()
|
||||
class RectangleObject(TiledObject):
|
||||
"""
|
||||
Rectangle shape defined by a point, width, and height.
|
||||
"""Rectangle shape defined by a point, width, and height.
|
||||
|
||||
See: https://doc.mapeditor.org/en/stable/manual/objects/#insert-rectangle
|
||||
(objects in tiled are rectangles by default, so there is no specific
|
||||
@@ -293,8 +280,7 @@ class RectangleObject(TiledObject):
|
||||
|
||||
@attr.s()
|
||||
class ElipseObject(TiledObject):
|
||||
"""
|
||||
Elipse shape defined by a point, width, and height.
|
||||
"""Elipse shape defined by a point, width, and height.
|
||||
|
||||
See: https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#ellipse
|
||||
"""
|
||||
@@ -302,8 +288,7 @@ class ElipseObject(TiledObject):
|
||||
|
||||
@attr.s()
|
||||
class PointObject(TiledObject):
|
||||
"""
|
||||
Point defined by a point (x,y).
|
||||
"""Point defined by a point (x,y).
|
||||
|
||||
See: https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#point
|
||||
"""
|
||||
@@ -311,13 +296,12 @@ class PointObject(TiledObject):
|
||||
|
||||
@attr.s(auto_attribs=True, kw_only=True)
|
||||
class TileImageObject(TiledObject):
|
||||
"""
|
||||
Polygon shape defined by a set of connections between points.
|
||||
"""Polygon shape defined by a set of connections between points.
|
||||
|
||||
See: https://doc.mapeditor.org/en/stable/manual/objects/#insert-tile
|
||||
|
||||
Attributes:
|
||||
:gid (int): Refference to a global tile id.
|
||||
gid (int): Refference to a global tile id.
|
||||
"""
|
||||
|
||||
gid: int
|
||||
@@ -325,13 +309,12 @@ class TileImageObject(TiledObject):
|
||||
|
||||
@attr.s(auto_attribs=True, kw_only=True)
|
||||
class PolygonObject(TiledObject):
|
||||
"""
|
||||
Polygon shape defined by a set of connections between points.
|
||||
"""Polygon shape defined by a set of connections between points.
|
||||
|
||||
See: https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#polygon
|
||||
|
||||
Attributes:
|
||||
:points (List[OrderedPair])
|
||||
points (List[OrderedPair]): FIXME
|
||||
"""
|
||||
|
||||
points: List[OrderedPair]
|
||||
@@ -339,14 +322,13 @@ class PolygonObject(TiledObject):
|
||||
|
||||
@attr.s(auto_attribs=True, kw_only=True)
|
||||
class PolylineObject(TiledObject):
|
||||
"""
|
||||
Polyline defined by a set of connections between points.
|
||||
"""Polyline defined by a set of connections between points.
|
||||
|
||||
See:
|
||||
https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#polyline
|
||||
|
||||
Attributes:
|
||||
:points (List[Tuple[int, int]]): List of coordinates relative to \
|
||||
points (List[Tuple[int, int]]): List of coordinates relative to \
|
||||
the location of the object.
|
||||
"""
|
||||
|
||||
@@ -355,26 +337,25 @@ class PolylineObject(TiledObject):
|
||||
|
||||
@attr.s(auto_attribs=True, kw_only=True)
|
||||
class TextObject(TiledObject):
|
||||
"""
|
||||
Text object with associated settings.
|
||||
"""Text object with associated settings.
|
||||
|
||||
See: https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#text
|
||||
and https://doc.mapeditor.org/en/stable/manual/objects/#insert-text
|
||||
|
||||
Attributes:
|
||||
:font_family (str): The font family used (default: “sans-serif”)
|
||||
:font_size (int): The size of the font in pixels. (default: 16)
|
||||
:wrap (bool): Whether word wrapping is enabled. (default: False)
|
||||
:color (Color): Color of the text. (default: #000000)
|
||||
:bold (bool): Whether the font is bold. (default: False)
|
||||
:italic (bool): Whether the font is italic. (default: False)
|
||||
:underline (bool): Whether the text is underlined. (default: False)
|
||||
:strike_out (bool): Whether the text is striked-out. (default: False)
|
||||
:kerning (bool): Whether kerning should be used while rendering the \
|
||||
text. (default: False)
|
||||
:horizontal_align (str): Horizontal alignment of the text \
|
||||
(default: "left")
|
||||
:vertical_align (str): Vertical alignment of the text (defalt: "top")
|
||||
font_family (str): The font family used (default: "sans-serif")
|
||||
font_size (int): The size of the font in pixels. (default: 16)
|
||||
wrap (bool): Whether word wrapping is enabled. (default: False)
|
||||
color (Color): Color of the text. (default: #000000)
|
||||
bold (bool): Whether the font is bold. (default: False)
|
||||
italic (bool): Whether the font is italic. (default: False)
|
||||
underline (bool): Whether the text is underlined. (default: False)
|
||||
strike_out (bool): Whether the text is striked-out. (default: False)
|
||||
kerning (bool): Whether kerning should be used while rendering the \
|
||||
text. (default: False)
|
||||
horizontal_align (str): Horizontal alignment of the text \
|
||||
(default: "left")
|
||||
vertical_align (str): Vertical alignment of the text (defalt: "top")
|
||||
"""
|
||||
|
||||
text: str
|
||||
@@ -393,11 +374,10 @@ class TextObject(TiledObject):
|
||||
|
||||
@attr.s(auto_attribs=True, kw_only=True)
|
||||
class ObjectLayer(Layer):
|
||||
"""
|
||||
TiledObject Group Object.
|
||||
"""TiledObject Group Object.
|
||||
|
||||
The object group is in fact a map layer, and is hence called \
|
||||
“object layer” in Tiled.
|
||||
"object layer" in Tiled.
|
||||
|
||||
See:
|
||||
https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#objectgroup
|
||||
@@ -422,8 +402,7 @@ class ObjectLayer(Layer):
|
||||
|
||||
@attr.s(auto_attribs=True, kw_only=True)
|
||||
class LayerGroup(Layer):
|
||||
"""
|
||||
Layer Group.
|
||||
"""Layer Group.
|
||||
|
||||
A LayerGroup can be thought of as a layer that contains layers
|
||||
(potentially including other LayerGroups).
|
||||
@@ -433,7 +412,8 @@ class LayerGroup(Layer):
|
||||
See: https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#group
|
||||
|
||||
Attributes:
|
||||
|
||||
Layers (Optional[List[Union["LayerGroup", Layer, ObjectLayer]]]):
|
||||
Layers in group.
|
||||
"""
|
||||
|
||||
layers: Optional[List[Union["LayerGroup", Layer, ObjectLayer]]]
|
||||
@@ -441,33 +421,32 @@ class LayerGroup(Layer):
|
||||
|
||||
@attr.s(auto_attribs=True)
|
||||
class TileSet:
|
||||
"""
|
||||
Object for storing a TSX with all associated collision data.
|
||||
"""Object for storing a TSX with all associated collision data.
|
||||
|
||||
Args:
|
||||
:name (str): The name of this tileset.
|
||||
:max_tile_size (Size): The maximum size of a tile in this
|
||||
name (str): The name of this tileset.
|
||||
max_tile_size (Size): The maximum size of a tile in this
|
||||
tile set in pixels.
|
||||
:spacing (int): The spacing in pixels between the tiles in this
|
||||
spacing (int): The spacing in pixels between the tiles in this
|
||||
tileset (applies to the tileset image).
|
||||
:margin (int): The margin around the tiles in this tileset
|
||||
margin (int): The margin around the tiles in this tileset
|
||||
(applies to the tileset image).
|
||||
:tile_count (int): The number of tiles in this tileset.
|
||||
:columns (int): The number of tile columns in the tileset.
|
||||
tile_count (int): The number of tiles in this tileset.
|
||||
columns (int): The number of tile columns in the tileset.
|
||||
For image collection tilesets it is editable and is used when
|
||||
displaying the tileset.
|
||||
:grid (Grid): Only used in case of isometric orientation, and
|
||||
grid (Grid): Only used in case of isometric orientation, and
|
||||
determines how tile overlays for terrain and collision information
|
||||
are rendered.
|
||||
:tileoffset (Optional[OrderedPair]): Used to specify an offset in
|
||||
tileoffset (Optional[OrderedPair]): Used to specify an offset in
|
||||
pixels when drawing a tile from the tileset. When not present, no
|
||||
offset is applied.
|
||||
:image (Image): Used for spritesheet tile sets.
|
||||
:terrain_types (Dict[str, int]): List of of terrain types which
|
||||
image (Image): Used for spritesheet tile sets.
|
||||
terrain_types (Dict[str, int]): List of of terrain types which
|
||||
can be referenced from the terrain attribute of the tile object.
|
||||
Ordered according to the terrain element's appearance in the TSX
|
||||
file.
|
||||
:tiles (Optional[Dict[int, Tile]]): Dict of Tile objects by Tile.id.
|
||||
tiles (Optional[Dict[int, Tile]]): Dict of Tile objects by Tile.id.
|
||||
"""
|
||||
|
||||
name: str
|
||||
@@ -490,8 +469,7 @@ TileSetDict = Dict[int, TileSet]
|
||||
|
||||
@attr.s(auto_attribs=True, kw_only=True)
|
||||
class Tile:
|
||||
"""
|
||||
Individual tile object.
|
||||
"""Individual tile object.
|
||||
|
||||
Args:
|
||||
:id (int): The local tile ID within its tileset.
|
||||
@@ -513,8 +491,7 @@ class Tile:
|
||||
|
||||
@attr.s(auto_attribs=True)
|
||||
class TileMap:
|
||||
"""
|
||||
Object for storing a TMX with all associated layers and properties.
|
||||
"""Object for storing a TMX with all associated layers and properties.
|
||||
|
||||
See: https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#map
|
||||
|
||||
@@ -524,8 +501,8 @@ class TileMap:
|
||||
:version (str): The TMX format version.
|
||||
:tiledversion (str): The Tiled version used to save the file. May
|
||||
be a date (for snapshot builds).
|
||||
:orientation (str): Map orientation. Tiled supports “orthogonal”,
|
||||
“isometric”, “staggered” and “hexagonal”
|
||||
:orientation (str): Map orientation. Tiled supports "orthogonal",
|
||||
"isometric", "staggered" and "hexagonal"
|
||||
:renderorder (str): The order in which tiles on tile layers are
|
||||
rendered. Valid values are right-down, right-up, left-down and
|
||||
left-up. In all cases, the map is drawn row-by-row. (only
|
||||
@@ -534,12 +511,12 @@ class TileMap:
|
||||
:tile_size (Size): The width of a tile.
|
||||
:infinite (bool): If the map is infinite or not.
|
||||
:hexsidelength (int): Only for hexagonal maps. Determines the width or
|
||||
height (depending on the staggered axis) of the tile’s edge, in
|
||||
height (depending on the staggered axis) of the tile's edge, in
|
||||
pixels.
|
||||
:stagger_axis (str): For staggered and hexagonal maps, determines
|
||||
which axis (“x” or “y”) is staggered.
|
||||
which axis ("x" or "y") is staggered.
|
||||
:staggerindex (str): For staggered and hexagonal maps, determines
|
||||
whether the “even” or “odd” indexes along the staggered axis are
|
||||
whether the "even" or "odd" indexes along the staggered axis are
|
||||
shifted.
|
||||
:backgroundcolor (##FIXME##): The background color of the map.
|
||||
:nextlayerid (int): Stores the next available ID for new layers.
|
||||
|
||||
@@ -5,8 +5,10 @@ import pytiled_parser.objects as objects
|
||||
|
||||
|
||||
def parse_color(color: str) -> objects.Color:
|
||||
"""
|
||||
Converts the color formats that Tiled uses into ones that Arcade accepts.
|
||||
"""Convert Tiled color format into Arcade's.
|
||||
|
||||
Args:
|
||||
color (str): Tiled formatted color string.
|
||||
|
||||
Returns:
|
||||
:Color: Color object in the format that Arcade understands.
|
||||
@@ -34,7 +36,8 @@ def _get_tile_set_key(gid: int, tile_set_keys: List[int]) -> int:
|
||||
"""Gets tile set key given a tile GID.
|
||||
|
||||
Args:
|
||||
gid: Global ID of the tile.
|
||||
gid (int): Global ID of the tile.
|
||||
tile_set_keys (List[int]): List of tile set keys.
|
||||
|
||||
Returns:
|
||||
int: The key of the tile set that contains the tile for the GID.
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
"""Handle XML parsing for TMX files"""
|
||||
|
||||
import base64
|
||||
import functools
|
||||
import gzip
|
||||
@@ -8,12 +10,24 @@ from pathlib import Path
|
||||
from typing import Callable, Dict, List, Optional, Tuple, Union
|
||||
|
||||
import pytiled_parser.objects as objects
|
||||
import pytiled_parser.utilities as utilities
|
||||
|
||||
|
||||
def _decode_base64_data(
|
||||
data_text: str, layer_width: int, compression: Optional[str] = None
|
||||
) -> List[List[int]]:
|
||||
"""Decode base64 data.
|
||||
|
||||
Args:
|
||||
data_text (str): Data to be decoded.
|
||||
layer_width (int): Width of each layer in tiles.
|
||||
compression (Optional[str]): The type of compression for the data.
|
||||
|
||||
Raises:
|
||||
ValueError: If compression type is unsupported.
|
||||
|
||||
Returns:
|
||||
:List[List[int]]: Tile grid.
|
||||
"""
|
||||
tile_grid: List[List[int]] = [[]]
|
||||
|
||||
unencoded_data = base64.b64decode(data_text)
|
||||
@@ -50,7 +64,11 @@ def _decode_base64_data(
|
||||
def _decode_csv_data(data_text: str) -> List[List[int]]:
|
||||
"""Decodes csv encoded layer data.
|
||||
|
||||
Credit:
|
||||
Args:
|
||||
data_text (str): Data to be decoded.
|
||||
|
||||
Returns:
|
||||
List[List[int]]: Tile grid.
|
||||
"""
|
||||
tile_grid = []
|
||||
lines: List[str] = data_text.split("\n")
|
||||
@@ -76,24 +94,33 @@ def _decode_data(
|
||||
"""Decodes data or chunk data.
|
||||
|
||||
Args:
|
||||
:element (Element): Element to have text decoded.
|
||||
:layer_width (int): Number of tiles per column in this layer. Used
|
||||
element (Element): Element to have text decoded.
|
||||
layer_width (int): Number of tiles per column in this layer. Used
|
||||
for determining when to cut off a row when decoding base64
|
||||
encoding layers.
|
||||
:encoding (str): Encoding format of the layer data.
|
||||
:compression (str): Compression format of the layer data.
|
||||
encoding (str): Encoding format of the layer data.
|
||||
compression (str): Compression format of the layer data.
|
||||
|
||||
Raises:
|
||||
ValueError: Encoding type is not supported.
|
||||
ValueError: Compression is not supported for this encoding type.
|
||||
ValueError: Compression type is not supported
|
||||
AttributeError: No data in element.
|
||||
|
||||
Returns:
|
||||
List[List[int]]: Tile grid.
|
||||
"""
|
||||
# etree.Element.text comes with an appended and a prepended '\n'
|
||||
supported_encodings = ["base64", "csv"]
|
||||
if encoding not in supported_encodings:
|
||||
raise ValueError("{encoding} is not a valid encoding")
|
||||
raise ValueError(f"{encoding} is not a supported encoding")
|
||||
|
||||
supported_compression = [None, "gzip", "zlib"]
|
||||
if compression is not None:
|
||||
if encoding != "base64":
|
||||
raise ValueError("{encoding} does not support compression")
|
||||
raise ValueError(f"{encoding} does not support compression")
|
||||
if compression not in supported_compression:
|
||||
raise ValueError("{compression} is not a valid compression type")
|
||||
raise ValueError(f"{compression} is not a supported compression")
|
||||
|
||||
try:
|
||||
data_text: str = element.text # type: ignore
|
||||
@@ -112,11 +139,11 @@ def _parse_data(element: etree.Element, layer_width: int) -> objects.LayerData:
|
||||
Will parse CSV, base64, gzip-base64, or zlip-base64 encoded data.
|
||||
|
||||
Args:
|
||||
:element (Element): Data element to parse.
|
||||
:width (int): Layer width. Used for base64 decoding.
|
||||
element (Element): Data element to parse.
|
||||
layer_width (int): Layer width. Used for base64 decoding.
|
||||
|
||||
Returns:
|
||||
:LayerData: Data object containing layer data or chunks of data.
|
||||
LayerData: Data object containing layer data or chunks of data.
|
||||
"""
|
||||
encoding = element.attrib["encoding"]
|
||||
compression = None
|
||||
@@ -206,6 +233,9 @@ def _parse_tile_layer(element: etree.Element,) -> objects.TileLayer:
|
||||
Args:
|
||||
element: The layer element to be parsed.
|
||||
|
||||
Raises:
|
||||
ValueError: Element has no chile data element.
|
||||
|
||||
Returns:
|
||||
TileLayer: The tile layer object.
|
||||
"""
|
||||
@@ -261,12 +291,12 @@ def _parse_objects(
|
||||
try:
|
||||
width = float(object_element.attrib["width"])
|
||||
except KeyError:
|
||||
width = 0
|
||||
width = 0.0
|
||||
|
||||
try:
|
||||
height = float(object_element.attrib["height"])
|
||||
except KeyError:
|
||||
height = 0
|
||||
height = 0.0
|
||||
|
||||
tiled_object.size = objects.Size(width, height)
|
||||
|
||||
@@ -305,12 +335,7 @@ def _parse_object_layer(element: etree.Element,) -> objects.ObjectLayer:
|
||||
"""Parse the objectgroup element given.
|
||||
|
||||
Args:
|
||||
layer_type (objects.LayerType):
|
||||
id: The id of the layer.
|
||||
name: The name of the layer.
|
||||
offset: The offset of the layer.
|
||||
opacity: The opacity of the layer.
|
||||
properties: The Properties object of the layer.
|
||||
element (etree.Element): Element to be parsed.
|
||||
|
||||
Returns:
|
||||
ObjectLayer: The object layer object.
|
||||
@@ -347,12 +372,7 @@ def _parse_layer_group(element: etree.Element,) -> objects.LayerGroup:
|
||||
"""Parse the objectgroup element given.
|
||||
|
||||
Args:
|
||||
layer_type (objects.LayerType):
|
||||
id: The id of the layer.
|
||||
name: The name of the layer.
|
||||
offset: The offset of the layer.
|
||||
opacity: The opacity of the layer.
|
||||
properties: The Properties object of the layer.
|
||||
element (etree.Element): Element to be parsed.
|
||||
|
||||
Returns:
|
||||
LayerGroup: The layer group object.
|
||||
@@ -390,12 +410,11 @@ def _get_layer_parser(
|
||||
"""
|
||||
if layer_tag == "layer":
|
||||
return _parse_tile_layer
|
||||
elif layer_tag == "objectgroup":
|
||||
if layer_tag == "objectgroup":
|
||||
return _parse_object_layer
|
||||
elif layer_tag == "group":
|
||||
if layer_tag == "group":
|
||||
return _parse_layer_group
|
||||
else:
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
def _get_layers(map_element: etree.Element) -> List[objects.Layer]:
|
||||
@@ -426,6 +445,13 @@ def _parse_external_tile_set(
|
||||
"""Parses an external tile set.
|
||||
|
||||
Caches the results to speed up subsequent maps with identical tilesets.
|
||||
|
||||
Args:
|
||||
parent_dir (Path): Directory that TMX is in.
|
||||
tile_set_element (etree.Element): Tile set element.
|
||||
|
||||
Returns:
|
||||
objects.Tileset: The tileset being parsed.
|
||||
"""
|
||||
source = Path(tile_set_element.attrib["source"])
|
||||
tile_set_tree = etree.parse(str(parent_dir / Path(source))).getroot()
|
||||
@@ -436,6 +462,15 @@ def _parse_external_tile_set(
|
||||
def _parse_tiles(
|
||||
tile_element_list: List[etree.Element]
|
||||
) -> Dict[int, objects.Tile]:
|
||||
"""Parse a list of tile elements.
|
||||
|
||||
Args:
|
||||
tile_element_list (List[etree.Element]): List of tile elements.
|
||||
|
||||
Returns:
|
||||
Dict[int, objects.Tile]: Dictionary containing Tile objects by their
|
||||
ID.
|
||||
"""
|
||||
tiles: Dict[int, objects.Tile] = {}
|
||||
for tile_element in tile_element_list:
|
||||
# id is not optional
|
||||
@@ -448,7 +483,6 @@ def _parse_tiles(
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
tile_terrain = None
|
||||
try:
|
||||
tile_terrain_attrib = tile_element.attrib["terrain"]
|
||||
except KeyError:
|
||||
@@ -518,9 +552,13 @@ def _parse_tiles(
|
||||
def _parse_image_element(image_element: etree.Element) -> objects.Image:
|
||||
"""Parse image element given.
|
||||
|
||||
Args:
|
||||
image_element (etree.Element): Image element to be parsed.
|
||||
|
||||
Returns:
|
||||
: Color in Arcade's preferred format.
|
||||
objects.Image: FIXME what is this?
|
||||
"""
|
||||
# FIXME doc
|
||||
image = objects.Image(image_element.attrib["source"])
|
||||
|
||||
width_attrib = image_element.attrib.get("width")
|
||||
@@ -542,14 +580,19 @@ def _parse_properties_element(
|
||||
) -> objects.Properties:
|
||||
"""Adds Tiled property to Properties dict.
|
||||
|
||||
Args:
|
||||
:name (str): Name of property.
|
||||
:property_type (str): Type of property. Can be string, int, float,
|
||||
Each property element has a number of attributes:
|
||||
name (str): Name of property.
|
||||
property_type (str): Type of property. Can be string, int, float,
|
||||
bool, color or file. Defaults to string.
|
||||
:value (str): The value of the property.
|
||||
value (str): The value of the property.
|
||||
|
||||
Args:
|
||||
properties_element (etree.Element): Element to be parsed.
|
||||
|
||||
Returns:
|
||||
:Properties: Properties Dict object.
|
||||
objects.Properties: Dict of the property values by property name.
|
||||
|
||||
|
||||
"""
|
||||
properties: objects.Properties = {}
|
||||
for property_element in properties_element.findall("./property"):
|
||||
@@ -586,8 +629,13 @@ def _parse_properties_element(
|
||||
|
||||
|
||||
def _parse_tile_set(tile_set_element: etree.Element) -> objects.TileSet:
|
||||
"""
|
||||
Parses a tile set that is embedded into a TMX.
|
||||
"""Parses a tile set that is embedded into a TMX.
|
||||
|
||||
Args:
|
||||
tile_set_element (etree.Element): Element to be parsed.
|
||||
|
||||
Returns:
|
||||
objects.TileSet: Tile Set from element.
|
||||
"""
|
||||
# get all basic attributes
|
||||
name = tile_set_element.attrib["name"]
|
||||
@@ -673,8 +721,8 @@ def _parse_tile_set(tile_set_element: etree.Element) -> objects.TileSet:
|
||||
|
||||
# Go back and create a circular link so tiles know what tileset they are
|
||||
# part of. Needed for animation.
|
||||
for my_id, my_tile in tiles.items():
|
||||
my_tile.tileset = tileset
|
||||
for id_, tile in tiles.items():
|
||||
tile.tileset = tileset
|
||||
|
||||
return tileset
|
||||
|
||||
@@ -682,6 +730,15 @@ def _parse_tile_set(tile_set_element: etree.Element) -> objects.TileSet:
|
||||
def _get_tile_sets(
|
||||
map_element: etree.Element, parent_dir: Path
|
||||
) -> objects.TileSetDict:
|
||||
"""Get tile sets.
|
||||
|
||||
Args:
|
||||
map_element (etree.Element): Element to be parsed.
|
||||
parent_dir (Path): Directory that TMX is in.
|
||||
|
||||
Returns:
|
||||
objects.TileSetDict: Dict of tile sets in the TMX by first_gid
|
||||
"""
|
||||
# parse all tilesets
|
||||
tile_sets: objects.TileSetDict = {}
|
||||
tile_set_element_list = map_element.findall("./tileset")
|
||||
@@ -698,7 +755,7 @@ def _get_tile_sets(
|
||||
# check if is an external TSX
|
||||
source = tile_set_element.attrib["source"]
|
||||
except KeyError:
|
||||
# the tile set in embedded
|
||||
# the tile set is embedded
|
||||
name = tile_set_element.attrib["name"]
|
||||
tile_sets[first_gid] = _parse_tile_set(tile_set_element)
|
||||
else:
|
||||
@@ -711,6 +768,14 @@ def _get_tile_sets(
|
||||
|
||||
|
||||
def parse_tile_map(tmx_file: Union[str, Path]) -> objects.TileMap:
|
||||
"""Parse tile map.
|
||||
|
||||
Args:
|
||||
tmx_file (Union[str, Path]): TMX file to be parsed.
|
||||
|
||||
Returns:
|
||||
objects.TileMap: TileMap object generated from the TMX file provided.
|
||||
"""
|
||||
# setting up XML parsing
|
||||
map_tree = etree.parse(str(tmx_file))
|
||||
map_element = map_tree.getroot()
|
||||
@@ -732,7 +797,7 @@ def parse_tile_map(tmx_file: Union[str, Path]) -> objects.TileMap:
|
||||
tile_size = objects.Size(tile_width, tile_height)
|
||||
|
||||
infinite_attribute = map_element.attrib["infinite"]
|
||||
infinite = True if infinite_attribute == "true" else False
|
||||
infinite = bool(infinite_attribute == "true")
|
||||
|
||||
next_layer_id = int(map_element.attrib["nextlayerid"])
|
||||
next_object_id = int(map_element.attrib["nextobjectid"])
|
||||
|
||||
Reference in New Issue
Block a user