U
    Pe1'                     @   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	m
Z
 ddlmZmZmZ G dd	 d	eZd
d Zdd Zdd ZdS )a  
JoyCursor
=========

.. versionadded:: 1.10.0

The JoyCursor is a tool for navigating with a joystick as if using a mouse
or touch. Most of the actions that are possible for a mouse user are available
in this module.

For example:

    * left click
    * right click
    * double click (two clicks)
    * moving the cursor
    * holding the button (+ moving at the same time)
    * selecting
    * scrolling

There are some properties that can be edited live, such as intensity of the
JoyCursor movement and toggling mouse button holding.

Usage
-----

For normal module usage, please see the :mod:`~kivy.modules` documentation
and these bindings:

+------------------+--------------------+
| Event            | Joystick           |
+==================+====================+
| cursor move      | Axis 3, Axis 4     |
+------------------+--------------------+
| cursor intensity | Button 0, Button 1 |
+------------------+--------------------+
| left click       | Button 2           |
+------------------+--------------------+
| right click      | Button 3           |
+------------------+--------------------+
| scroll up        | Button 4           |
+------------------+--------------------+
| scroll down      | Button 5           |
+------------------+--------------------+
| hold button      | Button 6           |
+------------------+--------------------+
| joycursor on/off | Button 7           |
+------------------+--------------------+

The JoyCursor, like Inspector, can also be imported and used as a normal
python module. This has the added advantage of being able to activate and
deactivate the module programmatically::

    from kivy.lang import Builder
    from kivy.base import runTouchApp
    runTouchApp(Builder.load_string("""
    #:import jc kivy.modules.joycursor
    BoxLayout:
        Button:
            text: 'Press & activate with Ctrl+E or Button 7'
            on_release: jc.create_joycursor(root.parent, root)
        Button:
            text: 'Disable'
            on_release: jc.stop(root.parent, root)
    """))
)startstopcreate_joycursor    )Clock)Logger)Widget)ColorLine)ObjectPropertyNumericPropertyBooleanPropertyc                       s   e Zd Ze 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 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 )	JoyCursorFg?   i'  r   c              	      s   t t| jf | d| _d| _d| _|   | j tdd t	| j
d d | jd d| _t	| j
dd  | jd d| _td	d t	| j
d d | jd| _t	| j
dd  | jd| _W 5 Q R X d
d | jD | _d S )NF)NN)   r   )gRQ?g{Gz?gQ?      ?)Zrgbar   g?)pointswidth)   r   r   r   c                 S   s   g | ]
}| qS  r   .0ir   r   :/tmp/pip-unpacked-wheel-xzebddm3/kivy/modules/joycursor.py
<listcomp>v   s     z&JoyCursor.__init__.<locals>.<listcomp>)superr   __init__avoid_bring_to_topZ	size_hintsize
set_cursorZcanvasr   r	   
cursor_ptscursor_width	cursor_ox	cursor_oycursor_xcursor_ypos)selfkwargs	__class__r   r   r   [   s2    

zJoyCursor.__init__c                 G   s4   | j s| jsd S d| _ ||  ||  d| _ d S )NTF)r   	activatedremove_widget
add_widget)r&   winargsr   r   r   on_window_childrenx   s    

zJoyCursor.on_window_childrenc                 C   s  |r| j |  t| jd| _| j d| j | j d| j | j d| j	 | j d| j
 | j j}|d | jd d  |d | jd d  f| _td ntdd	 | jD | _t| j | j d| j | j d| j | j d| j	 | j d| j
 | j |  td
 d S )Nr   Zon_joy_axison_joy_button_down	mouse_pos       @r   zJoyCursor: joycursor activatedc                 S   s   g | ]
}| qS r   r   r   r   r   r   r      s     z*JoyCursor.on_activated.<locals>.<listcomp>z JoyCursor: joycursor deactivated)r-   r,   r   Zschedule_intervalmove_cursormovefbindcheck_cursorset_intensitycheck_dispatchstop_cursorr1   r   r%   r   infoZ
unschedulefunbindr+   )r&   instancer*   r1   r   r   r   on_activated   s(    zJoyCursor.on_activatedc              
   G   sf   | j \}}| j\}}||t|d  || |t|d  |t|d  ||t|d  || g| _d S )Nr2   )r%   r   roundr   )r&   r.   ZpxpyZsxZsyr   r   r   r      s    

      zJoyCursor.set_cursorc                 C   s   | j }| j}|dkr@|| k r(| | _q||kr8|| _qd| _n@|dkrt|| k rZ|| _q||krl| | _qd| _nd| _d| _d S )N   r   r   )	intensity	dead_zoneoffset_xoffset_y)r&   r-   stickidZaxisidvaluerA   Zdeadr   r   r   r6      s     



