
    lj(@                    ^   U d Z ddlmZ ddlmZmZmZ ddlmZm	Z	 ddl
ZddlmZ ddlmZmZ ddlmZ dd	lmZmZ dd
lmZmZ ddlmZ ddlmZ ddlmZ ddlm Z  erddl!m"Z" ddl#m$Z$ g dZ%dZ&de'd<    G d de          Z( G d de(          Z) G d de(          Z* G d de*          Z+dS )z1Tools for displaying multiple animations at once.    )annotations)CallableIterableSequence)TYPE_CHECKINGAnyN)config)	Animationprepare_animation)RendererType)GroupMobject)OpenGLGroupOpenGLMobject)Scene)remove_list_redundancies)flatten_iterable_parameters)linear)OpenGLVGroup)VGroup)AnimationGroup
SuccessionLaggedStartLaggedStartMapg?floatDEFAULT_LAGGED_START_LAG_RATIOc                  x     e Zd ZdZddeddd" fdZd#dZd$dZd%dZd$dZ	d%dZ
d&dZd'dZd$dZd(d!Z xZS ))r   a  Plays a group or series of :class:`~.Animation`.

    Parameters
    ----------
    animations
        Sequence of :class:`~.Animation` objects to be played.
    group
        A group of multiple :class:`~.Mobject`.
    run_time
        The duration of the animation in seconds.
    rate_func
        The function defining the animation progress based on the relative
        runtime (see :mod:`~.rate_functions`) .
    lag_ratio
        Defines the delay after which the animation is applied to submobjects. A lag_ratio of
        ``n.nn`` means the next animation will play when ``nnn%`` of the current animation has played.
        Defaults to 0.0, meaning that all animations will be played together.

        This does not influence the total runtime of the animation. Instead the runtime
        of individual animations is adjusted so that the complete animation has the defined
        run time.
    Nr   )grouprun_time	rate_func	lag_ratio
animationsAnimation | Iterable[Animation]r   2Group | VGroup | OpenGLGroup | OpenGLVGroup | Noner   float | Noner    Callable[[float], float]r!   r   kwargsr   c                  t          |          }d |D             | _        || _        |Wt          d | j        D                       }t          d         t
          j        k    rt          | | _        nt          | | _        n|| _         t                      j        | j        f| j        |d| |                     |          | _        d S )Nc                ,    g | ]}t          |          S  )r   .0anims     V/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/animation/composition.py
<listcomp>z+AnimationGroup.__init__.<locals>.<listcomp>@   s!    HHHt,T22HHH    c                D    g | ]}|                                 |j        S r*   )is_introducermobjectr+   s     r.   r/   z+AnimationGroup.__init__.<locals>.<listcomp>D   s+    VVV$ASASAUAUVVVVr0   renderer)r    r!   )r   r"   r    r   r	   r   OPENGLr   r   r   super__init__init_run_timer   )
selfr   r   r    r!   r"   r'   arg_animmobjects	__class__s
            r.   r7   zAnimationGroup.__init__6   s     /z::HHxHHH"=/VV$/VVV H j!\%888JUK

 #H-

DJJ	
"&.I	
 	
IO	
 	
 	
  $11(;;r0   return!Sequence[Mobject | OpenGLMobject]c                *    t          | j                  S N)listr   r9   s    r.   get_all_mobjectszAnimationGroup.get_all_mobjectsS   s    DJr0   Nonec                    | j         st          d|  d          d| _        | j        r| j                                         | j         D ]}|                                 d S )NTrying to play Q without animations, this is not supported. Please add at least one subanimation.        )r"   
ValueErroranim_group_timesuspend_mobject_updatingr   suspend_updatingbeginr9   r-   s     r.   rM   zAnimationGroup.beginV   s     	8$ 8 8 8    #( 	*J'')))O 	 	DJJLLLL	 	r0   scener   c                D    | j         D ]}|                    |           d S r@   )r"   _setup_scener9   rO   r-   s      r.   rQ   zAnimationGroup._setup_sceneb   s4    O 	% 	%De$$$$	% 	%r0   c                    | j         D ]}|                                 d| j        d d <   d| j        d d <   | j        r| j                                         d S d S )NT)r"   finishanims_begunanims_finishedrK   r   resume_updatingrN   s     r.   rT   zAnimationGroup.finishf   ss    O 	 	DKKMMMM"!%AAA( 	)J&&(((((	) 	)r0   c                    |                      |           | j        D ]*}| j        r| j        |_        |                    |           +d S r@   )
_on_finishr"   removerclean_up_from_scenerR   s      r.   r[   z"AnimationGroup.clean_up_from_scenen   sY    O 	, 	,D| ,#|$$U++++	, 	,r0   dtc                x    | j         d         | j        | j         z           D ]}|                    |           d S )Nr-   )anims_with_timingsrU   rV   update_mobjects)r9   r\   r-   s      r.   r_   zAnimationGroup.update_mobjectsu   sR    +F3 333
 	% 	%D   $$$$	% 	%r0   c                    |                                   t          | j        d         d          | _        || j        n|S )a(  Calculates the run time of the animation, if different from ``run_time``.

        Parameters
        ----------
        run_time
            The duration of the animation in seconds.

        Returns
        -------
        run_time
            The duration of the animation in seconds.
        endr   )default)build_animations_with_timingsmaxr^   max_end_time)r9   r   s     r.   r8   zAnimationGroup.init_run_time{   sH     	**,,,   7 >JJJ$,$4t  (Br0   c                   t          j        d | j        D                       }|j        d         }g d}t          j        ||          | _        t          j        |t                    | _        t          j        |t                    | _        |dk    rdS |dd         | j	        z  }| j        | j        d<   t           j
                            |          | j        d         d	d<   | j        d         |z   | j        d
