a
    V$c                     @   s   d dl mZmZmZmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZ d dlZd dlZdd Zdd Zdd	 ZdS )
    )divisionabsolute_importwith_statementprint_functionunicode_literals)PY2
basestringbchrbordchropenpystrrangeroundstrtobytesunicodeNc                    sb    fdd}g }t jjjD ]"}||}|dur|||f q|sFdS |jdd d |d d S )	a/  
    Trues to find the focus with the shortest alt text containing `pattern`.
    If found, returns a random coordinate within that displayable.

    If `pattern` is None, returns a random coordinate that will trigger the
    default focus.

    If `pattern` could not be found, returns None, None.
    c                    sZ    d u r| j d u rdS d S | j d u r4tjjj }n
| j }  | v rR|S d S d S )Ndefault)xrenpydisplayttsroot_tts_allwidgetlower)ftpattern renpy/test\testfocus.pymatch)   s    


zfind_focus.<locals>.matchNc                 S   s   t | d | d fS )Nr   )len)ar    r    r!   <lambda>J       zfind_focus.<locals>.<lambda>)keyr      )r   r   focus
focus_listappendsort)r   r"   matchingr   altr    r   r!   
find_focus   s    r/   c                 C   s0   |d ur(t |tr$t||d  } n|} t| S )Nr(   )
isinstancefloatint)r   posxwidthr    r    r!   relative_positionN   s
    
r5   c           	      C   s  |\}}t jjjdur&t jjj\}}ntt jj}tt jj}| du rjt	||t jjt	||t jjfS | }| j
du r|  } d| _
d| _t jj| _t jj| _t	||| j| j
 }t	||| j| j }tdD ]}t|}t|}t jj||}|du r|j
du r@||f  S n(|j| jkr@|j| jkr@||f  S t| j
| j
| j }t| j| j| j }qt  tddS )a  
    Returns the virtual position of a coordinate located within focus `f`.
    If position is (None, None) returns the current mouse position (if in
    the focus), or a random position.

    If `f` is None, returns a position relative to the screen as a whole.
    Nr   d   z!Could not locate the displayable.)r   test	testmouse	mouse_posrandom	randrangeconfigscreen_widthscreen_heightr5   r   copyywhr   r2   r   renderfocus_at_pointr   argprint	Exception)	r   positionr3   posyr   r@   orig_f_infr    r    r!   find_positionX   s>    	



rM   )
__future__r   r   r   r   r   renpy.compatr   r   r	   r
   r   r   r   r   r   r   r   r   r   r:   r/   r5   rM   r    r    r    r!   <module>   s   80
