U
    Peb                     @   s   d Z dZddlmZmZmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZmZ ddlmZ G d	d
 d
e
ZG dd deZG dd deZG dd deZG dd dZdS )at
  
Animation
=========

:class:`Animation` and :class:`AnimationTransition` are used to animate
:class:`~kivy.uix.widget.Widget` properties. You must specify at least a
property name and target value. To use an Animation, follow these steps:

    * Setup an Animation object
    * Use the Animation object on a Widget

Simple animation
----------------

To animate a Widget's x or y position, simply specify the target x/y values
where you want the widget positioned at the end of the animation::

    anim = Animation(x=100, y=100)
    anim.start(widget)

The animation will last for 1 second unless :attr:`duration` is specified.
When anim.start() is called, the Widget will move smoothly from the current
x/y position to (100, 100).

Multiple properties and transitions
-----------------------------------

You can animate multiple properties and use built-in or custom transition
functions using :attr:`transition` (or the `t=` shortcut). For example,
to animate the position and size using the 'in_quad' transition::

    anim = Animation(x=50, size=(80, 80), t='in_quad')
    anim.start(widget)

Note that the `t=` parameter can be the string name of a method in the
:class:`AnimationTransition` class or your own animation function.

Sequential animation
--------------------

To join animations sequentially, use the '+' operator. The following example
will animate to x=50 over 1 second, then animate the size to (80, 80) over the
next two seconds::

    anim = Animation(x=50) + Animation(size=(80, 80), duration=2.)
    anim.start(widget)

Parallel animation
------------------

To join animations in parallel, use the '&' operator. The following example
will animate the position to (80, 10) over 1 second, whilst in parallel
animating the size to (800, 800)::

    anim = Animation(pos=(80, 10))
    anim &= Animation(size=(800, 800), duration=2.)
    anim.start(widget)

Keep in mind that creating overlapping animations on the same property may have
unexpected results. If you want to apply multiple animations to the same
property, you should either schedule them sequentially (via the '+' operator or
using the *on_complete* callback) or cancel previous animations using the
:attr:`~Animation.cancel_all` method.

Repeating animation
-------------------

.. versionadded:: 1.8.0

.. note::
    This is currently only implemented for 'Sequence' animations.

To set an animation to repeat, simply set the :attr:`Sequence.repeat`
property to `True`::

    anim = Animation(...) + Animation(...)
    anim.repeat = True
    anim.start(widget)

For flow control of animations such as stopping and cancelling, use the methods
already in place in the animation module.
)	AnimationAnimationTransition    )sqrtcossinpi)ChainMap)EventDispatcher)Clock)string_typesiterkeys)	WeakProxyc                       s   e Zd ZdZdZe ZdZ fddZe	dd Z
e	dd	 Ze	d
d Zedd Ze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d d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Z d2d3 Z!  Z"S )4r   a  Create an animation definition that can be used to animate a Widget.

    :Parameters:
        `duration` or `d`: float, defaults to 1.
            Duration of the animation, in seconds.
        `transition` or `t`: str or func
            Transition function for animate properties. It can be the name of a
            method from :class:`AnimationTransition`.
        `step` or `s`: float
            Step in milliseconds of the animation. Defaults to 0, which means
            the animation is updated for every frame.

            To update the animation less often, set the step value to a float.
            For example, if you want to animate at 30 FPS, use s=1/30.

    :Events:
        `on_start`: animation, widget
            Fired when the animation is started on a widget.
        `on_complete`: animation, widget
            Fired when the animation is completed or stopped on a widget.
        `on_progress`: animation, widget, progression
            Fired when the progression of the animation is changing.

    .. versionchanged:: 1.4.0
        Added s/step parameter.

    .. versionchanged:: 1.10.0
        The default value of the step parameter was changed from 1/60. to 0.
    N)on_starton_progresson_completec                    s|   t    d| _|d|dd| _|d|dd| _|d|d	d