<   dS )zDCreates a list of triplets of the form (anim, start_time, end_time).c                    g | ]	}|j         
S r*   )r   r+   s     r.   r/   z@AnimationGroup.build_animations_with_timings.<locals>.<listcomp>   s    HHHdmHHHr0   r   ))r-   O)startf8)ra   rj   )dtypeNr-   ri      ra   )nparrayr"   shapezerosr^   boolrU   rV   r!   add
accumulate)r9   	run_timesnum_animationsrk   lagss        r.   rc   z,AnimationGroup.build_animations_with_timings   s    HHHHHHII	"+???.0h~U.S.S.S')xd'K'K'K*,(>*N*N*NQF"~.*./'/1v/@/@/F/F(,)-)@)II)U&&&r0   alphac                   |                      |          | j        z  }|| j        k     }| j        }||d         k    }||d         k    }|| j        |z  | j         | z  z           }|d         |d         z
  }|dk    }	d||	<   ||d         z
  |z  }
|rd|
|
dk     |	z  <   nd|
|
dk    |	z  <   t          |d         |
d          D ]\  }}|                    |           || _        || _        || _        d S )Nri   ra   r   rm   r-   T)strict)r    re   rJ   r^   rU   rV   zipinterpolate)r9   rx   rJ   time_goes_backawt	new_begunnew_finished	to_updateru   with_zero_run_time
sub_alphasanim_to_update	sub_alphas                r.   r|   zAnimationGroup.interpolate   sP    ..//$2CC(4+?? %#s7|3	&U3	)t/B.Bl].RS
	 e$y'99	&!^()	$%%	'(::iG
 	B@AJ
Q*<<==@AJ
Q*<<=),fz$*
 *
 *
 	2 	2%NI &&y1111.$*r0   )r"   r#   r   r$   r   r%   r    r&   r!   r   r'   r   )r=   r>   r=   rD   )rO   r   r=   rD   r\   r   r=   rD   )r   r%   r=   r   rx   r   r=   rD   )__name__
__module____qualname____doc__r   r7   rC   rM   rQ   rT   r[   r_   r8   rc   r|   __classcell__r<   s   @r.   r   r      s        4 EI!%.4< < < < < < < <:       
 
 
 
% % % %) ) ) ), , , ,% % % %C C C C.V V V V !+ !+ !+ !+ !+ !+ !+ !+r0   r   c                  b     e Zd ZdZddd fd
ZddZddZddZddZddZ	ddZ
ddZ xZS ) r   a  Plays a series of animations in succession.

    Parameters
    ----------
    animations
        Sequence of :class:`~.Animation` objects to be played.
    lag_ratio
        Defines the delay after which the animation is applied to submobjects. A lag_ratio of
        ``n.nn`` means the next animation will play when ``nnn%`` of the current animation has played.
        Defaults to 1.0, meaning that the next animation will begin when 100% of the current
        animation has played.

        This does not influence the total runtime of the animation. Instead the runtime
        of individual animations is adjusted so that the complete animation has the defined
        run time.

    Examples
    --------
    .. manim:: SuccessionExample

        class SuccessionExample(Scene):
            def construct(self):
                dot1 = Dot(point=LEFT * 2 + UP * 2, radius=0.16, color=BLUE)
                dot2 = Dot(point=LEFT * 2 + DOWN * 2, radius=0.16, color=MAROON)
                dot3 = Dot(point=RIGHT * 2 + DOWN * 2, radius=0.16, color=GREEN)
                dot4 = Dot(point=RIGHT * 2 + UP * 2, radius=0.16, color=YELLOW)
                self.add(dot1, dot2, dot3, dot4)

                self.play(Succession(
                    dot1.animate.move_to(dot2),
                    dot2.animate.move_to(dot3),
                    dot3.animate.move_to(dot4),
                    dot4.animate.move_to(dot1)
                ))
    rm   r!   r"   r
   r!   r   r'   r   c               >     t                      j        |d|i| d S Nr!   r6   r7   r9   r!   r"   r'   r<   s       r.   r7   zSuccession.__init__   s*    *D	DVDDDDDr0   r=   rD   c                d    | j         st          d|  d          |                     d           d S )NrF   rG   r   )r"   rI   update_active_animationrB   s    r.   rM   zSuccession.begin   sP     	8$ 8 8 8   	$$Q'''''r0   c                N    | j         |                                  | j         d S d S r@   )active_animationnext_animationrB   s    r.   rT   zSuccession.finish   s4    #/!!! #/////r0   r\   c                L    | j         r| j                             |           d S d S r@   )r   r_   )r9   r\   s     r.   r_   zSuccession.update_mobjects   s4      	6!11"55555	6 	6r0   rO   Scene | Nonec                    |d S |                                  r?| j        D ]7}|                                 s!|j        |                    |j                   8|| _        d S r@   )r2   r"   r3   rs   rO   rR   s      r.   rQ   zSuccession._setup_scene   sl    =F 	, , ,))++ ,0HIIdl+++


