a
    «ˆcõ  ã                   @   sž   d Z ddlZddlmZ zddlmZ dZW n eyB   dZY n0 ddlmZ ddl	m
Z
mZmZmZmZ dd	lmZmZmZ G d
d„ deƒZddd„ZdS )z%
Simple utility code for animations.
é    N)Úwraps)Ú	decoratorTF)ÚTimer)ÚAnyÚ	HasTraitsÚButtonÚInstanceÚRange)ÚViewÚGroupÚItemc                   @   s”   e Zd ZdZedƒZedƒZeddddd�Ze	Z
eeed	ƒed
ƒdd�edƒedd�ddgd�Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS ) ÚAnimatora=   Convenience class to manage a timer and present a convenient
        UI.  This is based on the code in `tvtk.tools.visual`.
        Here is a simple example of using this class::

            >>> from mayavi import mlab
            >>> def anim():
            ...     f = mlab.gcf()
            ...     while 1:
            ...         f.scene.camera.azimuth(10)
            ...         f.scene.render()
            ...         yield
            ...
            >>> anim = anim()
            >>> t = Animator(500, anim.next)
            >>> t.edit_traits()

        This makes it very easy to animate your visualizations and control
        it from a simple UI.

        **Notes**

        If you want to modify the data plotted by an `mlab` function call,
        please refer to the section on: :ref:`mlab-animating-data`
    zStart AnimationzStop Animationé
   i † éô  z$frequency with which timer is called)ÚdescÚstartÚstopF)Zshow_labelsÚ_Údelay)ÚnamezAnimation ControllerÚOK)ÚtitleZbuttonsc                 O   s4   t  | ¡ || _d| _t||g|¢R i |¤Ž| _dS )a—  Constructor.

        **Parameters**

          :millisec: int specifying the delay in milliseconds
                     between calls to the callable.

          :callable: callable function to call after the specified
                     delay.

          :\*args: optional arguments to be passed to the callable.

          :\*\*kwargs: optional keyword arguments to be passed to the callable.

        N)r   Ú__init__r   Úuir   Útimer)ÚselfZmillisecÚcallableÚargsÚkwargs© r   úX/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/mayavi/tools/animator.pyr   J   s    
zAnimator.__init__c                 C   s   |   ¡ | _dS )zShow the animator UI.
        N)Zedit_traitsr   ©r   r   r   r    Úshowb   s    zAnimator.showc                 C   s   | j dur| j  ¡  dS )zClose the animator UI.
        N)r   Zdisposer!   r   r   r    Úcloseg   s    
zAnimator.closec                 C   s   | j  | j¡ d S ©N)r   ÚStartr   r!   r   r   r    Ú_start_firedo   s    zAnimator._start_firedc                 C   s   | j  ¡  d S r$   )r   ÚStopr!   r   r   r    Ú_stop_firedr   s    zAnimator._stop_firedc                 C   s0   | j }|d u rd S | ¡ r,| ¡  | |¡ d S r$   )r   Z	IsRunningr'   r%   )r   ÚvalueÚtr   r   r    Ú_delay_changedu   s    zAnimator._delay_changedN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r	   r   r   r   r
   r   r   Ztraits_viewr   r"   r#   r&   r(   r+   r   r   r   r    r      s,   ÿ
