diff --git a/arcade/gl/types.py b/arcade/gl/types.py index 158d38c3eb..f2ba7c2c99 100644 --- a/arcade/gl/types.py +++ b/arcade/gl/types.py @@ -2,6 +2,7 @@ import re from typing import Dict, Optional, Iterable, List, Sequence, Tuple, Union +from typing_extensions import TypeAlias from pyglet import gl @@ -16,6 +17,13 @@ GLuintLike = Union[gl.GLuint, int] PyGLuint = int + +OpenGlFilter: TypeAlias = Tuple[PyGLenum, PyGLenum] +BlendFunction: TypeAlias = Union[ + Tuple[PyGLenum, PyGLenum], + Tuple[PyGLenum, PyGLenum, PyGLenum, PyGLenum] +] + _float_base_format = (0, gl.GL_RED, gl.GL_RG, gl.GL_RGB, gl.GL_RGBA) _int_base_format = ( 0, diff --git a/arcade/scene.py b/arcade/scene.py index d36eb60ddc..033ccec06a 100644 --- a/arcade/scene.py +++ b/arcade/scene.py @@ -12,10 +12,11 @@ from __future__ import annotations -from typing import Dict, List, Optional, Union, Iterable, Tuple +from typing import Dict, List, Optional, Union, Iterable from arcade import Sprite, SpriteList from arcade.types import Color, RGBA255 +from arcade.gl.types import OpenGlFilter, BlendFunction from arcade.tilemap import TileMap from warnings import warn @@ -434,9 +435,9 @@ def update_animation( def draw( self, names: Optional[Iterable[str]] = None, - filter: Optional[int] = None, + filter: Optional[OpenGlFilter] = None, pixelated: bool = False, - blend_function: Optional[Union[Tuple[int, int], Tuple[int, int, int, int]]] = None, + blend_function: Optional[BlendFunction] = None, **kwargs ) -> None: """ diff --git a/arcade/sprite/base.py b/arcade/sprite/base.py index f318f8d8da..68e0d57b30 100644 --- a/arcade/sprite/base.py +++ b/arcade/sprite/base.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Iterable, List, TypeVar +from typing import TYPE_CHECKING, Iterable, List, TypeVar, Any import arcade from arcade.types import Point, Color, RGBA255, PointList @@ -49,7 +49,7 @@ def __init__( scale: float = 1.0, center_x: float = 0, center_y: float = 0, - **kwargs, + **kwargs: Any, ) -> None: self._position = (center_x, center_y) self._depth = 0.0 diff --git a/arcade/sprite/sprite.py b/arcade/sprite/sprite.py index ceeeb3a9a9..3447900885 100644 --- a/arcade/sprite/sprite.py +++ b/arcade/sprite/sprite.py @@ -8,6 +8,7 @@ from arcade.hitbox import HitBox, RotatableHitBox from arcade.texture import get_default_texture from arcade.types import PathOrTexture, Point +from arcade.gl.types import OpenGlFilter, BlendFunction from .base import BasicSprite from .mixins import PymunkMixin @@ -68,7 +69,7 @@ def __init__( center_x: float = 0.0, center_y: float = 0.0, angle: float = 0.0, - **kwargs, + **kwargs: Any, ): if isinstance(path_or_texture, Texture): _texture = path_or_texture @@ -256,7 +257,7 @@ def properties(self) -> Dict[str, Any]: return self._properties @properties.setter - def properties(self, value): + def properties(self, value: Dict[str, Any]): self._properties = value # --- Movement methods ----- @@ -314,7 +315,13 @@ def stop(self) -> None: # ---- Draw Methods ---- - def draw(self, *, filter=None, pixelated=None, blend_function=None) -> None: + def draw( + self, + *, + filter: Optional[OpenGlFilter] = None, + pixelated: Optional[bool] = None, + blend_function: Optional[BlendFunction] = None + ) -> None: """ A debug method which draws the sprite into the current OpenGL context. @@ -400,7 +407,7 @@ def remove_from_sprite_lists(self) -> None: self.physics_engines.clear() - def register_physics_engine(self, physics_engine) -> None: + def register_physics_engine(self, physics_engine: Any) -> None: """ Register a physics engine on the sprite. This is only needed if you actually need a reference diff --git a/arcade/sprite_list/sprite_list.py b/arcade/sprite_list/sprite_list.py index fdc4a42a3d..b8de470e44 100644 --- a/arcade/sprite_list/sprite_list.py +++ b/arcade/sprite_list/sprite_list.py @@ -24,7 +24,7 @@ Union, Generic, Callable, - cast, + cast, Sized, ) from arcade import ( @@ -34,6 +34,7 @@ gl, ) from arcade.types import Color, RGBA255 +from arcade.gl.types import OpenGlFilter, BlendFunction, PyGLenum from arcade.gl.buffer import Buffer from arcade.gl.vertex_array import Geometry @@ -957,7 +958,13 @@ def initialize(self): """ self._init_deferred() - def draw(self, *, filter=None, pixelated=None, blend_function=None): + def draw( + self, + *, + filter: Optional[Union[PyGLenum, OpenGlFilter]] = None, + pixelated: Optional[bool] = None, + blend_function: Optional[BlendFunction] = None + ) -> None: """ Draw this list of sprites. @@ -983,7 +990,12 @@ def draw(self, *, filter=None, pixelated=None, blend_function=None): # Set custom filter or reset to default if filter: - self.atlas.texture.filter = filter, filter + if hasattr(filter, '__len__', ): # assume it's a collection + if len(cast(Sized, filter)) != 2: + raise ValueError("Can't use sequence of length != 2") + self.atlas.texture.filter = tuple(filter) # type: ignore + else: # assume it's an int + self.atlas.texture.filter = cast(OpenGlFilter, (filter, filter)) else: self.atlas.texture.filter = self.ctx.LINEAR, self.ctx.LINEAR diff --git a/arcade/types.py b/arcade/types.py index 6dce6ed517..64c4a99cef 100644 --- a/arcade/types.py +++ b/arcade/types.py @@ -19,7 +19,8 @@ Sequence, Tuple, Union, - TYPE_CHECKING, TypeVar + TYPE_CHECKING, + TypeVar ) from typing_extensions import Self