| _t| jtrlt	t
| j| _|| _i | _d S )NFdduration      ?t
transitionlinearsstepr   )super__init___clock_installedpop	_duration_transition_step
isinstancer   getattrr   _animated_properties_widgets)selfkw	__class__ 2/tmp/pip-unpacked-wheel-xzebddm3/kivy/animation.pyr      s    
zAnimation.__init__c                 C   s   | j S )z.Return the duration of the animation.
        )r   r$   r(   r(   r)   r      s    zAnimation.durationc                 C   s   | j S )z0Return the transition of the animation.
        )r   r*   r(   r(   r)   r      s    zAnimation.transitionc                 C   s   | j S )z/Return the properties used to animate.
        )r"   r*   r(   r(   r)   animated_properties   s    zAnimation.animated_propertiesc                 G   sN   t |r0ttjD ]}|D ]}|| | qqnttjD ]}||  q:dS )zStop all animations that concern a specific widget / list of
        properties.

        Example::

            anim = Animation(x=50)
            anim.start(widget)

            # and later
            Animation.stop_all(widget, 'x')
        N)lenlistr   
_instancesstop_propertysetstop)widgetlargs	animationxr(   r(   r)   stop_all   s    zAnimation.stop_allc                 G   s   | dkr||rPt j D ]6}t|j D ]"}|d } |D ]}|| | q8q(qn(t jD ]}|j  |  qVt j  dS t	|rt
t jD ]}|D ]}|| | qqntt jD ]}||  qdS )a5  Cancel all animations that concern a specific widget / list of
        properties. See :attr:`cancel`.

        Example::

            anim = Animation(x=50)
            anim.start(widget)

            # and later
            Animation.cancel_all(widget, 'x')

        .. versionadded:: 1.4.0

        .. versionchanged:: 2.1.0
            If the parameter ``widget`` is None, all animated widgets will be
            the target and cancelled. If ``largs`` is also given, animation of
            these properties will be canceled for all animated widgets.
        Nr2   )r   r.   copytupler#   valuescancel_propertyclear_clock_uninstallr,   r-   r0   cancel)r2   r3   r4   infor5   r(   r(   r)   
cancel_all   s$    



zAnimation.cancel_allc                 C   s,   |  | | | |   | d| dS )z)Start the animation on a widget.
        r   N)r1   _initialize	_registerdispatchr$   r2   r(   r(   r)   start   s    

zAnimation.startc                 C   s.   | j |jd}|r | d| | | dS )z^Stop the animation previously applied to a widget, triggering the
        `on_complete` event.Nr   )r#   r   uidrB   r=   r$   r2   propsr(   r(   r)   r1      s    zAnimation.stopc                 C   s*   | j |jd |   | j s&|   dS )zCancel the animation previously applied to a widget. Same
        effect as :attr:`stop`, except the `on_complete` event will
        *not* be triggered!

        .. versionadded:: 1.4.0
        N)r#   r   rE   r<   _unregisterrC   r(   r(   r)   r=      s    zAnimation.cancelc                 C   s>   | j |jd}|sdS |d |d |d s:| | dS )zEven if an animation is running, remove a property. It will not be
        animated further. If it was the only/last property being animated,
        the animation will be stopped (see :attr:`stop`).
        N
properties)r#   getrE   r   r1   r$   r2   proprG   r(   r(   r)   r/      s    zAnimation.stop_propertyc                 C   s>   | j |jd}|sdS |d |d |d s:| | dS )zEven if an animation is running, remove a property. It will not be
        animated further. If it was the only/last property being animated,
        the animation will be canceled (see :attr:`cancel`)

        .. versionadded:: 1.4.0
        NrI   )r#   rJ   rE   r   r=   rK   r(   r(   r)   r:   	  s    zAnimation.cancel_propertyc                 C   s$   | j |jd}|r |d r dS dS )zbReturn True if a widget still has properties to animate.

        .. versionadded:: 1.8.0
        NrI   T)r#   rJ   rE   rF   r(   r(   r)   have_properties_to_animate  s    z$Animation.have_properties_to_animatec                 C   s   t j|  d S N)r   r.   addr*   r(   r(   r)   rA   %  s    zAnimation._registerc                 C   s   t j|  d S rN   )r   r.   discardr*   r(   r(   r)   rH   (  s    zAnimation._unregisterc                 C   s   |i d d }| j |j< |d }| j D ]L\}}t||}t|ttfrX|d d  }nt|trj|	 }||f||< q*| 
  d S )N)r2   rI   timerI   )r#   rE   r"   itemsr!   r    r8   r-   dictr7   _clock_install)r$   r2   r   pkeyvalueoriginal_valuer(   r(   r)   r@   +  s    