zJoyCursor.check_cursorc                 C   s:   | j }|dkr |dkr |d8 }n|dkr0|d7 }|| _ d S )Nr      r   )rA   )r&   r-   rE   buttonidrA   r   r   r   r7      s    
zJoyCursor.set_intensityc           	      C   s   |dkr| j  | _ |dkrd S | j\}}| jjd | }g }dddddd}|| }| jd|||| | j s| jd	|||| d S )
N   )rG   r@   r      rI   r   leftrightZscrollupZ
scrolldownZon_mouse_downZon_mouse_up)cursor_holdcenterr-   system_sizedispatch)	r&   r-   rE   rH   xy	modifiersactionsbuttonr   r   r   r8      s"    

zJoyCursor.check_dispatchc                 G   s`   | j d  | j7  < | j d  | j7  < g }| jr\| jd| jd | jjd | jd  | d S )Nr   r   Zon_mouse_move)r%   rC   rD   rM   r-   rP   rN   rO   )r&   r.   rS   r   r   r   r3      s    zJoyCursor.move_cursorc                 C   s>   d| _ d| _|d | jd d  |d | jd d  f| _d S )Nr   r2   r   )rC   rD   r   r%   )r&   r<   r1   r   r   r   r9      s
    zJoyCursor.stop_cursorc                 C   sT   |    | jd d | j_| jdd  | j_| jd d | j_| jdd  | j_d S )Nr   )r   r   r#   r   r$   r!   r"   )r&   r<   new_posr   r   r   on_pos   s
    zJoyCursor.on_posc                 G   sD   |d }|dkr(|dgkr(| j  | _ dS |dkr@| j r@d| _ dS d S )Ne   ZctrlT   F)r*   )r&   r-   Zscancoder.   rS   r   r   r   keyboard_shortcuts   s    
zJoyCursor.keyboard_shortcutsc                 C   s.   |dkr*| j  | _ | j r*dd |jD | _d S )N   c                 S   s   g | ]}t |d  qS )r2   )r>   r   r   r   r   r     s     z0JoyCursor.joystick_shortcuts.<locals>.<listcomp>)r*   r   r%   )r&   r-   rE   rH   r   r   r   joystick_shortcuts  s    
zJoyCursor.joystick_shortcuts)__name__
__module____qualname__r
   r-   r   r*   r   r    rM   rA   rB   rC   rD   r   r/   r=   r   r6   r7   r8   r3   r9   rW   r[   r]   __classcell__r   r   r(   r   r   Q   s(   
			
r   c                 G   s6   t | d|_| j|jj|jjd | d|jj dS )a  Create a JoyCursor instance attached to the *ctx* and bound to the
    Window's :meth:`~kivy.core.window.WindowBase.on_keyboard` event for
    capturing the keyboard shortcuts.

        :Parameters:
            `win`: A :class:`Window <kivy.core.window.WindowBase>`
                The application Window to bind to.
            `ctx`: A :class:`~kivy.uix.widget.Widget` or subclass
                The Widget for JoyCursor to attach to.

    )r-   childrenZon_keyboardr0   N)r   	joycursorbindr/   r[   r5   r]   )r-   ctxr.   r   r   r   r   
  s
    
r   c                    s   t  fdd d S )Nc                     s
   t  S )N)r   )trf   r-   r   r   <lambda>      zstart.<locals>.<lambda>)r   Zschedule_oncer-   rf   r   rh   r   r     s    r   c                 C   sL   t |drHd|j_| j|jj|jjd | d|jj | |j |`dS )z?Stop and unload any active JoyCursors for the given *ctx*.
    rd   Frb   r0   N)	hasattrrd   r*   Zunbindr/   r[   r;   r]   r+   rk   r   r   r   r   "  s    

r   N)__doc____all__Z
kivy.clockr   Zkivy.loggerr   Zkivy.uix.widgetr   Zkivy.graphicsr   r	   Zkivy.propertiesr
   r   r   r   r   r   r   r   r   r   r   <module>   s   C :