U
    P’“ez;  ã                   @   sˆ   d Z dZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ d	 aaG d
d„ deƒZG dd„ de	ƒZe de¡ d	S )a‡  
Mouse provider implementation
=============================

On linux systems, the mouse provider can be annoying when used with another
multitouch provider (hidinput or mtdev). The Mouse can conflict with them: a
single touch can generate one event from the mouse provider and another
from the multitouch provider.

To avoid this behavior, you can activate the "disable_on_activity" token in
the mouse configuration. Then, if any touches are created by another
provider, the mouse event will be discarded. Add this to your configuration::

    [input]
    mouse = mouse,disable_on_activity

Using multitouch interaction with the mouse
-------------------------------------------

.. versionadded:: 1.3.0

By default, the middle and right mouse buttons, as well as a combination of
ctrl + left mouse button are used for multitouch emulation.
If you want to use them for other purposes, you can disable this behavior by
activating the "disable_multitouch" token::

   [input]
   mouse = mouse,disable_multitouch

.. versionchanged:: 1.9.0

You can now selectively control whether a click initiated as described above
will emulate multi-touch. If the touch has been initiated in the above manner
(e.g. right mouse button), a `multitouch_sim` value will be added to the
touch's profile, and a `multitouch_sim` property will be added to the touch.
By default, `multitouch_sim` is True and multitouch will be emulated for that
touch. If, however, `multitouch_on_demand` is added to the config::

   [input]
   mouse = mouse,multitouch_on_demand

then `multitouch_sim` defaults to `False`. In that case, if `multitouch_sim`
is set to True before the mouse is released (e.g. in on_touch_down/move), the
touch will simulate a multi-touch event. For example::

    if 'multitouch_sim' in touch.profile:
        touch.multitouch_sim = True

.. versionchanged:: 2.1.0

Provider dispatches hover events by listening to properties/events in
:class:`~kivy.core.window.Window`. Dispatching can be disabled by setting
:attr:`MouseMotionEventProvider.disable_hover` to ``True`` or by adding
`disable_hover` in the config::

    [input]
    mouse = mouse,disable_hover

It's also possible to enable/disable hover events at runtime with
:attr:`MouseMotionEventProvider.disable_hover` property.

Following is a list of the supported values for the
:attr:`~kivy.input.motionevent.MotionEvent.profile` property list.

================ ==========================================================
Profile value    Description
---------------- ----------------------------------------------------------
button           Mouse button (one of `left`, `right`, `middle`, `scrollup`
                 or `scrolldown`). Accessed via the 'button' property.
pos              2D position. Also reflected in the
                 :attr:`~kivy.input.motionevent.MotionEvent.x`,
                 :attr:`~kivy.input.motionevent.MotionEvent.y`
                 and :attr:`~kivy.input.motionevent.MotionEvent.pos`
                 properties.
multitouch_sim   Specifies whether multitouch is simulated or not. Accessed
                 via the 'multitouch_sim' property.