zAnimation._initializec                 C   s&   | j r
d S t| j| j| _d| _ d S )NT)r   r
   Zschedule_interval_updater   
_update_evr*   r(   r(   r)   rT   >  s    zAnimation._clock_installc                 C   s4   | j s| jsd S d| _| jd k	r0| j  d | _d S )NF)r#   r   rZ   r=   r*   r(   r(   r)   r<   D  s    

zAnimation._clock_uninstallc                 C   s  | j }| j}| j}t| D ]}|| }|d }t|trntt|sn| j 	|d  | 
  | j s|   q|d d krd|d< n|d  |7  < | jrtd|d | j }nd}||}	|d  D ](\}
}|\}}||||	}t||
| q| d|| |dkr| | qd S )Nr2   rQ           r      rI   r   )r#   r   
_calculater-   keysr    r   r,   dirr   r<   rH   r   minrR   setattrrB   r1   )r$   dtZwidgetsr   Z	calculaterE   Zanimr2   progressr   rV   r9   abrW   r(   r(   r)   rY   L  s4    
zAnimation._updatec                    s   | j  ttsttrRttr*t}nt}| fddttD S ttri }tD ]2}|kr| ||< qh | | ||< qh|S d    S d S )Nc                    s    g | ]} | | qS r(   r(   ).0r5   r]   rd   re   r   r(   r)   
<listcomp>{  s     z(Animation._calculate.<locals>.<listcomp>r   )r]   r    r-   r8   ranger,   rS   r   )r$   rd   re   r   tpr   r5   r(   rg   r)   r]   t  s    
$
zAnimation._calculatec                 C   s   d S rN   r(   rC   r(   r(   r)   r     s    zAnimation.on_startc                 C   s   d S rN   r(   )r$   r2   rc   r(   r(   r)   r     s    zAnimation.on_progressc                 C   s   d S rN   r(   rC   r(   r(   r)   r     s    zAnimation.on_completec                 C   s
   t | |S rN   )Sequencer$   r4   r(   r(   r)   __add__  s    zAnimation.__add__c                 C   s
   t | |S rN   )Parallelrl   r(   r(   r)   __and__  s    zAnimation.__and__)#__name__
__module____qualname____doc__rZ   r0   r.   Z
__events__r   propertyr   r   r+   staticmethodr6   r?   rD   r1   r=   r/   r:   rM   rA   rH   r@   rT   r<   rY   r]   r   r   r   rm   ro   __classcell__r(   r(   r&   r)   r   ^   sB   




((r   c                       sL   e Zd Zdd Z fddZdd Zdd Zed	d
 Zedd Z	  Z
S )CompoundAnimationc                 C   sB   | j || | j|| | j |s>| j|s>| | d S rN   )anim1r/   anim2rM   r1   r$   r2   rL   r(   r(   r)   r/     s    
zCompoundAnimation.stop_propertyc                    s(   | j | | j| t | d S rN   )rx   r=   ry   r   rC   r&   r(   r)   r=     s    zCompoundAnimation.cancelc                 C   sB   | j || | j|| | j |s>| j|s>| | dS )ax  Even if an animation is running, remove a property. It will not be
        animated further. If it was the only/last property being animated,
        the animation will be canceled (see :attr:`cancel`)

        This method overrides `:class:kivy.animation.Animation`'s
        version, to cancel it on all animations of the Sequence.

        .. versionadded:: 1.10.0
        N)rx   r:   ry   rM   r=   rz   r(   r(   r)   r:     s    

z!CompoundAnimation.cancel_propertyc                 C   s   | j |p| j|S rN   )rx   rM   ry   rC   r(   r(   r)   rM     s    
z,CompoundAnimation.have_properties_to_animatec                 C   s   t i | jj| jjS rN   )r   ry   r+   rx   r*   r(   r(   r)   r+     s    z%CompoundAnimation.animated_propertiesc                 C   s   t dd S )Nz8Can't lookup transition attribute of a CompoundAnimation)AttributeErrorr*   r(   r(   r)   r     s    zCompoundAnimation.transition)rp   rq   rr   r/   r=   r:   rM   rt   r+   r   rv   r(   r(   r&   r)   rw     s   
rw   c                       s\   e Zd Z fddZedd Z fddZdd Zd	d
 Zdd Z	dd Z
dd Z  ZS )rk   c                    sH   t    d| _|| _|| _| jj| j| jd | jj| j| j	d d S )NF)r   r   )
r   r   repeatrx   ry   bindon_anim1_completeon_anim1_progresson_anim2_completeon_anim2_progressr$   rx   ry   r&   r(   r)   r     s    