þú
r   r   c                    s<   G ‡‡‡fdd„dt ƒ‰ ‡ fdd„}| du r0|S || ƒS dS )aç  A convenient decorator to animate a generator performing an animation.

    The `delay` parameter specifies the delay (in milliseconds) between calls
    to the decorated function. If `ui` is True, then a simple UI for the
    animator is also popped up.  The decorated function will return the
    `Animator` instance used and a user may call its `Stop` method to stop the
    animation.  The `support_movie` parameter is True by default and this
    makes it easy to record a movie with the decorator.  If this is turned
    off, one cannot record a movie of the animation.

    If an ordinary function is decorated a `TypeError` will be raised.

    **Parameters**

    :delay: int specifying the time interval in milliseconds between
            calls to the function.

    :ui: bool specifying if a UI controlling the animation is to be
         provided.

    :support_movie: bool specifying if the animation will support
                    recording of a movie.

    **Returns**

    The decorated function returns an `Animator` instance.

    **Examples**

    Here is the example provided in the Animator class documentation::

        >>> from mayavi import mlab
        >>> @mlab.animate
        ... def anim():
        ...     f = mlab.gcf()
        ...     while 1:
        ...         f.scene.camera.azimuth(10)
        ...         f.scene.render()
        ...         yield
        ...
        >>> a = anim() # Starts the animation.

    For more specialized use you can pass arguments to the decorator::

        >>> from mayavi import mlab
        >>> @mlab.animate(delay=500, ui=False)
        ... def anim():
        ...     f = mlab.gcf()
        ...     while 1:
        ...         f.scene.camera.azimuth(10)
        ...         f.scene.render()
        ...         yield
        ...
        >>> a = anim() # Starts the animation without a UI.

    **Notes**

    If you want to modify the data plotted by an `mlab` function call,
    please refer to the section on: :ref:`mlab-animating-data`.

    c                       s<   e Zd Z‡ ‡‡fdd„Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )zanimate.<locals>.Wrapperc                    s(   || _ ˆ| _ˆ | _ˆ| _d | _d | _d S r$   )Úfuncr   r   Ú_support_movieÚ_movie_makerÚ_next)r   Úfunction©r   Úsupport_movier   r   r    r   Ã   s    z!animate.<locals>.Wrapper.__init__c                 _   sŒ   t | jtjƒr| j}n| j|i |¤Ž}t |tjƒrtt|dƒrB|jn|j}|| _d | _t	| j
| jƒ}| jrp| ¡  |S d| jj }t|ƒ‚d S )NÚnextz2The function "%s" must be a generator (use yield)!)Ú
isinstancer0   ÚtypesÚGeneratorTypeÚhasattrr7   Ú__next__r3   r2   r   r   Ú_stepr   r"   r,   Ú	TypeError)r   r   ÚkwÚfr3   ÚaÚmsgr   r   r    Ú__call__Ë   s    ÿz!animate.<locals>.Wrapper.__call__c                 S   sD   z|   ¡  | jr|  ¡  W n$ ty>   | jr8| j ¡  ‚ Y n0 d S r$   )r3   r1   Ú_update_movie_makerÚStopIterationr2   Zanimation_stopr!   r   r   r    r=   Ý   s    
zanimate.<locals>.Wrapper._stepc                 S   sB   | j d u r4ddlm} |ƒ jj}|j| _ | j  ¡  n
| j  ¡  d S )Né   )Ú
get_engine)r2   Zengine_managerrG   Zcurrent_sceneÚsceneZmovie_makerZanimation_startZanimation_step)r   rG   rH   r   r   r    rD   ç   s    

z,animate.<locals>.Wrapper._update_movie_makerc                 _   s   | |i |¤ŽS r$   r   )r   r0   r   r?   r   r   r    Údecorator_callð   s    z'animate.<locals>.Wrapper.decorator_callN)r,   r-   r.   r   rC   r=   rD   rI   r   r5   r   r    ÚWrapperÀ   s
   
	rJ   c                    s(   t rtˆ | ƒj| ƒS t| ƒˆ | ƒƒS d S r$   )ÚHAS_DECORATORr   rI   r   )r4   )rJ   r   r    Ú_wrapperô   s    zanimate.<locals>._wrapperN)Úobject)r0   r   r   r6   rL   r   )rJ   r   r6   r   r    Úanimate�   s
    ?4rN   )Nr   TT)r/   r9   Ú	functoolsr   r   rK   ÚImportErrorZpyface.timer.apir   Z
traits.apir   r   r   r   r	   Ztraitsui.apir
   r   r   r   rN   r   r   r   r    Ú<module>   s   
i