
    ^jQ                    j   d Z ddlmZ ddlmZ ddlZddlmZmZm	Z	m
Z
mZmZmZmZ ddlmZmZmZmZmZ erddlmZmZmZ ddlmZmZmZ  G d	 d
ej        j                  Zej        j        dej        j         diZ!ej        j        dej        j         diZ"ddZ# G d dej$        j%                  Z&dS )zdAllows for a somewhat generic multitextured sprite.

This sprite behaves just like regular sprites.
    )annotations)TYPE_CHECKINGN)glActiveTextureglBindTextureglEnableGL_BLENDglBlendFunc	glDisableglGetIntegervGLint)GL_SRC_ALPHAGL_TEXTURE0GL_ONE_MINUS_SRC_ALPHAGL_TRIANGLESGL_MAX_TEXTURE_IMAGE_UNITS)TextureAbstractImage	Animation)BatchGroupShaderProgramc                  P     e Zd ZdZ	 dd fdZddZddZddZddZddZ	 xZ
S )MultiTextureSpriteGroupa  Shared Multi-texture Sprite rendering Group.

    The Group defines custom ``__eq__`` and ``__hash__`` methods, and so will be
    automatically coalesced with other MultiTexture Sprite Groups sharing the same
    parent Group, Textures, and blend parameters.
    Ntexturesdict[str, Texture]	blend_srcint
blend_destprogramShaderProgram | NoneparentGroup | NonereturnNonec                    t                                          |           || _        || _        || _        || _        dS )a  Create a sprite group for multiple textures and samplers.

        All textures must share the same target type.  The group is created internally
        when a :py:class:`~pyglet.multitexture_sprite.MultiTextureSprite` is created;
        applications usually do not need to explicitly create it.

        Args:
            textures:
                A dictionary of textures, with the keys being the GLSL sampler name.
            blend_src:
                OpenGL blend source mode; for example, ``GL_SRC_ALPHA``.
            blend_dest:
                OpenGL blend destination mode; for example, ``GL_ONE_MINUS_SRC_ALPHA``.
            program:
                A custom ShaderProgram.
            parent:
                Optional parent group.
        )r!   N)super__init__	_texturesr   r   r   )selfr   r   r   r   r!   	__class__s         b/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/experimental/multitexture_sprite.pyr'   z MultiTextureSpriteGroup.__init__   s@    ( 	'''!"$    c                   | j                                          t          | j                  D ]\  }}|| j         |<   t          | j                                                  D ]6\  }}t          t          |z              t          |j        |j	                   7t          t                     t          | j        | j                   dS )zCalled before this group is drawn to setup the shader state.

        The shader program is activated than then all textures are bound and the
        blend mode is setup.
        N)r   use	enumerater(   valuesr   r   r   targetidr   r   r	   r   r   )r)   idxnameitextures        r+   	set_statez!MultiTextureSpriteGroup.set_state4   s     	"4>22 	% 	%IC!$DL#DN$9$9$;$;<< 	6 	6JAwK!O,,,'.'*5555DNDO44444r,   c                    t          t                     | j                                         t	          t
                     dS )zCalled after all draw calls for the group have been made.

        When done the blend mode is disabled, the shader program is deactivated,
        and all textures are deactivated.
        N)r
   r   r   stopr   r   r)   s    r+   unset_statez#MultiTextureSpriteGroup.unset_stateF   s:     	($$$$$r,   strc                h    | j         j         dd | j                                        D              dS )N(c                    g | ]	\  }}||f
