U
    Pe                     @   sP   d Z d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
d	S )
aF  
Kinetic effect
==============

.. versionadded:: 1.7.0

The :class:`KineticEffect` is the base class that is used to compute the
velocity out of a movement. When the movement is finished, the effect will
compute the position of the movement according to the velocity, and reduce the
velocity with a friction. The movement stop until the velocity is 0.

Conceptually, the usage could be::

    >>> effect = KineticEffect()
    >>> effect.start(10)
    >>> effect.update(15)
    >>> effect.update(30)
    >>> effect.stop(48)

Over the time, you will start a movement of a value, update it, and stop the
movement. At this time, you'll get the movement value into
:attr:`KineticEffect.value`. On the example i've typed manually, the computed
velocity will be::

    >>> effect.velocity
    3.1619100231163046

After multiple clock interaction, the velocity will decrease according to
:attr:`KineticEffect.friction`. The computed value will be stored in
:attr:`KineticEffect.value`. The output of this `value` could be::

    46.30038145219605
    54.58302451968686
    61.9229016256196
    # ...

)KineticEffect    )time)EventDispatcher)NumericPropertyBooleanProperty)Clockc                       s   e Zd ZdZedZedZedZedZ	edZ
edZedZedZ fd	d
Zdd ZdddZdddZdddZdd Zdd Z  ZS )r   zIKinetic effect class. See module documentation for more information.
    r   g?F   g?g      ?g rh?c                    s,   g | _ t| jd| _tt| jf | d S Nr   )historyr   Zcreate_triggerupdate_velocitytrigger_velocity_updatesuperr   __init__)selfkwargs	__class__ 8/tmp/pip-unpacked-wheel-xzebddm3/kivy/effects/kinetic.pyr   u   s     zKineticEffect.__init__c                 C   s&   t || jk rd| _|  j|7  _d S r	   )absmin_distancevelocityvalue)r   distancer   r   r   apply_distance{   s    zKineticEffect.apply_distanceNc                 C   s&   d| _ |pt }d| _||fg| _dS )a  Start the movement.

        :Parameters:
            `val`: float or int
                Value of the movement
            `t`: float, defaults to None
                Time when the movement happen. If no time is set, it will use
                time.time()
        Tr   N)	is_manualr   r   r
   )r   valtr   r   r   start   s    

zKineticEffect.startc                 C   sV   |pt  }|| jd d  }| | | j||f t| j| jkrR| jd dS )zKUpdate the movement.

        See :meth:`start` for the arguments.
           r   N)r   r
   r   appendlenmax_historypop)r   r   r   r   r   r   r   update   s    

zKineticEffect.updatec                 C   s   d| _ |pt }|| jd d  }| | ||f}| jd }| jD ] }|d |d  dk r` qf|}qD|d |d  }t|d |d  }|t|d | _|   dS )zIStop the movement.

        See :meth:`start` for the arguments.
        Fr   r    r   gUUUUUU?g-C6?N)r   r   r
   r   r   maxr   r   )r   r   r   r   Znewest_sampleZ
old_samplesampledurationr   r   r   stop   s    



zKineticEffect.stopc                 C   s   d| _ |   dS )zCancel a movement. This can be used in case :meth:`stop` cannot be
        called. It will reset :attr:`is_manual` to False, and compute the
        movement if the velocity is > 0.
        FN)r   r   )r   r   r   r   cancel   s    zKineticEffect.cancelc                 C   sV   t | j| jkrd| _dS |  j| j| j | | j 8  _| | j|  |   dS )zX(internal) Update the velocity according to the frametime and
        friction.
        r   N)r   r   min_velocityfrictionstd_dtr   r   )r   dtr   r   r   r      s     zKineticEffect.update_velocity)N)N)N)__name__
__module____qualname____doc__r   r   r,   r   r   r   r#   r   r+   r-   r   r   r   r%   r)   r*   r   __classcell__r   r   r   r   r   0   s    	




r   N)r2   __all__r   Z
kivy.eventr   Zkivy.propertiesr   r   Z
kivy.clockr   r   r   r   r   r   <module>   s   &