================ ==========================================================

)ÚMouseMotionEventProvideré    )Ú	EventLoop)Údeque)ÚLogger)ÚMotionEventProvider)ÚMotionEventFactory)ÚMotionEventNc                       s:   e Zd Z‡ fdd„Z‡ fdd„Zd
dd„Zdd	„ Z‡  ZS )ÚMouseMotionEventc                    s   d| _ tƒ j||Ž d S )NF)Úmultitouch_simÚsuperÚ__init__)ÚselfÚargsÚkwargs©Ú	__class__© ú>/tmp/pip-unpacked-wheel-xzebddm3/kivy/input/providers/mouse.pyr   `   s    zMouseMotionEvent.__init__c                    s„   |d d… \| _ | _| j}| jrf|s.| d¡ t|ƒdkrD|d | _t|ƒdkrt|d | _| d¡ n|st| d¡ t	ƒ  
|¡ d S )Né   )ÚposÚbuttoné   é   r
   r   )ÚsxÚsyÚprofileÚis_touchÚextendÚlenr   r
   Úappendr   Údepack)r   r   r   r   r   r   r    d   s    



zMouseMotionEvent.depackFc              	   C   s¼   | j  dd ¡}|d krh|rhtd kr2ddlmama |jj  tddddƒtddd	f}W 5 Q R X || j _|d k	r¸|  ¡  | 	¡ \}}| j
|||jd
 | jd | jd f|d _|  ¡  d S )NÚ_drawelementr   )ÚColorÚEllipsegš™™™™™é?gš™™™™™É?gffffffæ?)é   r$   é   )ÚsizeÚsegments)Úrotationé
   é   )ÚudÚgetr"   Zkivy.graphicsr#   ÚcanvasÚafterr!   ÚpushZ_get_effective_sizeZscale_for_screenr(   ÚxÚyr   Úpop)r   ÚwinÚcreateÚdeÚwÚhr   r   r   Úupdate_graphicsy   s    

þz MouseMotionEvent.update_graphicsc                 C   s>   | j  dd ¡}|d k	r:|jj |d ¡ |jj |d ¡ d S )Nr!   r   r*   )r+   r2   r-   r.   Úremove)r   r3   r5   r   r   r   Úclear_graphics   s    zMouseMotionEvent.clear_graphics)F)Ú__name__Ú
__module__Ú__qualname__r   r    r8   r:   Ú__classcell__r   r   r   r   r	   ^   s   
r	   c                       sÒ   e Zd Zi Z‡ fdd„Zdd„ Zdd„ Zeeeƒ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d'd(„ Zd)d*„ Zd+d,„ Z‡  ZS )-r   c                    sÐ   t t| ƒ ||¡ tƒ | _i | _d| _d | _d | _d| _	d| _
d| _d | _d| _d| _| d¡}|D ]f}| ¡ }|dkr|qdqd|dkrŒd| _	qd|dkrœd| _
qd|dkr¬d| _qd|d	kr¼d| _qdt d
| ¡ qdd S )Nr   Fú,Ú Údisable_on_activityTÚdisable_multitouchÚdisable_hoverÚmultitouch_on_demandzMouse: unknown parameter <%s>)r   r   r   r   Úwaiting_eventÚtouchesÚcounterÚcurrent_dragÚ	alt_touchrA   rB   rD   Úhover_eventÚ_disable_hoverÚ_runningÚsplitÚstriprC   r   Úerror)r   Údevicer   Úargr   r   r   r   š   s4    
z!MouseMotionEventProvider.__init__c                 C   s   | j S ©N)rK   ©r   r   r   r   Ú_get_disable_hover¸   s    z+MouseMotionEventProvider._get_disable_hoverc                 C   s0   | j |kr,| jr&|r|  ¡  n|  ¡  || _ d S rR   )rK   rL   Ú_stop_hover_eventsÚ_start_hover_events)r   Úvaluer   r   r   Ú_set_disable_hover»   s    

z+MouseMotionEventProvider._set_disable_hoverc                 C   sf   t js
dS t jj}|d| jƒ |d| jƒ |d| jƒ |d| jƒ |d| jƒ | js\|  ¡  d| _	dS )zStart the mouse providerNÚon_mouse_downÚon_mouse_moveÚon_mouse_upÚ	on_rotateÚsystem_sizeT)
r   ÚwindowÚfbindÚon_mouse_pressÚon_mouse_motionÚon_mouse_releaseÚupdate_touch_graphicsrC   rV   rL   ©r   r_   r   r   r   ÚstartÎ   s    zMouseMotionEventProvider.startc                 C   sT   t jj}|d| jƒ |d| jƒ |d| jƒ |d| jƒ |d| jƒ |d| jƒ d S ©NÚ	mouse_posr]   Zon_cursor_enterZon_cursor_leaveZon_closer\   )r   r^   r_   Úbegin_or_update_hover_eventÚupdate_hover_eventÚbegin_hover_eventÚend_hover_eventrd   r   r   r   rV   Ü   s    z,MouseMotionEventProvider._start_hover_eventsc                 C   sf   t js
dS t jj}|d| jƒ |d| jƒ |d| jƒ |d| jƒ |d| jƒ | js\|  ¡  d| _	dS )zStop the mouse providerNrY   rZ   r[   r\   r]   F)
r   r^   Úfunbindr`   ra   rb   rc   rC   rU   rL   ©r   rl   r   r   r   Ústopå   s    zMouseMotionEventProvider.stopc                 C   sT   t jj}|d| jƒ |d| jƒ |d| jƒ |d| jƒ |d| jƒ |d| jƒ d S rf   )r   r^   rl   rh   ri   rj   rk   rm   r   r   r   rU   ó   s    z+MouseMotionEventProvider._stop_hover_eventsc                 C   s8   | j s
dS tjD ]"}|jjdkr"q|jtkr dS qdS )NFZKineticMotionEventT)rA   r   rF   r   r;   r	   )r   Útouchr   r   r   Útest_activityü   s    

z&MouseMotionEventProvider.test_activityc                 C   sN   d|j d  }| j ¡ D ]0}t||j ƒ|k rt||j ƒ|k r|  S qd S )Ng      $@r   )r]   rF   ÚvaluesÚabsr   r   )r   r3   r0   r1   Zfactorro   r   r   r   Ú
find_touch
  s
    $