r0   indexintc                f   || _         |t          | j                  k    rd | _        d | _        d | _        d S | j        |         | _        | j                            | j                   | j                                         | j	        |         d         | _        | j	        |         d         | _        d S )Nri   ra   )
active_indexlenr"   r   active_start_timeactive_end_timerQ   rO   rM   r^   )r9   r   s     r.   r   z"Succession.update_active_animation  s    !C((((6:D!37D"15D   $(OE$:D!!..tz:::!'')))%)%<U%CG%LD"#'#:5#A%#HD   r0   c                    | j         | j                                          |                     | j        dz              dS )zqProceeds to the next animation.

        This method is called right when the active animation finishes.
        Nrm   )r   rT   r   r   rB   s    r.   r   zSuccession.next_animation  sD    
  ,!((***$$T%6%:;;;;;r0   rx   c                N   |                      |          | j        z  }| j        1|| j        k    r&|                                  | j        || j        k    &| j        F| j        A|| j        z
  }| j        j        }|dk    r||z  nd}| j                            |           d S d S d S )NrH   g      ?)r    re   r   r   r   r   r   r|   )r9   rx   current_timeelapsedactive_run_timesubalphas         r.   r|   zSuccession.interpolate  s    ~~e,,t/@@".<4CW3W3W!!! ".<4CW3W3W ,1G1S"T%;;G"3<O4Cs4J4Jw00PSH!--h77777	 -,1S1Sr0   r"   r
   r!   r   r'   r   r   r   )rO   r   r=   rD   )r   r   r=   rD   r   )r   r   r   r   r7   rM   rT   r_   rQ   r   r   r|   r   r   s   @r.   r   r      s        " "H CD E E E E E E E E( ( ( (" " " "6 6 6 6   I I I I< < < <8 8 8 8 8 8 8 8r0   r   c                  *     e Zd ZdZedd
 fd	Z xZS )r   a  Adjusts the timing of a series of :class:`~.Animation` according to ``lag_ratio``.

    Parameters
    ----------
    animations
        Sequence of :class:`~.Animation` objects to be played.
    lag_ratio
        Defines the delay after which the animation is applied to submobjects. A lag_ratio of
        ``n.nn`` means the next animation will play when ``nnn%`` of the current animation has played.
        Defaults to 0.05, meaning that the next animation will begin when 5% of the current
        animation has played.

        This does not influence the total runtime of the animation. Instead the runtime
        of individual animations is adjusted so that the complete animation has the defined
        run time.

    Examples
    --------
    .. manim:: LaggedStartExample

        class LaggedStartExample(Scene):
            def construct(self):
                title = Text("lag_ratio = 0.25").to_edge(UP)

                dot1 = Dot(point=LEFT * 2 + UP, radius=0.16)
                dot2 = Dot(point=LEFT * 2, radius=0.16)
                dot3 = Dot(point=LEFT * 2 + DOWN, radius=0.16)
                line_25 = DashedLine(
                    start=LEFT + UP * 2,
                    end=LEFT + DOWN * 2,
                    color=RED
                )
                label = Text("25%", font_size=24).next_to(line_25, UP)
                self.add(title, dot1, dot2, dot3, line_25, label)

                self.play(LaggedStart(
                    dot1.animate.shift(RIGHT * 4),
                    dot2.animate.shift(RIGHT * 4),
                    dot3.animate.shift(RIGHT * 4),
                    lag_ratio=0.25,
                    run_time=4
                ))
    r   r"   r
   r!   r   r'   r   c               >     t                      j        |d|i| d S r   r   r   s       r.   r7   zLaggedStart.__init__V  s,     	*D	DVDDDDDr0   r   r   r   r   r   r   r7   r   r   s   @r.   r   r   )  sc        * *^ :E E E E E E E E E E E Er0   r   c                  ,     e Zd ZdZddefd fdZ xZS )r   aT  Plays a series of :class:`~.Animation` while mapping a function to submobjects.

    Parameters
    ----------
    animation_class
        :class:`~.Animation` to apply to mobject.
    mobject
        :class:`~.Mobject` whose submobjects the animation, and optionally the function,
        are to be applied.
    arg_creator
        Function which will be applied to :class:`~.Mobject`.
    run_time
        The duration of the animation in seconds.
    lag_ratio
        Defines the delay after which the animation is applied to submobjects. A lag_ratio of
        ``n.nn`` means the next animation will play when ``nnn%`` of the current animation has played.
        Defaults to 0.05, meaning that the next animation will begin when 5% of the current
        animation has played.

        This does not influence the total runtime of the animation. Instead the runtime
        of individual animations is adjusted so that the complete animation has the defined
        run time.
    kwargs
        Further keyword arguments that are passed to `animation_class`.

    Examples
    --------
    .. manim:: LaggedStartMapExample

        class LaggedStartMapExample(Scene):
            def construct(self):
                title = Tex("LaggedStartMap").to_edge(UP, buff=LARGE_BUFF)
                dots = VGroup(
                    *[Dot(radius=0.16) for _ in range(35)]
                    ).arrange_in_grid(rows=5, cols=7, buff=MED_LARGE_BUFF)
                self.add(dots, title)

                # Animate yellow ripple effect
                for mob in dots, title:
                    self.play(LaggedStartMap(
                        ApplyMethod, mob,
                        lambda m : (m.set_color, YELLOW),
                        lag_ratio = 0.1,
                        rate_func = there_and_back,
                        run_time = 2
                    ))
    N   animation_classtype[Animation]r3   r   arg_creator)Callable[[Mobject], Iterable[Any]] | Noner   r   r!   r'   r   c                   
 d	d}|fd|D             }t          |          
