mirror of
https://github.com/OMGeeky/pytiled_parser.git
synced 2026-02-23 15:49:52 +01:00
rf: define the tile layer data type
This commit is contained in:
@@ -12,10 +12,13 @@ from typing import Callable, Dict, List, Optional, Tuple, Union
|
|||||||
import pytiled_parser.objects as objects
|
import pytiled_parser.objects as objects
|
||||||
from pytiled_parser.utilities import parse_color
|
from pytiled_parser.utilities import parse_color
|
||||||
|
|
||||||
|
# See: https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#data
|
||||||
|
TileLayerData = List[List[int]]
|
||||||
|
|
||||||
|
|
||||||
def _decode_base64_data(
|
def _decode_base64_data(
|
||||||
data_text: str, layer_width: int, compression: Optional[str] = None
|
data_text: str, layer_width: int, compression: Optional[str] = None
|
||||||
) -> List[List[int]]:
|
) -> TileLayerData:
|
||||||
"""Decode base64 data.
|
"""Decode base64 data.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -27,9 +30,9 @@ def _decode_base64_data(
|
|||||||
ValueError: If compression type is unsupported.
|
ValueError: If compression type is unsupported.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
:List[List[int]]: Tile grid.
|
TileLayerData: Tile grid.
|
||||||
"""
|
"""
|
||||||
tile_grid: List[List[int]] = [[]]
|
tile_grid: TileLayerData = [[]]
|
||||||
|
|
||||||
unencoded_data = base64.b64decode(data_text)
|
unencoded_data = base64.b64decode(data_text)
|
||||||
if compression == "zlib":
|
if compression == "zlib":
|
||||||
@@ -62,14 +65,14 @@ def _decode_base64_data(
|
|||||||
return tile_grid
|
return tile_grid
|
||||||
|
|
||||||
|
|
||||||
def _decode_csv_data(data_text: str) -> List[List[int]]:
|
def _decode_csv_data(data_text: str) -> TileLayerData:
|
||||||
"""Decodes csv encoded layer data.
|
"""Decodes csv encoded layer data.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
data_text (str): Data to be decoded.
|
data_text (str): Data to be decoded.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
List[List[int]]: Tile grid.
|
TileLayerData: Tile grid.
|
||||||
"""
|
"""
|
||||||
tile_grid = []
|
tile_grid = []
|
||||||
lines: List[str] = data_text.split("\n")
|
lines: List[str] = data_text.split("\n")
|
||||||
@@ -86,12 +89,21 @@ def _decode_csv_data(data_text: str) -> List[List[int]]:
|
|||||||
return tile_grid
|
return tile_grid
|
||||||
|
|
||||||
|
|
||||||
|
TileLayerDecoder = Callable[[str, Optional[int], Optional[str]], TileLayerData]
|
||||||
|
|
||||||
|
|
||||||
|
def _get_tile_layer_decoder(
|
||||||
|
encoding: str, compression: Optional[str] = None
|
||||||
|
) -> TileLayerDecoder:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def _decode_tile_layer_data(
|
def _decode_tile_layer_data(
|
||||||
element: etree.Element,
|
element: etree.Element,
|
||||||
layer_width: int,
|
layer_width: int,
|
||||||
encoding: str,
|
encoding: str,
|
||||||
compression: Optional[str] = None,
|
compression: Optional[str] = None,
|
||||||
) -> List[List[int]]:
|
) -> TileLayerData:
|
||||||
"""Decodes tile layer data or chunk data.
|
"""Decodes tile layer data or chunk data.
|
||||||
|
|
||||||
See: https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#tmx-data
|
See: https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#tmx-data
|
||||||
@@ -111,7 +123,7 @@ def _decode_tile_layer_data(
|
|||||||
AttributeError: No data in element.
|
AttributeError: No data in element.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
List[List[int]]: Tile grid.
|
TileLayerData: Tile grid.
|
||||||
"""
|
"""
|
||||||
supported_encodings = ["base64", "csv"]
|
supported_encodings = ["base64", "csv"]
|
||||||
if encoding not in supported_encodings:
|
if encoding not in supported_encodings:
|
||||||
|
|||||||
Reference in New Issue
Block a user