
    lj0                        d Z ddlmZ g dZddlZddlmZ ddlmZ ddl	Z
ddlmZ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Zd*dZd+dZd,dZedfd-dZdez  fd.d Z	 d/d0d'Zd1d(ZdS )2z7Utility functions for continuous animation of mobjects.    )annotations)assert_is_mobject_methodalwaysf_alwaysalways_redrawalways_shiftalways_rotateturn_animation_into_updatercycle_animationN)Callable)TYPE_CHECKING)DEGREESRIGHT)Mobject)OpenGLMobject)	normalize)	Animationmethodr   returnNonec                |    t          j        |           sJ | j        }t          |t          t
          f          sJ d S N)inspectismethod__self__
isinstancer   r   )r   mobjects     h/home/agentuser/manim-venv/lib/python3.11/site-packages/manim/animation/updaters/mobject_update_utils.pyr   r       sA    F#####oGg78888888    r   c                z    t          |            | j        }| j        |                    fd           |S )Nc                     | gR i S r    )margsfunckwargss    r   <lambda>zalways.<locals>.<lambda>*   s!    $$q":4":":":6":": r   r   r   __func__add_updater)r   r$   r&   r   r%   s    `` @r   r   r   &   sJ    V$$$oG?D::::::;;;Nr   Callable[[Mobject], None]c                ~    t          |            | j        }| j        fd}|                    |           |S )z
    More functional version of always, where instead
    of taking in args, it takes in functions which output
    the relevant arguments.
    c                8    d D             } | g|R i  d S )Nc                "    g | ]} |            S r"   r"   ).0arg_generators     r   
<listcomp>z-f_always.<locals>.updater.<locals>.<listcomp>9   s    DDDMDDDr   r"   )mobr$   arg_generatorsr%   r&   s     r   updaterzf_always.<locals>.updater8   s<    DD^DDDS"4"""6"""""r   r(   )r   r3   r&   r   r4   r%   s    ``  @r   r   r   .   sa     V$$$oG?D# # # # # # #    Nr   r%   Callable[[], Mobject]c                P                                         fd           S )a  Redraw the mobject constructed by a function every frame.

    This function returns a mobject with an attached updater that
    continuously regenerates the mobject according to the
    specified function.

    Parameters
    ----------
    func
        A function without (required) input arguments that returns
        a mobject.

    Examples
    --------

    .. manim:: TangentAnimation

        class TangentAnimation(Scene):
            def construct(self):
                ax = Axes()
                sine = ax.plot(np.sin, color=RED)
                alpha = ValueTracker(0)
                point = always_redraw(
                    lambda: Dot(
                        sine.point_from_proportion(alpha.get_value()),
                        color=BLUE
                    )
                )
                tangent = always_redraw(
                    lambda: TangentLine(
                        sine,
                        alpha=alpha.get_value(),
                        color=YELLOW,
                        length=4
                    )
                )
                self.add(ax, sine, point, tangent)
                self.play(alpha.animate.set_value(1), rate_func=linear, run_time=2)
    c                >                                                S r   )become)_r%   r2   s    r   r'   zalways_redraw.<locals>.<lambda>i   s    cjj00 r   r*   )r%   r2   s   `@r   r   r   @   s6    P $&&COO00000111Jr   g?r   	directionnp.ndarray[np.float64]ratefloatc                <    |                      fd           | S )a~  A mobject which is continuously shifted along some direction
    at a certain rate.

    Parameters
    ----------
    mobject
        The mobject to shift.
    direction
        The direction to shift. The vector is normalized, the specified magnitude
        is not relevant.
    rate
        Length in Manim units which the mobject travels in one
        second along the specified direction.

    Examples
    --------

    .. manim:: ShiftingSquare

        class ShiftingSquare(Scene):
            def construct(self):
                sq = Square().set_fill(opacity=1)
                tri = Triangle()
                VGroup(sq, tri).arrange(LEFT)

                # construct a square which is continuously
                # shifted to the right
                always_shift(sq, RIGHT, rate=5)

                self.add(sq)
                self.play(tri.animate.set_fill(opacity=1))
    c                T    |                      |z  t                    z            S r   )shiftr   )r#   dtr;   r=   s     r   r'   zalways_shift.<locals>.<lambda>   s$    aggb4i)I:N:N.N&O&O r   r:   )r   r;   r=   s    ``r   r   r   m   s-    F OOOOOPPPNr      c                <    |                      fd           | S )a&  A mobject which is continuously rotated at a certain rate.

    Parameters
    ----------
    mobject
        The mobject to be rotated.
    rate
        The angle which the mobject is rotated by
        over one second.
    kwags
        Further arguments to be passed to :meth:`.Mobject.rotate`.

    Examples
    --------

    .. manim:: SpinningTriangle

        class SpinningTriangle(Scene):
            def construct(self):
                tri = Triangle().set_fill(opacity=1).set_z_index(2)
                sq = Square().to_edge(LEFT)

                # will keep spinning while there is an animation going on
                always_rotate(tri, rate=2*PI, about_point=ORIGIN)

                self.add(tri, sq)
                self.play(sq.animate.to_edge(RIGHT), rate_func=linear, run_time=1)
    c                &     | j         |z  fi S r   )rotate)r#   rB   r&   r=   s     r   r'   zalways_rotate.<locals>.<lambda>   s    hahrDy&C&CF&C&C r   r:   )r   r=   r&   s    ``r   r	   r	      s,    : CCCCCDDDNr   F	animationr   cyclebooldelayc                      j         }d _                                          |  _        d fd|                               |S )a  
    Add an updater to the animation's mobject which applies
    the interpolation and update functions of the animation

    If cycle is True, this repeats over and over.  Otherwise,
    the updater will be popped upon completion

    The ``delay`` parameter is the delay (in seconds) before the animation starts..

    Examples
    --------

    .. manim:: WelcomeToManim

        class WelcomeToManim(Scene):
            def construct(self):
                words = Text("Welcome to")
                banner = ManimBanner().scale(0.5)
                VGroup(words, banner).arrange(DOWN)

                turn_animation_into_updater(Write(words, run_time=0.9))
                self.add(words)
                self.wait(0.5)
                self.play(banner.expand(), run_time=0.5)
    Fr#   r   rB   r>   c                "   j         dk    r                                }|dk    rU                    d                               |                                            |                                d S j         |z  }r|dz  }nGt          j        |dd          }|dk    r+                                 |                                d S                     |                               |           xj         |z  c_         d S )Nr      )