d
v r
                    d           
fd|D             }	 t                      j        |	||d d S )
Nmobr   r=   c                    | S r@   r*   )r   s    r.   identityz)LaggedStartMap.__init__.<locals>.identity  s    
r0   c                &    g | ]} |          S r*   r*   )r,   submobr   s     r.   r/   z+LaggedStartMap.__init__.<locals>.<listcomp>  s#    ???V[[((???r0   r!   c                     g | ]
} |i S r*   r*   )r,   argsanim_kwargsr   s     r.   r/   z+LaggedStartMap.__init__.<locals>.<listcomp>  s(    RRRoot;{;;RRRr0   )r   r!   )r   r   r=   r   )dictpopr6   r7   )r9   r   r3   r   r   r!   r'   r   	args_listr"   r   r<   s    ` `      @r.   r7   zLaggedStartMap.__init__  s         #K????w???	6ll+%%OOK(((RRRRR	RRR
*x9MMMMMMr0   )r   r   r3   r   r   r   r   r   r!   r   r'   r   r   r   s   @r.   r   r   _  se        . .h BF9N N N N N N N N N N Nr0   r   ),r   
__future__r   collections.abcr   r   r   typingr   r   numpyrn   manim._configr	   manim.animation.animationr
   r   manim.constantsr   manim.mobject.mobjectr   r   #manim.mobject.opengl.opengl_mobjectr   r   manim.scene.scener   manim.utils.iterablesr   manim.utils.parameter_parsingr   manim.utils.rate_functionsr   .manim.mobject.opengl.opengl_vectorized_mobjectr   &manim.mobject.types.vectorized_mobjectr   __all__r   __annotations__r   r   r   r   r*   r0   r.   <module>r      sB   7 7 7 " " " " " " 8 8 8 8 8 8 8 8 8 8 % % % % % % % %                 B B B B B B B B ( ( ( ( ( ( 0 0 0 0 0 0 0 0 J J J J J J J J # # # # # # : : : : : : E E E E E E - - - - - - >KKKKKK======
K
K
K )-  , , , ,e+ e+ e+ e+ e+Y e+ e+ e+P`8 `8 `8 `8 `8 `8 `8 `8F3E 3E 3E 3E 3E. 3E 3E 3ElFN FN FN FN FN[ FN FN FN FN FNr0   