
    \
j\                         d Z ddlZddlZddlT ddlmZ ddlmZ ddlmZ ddlmZ  e	ed          oej
        Z G d	 d
ej                  Z G d dej                  Ze                    d           dS )a	  Display positioned, scaled and rotated images.

A sprite is an instance of an image displayed on-screen.  Multiple sprites can
display the same image at different positions on the screen.  Sprites can also
be scaled larger or smaller, rotated at any angle and drawn at a fractional
opacity.

The following complete example loads a ``"ball.png"`` image and creates a
sprite for that image.  The sprite is then drawn in the window's
draw event handler::

    import pyglet

    ball_image = pyglet.image.load('ball.png')
    ball = pyglet.sprite.Sprite(ball_image, x=50, y=50)

    window = pyglet.window.Window()

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

    pyglet.app.run()

The sprite can be moved by modifying the :py:attr:`~pyglet.sprite.Sprite.x` and 
:py:attr:`~pyglet.sprite.Sprite.y` properties.  Other
properties determine the sprite's :py:attr:`~pyglet.sprite.Sprite.rotation`,
:py:attr:`~pyglet.sprite.Sprite.scale` and
:py:attr:`~pyglet.sprite.Sprite.opacity`.

By default sprite coordinates are restricted to integer values to avoid
sub-pixel artifacts.  If you require to use floats, for example for smoother
animations, you can set the ``subpixel`` parameter to ``True`` when creating
the sprite (:since: pyglet 1.2).

The sprite's positioning, rotation and scaling all honor the original
image's anchor (:py:attr:`~pyglet.image.AbstractImage.anchor_x`,
:py:attr:`~pyglet.image.AbstractImage.anchor_y`).


Drawing multiple sprites
========================

Sprites can be "batched" together and drawn at once more quickly than if each
of their ``draw`` methods were called individually.  The following example
creates one hundred ball sprites and adds each of them to a :py:class:`~pyglet.graphics.Batch`.  The
entire batch of sprites is then drawn in one call::

    batch = pyglet.graphics.Batch()

    ball_sprites = []
    for i in range(100):
        x, y = i * 10, 50
        ball_sprites.append(pyglet.sprite.Sprite(ball_image, x, y, batch=batch))

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

Sprites can be freely modified in any way even after being added to a batch,
however a sprite can belong to at most one batch.  See the documentation for
:py:mod:`pyglet.graphics` for more details on batched rendering, and grouping of
sprites within batches.

.. versionadded:: 1.1
    N)*)clock)event)graphics)imageis_pyglet_doc_runc                   B     e Zd ZdZd	 fd	Zd Zd Zd Zd Zd Z	 xZ
S )
SpriteGroupzShared sprite rendering group.

    The group is automatically coalesced with other sprite groups sharing the
    same parent group, texture and blend parameters.
    Nc                     t          t          |                               |           || _        || _        || _        dS )a  Create a sprite group.

        The group is created internally within :py:class:`~pyglet.sprite.Sprite`; applications usually
        do not need to explicitly create it.

        :Parameters:
            `texture` : `~pyglet.image.Texture`
                The (top-level) texture containing the sprite image.
            `blend_src` : int
                OpenGL blend source mode; for example,
                ``GL_SRC_ALPHA``.
            `blend_dest` : int
                OpenGL blend destination mode; for example,
                ``GL_ONE_MINUS_SRC_ALPHA``.
            `parent` : `~pyglet.graphics.Group`
                Optional parent group.
        N)superr
   __init__texture	blend_src
