
    kjJ                         d Z ddlZddlmZ  G d de          Z G d d          Z G d	 d
          Z G d d          ZdS )aU  
Simple row based texture atlas created for fast runtime allocation.

* This atlas is partly based on the texture atlas in the Arcade project
* The allocator is based on Pyglet's row allocator

https://github.com/pyglet/pyglet/blob/master/pyglet/image/atlas.py
https://github.com/pythonarcade/arcade/blob/development/arcade/texture_atlas.py
    N   )	BaseImagec                       e Zd ZdS )AllocatorExceptionN)__name__
__module____qualname__     ]/home/agentuser/manim-venv/lib/python3.11/site-packages/moderngl_window/atlas/simple_atlas.pyr   r      s        Dr   r   c                   V    e Zd ZdZdZdededdfdZded	edeeef         fd
ZddZ	dS )_Rowz%
    A row in the texture atlas.
    )xyy2
max_heightr   r   returnNc                 >    d| _         || _        || _        || _        d S Nr   )r   r   r   r   )selfr   r   s      r   __init__z_Row.__init__   s"    $r   widthheightc                    |dk    s|dk    r#t          d                    ||                    || j        k    rt          d          | j        | j        }}| xj        |z  c_        t          | j        |z   | j                  | _        ||fS )z/Add a region to the row and return the positionr   zCannot allocate size: [{}, {}]z#Cannot allocate past the max height)r   formatr   r   r   maxr   )r   r   r   r   r   s        r   addz_Row.add!   s    A::1$%E%L%LUTZ%[%[\\\DO##$%JKKKvtv1%dfvotw//!tr   c                 .    | j         | j        z
  | _        dS )z
        Compacts the row to the smallest height.
        Should only be done once when the row is filled before adding a new row.
        N)r   r   r   r   s    r   compactz_Row.compact.   s    
 'DF*r   r   N)
r   r   r	   __doc__	__slots__intr   tupler   r    r
   r   r   r   r      s          /I# 3 4     c eCHo    + + + + + +r   r   c                   F    e Zd ZdZdedefdZdededeeef         fdZdS )	AllocatorzRow based allocatorr   r   c                 X    || _         || _        t          d| j                  g| _        d S r   )r   r   r   rowsr   r   r   s      r   r   zAllocator.__init__9   s,    
 !T[))*			r   r   c                    | j         D ]8}| j        |j        z
  |k    r#|j        |k    r|                    ||          c S 9| j        |k    ry| j        |j        z
  |k    rf|                                 t          |j        | j        |j        z
            }| j         	                    |           |                    ||          S t          d                    | ||                    )z
        Allocate a region.

        Returns:
            tuple[int, int]: The x,y location
        Raises:
            AllocatorException: if no more space
        z$No more space in {} for box [{}, {}])r)   r   r   r   r   r   r   r    r   appendr   r   )r   r   r   rownew_rows        r   alloczAllocator.alloc@   s     9 	. 	.CzCE!U**s~/G/Gwwuf----- :4;#76#A#AKKMMM364;#788GIW%%%;;uf--- !G!N!NtUZ\b!c!cdddr   N)r   r   r	   r"   r$   r   r%   r/   r
   r   r   r'   r'   6   sv        +c +3 + + + +e3 e ec3h e e e e e er   r'   c                   \   e Zd ZdZ	 	 	 ddej        dededed	ed
efdZe	dej        fd            Z
e	dej        fd            Ze	defd            Ze	defd            Ze	deeef         fd            Ze	deeef         fd            ZdeddfdZdeddfdZdededdfdZddZdS )TextureAtlasz
    A simple texture atlas using a row based allocation.
    There are more efficient ways to pack textures, but this
    is normally sufficient for dynamic atlases were textures
    are added on the fly runtime.
       r   Tctxr   r   
componentsborderauto_resizec                 T   || _         || _        || _        || _        || _        || _        | j         j        d         | _        | j                             | j	        | j                  | _
        | j                             | j
        g          | _        t          ||          | _        d S )NGL_MAX_VIEWPORT_DIMS)r4   )color_attachments)_ctx_width_height_components_border_auto_resizeinfo	_max_sizetexturesize_textureframebuffer_fbor'   
_allocator)r   r3   r   r   r4   r5   r6   s          r   r   zTextureAtlas.__init__d   s     	%' +/).9O*P 	))$)@P)QQ I))T]O)LL	#E622r   r   c                     | j         S )z)The moderngl contex this atlas belongs to)r:   r   s    r   r3   zTextureAtlas.ctx   s     yr   c                     | j         S )z,The moderngl texture with the atlas contents)rD   r   s    r   textruezTextureAtlas.textrue   s     }r   c                     | j         S )z!int: Width of the atlas in pixels)r;   r   s    r   r   zTextureAtlas.width   s     {r   c                     | j         S )z"int: Height of the atlas in pixels)r<   r   s    r   r   zTextureAtlas.height   s     |r   c                     | j         | j        fS )z5tuple[int, int]: The size of he atlas (width, height))r;   r<   r   s    r   rC   zTextureAtlas.size   s     {DL((r   c                     | j         S )zP
        tuple[int,int]: The maximum size of the atlas in pixels (x, y)
        )rA   r   s    r   max_sizezTextureAtlas.max_size   s    
 ~r   imageNc                     d S Nr
   r   rP   s     r   r   zTextureAtlas.add       r   c                     d S rR   r
   rS   s     r   removezTextureAtlas.remove   rT   r   c                     d S rR   r
   r*   s      r   resizezTextureAtlas.resize   rT   r   c                     d S rR   r
   r   s    r   rebuildzTextureAtlas.rebuild   rT   r   )r2   r   Tr!   )r   r   r	   r"   modernglContextr$   boolr   propertyr3   TexturerJ   r   r   r%   rC   rO   r   r   rV   rX   rZ   r
   r   r   r1   r1   \   s           3 33 3 	3
 3 3 3 3 3 36 X%    X )    X s    X     X )eCHo ) ) ) X) %S/    X t    I $    C           r   r1   )	r"   r[   baser   	Exceptionr   r   r'   r1   r
   r   r   <module>rb      s           	 	 	 	 	 	 	 	+ + + + + + + +D#e #e #e #e #e #e #e #eLM M M M M M M M M Mr   