-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
145a52e
commit 91e22e6
Showing
16 changed files
with
79 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
""" | ||
Copyright (C) 2012-2023, Leif Theden <[email protected]> | ||
Copyright (C) 2012-2024, Leif Theden <[email protected]> | ||
This file is part of pytmx. | ||
|
@@ -28,6 +28,3 @@ | |
logger.debug("cannot import pygame tools") | ||
|
||
__version__ = (3, 32) | ||
__author__ = "bitcraft" | ||
__author_email__ = "[email protected]" | ||
__description__ = "Map loader for TMX Files - Python 3.3 +" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
""" | ||
Copyright (C) 2012-2023, Leif Theden <[email protected]> | ||
Copyright (C) 2012-2024, Leif Theden <[email protected]> | ||
This file is part of pytmx. | ||
|
@@ -27,11 +27,12 @@ | |
import zlib | ||
from base64 import b64decode | ||
from collections import defaultdict, namedtuple | ||
from collections.abc import Iterable, Sequence | ||
from copy import deepcopy | ||
from itertools import chain, product | ||
from math import cos, radians, sin | ||
from operator import attrgetter | ||
from typing import Dict, Iterable, List, Optional, Sequence, Tuple, Union | ||
from typing import Optional, Union | ||
from xml.etree import ElementTree | ||
|
||
# for type hinting | ||
|
@@ -79,17 +80,17 @@ | |
Point = namedtuple("Point", ["x", "y"]) | ||
TileFlags = namedtuple("TileFlags", flag_names) | ||
empty_flags = TileFlags(False, False, False) | ||
ColorLike = Union[Tuple[int, int, int, int], Tuple[int, int, int], int, str] | ||
MapPoint = Tuple[int, int, int] | ||
ColorLike = Union[tuple[int, int, int, int], tuple[int, int, int], int, str] | ||
MapPoint = tuple[int, int, int] | ||
TiledLayer = Union[ | ||
"TiledTileLayer", "TiledImageLayer", "TiledGroupLayer", "TiledObjectGroup" | ||
] | ||
|
||
# need a more graceful way to handle annotations for optional dependencies | ||
if pygame: | ||
PointLike = Union[Tuple[int, int], pygame.Vector2, Point] | ||
PointLike = Union[tuple[int, int], pygame.Vector2, Point] | ||
else: | ||
PointLike = Union[Tuple[int, int], Point] | ||
PointLike = Union[tuple[int, int], Point] | ||
|
||
|
||
def default_image_loader(filename: str, flags, **kwargs): | ||
|
@@ -112,7 +113,7 @@ def load(rect=None, flags=None): | |
return load | ||
|
||
|
||
def decode_gid(raw_gid: int) -> Tuple[int, TileFlags]: | ||
def decode_gid(raw_gid: int) -> tuple[int, TileFlags]: | ||
"""Decode a GID from TMX data. | ||
Args: | ||
|
@@ -136,9 +137,9 @@ def decode_gid(raw_gid: int) -> Tuple[int, TileFlags]: | |
|
||
|
||
def reshape_data( | ||
gids: List[int], | ||
gids: list[int], | ||
width: int, | ||
) -> List[List[int]]: | ||
) -> list[list[int]]: | ||
"""Change 1D list to 2d list | ||
Args: | ||
|
@@ -156,7 +157,7 @@ def unpack_gids( | |
text: str, | ||
encoding: Optional[str] = None, | ||
compression: Optional[str] = None, | ||
) -> List[int]: | ||
) -> list[int]: | ||
"""Return all gids from encoded/compressed layer data | ||
Args: | ||
|
@@ -184,7 +185,7 @@ def unpack_gids( | |
raise ValueError(f"layer encoding {encoding} is not supported.") | ||
|
||
|
||
def convert_to_bool(value: str) -> bool: | ||
def convert_to_bool(value: Optional[Union[str, int, float]] = None) -> bool: | ||
"""Convert a few common variations of "true" and "false" to boolean | ||
Args: | ||
|
@@ -230,7 +231,7 @@ def rotate( | |
points: Sequence[Point], | ||
origin: Point, | ||
angle: Union[int, float], | ||
) -> List[Point]: | ||
) -> list[Point]: | ||
"""Rotate a sequence of points around an axis. | ||
Args: | ||
|
@@ -330,7 +331,7 @@ def rotate( | |
} | ||
|
||
|
||
def parse_properties(node: ElementTree.Element, customs: dict = None) -> Dict: | ||
def parse_properties(node: ElementTree.Element, customs: dict = None) -> dict: | ||
"""Parse a Tiled xml node and return a dict. | ||
Args: | ||
|
@@ -458,7 +459,7 @@ def __repr__(self): | |
class TiledClassType: | ||
"""Contains custom Tiled types.""" | ||
|
||
def __init__(self, name: str, members: List[dict]) -> None: | ||
def __init__(self, name: str, members: list[dict]) -> None: | ||
"""Creates the TiledClassType. | ||
Args: | ||
|
@@ -477,7 +478,7 @@ class TiledMap(TiledElement): | |
def __init__( | ||
self, | ||
filename: Optional[str] = None, | ||
custom_property_filename: Optional[List[str]] = None, | ||
custom_property_filename: Optional[list[str]] = None, | ||
image_loader=default_image_loader, | ||
**kwargs, | ||
) -> None: | ||
|
@@ -809,7 +810,7 @@ def get_tile_gid(self, x: int, y: int, layer: int) -> int: | |
logger.debug(msg.format(x, y, layer)) | ||
raise ValueError(msg.format(x, y, layer)) | ||
|
||
def get_tile_properties(self, x: int, y: int, layer: int) -> Optional[Dict]: | ||
def get_tile_properties(self, x: int, y: int, layer: int) -> Optional[dict]: | ||
"""Return the tile image GID for this location. | ||
Args: | ||
|
@@ -864,7 +865,7 @@ def get_tile_locations_by_gid(self, gid: int) -> Iterable[MapPoint]: | |
for x, y, _gid in [i for i in self.layers[l].iter_data() if i[2] == gid]: | ||
yield x, y, l | ||
|
||
def get_tile_properties_by_gid(self, gid: int) -> Optional[Dict]: | ||
def get_tile_properties_by_gid(self, gid: int) -> Optional[dict]: | ||
"""Get the tile properties of a tile GID. | ||
Args: | ||
|
@@ -1009,7 +1010,7 @@ def get_tileset_from_gid(self, gid: int) -> TiledTileset: | |
|
||
raise ValueError("Tileset not found") | ||
|
||
def get_tile_colliders(self) -> Iterable[Tuple[int, List[Dict]]]: | ||
def get_tile_colliders(self) -> Iterable[tuple[int, list[dict]]]: | ||
"""Return iterator of (gid, dict) pairs of tiles with colliders. | ||
Returns: | ||
|
@@ -1021,7 +1022,7 @@ def get_tile_colliders(self) -> Iterable[Tuple[int, List[Dict]]]: | |
if colliders: | ||
yield gid, colliders | ||
|
||
def pixels_to_tile_pos(self, position: Tuple[int, int]) -> Tuple[int, int]: | ||
def pixels_to_tile_pos(self, position: tuple[int, int]) -> tuple[int, int]: | ||
return int(position[0] / self.tilewidth), int(position[1] / self.tileheight) | ||
|
||
@property | ||
|
@@ -1138,7 +1139,7 @@ def register_gid_check_flags( | |
else: | ||
return self.register_gid(*decode_gid(tiled_gid)) | ||
|
||
def map_gid(self, tiled_gid: int) -> Optional[List[int]]: | ||
def map_gid(self, tiled_gid: int) -> Optional[list[int]]: | ||
"""Used to lookup a GID read from a TMX file's data. | ||
Args: | ||
|
@@ -1157,7 +1158,7 @@ def map_gid(self, tiled_gid: int) -> Optional[List[int]]: | |
logger.debug(msg) | ||
raise TypeError(msg) | ||
|
||
def map_gid2(self, tiled_gid: int) -> List[Tuple[int, Optional[int]]]: | ||
def map_gid2(self, tiled_gid: int) -> list[tuple[int, Optional[int]]]: | ||
"""WIP. need to refactor the gid code""" | ||
tiled_gid = int(tiled_gid) | ||
|
||
|
@@ -1375,7 +1376,7 @@ def __init__(self, parent, node) -> None: | |
def __iter__(self): | ||
return self.iter_data() | ||
|
||
def iter_data(self) -> Iterable[Tuple[int, int, int]]: | ||
def iter_data(self) -> Iterable[tuple[int, int, int]]: | ||
"""Yields X, Y, GID tuples for each tile in the layer. | ||
Returns: | ||
|
@@ -1574,17 +1575,15 @@ def read_points(text): | |
|
||
return self | ||
|
||
def apply_transformations(self) -> List[Point]: | ||
def apply_transformations(self) -> list[Point]: | ||
"""Return all points for object, taking in account rotation.""" | ||
if hasattr(self, "points"): | ||
return rotate(self.points, self, self.rotation) | ||
else: | ||
return rotate(self.as_points, self, self.rotation) | ||
|
||
@property | ||
def as_points( | ||
self, | ||
) -> List[Point]: | ||
def as_points(self) -> list[Point]: | ||
return [ | ||
Point(*i) | ||
for i in [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Copyright (C) 2012-2023, Leif Theden <[email protected]> | ||
Copyright (C) 2012-2024, Leif Theden <[email protected]> | ||
This file is part of pytmx. | ||
|
@@ -19,7 +19,7 @@ | |
""" | ||
import itertools | ||
import logging | ||
from typing import List, Optional, Union | ||
from typing import Optional, Union | ||
|
||
import pytmx | ||
from pytmx.pytmx import ColorLike, PointLike | ||
|
@@ -188,7 +188,7 @@ def build_rects( | |
layer: Union[int, str], | ||
tileset: Optional[Union[int, str]], | ||
real_gid: Optional[int], | ||
) -> List[pygame.Rect]: | ||
) -> list[pygame.Rect]: | ||
""" | ||
Generate a set of non-overlapping rects that represents the distribution of the specified gid. | ||
|
@@ -258,10 +258,10 @@ def build_rects( | |
|
||
|
||
def simplify( | ||
all_points: List[PointLike], | ||
all_points: list[PointLike], | ||
tilewidth: int, | ||
tileheight: int, | ||
) -> List[pygame.Rect]: | ||
) -> list[pygame.Rect]: | ||
"""Given a list of points, return list of rects that represent them | ||
kludge: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
""" | ||
Copyright (C) 2012-2022, Leif Theden <[email protected]> | ||
Copyright (C) 2012-2024, Leif Theden <[email protected]> | ||
This file is part of pytmx. | ||
|
@@ -19,7 +19,7 @@ | |
import dataclasses | ||
import logging | ||
from functools import partial | ||
from typing import Tuple | ||
from typing import Any, Optional | ||
|
||
from pygame.rect import Rect | ||
|
||
|
@@ -39,14 +39,14 @@ | |
class PygameSDL2Tile: | ||
texture: Texture | ||
srcrect: Rect | ||
size: Tuple[int, int] | ||
size: tuple[int, int] | ||
angle: float = 0.0 | ||
center: None = None | ||
flipx: bool = False | ||
flipy: bool = False | ||
|
||
|
||
def handle_flags(flags: pytmx.TileFlags): | ||
def handle_flags(flags: Optional[pytmx.TileFlags]) -> tuple[float, bool, bool]: | ||
""" | ||
Return angle and flip values for the SDL2 renderer | ||
|
@@ -94,7 +94,9 @@ def load_image(rect=None, flags=None) -> PygameSDL2Tile: | |
return load_image | ||
|
||
|
||
def load_pygame_sdl2(renderer: Renderer, filename: str, *args, **kwargs): | ||
def load_pygame_sdl2( | ||
renderer: Renderer, filename: str, *args: Any, **kwargs: Any | ||
) -> pytmx.TiledMap: | ||
""" | ||
Load a TMX file, images, and return a TiledMap class | ||
|
Oops, something went wrong.