S  r@   ).0r4   r6   s      r+   
<listcomp>z4MultiTextureSpriteGroup.__repr__.<locals>.<listcomp>Q   s     ,g,g,gWd7^,g,g,gr,   ))r*   __name__r(   itemsr:   s    r+   __repr__z MultiTextureSpriteGroup.__repr__P   s=    .)jj,g,gPTP^PdPdPfPf,g,g,gjjjjr,   other(object | Group | MultiTextureSpriteGroupboolc                   |j         | j         urdS t          | j                  t          |j                  k    rdS | j                                        D ]J\  }}||j        vr dS |j        |j        |         j        k    s|j        |j        |         j        k    r dS K| j        |j        u o| j        |j        k    o| j        |j        k    S )zDetermines if this group is the same as the other group.

        The method is broken out like this to make it easier to follow and read.
        F)	r*   lenr(   rE   r2   r1   r   r   r   )r)   rG   r4   r6   s       r+   __eq__zMultiTextureSpriteGroup.__eq__S   s    
 ?$.005t~#eo"6"6665!^1133 	 	MD'5?**uuzU_T25555?[_K`Kg9g9guu :h - 4%/145#33	5r,   c                   t          | j        | j        | j        | j        ft          d | j                                        D                       z   t          d | j                                        D                       z             S )Nc                    g | ]	}|j         
S r@   )r2   rA   r6   s     r+   rB   z4MultiTextureSpriteGroup.__hash__.<locals>.<listcomp>n   s     S S S S S Sr,   c                    g | ]	}|j         
S r@   )r1   rO   s     r+   rB   z4MultiTextureSpriteGroup.__hash__.<locals>.<listcomp>o   s     W W WG W W Wr,   )hashr!   r   r   r   tupler(   r0   r:   s    r+   __hash__z MultiTextureSpriteGroup.__hash__j   s    T[$,>?,   S S4>;P;P;R;R S S STTU   W Wt~?T?T?V?V W W WXX	Y Z Z 	Zr,   )NN)r   r   r   r   r   r   r   r    r!   r"   r#   r$   r#   r$   )r#   r<   )rG   rH   r#   rI   )r#   r   )rD   
__module____qualname____doc__r'   r7   r;   rF   rL   rS   __classcell__r*   s   @r+   r   r      s          OS      45 5 5 5$% % % %k k k k5 5 5 5.Z Z Z Z Z Z Z Zr,   r   	sampler2Dsampler2DArrayz.xy imagesr   r#   r   c                l   t                      }t          t          |           t          |           |j        k    sJ d|j         d            d                    d |                                 D                       }d                    d |                                 D                       }d                    d |                                 D                       }d| d| d	| d
}d                    d |                                 D                       }d                    d |                                 D                       }d                    d |                                 D                       }d| d| d| d}t          j	        j
                            |df|df          S )a  Creates the default multi-texture shader based on the dict of textures passed in.

    The default shader program will 'overlay' each texture layer on top of each other taking
    into account of the alpha channel of each texture.  Textures can be either normal
    2D textures or 2D texture arrays.  The maximum number of textures you can layer is
    determined by the maximum number of samplers you can have in a fragment shader.
    zOnly z Texture Units are available.