blend_dest)selfr   r   r   parent	__class__s        G/DATA/AppData/hermes/venv/lib/python3.11/site-packages/pyglet/sprite.pyr   zSpriteGroup.__init__z   s<    $ 	k4  ))&111"$    c                    t          | j        j                   t          | j        j        | j        j                   t          t                     t          t                     t          | j	        | j
                   d S N)glEnabler   targetglBindTextureidglPushAttribGL_COLOR_BUFFER_BITGL_BLENDglBlendFuncr   r   r   s    r   	set_statezSpriteGroup.set_state   se    $%%%dl)4<?;;;()))DNDO44444r   c                 T    t                       t          | j        j                   d S r   )glPopAttrib	glDisabler   r   r    s    r   unset_statezSpriteGroup.unset_state   s$    $,%&&&&&r   c                 0    | j         j        d| j        dS )N())r   __name__r   r    s    r   __repr__zSpriteGroup.__repr__   s    >222DLLLAAr   c                     |j         | j         u oa| j        |j        u oS| j        j        |j        j        k    o9| j        j        |j        j        k    o| j        |j        k    o| j        |j        k    S r   )r   r   r   r   r   r   r   )r   others     r   __eq__zSpriteGroup.__eq__   sz    4>1 4u|+4#u}';;4 5=#334 %/1	4
 5#33	5r   c                     t          t          | j                  | j        j        | j        j        | j        | j        f          S r   )hashr   r   r   r   r   r   r    s    r   __hash__zSpriteGroup.__hash__   s:    R__\_dl&9^T_6 7 7 	7r   r   )r)   
__module____qualname____doc__r   r!   r%   r*   r-   r0   __classcell__)r   s   @r   r
   r
   s   s         % % % % % %.5 5 5' ' 'B B B5 5 57 7 7 7 7 7 7r   r
   c                      e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdZdZddeeddd	dfd
Zd Zd Zd Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zd Zd Zd Zd Zed             Zej        d             Zed             Z e j        d             Z ed             Z!e!j        d             Z!ed             Z"e"j        d             Z"ed              Z#e#j        d!             Z#ed"             Z$e$j        d#             Z$ed$             Z%e%j        d%             Z%d5d&Z&ed'             Z'ed(             Z(ed)             Z)e)j        d*             Z)ed+             Z*e*j        d,             Z*ed-             Z+e+j        d.             Z+ed/             Z,e,j        d0             Z,ed1             Z-e-j        d2             Z-d3 Z.e/rd4 Z0dS dS )6SpritezQInstance of an on-screen image.

    See the module documentation for usage.
    Nr   F   )r7   r7   r7         ?Tdynamicc
                    ||| _         || _        || _        t          |t          j                  rn|| _        |j        d         j                                        | _	        |j        d         j
        | _        | j        rt          j        | j        | j                   n|                                | _	        t          | j	        |||          | _        || _        |	| _        |                                  dS )a  Create a sprite.

        :Parameters:
            `img` : `~pyglet.image.AbstractImage` or `~pyglet.image.Animation`
                Image or animation to display.
            `x` : int
                X coordinate of the sprite.
            `y` : int
                Y coordinate of the sprite.
            `blend_src` : int
                OpenGL blend source mode.  The default is suitable for
                compositing sprites drawn from back-to-front.
            `blend_dest` : int
                OpenGL blend destination mode.  The default is suitable for
                compositing sprites drawn from back-to-front.
            `batch` : `~pyglet.graphics.Batch`
                Optional batch to add the sprite to.
            `group` : `~pyglet.graphics.Group`
                Optional parent group of the sprite.
            `usage` : str
                Vertex buffer object usage hint, one of ``"none"``,
                ``"stream"``, ``"dynamic"`` (default) or ``"static"``.  Applies
                only to vertex data.
            `subpixel` : bool
                Allow floating-point coordinates for the sprite. By default,
                coordinates are restricted to integer values.
        Nr   )_batch_x_y
isinstancer   	Animation
_animationframesget_texture_textureduration_next_dtr   schedule_once_animater
   _group_usage	_subpixel_create_vertex_list)
