55from pathlib import Path
66from typing import (
77 TYPE_CHECKING ,
8- Dict ,
9- List ,
108 Optional ,
119 Sequence ,
12- Tuple ,
1310 Union ,
1411)
1512from weakref import WeakSet , WeakValueDictionary , finalize
@@ -94,7 +91,7 @@ class DefaultTextureAtlas(TextureAtlasBase):
9491
9592 def __init__ (
9693 self ,
97- size : Tuple [int , int ],
94+ size : tuple [int , int ],
9895 * ,
9996 border : int = 1 ,
10097 textures : Optional [Sequence ["Texture" ]] = None ,
@@ -104,7 +101,7 @@ def __init__(
104101 ):
105102 self ._ctx = ctx or get_window ().ctx
106103 self ._max_size = self ._ctx .info .MAX_VIEWPORT_DIMS
107- self ._size : Tuple [int , int ] = size
104+ self ._size : tuple [int , int ] = size
108105 self ._allocator = Allocator (* self ._size )
109106 self ._auto_resize = auto_resize
110107 self ._capacity = capacity
@@ -143,8 +140,8 @@ def __init__(
143140 # The texture regions are clones of the image regions with transforms applied
144141 # in order to map the same image using different orders or texture coordinates.
145142 # The key is the cache name for a texture
146- self ._image_regions : Dict [str , AtlasRegion ] = dict ()
147- self ._texture_regions : Dict [str , AtlasRegion ] = dict ()
143+ self ._image_regions : dict [str , AtlasRegion ] = dict ()
144+ self ._texture_regions : dict [str , AtlasRegion ] = dict ()
148145
149146 # Ref counter for images and textures. Per atlas we need to keep
150147 # track of ho many times an image is used in textures to determine
@@ -162,7 +159,7 @@ def __init__(
162159 # All textures added to the atlas
163160 self ._textures : WeakSet [Texture ] = WeakSet ()
164161 # atlas_name: Set of textures with matching atlas name
165- self ._unique_textures : Dict [str , WeakSet ["Texture" ]] = dict ()
162+ self ._unique_textures : dict [str , WeakSet ["Texture" ]] = dict ()
166163
167164 # Add all the textures
168165 for tex in textures or []:
@@ -187,7 +184,7 @@ def max_height(self) -> int:
187184 return self ._max_size [1 ]
188185
189186 @property
190- def max_size (self ) -> Tuple [int , int ]:
187+ def max_size (self ) -> tuple [int , int ]:
191188 """
192189 The maximum size of the atlas in pixels (x, y)
193190 """
@@ -227,7 +224,7 @@ def texture_uv_texture(self) -> "Texture2D":
227224 return self ._texture_uvs .texture
228225
229226 @property
230- def textures (self ) -> List ["Texture" ]:
227+ def textures (self ) -> list ["Texture" ]:
231228 """
232229 All textures instance added to the atlas regardless
233230 of their internal state. See :py:meth:`unique_textures``
@@ -236,7 +233,7 @@ def textures(self) -> List["Texture"]:
236233 return list (self ._textures )
237234
238235 @property
239- def unique_textures (self ) -> List ["Texture" ]:
236+ def unique_textures (self ) -> list ["Texture" ]:
240237 """
241238 All unique textures in the atlas.
242239
@@ -245,23 +242,23 @@ def unique_textures(self) -> List["Texture"]:
245242 can be found in :py:meth:`textures`.
246243 """
247244 # Grab the first texture from each set
248- textures : List [Texture ] = []
245+ textures : list [Texture ] = []
249246 for tex_set in self ._unique_textures .values ():
250247 if len (tex_set ) == 0 :
251248 raise RuntimeError ("Empty set in unique textures" )
252249 textures .append (next (iter (tex_set )))
253250 return textures
254251
255252 @property
256- def images (self ) -> List ["ImageData" ]:
253+ def images (self ) -> list ["ImageData" ]:
257254 """
258255 Return a list of all the images in the atlas.
259256
260257 A new list is constructed from the internal weak set of images.
261258 """
262259 return list (self ._images .values ())
263260
264- def add (self , texture : "Texture" ) -> Tuple [int , AtlasRegion ]:
261+ def add (self , texture : "Texture" ) -> tuple [int , AtlasRegion ]:
265262 """
266263 Add a texture to the atlas.
267264
@@ -271,7 +268,7 @@ def add(self, texture: "Texture") -> Tuple[int, AtlasRegion]:
271268 """
272269 return self ._add (texture )
273270
274- def _add (self , texture : "Texture" , create_finalizer = True ) -> Tuple [int , AtlasRegion ]:
271+ def _add (self , texture : "Texture" , create_finalizer = True ) -> tuple [int , AtlasRegion ]:
275272 """
276273 Internal add method with additional control. We we rebuild the atlas
277274 we don't want to create finalizers for the texture or they will be
@@ -367,7 +364,7 @@ def remove(self, texture: "Texture") -> None:
367364 "and let the python garbage collector handle the removal."
368365 )
369366
370- def _allocate_texture (self , texture : "Texture" ) -> Tuple [int , AtlasRegion ]:
367+ def _allocate_texture (self , texture : "Texture" ) -> tuple [int , AtlasRegion ]:
371368 """
372369 Add or update a unique texture in the atlas.
373370 This is mainly responsible for updating the texture coordinates
@@ -391,7 +388,7 @@ def _allocate_texture(self, texture: "Texture") -> Tuple[int, AtlasRegion]:
391388
392389 return slot , texture_region
393390
394- def _allocate_image (self , image_data : "ImageData" ) -> Tuple [int , int , int , AtlasRegion ]:
391+ def _allocate_image (self , image_data : "ImageData" ) -> tuple [int , int , int , AtlasRegion ]:
395392 """
396393 Attempts to allocate space for an image in the atlas or
397394 update the existing space for the image.
@@ -593,7 +590,7 @@ def has_image(self, image_data: "ImageData") -> bool:
593590 """Check if an image is already in the atlas"""
594591 return image_data .hash in self ._images
595592
596- def resize (self , size : Tuple [int , int ]) -> None :
593+ def resize (self , size : tuple [int , int ], force = False ) -> None :
597594 """
598595 Resize the atlas.
599596
@@ -607,19 +604,21 @@ def resize(self, size: Tuple[int, int]) -> None:
607604 undefined state.
608605
609606 :param size: The new size
607+ :param force: Force a resize even if the size is the same
610608 """
611609 LOG .info ("[%s] Resizing atlas from %s to %s" , id (self ), self ._size , size )
612610 # print("Resizing atlas from", self._size, "to", size)
613611
614612 # Only resize if the size actually changed
615- if size == self ._size :
613+ if size == self ._size and not force :
616614 return
617615
618616 self ._check_size (size )
619617 resize_start = time .perf_counter ()
620618
621619 # Keep a reference to the old atlas texture so we can copy it into the new one
622620 atlas_texture_old = self ._texture
621+ atlas_texture_old .filter = self ._ctx .NEAREST , self ._ctx .NEAREST
623622 self ._size = size
624623
625624 # Create new image uv data temporarily keeping the old one
@@ -729,7 +728,7 @@ def use_uv_texture(self, unit: int = 0) -> None:
729728 def render_into (
730729 self ,
731730 texture : "Texture" ,
732- projection : Optional [Tuple [float , float , float , float ]] = None ,
731+ projection : Optional [tuple [float , float , float , float ]] = None ,
733732 ):
734733 """
735734 Render directly into a sub-section of the atlas.
@@ -812,7 +811,7 @@ def to_image(
812811 flip : bool = False ,
813812 components : int = 4 ,
814813 draw_borders : bool = False ,
815- border_color : Tuple [int , int , int ] = (255 , 0 , 0 ),
814+ border_color : tuple [int , int , int ] = (255 , 0 , 0 ),
816815 ) -> Image .Image :
817816 """
818817 Convert the atlas to a Pillow image.
@@ -854,7 +853,7 @@ def show(
854853 flip : bool = False ,
855854 components : int = 4 ,
856855 draw_borders : bool = False ,
857- border_color : Tuple [int , int , int ] = (255 , 0 , 0 ),
856+ border_color : tuple [int , int , int ] = (255 , 0 , 0 ),
858857 ) -> None :
859858 """
860859 Show the texture atlas using Pillow.
@@ -880,7 +879,7 @@ def save(
880879 flip : bool = False ,
881880 components : int = 4 ,
882881 draw_borders : bool = False ,
883- border_color : Tuple [int , int , int ] = (255 , 0 , 0 ),
882+ border_color : tuple [int , int , int ] = (255 , 0 , 0 ),
884883 ) -> None :
885884 """
886885 Save the texture atlas to a png.
@@ -901,7 +900,7 @@ def save(
901900 border_color = border_color ,
902901 ).save (path , format = "png" )
903902
904- def _check_size (self , size : Tuple [int , int ]) -> None :
903+ def _check_size (self , size : tuple [int , int ]) -> None :
905904 """Check it the atlas exceeds the hardware limitations"""
906905 if size [0 ] > self ._max_size [0 ] or size [1 ] > self ._max_size [1 ]:
907906 raise Exception (
0 commit comments