|
| 1 | +"""Fundamental aliases, classes, and related constants. |
| 2 | +
|
| 3 | +As general rules: |
| 4 | +
|
| 5 | +#. Things only go in this module if they serve multiple purposes |
| 6 | + throughout arcade |
| 7 | +#. Only expose the most important classes at this module's top level |
| 8 | +
|
| 9 | +For example, color-related types and related aliases go in |
| 10 | +``arcade.types`` because they're used throughout the codebase. This |
| 11 | +includes all the following areas: |
| 12 | +
|
| 13 | +#. :py:class:`~arcade.Sprite` |
| 14 | +#. :py:class:`~arcade.SpriteList` |
| 15 | +#. :py:class:`~arcade.Text` |
| 16 | +#. The :py:mod:`arcade.gui` widgets |
| 17 | +#. Functions in :py:mod:`arcade.drawing_commands` |
| 18 | +
|
| 19 | +However, since the color types, aliases, and constants are all related, |
| 20 | +they go in the :py:mod:`arcade.types.color` submodule. |
| 21 | +""" |
| 22 | +from __future__ import annotations |
| 23 | + |
| 24 | +# Don't lint import order since we have conditional compatibility shims |
| 25 | +# flake8: noqa: E402 |
| 26 | +import sys |
| 27 | +from pathlib import Path |
| 28 | +from typing import ( |
| 29 | + List, |
| 30 | + NamedTuple, |
| 31 | + Optional, |
| 32 | + Sequence, |
| 33 | + Tuple, |
| 34 | + Union, |
| 35 | + TYPE_CHECKING, |
| 36 | + TypeVar |
| 37 | +) |
| 38 | + |
| 39 | +from pytiled_parser import Properties |
| 40 | + |
| 41 | + |
| 42 | +# Backward-compatibility for buffer protocol objects |
| 43 | +# To learn more, see https://docs.python.org/3.8/c-api/buffer.html |
| 44 | +if sys.version_info >= (3, 12): |
| 45 | + from collections.abc import Buffer as BufferProtocol |
| 46 | +else: |
| 47 | + # The most common built-in buffer protocol objects |
| 48 | + import ctypes |
| 49 | + from array import array |
| 50 | + from collections.abc import ByteString |
| 51 | + |
| 52 | + # This is used instead of the typing_extensions version since they |
| 53 | + # use an ABC which registers virtual subclasses. This will not work |
| 54 | + # with ctypes.Array since virtual subclasses must be concrete. |
| 55 | + # See: https://peps.python.org/pep-0688/ |
| 56 | + BufferProtocol = Union[ByteString, memoryview, array, ctypes.Array] |
| 57 | + |
| 58 | + |
| 59 | +#: 1. Makes pyright happier while also telling readers |
| 60 | +#: 2. Tells readers we're converting any ints to floats |
| 61 | +AsFloat = Union[float, int] |
| 62 | + |
| 63 | + |
| 64 | +# Generic color aliases |
| 65 | +from arcade.types.color import RGB |
| 66 | +from arcade.types.color import RGBA |
| 67 | +from arcade.types.color import RGBOrA |
| 68 | + |
| 69 | +# Specific color aliases |
| 70 | +from arcade.types.color import RGB255 |
| 71 | +from arcade.types.color import RGBA255 |
| 72 | +from arcade.types.color import RGBNormalized |
| 73 | +from arcade.types.color import RGBANormalized |
| 74 | +from arcade.types.color import RGBOrA255 |
| 75 | +from arcade.types.color import RGBOrANormalized |
| 76 | + |
| 77 | +# The Color helper type |
| 78 | +from arcade.types.color import Color |
| 79 | + |
| 80 | + |
| 81 | +__all__ = [ |
| 82 | + "AsFloat", |
| 83 | + "BufferProtocol", |
| 84 | + "Color", |
| 85 | + "IPoint", |
| 86 | + "PathOr", |
| 87 | + "PathOrTexture", |
| 88 | + "Point", |
| 89 | + "Point3", |
| 90 | + "PointList", |
| 91 | + "EMPTY_POINT_LIST", |
| 92 | + "Rect", |
| 93 | + "RectList", |
| 94 | + "RGB", |
| 95 | + "RGBA", |
| 96 | + "RGBOrA", |
| 97 | + "RGB255", |
| 98 | + "RGBA255", |
| 99 | + "RGBOrA255", |
| 100 | + "RGBNormalized", |
| 101 | + "RGBANormalized", |
| 102 | + "RGBOrANormalized", |
| 103 | + "Size2D", |
| 104 | + "TiledObject", |
| 105 | + "Velocity" |
| 106 | +] |
| 107 | + |
| 108 | + |
| 109 | +_T = TypeVar('_T') |
| 110 | + |
| 111 | +#: ``Size2D`` helps mark int or float sizes. Use it like a |
| 112 | +#: :py:class:`typing.Generic`'s bracket notation as follows: |
| 113 | +#: |
| 114 | +#: .. code-block:: python |
| 115 | +#: |
| 116 | +#: def example_Function(size: Size2D[int], color: RGBA255) -> Texture: |
| 117 | +#: """An example of how to use Size2D. |
| 118 | +#: |
| 119 | +#: Look at signature above, not the missing function body. The |
| 120 | +#: ``size`` argument is how you can mark texture sizes, while |
| 121 | +#: you can use ``Size2D[float]`` to denote float regions. |
| 122 | +#: |
| 123 | +#: :param size: A made-up hypothetical argument. |
| 124 | +#: :param color: Hypothetical texture-related argument. |
| 125 | +#: """ |
| 126 | +#: ... # No function definition |
| 127 | +#: |
| 128 | +Size2D = Tuple[_T, _T] |
| 129 | + |
| 130 | +# Point = Union[Tuple[AsFloat, AsFloat], List[AsFloat]] |
| 131 | +Point = Tuple[AsFloat, AsFloat] |
| 132 | +Point3 = Tuple[AsFloat, AsFloat, AsFloat] |
| 133 | +IPoint = Tuple[int, int] |
| 134 | + |
| 135 | + |
| 136 | +# We won't keep this forever. It's a temp stub for particles we'll replace. |
| 137 | +Velocity = Tuple[AsFloat, AsFloat] |
| 138 | + |
| 139 | +PointList = Sequence[Point] |
| 140 | +# Speed / typing workaround: |
| 141 | +# 1. Eliminate extra allocations |
| 142 | +# 2. Allows type annotation to be cleaner, primarily for HitBox & subclasses |
| 143 | +EMPTY_POINT_LIST: PointList = tuple() |
| 144 | + |
| 145 | + |
| 146 | +Rect = Union[Tuple[int, int, int, int], List[int]] # x, y, width, height |
| 147 | +RectList = Union[Tuple[Rect, ...], List[Rect]] |
| 148 | +FloatRect = Union[Tuple[AsFloat, AsFloat, AsFloat, AsFloat], List[AsFloat]] # x, y, width, height |
| 149 | + |
| 150 | + |
| 151 | +# Path handling |
| 152 | +PathLike = Union[str, Path, bytes] |
| 153 | +_POr = TypeVar('_POr') # Allows PathOr[TypeNameHere] syntax |
| 154 | +PathOr = Union[PathLike, _POr] |
| 155 | + |
| 156 | + |
| 157 | +# Specific utility resource aliases with type imports |
| 158 | +if TYPE_CHECKING: |
| 159 | + # The linters are wrong: this is used, so we noqa it |
| 160 | + from arcade.texture import Texture # noqa: F401 |
| 161 | + |
| 162 | +PathOrTexture = PathOr["Texture"] |
| 163 | + |
| 164 | + |
| 165 | +class TiledObject(NamedTuple): |
| 166 | + shape: Union[Point, PointList, Rect] |
| 167 | + properties: Optional[Properties] = None |
| 168 | + name: Optional[str] = None |
| 169 | + type: Optional[str] = None |
0 commit comments