zSequence.__init__c                 C   s   | j j| jj S rN   )rx   r   ry   r*   r(   r(   r)   r     s    zSequence.durationc                    sH   | j |jd }| j| | j| |r8| d| t | d S Nr   )	r#   r   rE   rx   r1   ry   rB   r   r=   rF   r&   r(   r)   r1     s    zSequence.stopc                 C   s:   |  | d| j|j< |   | d| | j| d S )NTr   )r1   r#   rE   rA   rB   rx   rD   rC   r(   r(   r)   rD     s
    
zSequence.startc                 C   s    |j | jkrd S | j| d S rN   )rE   r#   ry   rD   r$   instancer2   r(   r(   r)   r~     s    zSequence.on_anim1_completec                 C   s   |  d||d  d S )Nr          @rB   r$   r   r2   rc   r(   r(   r)   r     s    zSequence.on_anim1_progressc                 C   s>   |j | jkrdS | jr$| j| n| d| | | dS )z^Repeating logic used with boolean variable "repeat".

        .. versionadded:: 1.7.1
        Nr   )rE   r#   r|   rx   rD   rB   r=   r   r(   r(   r)   r     s    zSequence.on_anim2_completec                 C   s   |  d|d|d   d S )Nr         ?r   r   r   r(   r(   r)   r     s    zSequence.on_anim2_progress)rp   rq   rr   r   rt   r   r1   rD   r~   r   r   r   rv   r(   r(   r&   r)   rk     s   
rk   c                       sD   e Zd Z fddZedd Z fddZdd Zd	d
 Z  Z	S )rn   c                    s:   t    || _|| _| jj| jd | jj| jd d S )N)r   )r   r   rx   ry   r}   on_anim_completer   r&   r(   r)   r   
  s
    
zParallel.__init__c                 C   s   t | jj| jjS rN   )maxrx   r   ry   r*   r(   r(   r)   r     s    zParallel.durationc                    sD   | j | | j| | j|jd r4| d| t | d S r   )	rx   r1   ry   r#   r   rE   rB   r   r=   rC   r&   r(   r)   r1     s
    zParallel.stopc                 C   sJ   |  | | j| | j| ddi| j|j< |   | d| d S )Ncompleter   r   )r1   rx   rD   ry   r#   rE   rA   rB   rC   r(   r(   r)   rD     s    