r   imgxyr   r   batchgroupusagesubpixels
             r   r   zSprite.__init__   s    F DKc5?++ 	.!DOJqM/;;==DMJqM2DM} B#DM4=AAAOO--DM!$-JNN!  """""r   c                 \    	 | j         | j                                          d S d S #  Y d S xY wr   )_vertex_listdeleter    s    r   __del__zSprite.__del__   sC    	 ,!((***** -,	DDs    & +c                     | j         rt          j        | j                   | j                                         d| _        d| _        d| _        dS )zForce immediate removal of the sprite from video memory.

        This is often necessary when using batches, as the Python garbage
        collector will not necessarily call the finalizer as soon as the
        sprite is garbage.
        N)r@   r   
unschedulerG   rT   rU   rC   rH   r    s    r   rU   zSprite.delete   sR     ? 	,T]+++  """  r   c                 "   | xj         dz  c_         | j         t          | j        j                  k    r%d| _         |                     d           | j        d S | j        j        | j                  }|                     |j                                                   |j	        X|j	        | j
        |z
  z
  }t          t          d|          |j	                  }t          j        | j        |           || _
        d S |                     d           d S )N   r   on_animation_end)_frame_indexlenr@   rA   dispatch_eventrT   _set_texturer   rB   rD   rE   minmaxr   rF   rG   )r   dtframerD   s       r   rG   zSprite._animate  s   QDO$: ; ;;; !D 2333 (&t'89%+1133444>%~);<H3q(++U^<<Hx888$DMMM 233333r   c                     | j         S )zGraphics batch.

        The sprite can be migrated from one batch to another, or removed from
        its batch (for individual drawing).  Note that this can be an expensive
        operation.

        :type: :py:class:`pyglet.graphics.Batch`
        )r;   r    s    r   rO   zSprite.batch"  s     {r   c                    | j         |k    rd S |<| j         5| j                             | j        t          | j        |           || _         d S | j                                         || _         |                                  d S r   )r;   migraterT   GL_QUADSrH   rU   rK   )r   rO   s     r   rO   zSprite.batch.  s    ;%F!8K 18T[%PPPDKKK$$&&&DK$$&&&&&r   c                     | j         j        S )zParent graphics group.

        The sprite can change its rendering group, however this can be an
        expensive operation.

        :type: :py:class:`pyglet.graphics.Group`
        )rH   r   r    s    r   rP   zSprite.group;  s     {!!r   c                     | j         j        |k    rd S t          | j        | j         j        | j         j        |          | _         | j        3| j                            | j        t          | j         | j                   d S d S r   )
rH   r   r
   rC   r   r   r;   rf   rT   rg   )r   rP   s     r   rP   zSprite.groupF  s    ;&&F!$-"&+"7"&+"8"') ) ;"K 18T[ $- - - - - #"r   c                 ,    | j         r| j         S | j        S )zImage or animation to display.

        :type: :py:class:`~pyglet.image.AbstractImage` or
               :py:class:`~pyglet.image.Animation`
        )r@   rC   r    s    r   r   zSprite.imageR  s     ? 	#?"}r   c                    | j          t          j        | j                   d | _         t	          |t
          j                  r|| _         d| _        |                     |j	        d         j        
                                           |j	        d         j        | _        | j        rt          j        | j        | j                   n'|                     |
                                           |                                  d S )Nr   )r@   r   rX   rG   r>   r   r?   r\   r_   rA   rB   rD   rE   rF   _update_position)r   rL   s     r   r   zSprite.image]  s    ?&T]+++"DOc5?++ 	1!DO !Dcjm1==??@@@JqM2DM} B#DM4=AAAcoo//000r   c                 v   |j         | j        j         urt          || j        j        | j        j        | j        j                  | _        | j        |j        | j	        j        d d <   nK| j	        
                                 || _        |                                  n|j        | j	        j        d d <   || _        d S r   )r   rC   r
   rH   r   r   r   r;   
tex_coordsrT   rU   rK   )r   r   s     r   r_   zSprite._set_texturen  s    :T]---%g&*k&;&*k&<&*k&8: :DK {"292D!,QQQ//!((*** '((****.5.@D(+r   c           	      b   | j         rd| j        z  }n
d| j        z  }| j        )t          j        d|dd| j        j        f          | _        n:| j                            dt          | j
        |dd| j        j        f          | _        |                                  |                                  d S )Nzv2f/%szv2i/%s   c4Bt3f)rJ   rI   r;   r   vertex_listrC   rn   rT   addrg   rH   rl   _update_color)r   vertex_formats     r   rK   zSprite._create_vertex_list~  s    > 	3$t{2MM$t{2M; ( 4=%%1I)J!L !LD !%8T[-H`@a!c !cDr   c                     | j         }| j        | j        z  }| j        | j        z  }| j        sd}n| j        r|j         |z  }|j         |z  }||j        |z  z   }||j	        |z  z   }| j
        }	| j        }
t          j        | j                   }t          j        |          }t          j        |          }||z  ||z  z
  |	z   }||z  ||z  z   |
z   }||z  ||z  z
  |	z   }||z  ||z  z   |
z   }||z  ||z  z
  |	z   }||z  ||z  z   |
z   }||z  ||z  z
  |	z   }||z  ||z  z   |
z   }||||||||f}n|dk    s|dk    rI| j
        |j        |z  z
  }| j        |j        |z  z
  }||j        |z  z   }||j	        |z  z   }||||||||f}n<| j
        |j        z
  }| j        |j        z
  }||j        z   }||j	        z   }||||||||f}| j        st#          |d                   t#          |d                   t#          |d                   t#          |d                   t#          |d                   t#          |d                   t#          |d	                   t#          |d
                   f}|| j        j        d d <   d S )N)r   r   r   r   r   r   r   r   r8   r   rZ         rp            )rC   _scale_scale_x_scale_y_visible	_rotationanchor_xanchor_ywidthheightr<   r=   mathradianscossinrJ   intrT   vertices)r   rL   scale_xscale_yr   x1y1x2y2rM   rN   rcrsraxaybxbycxcydxdys                         r   rl   zSprite._update_position  s   m+-+-} !	8/HH^ 	8,(B,(Bci'))Bcj7**BAAdn---A!B!Bb27"Q&Bb27"Q&Bb27"Q&Bb27"Q&Bb27"Q&Bb27"Q&Bb27"Q&Bb27"Q&BBBBB7HH^^w#~~3<'11B3<'11Bci'))Bcj7**BBBBB7HH3<'B3<'BciBcjBBBBB7H~ 	<HQK((#hqk*:*:HQK((#hqk*:*:HQK((#hqk*:*:HQK((#hqk*:*:<H )1"111%%%r   c                 p    | j         \  }}}|||t          | j                  gdz  | j        j        d d <   d S )Nrp   )_rgbr   _opacityrT   colors)r   r   gbs       r   ru   zSprite._update_color  sA    )1a'(!QDM0B0B&Ca&G ###r   c                     | j         | j        fS )zThe (x, y) coordinates of the sprite, as a tuple.

        :Parameters:
            `x` : int
                X coordinate of the sprite.
            `y` : int
                Y coordinate of the sprite.
        )r<   r=   r    s    r   positionzSprite.position  s     wr   c                 L    |\  | _         | _        |                                  d S r   )r<   r=   rl   )r   r   s     r   r   zSprite.position  s(    #r   c                     | j         S )z8X coordinate of the sprite.

        :type: int
        )r<   r    s    r   rM   zSprite.x       wr   c                 <    || _         |                                  d S r   )r<   rl   )r   rM   s     r   rM   zSprite.x  !    r   c                     | j         S )z8Y coordinate of the sprite.

        :type: int
        )r=   r    s    r   rN   zSprite.y  r   r   c                 <    || _         |                                  d S r   )r=   rl   )r   rN   s     r   rN   zSprite.y  r   r   c                     | j         S )zClockwise rotation of the sprite, in degrees.

        The sprite image will be rotated about its image's (anchor_x, anchor_y)
        position.

        :type: float
        )r   r    s    r   rotationzSprite.rotation  s     ~r   c                 <    || _         |                                  d S r   )r   rl   )r   r   s     r   r   zSprite.rotation  s!    !r   c                     | j         S )zBase Scaling factor.

        A scaling factor of 1 (the default) has no effect.  A scale of 2 will
        draw the sprite at twice the native size of its image.

        :type: float
        )r}   r    s    r   scalezSprite.scale  s     {r   c                 <    || _         |                                  d S r   )r}   rl   )r   r   s     r   r   zSprite.scale  s!    r   c                     | j         S )zHorizontal scaling factor.

         A scaling factor of 1 (the default) has no effect.  A scale of 2 will
         draw the sprite at twice the native width of its image.

        :type: float
        )r~   r    s    r   r   zSprite.scale_x       }r   c                 <    || _         |                                  d S r   )r~   rl   )r   r   s     r   r   zSprite.scale_x  !    r   c                     | j         S )zVertical scaling factor.

         A scaling factor of 1 (the default) has no effect.  A scale of 2 will
         draw the sprite at twice the native height of its image.

        :type: float
        )r   r    s    r   r   zSprite.scale_y  r   r   c                 <    || _         |                                  d S r   )r   rl   )r   r   s     r   r   zSprite.scale_y#  r   r   c                     ||| _         ||| _        ||| _        ||| _        ||| _        ||| _        |                                  dS )a  Simultaneously change the position, rotation or scale.

        This method is provided for performance. In cases where
        multiple Sprite attributes need to be updated at the same
        time, it is more efficent to update them together using
        the update method, rather than modifying them one by one.

        :Parameters:
            `x` : int
                X coordinate of the sprite.
            `y` : int
                Y coordinate of the sprite.
            `rotation` : float
                Clockwise rotation of the sprite, in degrees.
            `scale` : float
                Scaling factor.
            `scale_x` : float
                Horizontal scaling factor.
            `scale_y` : float
                Vertical scaling factor.
        N)r<   r=   r   r}   r~   r   rl   )r   rM   rN   r   r   r   r   s          r   updatezSprite.update(  sh    , =DG=DG%DNDK#DM#DMr   c                    | j         r6| j        j        t          | j                  z  t          | j                  z  S t          | j        j        t          | j                  z  t          | j                  z            S )zgScaled width of the sprite.

        Read-only.  Invariant under rotation.

        :type: int
        )rJ   rC   r   absr~   r}   r   r    s    r   r   zSprite.widthL  si     > 	T=&T]););;c$+>N>NNNt}*S-?-??#dkBRBRRSSSr   c                    | j         r6| j        j        t          | j                  z  t          | j                  z  S t          | j        j        t          | j                  z  t          | j                  z            S )zhScaled height of the sprite.

        Read-only.  Invariant under rotation.

        :type: int
        )rJ   rC   r   r   r   r}   r   r    s    r   r   zSprite.heightY  si     > 	U='#dm*<*<<s4;?O?OOOt}+c$-.@.@@3t{CSCSSTTTr   c                     | j         S )a  Blend opacity.

        This property sets the alpha component of the colour of the sprite's
        vertices.  With the default blend mode (see the constructor), this
        allows the sprite to be drawn with fractional opacity, blending with the
        background.

        An opacity of 255 (the default) has no effect.  An opacity of 128 will
        make the sprite appear translucent.

        :type: int
        )r   r    s    r   opacityzSprite.opacityf  s     }r   c                 <    || _         |                                  d S r   )r   ru   )r   r   s     r   r   zSprite.opacityv  s!    r   c                     | j         S )aT  Blend color.

        This property sets the color of the sprite's vertices. This allows the
        sprite to be drawn with a color tint.

        The color is specified as an RGB tuple of integers '(red, green, blue)'.
        Each color component must be in the range 0 (dark) to 255 (saturated).

        :type: (int, int, int)
        )r   r    s    r   colorzSprite.color{  s     yr   c                 |    t          t          t          |                    | _        |                                  d S r   )listmapr   r   ru   )r   rgbs     r   r   zSprite.color  s1    S#''	r   c                     | j         S )z?True if the sprite will be drawn.

        :type: bool
        )r   r    s    r   visiblezSprite.visible  s     }r   c                 <    || _         |                                  d S r   )r   rl   )r   r   s     r   r   zSprite.visible  r   r   c                     | j         S )zPause/resume the Sprite's Animation

        If `Sprite.image` is an Animation, you can pause or resume
        the animation by setting this property to True or False.
        If not an Animation, this has no effect.

        :type: bool
        )_pausedr    s    r   pausedzSprite.paused  s     |r   c                    t          | d          r|| j        k    rd S |du rt          j        | j                   nI| j        j        | j                 }|j        | _	        | j	        rt          j
        | j        | j	                   || _        d S )Nr@   T)hasattrr   r   rX   rG   r@   rA   r\   rD   rE   rF   )r   pauserc   s      r   r   zSprite.paused  s    t\** 	et|.C.CFD==T]++++O*4+<=E!NDM} B#DM4=AAAr   c                     | j         S )zThe current Animation frame.

        If the `Sprite.image` is an `Animation`,
        you can query or set the current frame.
        If not an Animation, this will always
        be 0.

        :type: int
        )r\   r    s    r   frame_indexzSprite.frame_index  s       r   c           	          | j         d S t          dt          |t          | j         j                  dz
                      | _        d S )Nr   rZ   )r@   ra   r`   r]   rA   r\   )r   indexs     r   r   zSprite.frame_index  sE     ?"F3uc$/2H.I.I!.K#L#LMMr   c                     | j                                          | j                            t                     | j                                          dS )zDraw the sprite at its current position.

        See the module documentation for hints on drawing multiple sprites
        efficiently.
        N)rH   set_state_recursiverT   drawrg   unset_state_recursiver    s    r   r   zSprite.draw  sI     	'')))x((())+++++r   c                     dS )a  The sprite animation reached the final frame.

            The event is triggered only if the sprite has an animation, not an
            image.  For looping animations, the event is triggered each time
            the animation loops.

            :event:
            N r    s    r   r[   zSprite.on_animation_end  s      r   )NNNNNN)1r)   r1   r2   r3   r;   r@   r\   r   r   r   r   r}   r~   r   r   rT   GL_SRC_ALPHAGL_ONE_MINUS_SRC_ALPHAr   rV   rU   rG   propertyrO   setterrP   r   r_   rK   rl   ru   r   rM   rN   r   r   r   r   r   r   r   r   r   r   r   r   r   _is_pyglet_doc_runr[   r   r   r   r6   r6      s        
 FJLGIHDFHHHL Q'2 5# 5# 5# 5#n     4 4 4& 	 	 X	 \
' 
' \
' " " X" \	- 	- \	-   X \    \          +1 +1 +1ZH H H 	  	  X	  _    _    X X    X    X X    X    X _    _    X \    \    X ^    ^    X ^    ^ "  "  "  " H 
T 
T X
T 
U 
U X
U   X ^  ^   X \  \   X ^    ^  	 	 X	 ]
 
 ]
 
! 
! X
! N N N, , ,  		 	 	 	 		 	r   r6   r[   )r3   sysr   	pyglet.glpygletr   r   r   r   r   r   r   Groupr
   EventDispatcherr6   register_event_typer   r   r   <module>r      s  HA AF 


                             WS"566P3;P 87 87 87 87 87(. 87 87 87vm m m m mU" m m m`   - . . . . .r   