3
WIÃ\õ  ã               @   s    d Z ddlZddlmZ yddlmZ dZW n ek
rD   dZY nX 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 † iô  z$frequency with which timer is called)ÚdescÚstartÚstopF)Úshow_labelsÚ_Údelay)ÚnamezAnimation ControllerÚOK)ÚtitleÚbuttonsc             O   s.   t j| ƒ || _d| _t||f|ž|Ž| _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/venv/lib/python3.6/site-packages/mayavi/tools/animator.pyr   J   s    
zAnimator.__init__c             C   s   | j ƒ | _dS )zShow the animator UI.
        N)Úedit_traitsr   )r   r    r    r!   Úshowb   s    zAnimator.showc             C   s   | j dk	r| j jƒ  dS )zClose the animator UI.
        N)r   Údispose)r   r    r    r!   Úcloseg   s    
zAnimator.closec             C   s   | j j| jƒ d S )N)r   ÚStartr   )r   r    r    r!   Ú_start_firedo   s    zAnimator._start_firedc             C   s   | j jƒ  d S )N)r   ÚStop)r   r    r    r!   Ú_stop_firedr   s    zAnimator._stop_firedc             C   s0   | j }|d krd S |jƒ r,|jƒ  |j|ƒ d S )N)r   Ú	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   Útraits_viewr   r#   r%   r'   r)   r-   r    r    r    r!   r      s&   

r   éô  c                s<   G ‡‡‡fdd„dt ƒ‰ ‡ fdd„}| dk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 )N)Ú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||Ž}t |tjƒrpt|dƒr>|jn|j}|| _d | _t	| j
| jƒ}| jrl|jƒ  |S d| jj }t|ƒ‚d S )NÚnextz2The function "%s" must be a generator (use yield)!)Ú
isinstancer4   ÚtypesÚGeneratorTypeÚhasattrr:   Ú__next__r7   r6   r   r   Ú_stepr   r#   r.   Ú	TypeError)r   r   ÚkwÚfr7   ÚaÚmsgr    r    r!   Ú__call__Ë   s    
z!animate.<locals>.Wrapper.__call__c             S   sF   y| j ƒ  | jr| jƒ  W n& tk
r@   | jr:| jjƒ  ‚ Y nX d S )N)r7   r5   Ú_update_movie_makerÚStopIterationr6   Zanimation_stop)r   r    r    r!   r@   Ý   s    
zanimate.<locals>.Wrapper._stepc             S   sB   | j d kr4ddlm} |ƒ jj}|j| _ | j jƒ  n
| j jƒ  d S )Né   )Ú
get_engine)r6   Úengine_managerrJ   Úcurrent_sceneÚsceneZmovie_makerZanimation_startZanimation_step)r   rJ   rM   r    r    r!   rG   ç   s    

z,animate.<locals>.Wrapper._update_movie_makerc             _   s
   | ||ŽS )Nr    )r   r4   r   rB   r    r    r!   Údecorator_callð   s    z'animate.<locals>.Wrapper.decorator_callN)r.   r/   r0   r   rF   r@   rG   rN   r    )r   r9   r   r    r!   ÚWrapperÀ   s
   
	rO   c                s(   t rtˆ | ƒj| ƒS t| ƒˆ | ƒƒS d S )N)ÚHAS_DECORATORr   rN   r   )r8   )rO   r    r!   Ú_wrapperô   s    zanimate.<locals>._wrapperN)Úobject)r4   r   r   r9   rQ   r    )rO   r   r9   r   r!   Úanimate�   s
    ?4rS   )Nr3   TT)r1   r<   Ú	functoolsr   r   rP   ÚImportErrorÚpyface.timer.apir   Ú
traits.apir   r   r   r   r	   Útraitsui.apir
   r   r   r   rS   r    r    r    r!   Ú<module>   s   
i