total_timeget_run_timeinterpolateupdate_mobjectsfinishremove_updaternpclip)r#   rB   run_time
time_ratioalpharG   rH   updates        r   rY   z+turn_animation_into_updater.<locals>.update   s)   1$$ --//H 1}}%%a((())"---  """  ((("-8J "Q
Aq11A::$$&&&$$V,,,F!!%(((%%b)))"r   )r#   r   rB   r>   )r   suspend_mobject_updatingbeginrN   r*   )rG   rH   rJ   r&   r   rY   s   ``   @r   r
   r
      st    8 G).I&OO!6I# # # # # # # #4 Nr   c                     t          | fddi|S )NrH   T)r
   )rG   r&   s     r   r   r      s    &yGGGGGGr   )r   r   r   r   )r   r   r   r   )r   r+   r   r   )r%   r5   r   r   )r   r   r;   r<   r=   r>   r   r   )r   r   r=   r>   r   r   )Fr   )rG   r   rH   rI   rJ   r>   r   r   )rG   r   r   r   )__doc__
__future__r   __all__r   collections.abcr   typingr   numpyrT   manim.constantsr   r   manim.mobject.mobjectr   manim.openglr   manim.utils.space_opsr   manim.animation.animationr   r   r   r   r   r   r	   r
   r   r"   r   r   <module>rh      s   = = " " " " " "	 	 	  $ $ $ $ $ $                 * * * * * * * * ) ) ) ) ) ) & & & & & & + + + + + + 43333339 9 9 9      $* * * *\ ;@s$ $ $ $ $N 35w,     D ?@< < < < <~H H H H H Hr   