z#MouseMotionEventProvider.find_touchc                 C   s   |  j d7  _ | jt| j ƒ S )Nr*   )rG   rP   ÚstrrS   r   r   r   Úcreate_event_id  s    z(MouseMotionEventProvider.create_event_idc                 C   s‚   |   ¡ }|||g}|r$|| j g7 }t| j||ddd | _}	||	_|	| j|< |rn| j o`| j }
|	 ||
¡ | j	 
d|	f¡ |	S )NTro   )r   Útype_idÚbegin)ru   rD   r	   rP   rH   Úis_double_taprF   rB   r8   rE   r   )r   r3   ÚnxÚnyrx   Údo_graphicsr   Zevent_idr   ro   Zcreate_flagr   r   r   Úcreate_touch  s(    
  ý
þz%MouseMotionEventProvider.create_touchc                 C   s<   |j | jkr8| j|j = | ¡  | j d|f¡ | |¡ d S ©NÚend)ÚidrF   Úupdate_time_endrE   r   r:   )r   r3   ro   r   r   r   Úremove_touch.  s
    
z%MouseMotionEventProvider.remove_touchc                 C   sˆ   |j |jŽ \}}||j }||j }||f}| j}|rB| |¡ nt| j|  ¡ |dd | _}|dkrt| ¡  d | _| j	 
||f¡ d S )NÚhover)rv   r~   )Úto_normalized_posrg   Z_densityrJ   Úmover	   rP   ru   r€   rE   r   )r   r3   Úetypery   rz   r   r‚   r   r   r   Úcreate_hover5  s"    

üz%MouseMotionEventProvider.create_hoverc           	      C   s€   |  ||¡\}}d| }| jrN| j}| ||g¡ | |¡ | j d|f¡ n.| jd k	r|d|kr|d|k}|  ||||dg ¡ d S )Nç      ð?ÚupdateÚaltÚshiftT)rƒ   rH   r„   r8   rE   r   rI   r|   )	r   r3   r0   r1   Ú	modifiersry   rz   ro   rx   r   r   r   ra   J  s    
z(MouseMotionEventProvider.on_mouse_motionc                 C   sŠ   |   ¡ rd S | ||¡\}}d| }|  |||¡}|r>|| _nHd|k}	| j o\|dkp\d|k}
|  ||||	|
|¡}d|kr†|| _d | _d S )Nr‡   rŠ   ÚleftÚctrlr‰   )rp   rƒ   rs   rH   rB   r|   rI   )r   r3   r0   r1   r   r‹   ry   rz   Zfound_touchrx   r{   ro   r   r   r   r`   W  s,    þ     ÿz'MouseMotionEventProvider.on_mouse_pressc           
      C   sª   |dkr.t | j ¡ ƒD ]}|  ||¡ qd | _| j}|rŒ|dk}d|k}| jp^d|jkp^|j }	|rh|sl|	r€|  ||¡ d | _n| |d¡ | j	r¦|  || j	¡ d | _	d S )NÚall)rŒ   ZscrollupZ
scrolldownZ
scrollleftZscrollrightr   r
   T)
ÚlistrF   rq   r   rH   rB   r   r
   r8   rI   )
r   r3   r0   r1   r   r‹   ro   Z	not_rightZnot_ctrlZ	not_multir   r   r   rb   l  s(    ÿýz)MouseMotionEventProvider.on_mouse_releasec                 G   s   | j  ¡ D ]}| |¡ q
d S rR   )rF   rq   r8   )r   r3   r   ro   r   r   r   rc   ‰  s    z.MouseMotionEventProvider.update_touch_graphicsc                 G   s   | j r
dnd}|  ||¡ d S )Nrˆ   rw   ©rJ   r†   )r   r3   r   r…   r   r   r   rh     s    z4MouseMotionEventProvider.begin_or_update_hover_eventc                 G   s   | j s|  |d¡ d S )Nrw   r   ©r   r3   r   r   r   r   rj   ‘  s    z*MouseMotionEventProvider.begin_hover_eventc                 G   s   | j r|  |d¡ d S )Nrˆ   r   r‘   r   r   r   ri   •  s    z+MouseMotionEventProvider.update_hover_eventc                 G   s   | j r|  |d¡ d S r}   r   r‘   r   r   r   rk   ™  s    z(MouseMotionEventProvider.end_hover_eventc                 C   s2   z| j  ¡ }||Ž  qW n tk
r,   Y nX dS )z4Update the mouse provider (pop event from the queue)N)rE   ÚpopleftÚ
IndexError)r   Zdispatch_fnÚeventr   r   r   rˆ     s
    
zMouseMotionEventProvider.update)r;   r<   r=   Z__handlers__r   rT   rX   ÚpropertyrC   re   rV   rn   rU   rp   rs   ru   r|   r   r†   ra   r`   rb   rc   rh   rj   ri   rk   rˆ   r>   r   r   r   r   r   —   s0   	

		r   Zmouse)Ú__doc__Ú__all__Z	kivy.baser   Úcollectionsr   Zkivy.loggerr   Zkivy.input.providerr   Zkivy.input.factoryr   Zkivy.input.motioneventr   r"   r#   r	   r   Úregisterr   r   r   r   Ú<module>   s   P9  