zParallel.startc                 C   s:   | j |j d  d7  < | j |j d dkr6| | d S )Nr   r\      )r#   rE   r1   r   r(   r(   r)   r   %  s    zParallel.on_anim_complete)
rp   rq   rr   r   rt   r   r1   rD   r   rv   r(   r(   r&   r)   rn     s   
rn   c                   @   s  e Zd ZdZedd Zedd Zedd Zedd	 Zed
d Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zed d! Zed"d# Zed$d% Zed&d' Zed(d) Zed*d+ Zed,d- Zed.d/ Zed0d1 Zed2d3 Zed4d5 Zed6d7 Zed8d9 Z ed:d; Z!ed<d= Z"ed>d? Z#ed@dA Z$edBdC Z%dDS )Er   a  Collection of animation functions to be used with the Animation object.
    Easing Functions ported to Kivy from the Clutter Project
    https://developer.gnome.org/clutter/stable/ClutterAlpha.html

    The `progress` parameter in each animation function is in the range 0-1.
    c                 C   s   | S )z!.. image:: images/anim_linear.pngr(   rc   r(   r(   r)   r   3  s    zAnimationTransition.linearc                 C   s   | |  S )z+.. image:: images/anim_in_quad.png
        r(   r   r(   r(   r)   in_quad8  s    zAnimationTransition.in_quadc                 C   s   d|  | d  S )z,.. image:: images/anim_out_quad.png
              r   r(   r   r(   r(   r)   out_quad>  s    zAnimationTransition.out_quadc                 C   s8   | d }|dk rd| | S |d8 }d||d  d  S )z/.. image:: images/anim_in_out_quad.png
        r   r\   r   r         r   r(   rc   rU   r(   r(   r)   in_out_quadD  s
    zAnimationTransition.in_out_quadc                 C   s   | |  |  S )z,.. image:: images/anim_in_cubic.png
        r(   r   r(   r(   r)   in_cubicN  s    zAnimationTransition.in_cubicc                 C   s   | d }|| | d S )z-.. image:: images/anim_out_cubic.png
        r   r(   r   r(   r(   r)   	out_cubicT  s    zAnimationTransition.out_cubicc                 C   s<   | d }|dk r d| | | S |d8 }d|| | d  S )z0.. image:: images/anim_in_out_cubic.png
        r   r\   r   r   r(   r   r(   r(   r)   in_out_cubic[  s
    z AnimationTransition.in_out_cubicc                 C   s   | |  |  |  S )z,.. image:: images/anim_in_quart.png
        r(   r   r(   r(   r)   in_quarte  s    zAnimationTransition.in_quartc                 C   s    | d }d|| | | d  S )z-.. image:: images/anim_out_quart.png
        r   r   r(   r   r(   r(   r)   	out_quartk  s    zAnimationTransition.out_quartc                 C   sD   | d }|dk r$d| | | | S |d8 }d|| | | d  S )z0.. image:: images/anim_in_out_quart.png
        r   r\   r   r   r   r(   r   r(   r(   r)   in_out_quartr  s
    z AnimationTransition.in_out_quartc                 C   s   | |  |  |  |  S )z,.. image:: images/anim_in_quint.png
        r(   r   r(   r(   r)   in_quint|  s    zAnimationTransition.in_quintc                 C   s    | d }|| | | | d S )z-.. image:: images/anim_out_quint.png
        r   r(   r   r(   r(   r)   	out_quint  s    zAnimationTransition.out_quintc                 C   sL   | d }|dk r(d| | | | | S |d8 }d|| | | | d  S )z0.. image:: images/anim_in_out_quint.png
        r   r\   r   r   r(   r   r(   r(   r)   in_out_quint  s
    z AnimationTransition.in_out_quintc                 C   s   dt | td   d S )z+.. image:: images/anim_in_sine.png
        r   r   r   r   r   r   r(   r(   r)   in_sine  s    zAnimationTransition.in_sinec                 C   s   t | td  S )z,.. image:: images/anim_out_sine.png
        r   )r   r   r   r(   r(   r)   out_sine  s    zAnimationTransition.out_sinec                 C   s   dt t|  d  S )z/.. image:: images/anim_in_out_sine.png
        r   r   r   r   r(   r(   r)   in_out_sine  s    zAnimationTransition.in_out_sinec                 C   s   | dkrdS t dd| d  S )z+.. image:: images/anim_in_expo.png
        r   r[   r   
   r   powr   r(   r(   r)   in_expo  s    zAnimationTransition.in_expoc                 C   s    | dkrdS t dd|   d S )z,.. image:: images/anim_out_expo.png
        r   r   r   r   r(   r(   r)   out_expo  s    zAnimationTransition.out_expoc                 C   s^   | dkrdS | dkrdS | d }|dk r>dt dd|d   S |d8 }dt dd|  d	  S )
z/.. image:: images/anim_in_out_expo.png
        r   r[   r   r   r\   r   r   r   r   r   r   r(   r(   r)   in_out_expo  s    zAnimationTransition.in_out_expoc                 C   s   dt d| |   d  S )z+.. image:: images/anim_in_circ.png
        r   r   r   r   r(   r(   r)   in_circ  s    zAnimationTransition.in_circc                 C   s   | d }t d||  S )z,.. image:: images/anim_out_circ.png
        r   r   r   r(   r(   r)   out_circ  s    zAnimationTransition.out_circc                 C   sH   | d }|dk r(dt d||  d  S |d8 }dt d||  d  S )z/.. image:: images/anim_in_out_circ.png
        r   r\   r   r   r   r   r   r   r(   r(   r)   in_out_circ  s
    zAnimationTransition.in_out_circc                 C   sL   d}|d }| }|dkrdS |d8 }t dd| t|| dt  |   S )z... image:: images/anim_in_elastic.png
        333333?      @r\   r   r   r   r   r   r   rc   rU   r   qr(   r(   r)   
in_elastic  s    zAnimationTransition.in_elasticc                 C   sF   d}|d }| }|dkrdS t dd| t|| dt  |  d S )z/.. image:: images/anim_out_elastic.png
        r   r   r\   r   r   r   r   r   r(   r(   r)   out_elastic  s    zAnimationTransition.out_elasticc                 C   s   d}|d }| d }|dkr dS |dk rZ|d8 }dt dd| t|| dt  |   S |d8 }t dd	| t|| dt  |  d
 d S dS )z2.. image:: images/anim_in_out_elastic.png
        g?r   r   r   r\   r   r   r   r   r   Nr   r   r(   r(   r)   in_out_elastic  s    *z"AnimationTransition.in_out_elasticc                 C   s   | |  d|  d  S )z+.. image:: images/anim_in_back.png
        8՜@aq89?r(   r   r(   r(   r)   in_back  s    zAnimationTransition.in_backc                 C   s    | d }|| d| d  d S )z,.. image:: images/anim_out_back.png
        r   r   r   r(   r   r(   r(   r)   out_back  s    zAnimationTransition.out_backc                 C   sX   | d }d}|dk r0d|| |d | |   S |d8 }d|| |d | |  d  S )z/.. image:: images/anim_in_out_back.png
        r   g@t_@r\   r   r   r(   )rc   rU   r   r(   r(   r)   in_out_back  s    zAnimationTransition.in_out_backc                 C   sx   | | }|dk rd| | S |dk r<|d8 }d| | d S |dk r\|d8 }d| | d S |d	8 }d| | d
 S d S )NgF]tE?g     @@gF]tE?gtE]t?g      ?g]tE?g/袋.?g      ?g.袋?g     ?r(   )r   r   rU   r(   r(   r)   _out_bounce_internal  s    z(AnimationTransition._out_bounce_internalc                 C   s   dt ||  | S )Nr   r   r   )r   r   r(   r(   r)   _in_bounce_internal(  s    z'AnimationTransition._in_bounce_internalc                 C   s   t | dS )z-.. image:: images/anim_in_bounce.png
        r   )r   r   r   r(   r(   r)   	in_bounce,  s    zAnimationTransition.in_bouncec                 C   s   t | dS )z... image:: images/anim_out_bounce.png
        r   r   r   r(   r(   r)   
out_bounce2  s    zAnimationTransition.out_bouncec                 C   s8   | d }|dk r t |dd S t |d dd d S )z1.. image:: images/anim_in_out_bounce.png
        r   r   r   )r   r   r   r   r(   r(   r)   in_out_bounce8  s    z!AnimationTransition.in_out_bounceN)&rp   rq   rr   rs   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r(   r(   r(   r)   r   +  s   



	


	


	


	








	











r   N)rs   __all__mathr   r   r   r   collectionsr   Z
kivy.eventr	   Z
kivy.clockr
   Zkivy.compatr   r   Zkivy.weakproxyr   r   rw   rk   rn   r   r(   r(   r(   r)   <module>   s   S  @/=#