c                    g | ]}d | d	S )in vec3 _coords;r@   rA   r4   s     r+   rB   z*_get_default_mt_shader.<locals>.<listcomp>   s$    SSST8$888SSSr,   c                    g | ]}d | d	S )z	out vec3 _coords_frag;r@   rc   s     r+   rB   z*_get_default_mt_shader.<locals>.<listcomp>   s$    ZZZD ?D ? ? ?ZZZr,   c                    g | ]
}| d | dS )z_coords_frag = rb   r@   rc   s     r+   rB   z*_get_default_mt_shader.<locals>.<listcomp>   s*    'i'i'iSW4(N(N(N(N(N'i'i'ir,   z
    #version 150 core

    in vec3 translate;
    in vec4 colors;
    in vec2 scale;
    in vec3 position;
    in float rotation;

    z#

    out vec4 vertex_colors;

    a  

    uniform WindowBlock {
        mat4 projection;
        mat4 view;
    } window;
    mat4 m_scale = mat4(1.0);
    mat4 m_rotation = mat4(1.0);
    mat4 m_translate = mat4(1.0);

    void main()
    {
        m_scale[0][0] = scale.x;
        m_scale[1][1] = scale.y;
        m_translate[3][0] = translate.x;
        m_translate[3][1] = translate.y;
        m_translate[3][2] = translate.z;
        m_rotation[0][0] =  cos(-radians(rotation));
        m_rotation[0][1] =  sin(-radians(rotation));
        m_rotation[1][0] = -sin(-radians(rotation));
        m_rotation[1][1] =  cos(-radians(rotation));
        gl_Position = window.projection * window.view * m_translate * m_rotation * m_scale * vec4(position, 1.0);
        vertex_colors = colors;

        z
    }
    c                    g | ]}d | d	S )ra   re   r@   rc   s     r+   rB   z*_get_default_mt_shader.<locals>.<listcomp>   s$    XXX$=$===XXXr,   c                F    g | ]\  }}d t           |j                  d| dS )zuniform  ;)_SAMPLER_TYPESr1   rA   r4   texs      r+   rB   z*_get_default_mt_shader.<locals>.<listcomp>   s:    !q!q!qV^VZ[^"Q^CJ-G"Q"Q$"Q"Q"Q!q!q!qr,   c           	     L    g | ]!\  }}d | d| dt           |j                  d"S )z  color = layer(texture(z, _coords_fragz
), color);)_SAMPLER_COORDSr1   rl   s      r+   rB   z*_get_default_mt_shader.<locals>.<listcomp>   sc       ^   ^   ^  BK  BF  HK |4 | |4 | |UdeheoUp | | |   ^   ^   ^r,   z7
    #version 150 core

    in vec4 vertex_colors;
    z"

    out vec4 final_colors;

    z

    vec4 layer(vec4 foreground, vec4 background) {
        return foreground * foreground.a + background * (1.0 - foreground.a);
    }

    void main() {
        vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
        z9
        final_colors = color * vertex_colors;
    }
    vertexfragment)r   r   r   rK   valuejoinkeysrE   pygletglcurrent_contextcreate_program)	r]   max_texin_tex_coordsout_tex_coordstex_coords_assignmentsvertex_sourceuniform_samplerstex_operationsfragment_sources	            r+   _get_default_mt_shaderr      s    ggG,g666v;;'-''')])])])]''' IISSV[[]]SSSTTMYYZZFKKMMZZZ[[N!YY'i'i[a[f[f[h[h'i'i'ijj' ' ' ' 'J 
 K' ' 'MR IIXX&++--XXXYYMyy!q!qbhbnbnbpbp!q!q!qrrYY   ^   ^  OU  O[  O[  O]  O]   ^   ^   ^  _  _N 	     
!  O* 9$33]H4MP_akOlmmmr,   c            	      2    e Zd ZdZeZdddeeddddf	d3dZd4 fdZ	d5dZ
d4dZd6dZd4d Zd7d$Zd8d%Zd9d'Zd:d*Zed4d+            Zej        d;d-            Zed4d.            Zej        d<d/            Zed=d0            Zej        d>d2            Z xZS )?MultiTextureSpritea  Creates a multi-textured sprite.

    Multi-textured sprites behave just like regular sprites except they can
    contain multiple texture layers.  The default behavior is to overlay each
    layer on top of each other.  Each texture layer can be either a static image
    or an animation.  If the default behavior is not desired then a custom
    shader program can be supplied overriding the default program.

    The following complete example loads a 2 layer sprite and draws it to the
    screen::

      import pyglet

      batch = pyglet.graphics.Batch()

      logo_image = pyglet.image.load('logo.png')
      kitten_image = pyglet.image.load('kitten.png')
      sprite = pyglet.experimental.MultiTextureSprite({'kitten': kitten_image, 'logo': logo_image},
                                                      x=50, y=50, batch=batch)

      window = pyglet.window.Window()

      @window.event
      def on_draw():
        batch.draw()

      pyglet.app.run()

    If a custom program is provided then several assumptions are made by the
    MultiTextureSprite class.

    * The vertex shader is expected to have each texture coordinates passed in
      with variables named ``<name>_coords`` as vec3 types where ``<name>`` is
      the name of the layer given to the constructor.
    * The fragment shader is expected to have samplers of the appropriate type
      with variables named ``<name>`` where ``<name>`` is the name of the layer
      given to the constructor.

    r   NFr]   $dict[str, AbstractImage | Animation]xfloatyzr   r   r   batchBatch | Nonegroupr"   subpixelrI   r   r    r#   r$   c                6   i | _         d| _        |                                D ]\  }}t          |t          j        j                  r-|j        d         j                                        | j         |<   n|                                | j         |<   | j        r@| j         |         j	        | j         |         j
        z  | j        j	        | j        j
        z  k    r| j         |         | _        |
st          | j                   | _        n|
| _        || _        || _        || _        i | _        |                                D ]\  }}t          |t          j        j                  rg|d|j        d         j        d| j        |<   |j        d         j        r7t          j                            | j        | j        |         d         |           || _        || _        || _        || _        |                                 | _        |	| _        |                                  dS )a  Create a Sprite instance.

        If the texture layers are different sizes then the largest texture by area is picked for
        size of the sprite and then the other layers are scaled to that size.

        Args:
            images:
                A dict object with the key being the name of the texture and the
                value is either an Animation or AbstractImage. Currently,
                each item must be of the same size.
            x:
                X coordinate of the sprite.
            y:
                Y coordinate of the sprite.
            z:
                Z coordinate of the sprite.
            blend_src:
                OpenGL blend source mode.  The default is suitable for
                compositing sprites drawn from back-to-front.
            blend_dest:
                OpenGL blend destination mode.  The default is suitable for
                compositing sprites drawn from back-to-front.
            batch:
                Optional batch to add the sprite to.
            group:
                Optional parent group of the sprite.
            subpixel:
                Allow floating-point coordinates for the sprite. By default,
                coordinates are restricted to integer values.
            program:
                A specific shader program to initialize the sprite with.  The
                default multi-texture overlay shader will be used if one is
                not provided.
        Nr   	animation	frame_idxnext_dtr   )r(   _texturerE   
isinstancerv   imager   framesget_textureheightwidthr   _program_x_y_z_animationsdurationclockschedule_once_animate_batch
_blend_src_blend_dest_user_groupget_sprite_group_group	_subpixel_create_vertex_list)r)   r]   r   r   r   r   r   r   r   r   r   r4   imgs                r+   r'   zMultiTextureSprite.__init__   s   X  	5 	5ID##v|566 9'*z!}':'F'F'H'Ht$$'*'8'8t$= 5T^D%9%@4>RVCWC]%]bfbobvy}  zG  zM  cM  %N  %N $t 4 	$24>BBDMM#DM   	g 	gID##v|566 g 9<!X[XbcdXeXn)p)p &:a=) gL..t}d>Nt>TU^>_aefff#% ++--! 	  """""r,   c                    | j         r$t          j                            | j                   t                                                       dS )zForce immediate removal of the sprite from video memory.

        This method removes any scheduled animation calls and then calls
        pyglet.sprite.Sprite.delete method.
        N)r   rv   r   
unscheduler   r&   delete)r)   r*   s    r+   r   zMultiTextureSprite.deleteT  sB      	3L##DM222r,   r   c                f    |                      | j        | j        | j        | j        | j                  S )zCreates and returns a group to be used to render the sprite.

        This is used internally to create a consolidated group for rendering.
        )group_classr(   r   r   r   r   r:   s    r+   r   z#MultiTextureSprite.get_sprite_group_  s/    
 AQSWS`bfbrsssr,   c                R   || j         v r| j         |         }|dxx         dz  cc<   |d         t          |d         j                  k    r#d|d<   |                     d           | j        d S |d         j        |d                  }|j        |j        |d         |z
  z
  }t          t          d|          |j                  }||d<   |                     ||j	        
                                           t          j                            | j        |d         |           d S d S d S )Nr      r   r   on_animation_endr   )r   rK   r   dispatch_event_vertex_listr   minmax_set_multi_texturer   r   rv   r   r   r   )r)   dtkeyr   framer   s         r+   r   zMultiTextureSprite._animatef  s<   $"""(-Ik"""a'"""%Y{-C-J)K)KKK)*	+&##$6777$,Fk*1)K2HIE~) >Yy-AB-FGs1h//@@'/	)$''U[-D-D-F-FGGG**4=)I:NPSTTTTT! #" *)r,   new_texr   c                   |j         | j        |         j         urp| j                                        | _        || j        |<   | j                                         |                                 | _        |                                  d S || j        |<   | j        |         j        t          | j        | d          d d <   d S )N_coords)
r2   r(   copyr   r   r   r   r   
tex_coordsgetattr)r)   r   r   s      r+   r   z%MultiTextureSprite._set_multi_texturey  s    :T^C0333
 "^0022DN")DN3$$&&&//11DK$$&&&&&")DN3<@N3<O<ZGD%ooo66qqq999r,   c           
        i }| j                                         D ]\  }}d|j        f|| d<    | j        j        dt
          g d| j        | j        fd|                                 fd| j	        dz  fd| j
        | j        | j        fdz  fd| j        | j        z  | j        | j        z  fdz  fd| j        fdz  fd|| _        dS )zZ
        Override so we can send over texture coords for each texture being used.
        fr      )r   r      r   r      Bn)positioncolors	translatescalerotationN)r(   rE   r   r   vertex_list_indexedr   r   r   _get_vertices_rgbar   r   r   _scale_scale_x_scale_y	_rotationr   )r)   r   r4   rm   s       r+   r   z&MultiTextureSprite._create_vertex_list  s    
--// 	A 	AID#,/+@J$'''((=DM=|///dk4--//0$*q.)TWdgtw7!;<t}4dkDM6QRUVVWDN,q01   r,   r4   r<   r   c                   || j         v rx| j         |         }|t          |d         j                  k     rO||d<   |d         j        |d                  }|                     ||j                                                   dS dS dS )aU  Set the current Animation frame for the requested texture layer

        If the texture layer isn't an animation then this method has no effect.

        Args:
            name:
              The dict key given for the texture layer in the constructor
            frame_idx:
              The frame index to set for the given layer.
        r   r   N)r   rK   r   r   r   r   )r)   r4   r   r   r   s        r+   set_frame_indexz"MultiTextureSprite.set_frame_index  s     4###(.I3y5<====)2	+&!+.5i6LM''ek.E.E.G.GHHHHH $#==r,   c                B    || j         v r| j         |         }|d         S dS )a  Get the current Animation frame for the requested texture layer

        If the texture layer isn't an animation then this method always returns 0.

        Args:
            name:
              The dict key given for the texture layer in the constructor
        r   r   )r   )r)   r4   r   s      r+   get_frame_indexz"MultiTextureSprite.get_frame_index  s/     4###(.I[))qr,    AbstractImage | Animation | Nonec                ^    || j         v r| j         |         S || j        v r| j        |         S dS )zReturn the requested layer.  If it is not found then None is returned

        Args:
            name:
              The dict key given for the texture layer in the constructor.
        N)r   r(   )r)   r4   s     r+   	get_layerzMultiTextureSprite.get_layer  s?     4####D))T^##>$''tr,   r   AbstractImage | Animationc                   || j         v r>t          j                            | j                   | j                             |           d}t          |t          j        j                  r|d|j	        d         j
        d| j         |<   |j	        d         j
        r>| j        s7t          j                            | j        | j         |         d         |           |j	        d         j                                        }n|                                }|r]|                     ||           |j        |j        z  | j        j        | j        j        z  k    r|| _        |                                  dS dS dS )a  Sets the layer to the new image or animation.

        This method has no effect if name is not a valid layer.  Note: if you
        want to swap out a layer which is an animation then this will cause
        all other animated layers for this sprite to pause until the swap is done.

        Args:
            name:
              The dict key given for the texture layer in the constructor
            img:
               The Image or Animation to set
        Nr   r   r   )r   rv   r   r   r   popr   r   r   r   r   _pausedr   r   r   r   r   r   _update_position)r)   r4   r   rm   s       r+   	set_layerzMultiTextureSprite.set_layer  sh    4###L##DM222  &&& c6<122 	$36QSVS]^_S`Si%j%jDT"z!}% cdl c**4=$:J4:PQZ:[]abbb*Q-%1133CC//##C 	(##D#...	CJ&4=+>AU+UVV #%%'''''	( 	( WVr,   c                     t          d          )Nz[MultiTextureSprite does not support the frame_index property.  Use get_frame_index instead.NotImplementedErrorr:   s    r+   frame_indexzMultiTextureSprite.frame_index      !"  A  A  	Ar,   indexc                     t          d          )Nz[MultiTextureSprite does not support the frame_index property.  Use set_frame_index instead.r   )r)   r   s     r+   r   zMultiTextureSprite.frame_index  r   r,   c                     t          d          )NzOMultiTextureSprite does not support the image property.  Use get_layer instead.r   r:   s    r+   r   zMultiTextureSprite.image      !"stttr,   c                     t          d          )NzOMultiTextureSprite does not support the image property.  Use set_layer instead.r   )r)   r   s     r+   r   zMultiTextureSprite.image  r   r,   c                    | j         S )N)r   r:   s    r+   pausedzMultiTextureSprite.paused  s
    |r,   pausec                   t          | d          r|| j        k    rd S || _        |r&t          j                            | j                   d S | j                                        D ]f\  }}|d         j        |d                  }|j	        rA|j	        |d<   t          j        
                    | j        | j        |         d         |           gd S )Nr   r   r   r   )hasattrr   rv   r   r   r   r   rE   r   r   r   )r)   r   r4   r   r   s        r+   r   zMultiTextureSprite.paused  s    t]++ 	u/D/DF 	gL##DM22222 $(#3#9#9#;#; g gi!+.5i6LM> g+0>Ii(L..t}d>Nt>TU^>_aefff	g gr,   )r]   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r"   r   rI   r   r    r#   r$   rT   )r#   r   )r   r   r#   r$   )r4   r<   r   r   r#   r$   )r4   r<   r#   r   )r4   r<   r#   r   )r4   r<   r   r   r#   r$   )r   r   r#   r$   )r   r   r#   r$   )r#   rI   )r   rI   r#   r$   )rD   rU   rV   rW   r   r   r   r   r'   r   r   r   r   r   r   r   r   r   propertyr   setterr   r   rX   rY   s   @r+   r   r      s       & &N *K !".#9'+'+"'15T# T# T# T# T#l	 	 	 	 	 	t t t tU U U U&[ [ [ [   "I I I I$      $( $( $( $(L A A A XA A A A A u u u Xu \u u u \u    X ]g g g ]g g g g gr,   r   )r]   r   r#   r   )'rW   
__future__r   typingr   rv   	pyglet.glr   r   r   r   r	   r
   r   r   r   r   r   r   r   pyglet.imager   r   r   pyglet.graphicsr   r   r   graphicsr   rw   GL_TEXTURE_2DGL_TEXTURE_2D_ARRAYrk   rp   r   spriteSpriter   r@   r,   r+   <module>r      s    # " " " " "              v v v v v v v v v v v v v v v v v v v v q q q q q q q q q q q q q q <>>>>>>>>>>;;;;;;;;;;]Z ]Z ]Z ]Z ]Zfo3 ]Z ]Z ]ZD I[
I!#3 IU
I!2Rn Rn Rn Rnj}g }g }g }g }g- }g }g }g }g }gr,   